Menu

Returning a byte value of 0 when reading from USART

Help
2022-08-07
2022-08-08
  • Jeff Weinmann

    Jeff Weinmann - 2022-08-07

    Hello again,

    I'm in the process of converting code from MikroBasic to GCB and I'm getting a strange value back from USART2.

    Basically the code in MB worked by checking if there was data ready on either UART1 or UART2, then reading it:

    'MikroBasic Code...
            if UART1_Data_Ready() = 1 then
               receive = UART1_Read()
               Select Case receive
                      Case "0"
                           OpMode = 0 'Initial Operation
                           AutoPPS = 0
    
                      Case "1"
                           'Cant enter diagnostic until we have a fix
                           If GPSMode = 1 then
                             OpMode = 1 'GPS Diagnostic
                             UART1_Write_Text("GPS Active")'Send back acknowledge to Interface
                             UART1_Write(10)
                   End Select
               end if
    

    I'm trying to do something similar, in GCB. I'm only looking for 1 return byte from the comport device, a number from 0 to 5. Here's my implementation:

    Do Forever
    'Pole MCU for Data
          'If there is no new data, HSerReceive will return default value of 255
          comport = 2
          HSerReceive2(TempByte)
          'Do nothing if it is default
          If TempByte <> 255 Then
            if TempByte = 0 then
                  HSerPrint "TempByte is 0", 2
                  HSerSend 13,2
                  HSerSend 10,2
            else
                  HSerPrint "TempByte is NOT 0, but I cannot get any other value so this never gets hit.", 2
                  HSerSend 13,2
                  HSerSend 10,2
            end if
    
          End If
          comport = 1
    
          Wait 100 ms
    
        Loop
    

    I'm using the USART2 on a PIC18F16Q40. I set up the USARTS (and I2c) with PPS:

    #startup InitPPS, 85
        #define PPSToolPart 18f16q40
    
        Sub InitPPS
    
                'Module: I2C1
                RB6PPS = 0x0021    'SCL1 > RB6
                I2C1SCLPPS = 0x000E    'RB6 > SCL1 (bi-directional)
                RB4PPS = 0x0022    'SDA1 > RB4
                I2C1SDAPPS = 0x000C    'RB4 > SDA1 (bi-directional)
    
                'Module: UART pin directions
                Dir PORTC.6 Out    ' Make TX1 pin an output
                Dir PORTC.7 In    ' Make RX1 pin an input
                Dir PORTC.0 Out    ' Make TX2 pin an output
                Dir PORTC.1 In    ' Make RX2 pin an input
                'Module: UART1
                RC6PPS = 0x0010    'TX1 > RC6
                U1RXPPS = 0x0017    'RC7 > RX1
                'Module: UART2
                RC0PPS = 0x0013    'TX2 > RC0
                U2RXPPS = 0x0011    'RC1 > RX2
        End Sub
    
        'USART settings for UART1 and UART2
    #define USART_BAUD_RATE 9600
    #define USART_BLOCKING
    #define USART_TX_BLOCKING
    
    #define USART2_BAUD_RATE 9600
    '#define USART2_BLOCKING
    '#define USART2_TX_BLOCKING
    '#define USART_DELAY OFF
    

    USART2 is non blocking so I'm trying to read as it comes in. I don't have more than 1 byte that I'm looking for so a ring buffer i think is overkill for this.

    I did try

    On Interrupt UsartRX2Ready Call getdata

    sub getdata
    'Receive code....
    ....
    ....
    end sub

    and got :
    Error: Invalid interrupt event: USARTRX2READY

    I think if I can add the interrupt on USART2 and get it working i might be further along.

    What is strange is that every time I send the 1 byte flag from the com device, my polling and my return messages work, I can never get anything but a '0' byte value

    May I get help with proper syntax to setup an interrupt on USART2? thanks Any thoughts on the 0 value byte returned?

    Jeff

     
  • mmotte

    mmotte - 2022-08-08

    Jeff ,
    I am not sure what you are doing with the gps but I did some work a while back.
    https://sourceforge.net/p/gcbasic/discussion/629990/thread/0e660522/

    .......................................

         comport = 2
          HSerReceive2(TempByte)
          'Do nothing if it is default
    

    to make this code more efficient, instead of checking for the default value, check directly for data available.
    Just above this code

    If  USART2HasData then
         comport = 2
          HSerReceive2(TempByte)
    
          ...... rest of you code 
      endif    
    

    GL
    Mike

    USART2HasData is in the GCB usart.h library file . The usart only has one byte data buffer. If you need more then use a buffer.

     

Log in to post a comment.