Announcement

Collapse
No announcement yet.

HSERIN/HSEROUT - Parity error

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • HSERIN/HSEROUT - Parity error

    [SIZE=11px]HSEROUT - Serius problem at 9600.8, E, 1 (9 bit)
    PBP 3.1, device PIC18F14K22 @ 20MHz
    I encountered a very serious problem on parity management, ie the parity I set "EVEN" is not correctly interpreted in HSERIN, moreover in HSEROUT it is correctly transmitted for the first 5 bytes and then either is wrong or is not transmitted correctly causing the Framing Error. (the interrupt is disabled)
    I use this serial port combined with a MAX3485 to connect to the Modbus-rtu network.
    The program works perfectly without communication parity: 9600,8,N,1
    DEFINE HSER_BAUD 9600
    DEFINE HSER_RCSTA 90h 'Set receive register
    DEFINE HSER_TXSTA 60h 'Set transmit register (it was 20h)
    DEFINE HSER_CLROERR 1 'Clear overrun error upon execution of every
    DEFINE HSER_BITS 9 'Use 8 bits + parity
    DEFINE HSER_EVEN 1 'Use only if even parity desired

    Also without this setting it does not receive correctly, although it should already be included in the Define's
    RCSTA.6 = 1 ' 6.RX9 9 bit disable (0=8bit)

    Simple transmission routine not under interrupt:
    NBTx = 40
    gosub open_485
    HSEROUT [str BufTx\NBTx]
    gosub close_485

    Simple reception routine under interrupt:
    if RCIF then
    'the following instruction can not be used because when it receives a character (8, E, 1) it goes into a parity error
    'HSERIN byError, 2, byPass, [str BufRx\8]
    'instead the following instruction avoids the parity error and the BufRx contains the right data
    hserin 2, byPass, [str BufRx\8]
    goto prosegui
    byError:
    ParityError = 1
    byPass:
    for B0 = 0 to 7
    BufRx[B0] = 0
    next B0
    prosegui:
    endif

    Maybe I did not do some settings correctly?
    Please, help me
    Umberto


    Last edited by maisoft; 1 week ago.

  • #2
    bit difficult to determine whats really wong from code snippets
    DEFINE HSER_RCSTA 90h 'Set receive register
    will not allow parity generation
    use
    DEFINE HSER_RCSTA 0d0h 'Set receive register to use parity


    here is a complete compliable pgm to demonstrate even parity actually works
    Code:
     ;pic16f648a @4mhx  even parity send receive demo  @9600  e81
    
    #CONFIG
    cfg = _INTOSC_OSC_NOCLKOUT
    cfg&= _WDT_ON
    cfg&= _PWRTE_OFF
    cfg&= _MCLRE_ON
    cfg&= _BODEN_ON
    cfg&= _LVP_OFF
    cfg&= _DATA_CP_OFF
    cfg&= _CP_OFF
      __CONFIG cfg
    
    #ENDCONFIG
     BufTx var byte[8]
     BufRx var byte[8]
     rcif var pir1.5
     led var porta.0
     clear
     CMCON=7
     trisb=%11111011
     trisa=%11111110
    ;The program works perfectly with communication parity: 9600,8,E,1
     DEFINE HSER_BAUD 9600
     DEFINE HSER_RCSTA 0D0h 'Set receive register
     DEFINE HSER_TXSTA 64h 'Set transmit register use brgh
     DEFINE HSER_CLROERR 1 'Clear overrun error upon execution of every
     DEFINE HSER_BITS 9 'Use 8 bits + parity
     DEFINE HSER_EVEN 1 'Use only if even parity desired
     DEFINE HSER_SPBRG 25  ' 9600 Baud @ 4MHz, 0.16%
    
    
    
    arraywrite buftx,["ready",0]
    
    
    HSEROUT [str BufTx]
    RCIF=0
    arraywrite buftx,["p-error",0]
    loopy:
        ' Simple reception routine under interrupt:
        if RCIF then
            led=1
            hserin   pError, [str BufRx\8]   ;
            led=0
            pauseus 2000                    
            HSEROUT [str Bufrx]
        endif  
    goto loopy
    pError:
         HSEROUT [str BufTx]
    goto loopy

    Click image for larger version  Name:	screenshot.png Views:	1 Size:	42.5 KB ID:	7369Click image for larger version  Name:	parity.jpg Views:	1 Size:	84.4 KB ID:	7370
    Last edited by richard; 1 week ago.

    Comment


    • #3
      Thanks Richard for the quick and complete answer to my question.

      I had set "DEFINE HSER_RCSTA D0h" but the compiler reported an error then I added "RCSTA.6 = 1".
      Now setting "0D0h" instead of "D0h" is fine.
      But the problem remains and I try to describe what happens (sorry for my bad English).

      I have made simple tests of receiving and transmitting a single byte (9600.8, E, 1) and I have seen the reaction of HSERIN / HSEROUT in some cases.
      Please note that HSERIN is wrong when bit 1 of the received byte is set:
      So when the byte is value (hexadecimal) 00,01,04,05,08,0,0,0C, 0D HSERIN is correct
      When the byte is valid: 02,0306,07,0A, 0B, 0E, 0F HSERIN is wrong and a parity error occurs.
      If I do not look at the error, which is not there, the byte is always correct.

      To check with the oscilloscope I wrote this program and analyzed the data. The result is correct correct.

      looptest:
      gosub open_485
      for B0 = 0 to 9
      LOOKUP B0, [0,3,5,6,9,1,2,4,7,8], B1
      hserout [rep B1\10]
      pause 1000
      next B0
      gosub close_485
      pause 5000
      goto looptest
      Data transmission is perfect, parity is accurate. .

      I understand that it is very strange but I repeated the test several times and I always get this result.
      Sincerely
      Umberto
      Last edited by maisoft; 1 week ago.

      Comment


      • #4


        Please note that HSERIN is wrong when bit 1 of the received byte is set:
        So when the byte is value (hexadecimal) 00,01,04,05,08,0,0,0C, 0D HSERIN is correct
        When the byte is valid: 02,0306,07,0A, 0B, 0E, 0F HSERIN is wrong and a parity error occurs.
        If I do not look at the error, which is not there, the byte is always correct.
        if I change my code to display rx char's in hex format I can receive both of those strings with no Click image for larger version

Name:	parity1.jpg
Views:	1
Size:	80.5 KB
ID:	7373Click image for larger version

Name:	parity.jpg
Views:	1
Size:	79.9 KB
ID:	7374 error, parity works correctly





        Code:
         ;pic16f648a @4mhx  even parity send receive demo  @9600  e81
        
        #CONFIG
        cfg = _INTOSC_OSC_NOCLKOUT
        cfg&= _WDT_ON
        cfg&= _PWRTE_OFF
        cfg&= _MCLRE_ON
        cfg&= _BODEN_ON
        cfg&= _LVP_OFF
        cfg&= _DATA_CP_OFF
        cfg&= _CP_OFF
          __CONFIG cfg
        
        #ENDCONFIG
         BufTx var byte[8]
         BufRx var byte[8]
         rcif var pir1.5
         led var porta.0
         i var byte
         clear
         CMCON=7
         trisb=%11111011
         trisa=%11111110
        ;The program works perfectly with communication parity: 9600,8,N,1
         DEFINE HSER_BAUD 9600
         DEFINE HSER_RCSTA 0D0h 'Set receive register
         DEFINE HSER_TXSTA 64h 'Set transmit register use brgh
         DEFINE HSER_CLROERR 1 'Clear overrun error upon execution of every
         DEFINE HSER_BITS 9 'Use 8 bits + parity
         DEFINE HSER_EVEN 1 'Use only if even parity desired
         DEFINE HSER_SPBRG 25  ' 9600 Baud @ 4MHz, 0.16%
        
        '$02$03$06$07$0A$0B$0E$0F 
        '$0$01$04$05$08$0$0C$0D  
        arraywrite buftx,["ready",0]
        
        
        HSEROUT [str BufTx,13,10]
        RCIF=0
        arraywrite buftx,["p-error",0]
        loopy:
            ' Simple reception routine under interrupt:
            if RCIF then
                led=1
                hserin   pError, [str BufRx\8]   ;
                led=0 
                pauseus 2000
                for i = 0 to 6                  
                HSEROUT [hex2 Bufrx[i],","]
                next
                HSEROUT [hex2 Bufrx[i],13,10]
            endif   
        goto loopy
        pError:
             HSEROUT [str BufTx,13,10]
        goto loopy

        Comment

        Working...
        X