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...ifUART1_Data_Ready()=1thenreceive=UART1_Read()SelectCasereceiveCase"0"OpMode=0'Initial OperationAutoPPS=0Case"1"'Cant enter diagnostic until we have a fixIfGPSMode=1thenOpMode=1'GPS DiagnosticUART1_Write_Text("GPS Active")'Send back acknowledge to InterfaceUART1_Write(10)EndSelectendif
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:
DoForever'Pole MCU for Data'If there is no new data, HSerReceive will return default value of 255comport=2HSerReceive2(TempByte)'Do nothing if it is defaultIfTempByte<>255ThenifTempByte=0thenHSerPrint"TempByte is 0", 2HSerSend13,2HSerSend10,2elseHSerPrint"TempByte is NOT 0, but I cannot get any other value so this never gets hit.", 2HSerSend13,2HSerSend10,2endifEndIfcomport=1Wait100msLoop
I'm using the USART2 on a PIC18F16Q40. I set up the USARTS (and I2c) with PPS:
#startupInitPPS, 85
#definePPSToolPart18f16q40SubInitPPS'Module: I2C1RB6PPS=0x0021'SCL1 > RB6I2C1SCLPPS=0x000E'RB6 > SCL1 (bi-directional)RB4PPS=0x0022'SDA1 > RB4I2C1SDAPPS=0x000C'RB4 > SDA1 (bi-directional)'Module: UART pin directionsDirPORTC.6Out' Make TX1 pin an outputDirPORTC.7In' Make RX1 pin an inputDirPORTC.0Out' Make TX2 pin an outputDirPORTC.1In' Make RX2 pin an input'Module: UART1RC6PPS=0x0010'TX1 > RC6U1RXPPS=0x0017'RC7 > RX1'Module: UART2RC0PPS=0x0013'TX2 > RC0U2RXPPS=0x0011'RC1 > RX2EndSub'USART settings for UART1 and UART2
#defineUSART_BAUD_RATE9600
#defineUSART_BLOCKING
#defineUSART_TX_BLOCKING
#defineUSART2_BAUD_RATE9600'#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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
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:
I'm using the USART2 on a PIC18F16Q40. I set up the USARTS (and I2c) with PPS:
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
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/
.......................................
to make this code more efficient, instead of checking for the default value, check directly for data available.
Just above this code
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.