Hi,
Im having issues whereby the PIC not working/ or just gets hanged after the data is received. I think there's coding mistake because uart interrupt taking place and I having some difficulties to clear up the appropriate bits. So anyone can help check and let me know the mistakes. FYI im new to PIC Programming.
Thank you.
Here's the code:
'********************************************************************
dim OUT_K1 as sbit at PORTE.2'
dim Null as char'
dim ID as word'
dim TerminationString as string[3]
const STRING_MAX_SIZE=8 '8
dim ReceiveBuffer as string[STRING_MAX_SIZE+1]
dim IDvalue as string[5]'
dim StringI as string[5]'
' DEVICE 18F4520 8mhz
sub procedure interrupt
if (UART1_Data_Ready()=1) then ' Wait for response from the transmitter. The string format should be: "Sxxxx]"
UART1_Read_Text(ReceiveBuffer, ' Receive the message from the board
"]", ' Receive until Termination Character "]" occures
7) '7 ' Search for total number of characters
end if
'
if(ReceiveBuffer[1]="S")then ' Moving only when Incoming string starts with S
StringI=ReceiveBuffer[2]+ReceiveBuffer[3]' Parsing only the first two digits
StringI=StringI+Null
ID=Strtoword(StringI[0]+StringI[1]+StringI[2])
end if
'
if (UART1_Data_Ready()=0) then
ReceiveBuffer[0]=0 'Clear up the Buffer when not reading
end if
end sub
main:
ADCON1=0x0f 'PORTA inputs all are digital
ADCON0=0x00 'Disable analog functions
CMCON=0x07 'Turn off Comparators
CCP1CON=0x00'Turns off Capture/ Compare/PWM
TRISA=0xff '00111111
TRISD=0x00 '00000000
TRISC=0x39 '00111001
TRISB=0x3C '00111100
TRISE=0x03 '00000011
TerminationString[0] = 13
TerminationString[1] = 10
TerminationString[2] = 0
ReceiveBuffer[0]=0' Clear receive buffer
Delay_ms(1000)
INTCON.GIE=1
INTCON.PEIE=1
ID=0 '
Null="" ' Null Character
UART1_Init(9600) '
Delay_ms(1000)
'
while (true)
if (ID>90) then
OUT_K1=1 'turn the relay on
else
OUT_K1=0
end if
wend
end.
UART Receive error 18F4520
Re: UART Receive error 18F4520
Hi,
You can implement something like this for getting the data from UART, it was written for another MCU in C but you can get the idea :
Regards,
Filip.
You can implement something like this for getting the data from UART, it was written for another MCU in C but you can get the idea :
Code: Select all
void interrupt() {
if (RCIF_bit == 1) { // If interrupt is generated by RCIF
txt[i] = UART1_Read(); // Read data and store it to txrt string
i++; // Increment string index
if (i == 768) { // If index = 768,
i = 0; // set it to zero
ready = 1; // Ready for parsing data (768 received bytes)
}
RCIF_bit = 0; // Set RCIF to 0
}
}
void main() {
ready = 0; // Initialize variables
i = 0;
UART1_Init(9600); // Initialize UART module at 9600
RC1IE_bit = 1; // Enable USART Receiver interrupt
GIE_bit = 1; // Enable Global interrupt
PEIE_bit = 1; // Enable Peripheral interrupt
while(1) {
OERR1_bit = 0; // Set OERR to 0
FERR1_bit = 0; // Set FERR to 0
if(ready == 1) { // If the data in txt array is ready do:
ready = 0;
// parse the data
}
}
}
Filip.
Re: UART Receive error 18F4520
hi filip,
Yes its finally working. Thanks for the guide.
Yes its finally working. Thanks for the guide.