News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Testing value in UART Receive register

Started by charliecoutas, Sep 27, 2022, 10:04 AM

Previous topic - Next topic

charliecoutas

Is it valid to do this and compare the USART receive reg directly:

   Select Vector
     Case 1
        If RC1REG = "@" Then
          etc

I have just struggled badly trying to find a problem but the above doesn't work. The comparison always fails.

If I do this:

   Select Vector
      Case 1
        temp = RC1REG
        If temp = "@"
           etc

Then it works! Can somebody explain this for me please.

Charlie

tumbleweed

two questions -

1. which device?
2. in the first example, are you sure it's the 'if' that doesn't work? Do you try to use RC1REG again later in the code? If so, it may contain a different value the second time you read it.

top204

#2
You cannot use the RCxREG SFRs by themselves for comparisons then transferrals because they are part of the device's receive buffer and once they are read they loose the value contained in them.

Create a global variable that has the Access directive after it, so it will sit in Bankless RAM if using an 18F device, or sit in low RAM for other devices, and load that variable with the RCxREG. Then use that for the comparisons and transferring.

For example, I created an interrupt serial receiver that would only accept ASCII values and I used the code:

    USART1_bRXByte = RCREG1                                     ' Place the byte received into USART1_bRXByte
    Select USART1_bRXByte                                       ' \
        Case 13 To 123                                          ' / Allow only ASCII characters in the serial buffer
            Inc USART1_bIndexIn                                 ' Move up the buffer
            If USART1_bIndexIn >= _cUSART1_BufferSize Then      ' End of buffer reached?
                USART1_bIndexIn = 0                             ' Yes. So reset USART1_bIndexIn
            EndIf
            USART1_wFSR1 = AddressOf(USART1_bRingBuffer)        ' Point FSR1L\H to USART1_bRingBuffer array
            USART1_wFSR1 = USART1_wFSR1 + USART1_bIndexIn       ' Add the buffer position to FSR1L\H
            INDF1 = USART1_bRXByte                              ' Place the received character into the buffer
            USART1_tCharInBuffer = True                         ' Indicate that there is an ASCII character in the buffer
    EndSelect

charliecoutas

Thanks Les. I have done as you say and it works just fine.

Charlie