HSERIN2 not working on PIC18F97J60 ???


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    Italy
    Posts
    72

    Default

    Hello guys, I'm back with another issue related to PIC18F97J60 and serial ports.
    Even if the good Mister-e pointed out and actually solved the issue of HSERIN2 not working at all for that chip, seems that there is something still weird there.

    Explanation:
    - In order to understand if there is a byte into the port1 buffer, I poll PIR1.5 byte;
    - if found it set to 1, it means there is a character. Then start HSERIN. I don't care the first character since there are 3 characters as preamble;
    - If a character is found, the do some process and write outside with HSEROUT
    - If no data, I'll check the register PIR3.5 to know if a character is in the buffer for the port 2.
    - the same as before, if there is one immediately start HSERIN2 then some process and write outside with HSEROUT2.

    Well, anything is working BUT the code that deal with HSERIN, it work only ONCE. I've tried MANY times even with a digital scope. Data are really coming to the Port 1 but the PIR1.5 is everytime 0.
    I've figured out that maybe need to toggle the CREN bit. Nothing to do.
    Instead the bit PIR3.5 is still working very well and all the time.
    I've also putted a LED (named LED_48VFault in the code) to see WHEN the code after the checking of PIR1.5 is executed.
    Since the high timeout on HSERIN (200ms) I'm able to see it flashing when it execute.
    The problem is that it execute just ONCE. The first time that I send some command to the Port1, it flash and anything is working. Since the second time, no more working but the Port2 it work regularly and as expected.

    Below is the code:

    Code:
    Include "modedefs.bas"          'Inline compiler's pre-defined constants
    define OSC 25                   'Set the Crystal Clock speed
    
    define HSER_CLROERR 1            'No Error Overflow on Serial Port 1
    define HSER2_CLROERR 1          'No Error Overflow on Serial Port 2
    
    ADCON0 = 0                      'AD converter module 0 = disabled
    ADCON1 = %00001111              'Set all analog pins to digital
    ADCON2 = %00000000
    CMCON  = %00000111              'Comparator = disabled
    INTCON = %00111000
    INTCON2 = %10000000
    INTCON3 = %00000000
    RCON.7 = %00000000
    T0CON  = %00000000              'Timer0 disabled (see page 163)
    T1CON  = %00000000              'Timer1 disabled (see page 167)
    T2CON  = %00000000              'Timer2 disabled (see page 173)
    T3CON  = %00000000              'Timer3 disabled (see page 175)
    T4CON  = %00000000              'Timer4 disabled (see page 179)
    CCP4CON = %00000000             'Capture/Compare/PWM 4 disabled (see page 181)
    CCP5CON = %00000000             'Capture/Compare/PWM 5 disabled (see page 181)
    CCP1CON = %00000000             'Capture/Compare/PWM 1 disabled (see page 189)
    CCP2CON = %00000000             'Capture/Compare/PWM 2 disabled (see page 181)
    CCP3CON = %00000000             'Capture/Compare/PWM 3 disabled (see page 181)
    EECON2.5 = 0                    'Ethernet Module Disabled
    
    
    SSP1CON1.5 = 0                   'Disable SPI 1 and set pins as GPIO
    SSP2CON1.5 = 0                   'Disable SPI 2 and set pins as GPIO
    
    BAUDCON1 = %00001000             'BRG16=1; See page 304
    BAUDCON2 = %00001000
    
                                            'Serial Port 1 speed setting:
    SPBRGH1 = $00                    '115200 bps for serial port 1 =53 ($35)
    SPBRG1  = $35
    
                                     'Serial Port 2 speed setting:
    SPBRGH2 = $00                    '115200 bps for serial port 2 =53 ($35)
    SPBRG2  = $35
    
    TXSTA1 = %00100100               'Tx1 enabled,8bit,Async, see page 302
    RCSTA1 = %10010000               'Rx1 enabled,8 bit,Async,No frame&overfl.err
                                     'See page 303
    
    TXSTA2 = %00100100               'Tx2 enabled,8bit,Async,Hi-speed see page 302
    RCSTA2 = %10010000               'Rx2 enabled,8 bit,Async,Hi-Speed
                                     'No frame&overfl.err (See page 303)
    
                
    'Port D features setting:
    PSPCON.4 = 0                    'Port D set to be used as GPIO
    
    TRISG.0 = 0                     
    TRISG.1 = 0                     'PortG1 = Output (External RS232-TX2)
    TRISG.2 = 1                     'PortG2 = Input  (External RS232-RX2)
    TRISG.3 = 0                     
    TRISG.4 = 1                     
    TRISG.5 = 1                     
    TRISG.6 = 1                     
    TRISG.7 = 1                     
    LATG = 0
    
    syncByte        var byte
    ADDRByte       var byte
    CmdType        var byte
    CmdID            var byte
    preamble         var byte
    PortAddr         var byte
    LED_SysAlive   var PORTJ.0
    LED_48VFault   var PORTJ.1
    
    MainLoop:
        LED_SysAlive =  ~ Led_SysAlive 'Blink a led as fast is possible
        Gosub ReadPort1
        Gosub ReadPort2
        Goto MainLoop
    
    ReadPort1:        
        'The following lines are commented because do not produce any visible results.
        'if RCSTA1.1=1 then
            'RCSTA1.4=0
            'RCSTA1.4=1
        'endif
        'RCSTA1 = %10010000
    
            
        IF PIR1.5 = 1 then         'Data ready on Port1:
            PortAddr = 1
    preamble1:
            LED_48VFault  = 1
            hserin 200, NoData1,[syncByte]      
            if syncByte = "-" then 
                goto preamble1
            endif
            IF syncByte = "U" then        
                hserin 200, NoData1,[ADDRByte]    'Expect the address of the Unit
                IF ADDRByte = 1 Then
                    'Data stream start coming now to this unit
                    hserin 200, NoData1,[CmdType, CmdID, preamble]
                    Select Case CmdID
                           Case = 1
                                'Do something
                                 Gosub Answer
                           Case = 2
                                'Do something else
                                Gosub Answer
                    End Select
               End IF
            End IF
        End IF
    
    NoData1:
        syncByte = 0
        CmdType = 0
        LED_48VFault = 0
        Return
    
    
    ReadPort2:        
        IF PIR3.5 = 1 then         'Data ready on Port2:
            PortAddr = 2
    preamble2:    
            hserin2 200, NoData2,[syncByte]      
            if syncByte = "-" then 
                goto preamble2
            endif
            IF syncByte = "U" then        
                hserin2 200, NoData2,[ADDRByte]    'Expect the address of the Unit
                IF ADDRByte = 1 Then
                    'Data stream start coming now to this unit
                    hserin2 200, NoData2,[CmdType, CmdID, preamble]
                    Select Case CmdID
                           Case = 1
                                'Do something
                                 Gosub Answer
                           Case = 2
                                'Do something else
                                Gosub Answer
                    End Select
               End IF
            End IF
        End IF
    
    NoData2:
        syncByte = 0
        CmdType = 0
        Return
    
    Answer:
        IF PortAddr = 1 Then
              HSEROUT ["U", 1, CmdType, CmdID] 
        Else
              HSEROUT2 ["U", 1, CmdType, CmdID]
        End IF
        Return
    Some clue?

    Thank you very much to all

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default

    I haven't go really deep in your code, but usually when you deal with HSERIN(2)/HSEROUT(2), you must use the DEFINEs instead of writing to the PIC registers, unless PBP will assume the default 2400 bauds.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Aug 2006
    Location
    Italy
    Posts
    72

    Default

    Quote Originally Posted by mister_e View Post
    I haven't go really deep in your code, but usually when you deal with HSERIN(2)/HSEROUT(2), you must use the DEFINEs instead of writing to the PIC registers, unless PBP will assume the default 2400 bauds.
    Ok, thank you for the suggestion.

    I'll try right now.

  4. #4
    Join Date
    Aug 2006
    Location
    Italy
    Posts
    72

    Default

    Another sleepless night.

    Nothing to do Mister-E. Still the same behaviour even with your suggestions.

    Some hint more:
    In the segment of code where I'm talking about "Do something" and "Do something else", really I'm using the instructions I2CRead and I2CWrite.

    Seems that if I call the sequence:
    HSERIN
    HSEROUT
    I2CREAD
    then the HSERIN does not work anymore

    Again,
    If I remove the checking of the PIR1.5 bit, EVERYTHING IS WORKING !!!!!!
    As per the above code that DOES NOT WORK:

    ReadPort1:
    ....
    IF PIR1.5 = 1 Then
    HSERIN 200, NoData1, [syncByte]
    .....
    .....
    EndIF

    If I remove the line IF PIR1.5 = 1 (and his EndIF, of course) even using:
    HSERIN 10,NoData1,[syncByte] 'ALL IS WORKING FINE, but I miss 10ms each time !!!

    How this is possible?


    Again:
    I'm using Tibbo EM200 that suppose to talk with the serial port 1. His outputs are at CMOS level (HIGH when idle) and I'm using HSERIN. I should consider something in this case?

    Again:
    I've tried SERIN2. Much worste.
    I used:
    SERIN2 PortC.7, 16390, 200, NoData1, [syncByte] 'Inverted mode 38400
    and even
    SERIN2 PortC.7, 6, 200, NoData1, [syncByte] 'True mode 38400
    In both cases, the program pass the checking about PIR1.5 but then is supposed to go to NoData1 in case of no data in 200ms. Well, I don't know where it go, since the LED_48VFault is supposed to be switched off when the program go to NoData1. Instead it still remain switched ON... and the remaining part of the program is still working!!!

    Any idea?
    Should I hire an exorcist?

    Thank you.

Similar Threads

  1. 2x16 lcd not working with pic16f72
    By vu2iia in forum Schematics
    Replies: 4
    Last Post: - 16th February 2011, 14:59
  2. Blink.Bas on 18f45k20 Newbie seeks working example.
    By DiscoEd in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 29th December 2009, 03:36
  3. Pic 16 F628A not working
    By turkuaz in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 17th March 2009, 12:26
  4. Second serial port access ( Hserin2 )
    By MegaADY in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 9th October 2007, 18:01
  5. Hserin not working...
    By robert0 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 22nd August 2005, 12:25

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts