UART interrupt problem
Posted: 18 Mar 2013 19:14
I am using a ready for PIC board, and have connected a GPS receiver to UART2 (Rx-RB6). Every second the GPS receiver sends a string of characters to the PIC. I want to store the characters from the GPS module in a character array and then search for "$GPGGA" which is the start of the GPS data that I need.
The problem I'm having is with triggering the PIC to store data every time it is sent to UART2, I tried using UART2_Data_Ready() but this did not work. I have noticed that most people use UART interrupts to trigger the PIC to store data. But I cant get it to work. Please help me
My code checks if data is ready to receive if the there is no data it displays "Not ready" to the LCD, if data is revived and it contains the string of characters "$GPGGA" it displays "data found" to the LCD and if data is received but "GPGGA" is not found it displays "no data".
When I run the program the LCD displays "Not ready" thus I can say with reasonable certainty that the interrupt code is not working. My code is below:
/*-------------------------------------------------------define LCD Connections ------------------------------------- */
sbit LCD_RS at LATA2_bit;
sbit LCD_EN at LATA3_bit;
sbit LCD_D4 at LATA4_bit;
sbit LCD_D5 at LATA5_bit;
sbit LCD_D6 at LATE0_bit;
sbit LCD_D7 at LATE1_bit;
sbit LCD_RS_Direction at TRISA2_bit;
sbit LCD_EN_Direction at TRISA3_bit;
sbit LCD_D4_Direction at TRISA4_bit;
sbit LCD_D5_Direction at TRISA5_bit;
sbit LCD_D6_Direction at TRISE0_bit;
sbit LCD_D7_Direction at TRISE1_bit;
/*-------------------------------------------------------gps variables-----------------------------------------------------*/
int i;
unsigned short ready;
char GPSin[1024]; //GPS data dump
char *pointer;
/*----------------------------------------------------------GPS subroutine----------------------------------------------------------------------------*/
GPS (void)
{
if(ready == 1) // If the data in txt array is ready do:
{
ready = 0;
pointer = strstr(GPSin,"$GPGGA");
if(pointer != 0) // If txt array contains "$GPGGA" string
{
Lcd_Out(1,1,"Data Found");
delay_ms(2000); // display for 2 seconds
lcd_Cmd(_LCD_CLEAR); //clear LCD display
}
else
{
Lcd_Out(1,1,"No data"); //LCD test message
delay_ms(2000); // display for 2 seconds
lcd_Cmd(_LCD_CLEAR); //clear LCD display
}
}
else
{
Lcd_Out(1,1,"Not ready"); //LCD test message
delay_ms(2000); // display for 2 seconds
lcd_Cmd(_LCD_CLEAR); //clear LCD display
}
}
/*----------------------------------------------------------Read GPS data interupt--------------------------------------------------------------------*/
void interrupt()
{
if (RCIF_bit == 0) // If interrupt is generated by RCIF
{
GPSin = UART2_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 GPS data
}
RCIF_bit = 0; // Set RCIF to 0
}
}
/*--------------------------------------------------------Main program-------------------------------------------------------------------------------*/
void main()
{
ANSELA = 0; // set all pins as digital
ANSELB = 0; // set all pins as digital
ANSELC = 0; // set all pins as digital
ANSELD = 0; // set all pins as digital
C1ON_bit = 0; // Disable comparator
C2ON_bit = 0; // Disable comparator
Lcd_Init(); // Initalise LCD
delay_ms(100);
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD curser off
Uart2_Init(4800); // Initialize UART module and set board rate
Delay_ms(100);
RC1IE_bit = 1; // Enable USART Receiver interrupt
GIE_bit = 1; // Enable Global interrupt
PEIE_bit = 1; // Enable Peripheral interrupt
ready=0;
i=0;
Lcd_Out(1,1,"LCD test"); //LCD test message
delay_ms(2000); // display for 2 seconds
lcd_Cmd(_LCD_CLEAR); //clear LCD display
while(1)
{
GPS(); //GPS recive data subroutine
}
}
The problem I'm having is with triggering the PIC to store data every time it is sent to UART2, I tried using UART2_Data_Ready() but this did not work. I have noticed that most people use UART interrupts to trigger the PIC to store data. But I cant get it to work. Please help me
My code checks if data is ready to receive if the there is no data it displays "Not ready" to the LCD, if data is revived and it contains the string of characters "$GPGGA" it displays "data found" to the LCD and if data is received but "GPGGA" is not found it displays "no data".
When I run the program the LCD displays "Not ready" thus I can say with reasonable certainty that the interrupt code is not working. My code is below:
/*-------------------------------------------------------define LCD Connections ------------------------------------- */
sbit LCD_RS at LATA2_bit;
sbit LCD_EN at LATA3_bit;
sbit LCD_D4 at LATA4_bit;
sbit LCD_D5 at LATA5_bit;
sbit LCD_D6 at LATE0_bit;
sbit LCD_D7 at LATE1_bit;
sbit LCD_RS_Direction at TRISA2_bit;
sbit LCD_EN_Direction at TRISA3_bit;
sbit LCD_D4_Direction at TRISA4_bit;
sbit LCD_D5_Direction at TRISA5_bit;
sbit LCD_D6_Direction at TRISE0_bit;
sbit LCD_D7_Direction at TRISE1_bit;
/*-------------------------------------------------------gps variables-----------------------------------------------------*/
int i;
unsigned short ready;
char GPSin[1024]; //GPS data dump
char *pointer;
/*----------------------------------------------------------GPS subroutine----------------------------------------------------------------------------*/
GPS (void)
{
if(ready == 1) // If the data in txt array is ready do:
{
ready = 0;
pointer = strstr(GPSin,"$GPGGA");
if(pointer != 0) // If txt array contains "$GPGGA" string
{
Lcd_Out(1,1,"Data Found");
delay_ms(2000); // display for 2 seconds
lcd_Cmd(_LCD_CLEAR); //clear LCD display
}
else
{
Lcd_Out(1,1,"No data"); //LCD test message
delay_ms(2000); // display for 2 seconds
lcd_Cmd(_LCD_CLEAR); //clear LCD display
}
}
else
{
Lcd_Out(1,1,"Not ready"); //LCD test message
delay_ms(2000); // display for 2 seconds
lcd_Cmd(_LCD_CLEAR); //clear LCD display
}
}
/*----------------------------------------------------------Read GPS data interupt--------------------------------------------------------------------*/
void interrupt()
{
if (RCIF_bit == 0) // If interrupt is generated by RCIF
{
GPSin = UART2_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 GPS data
}
RCIF_bit = 0; // Set RCIF to 0
}
}
/*--------------------------------------------------------Main program-------------------------------------------------------------------------------*/
void main()
{
ANSELA = 0; // set all pins as digital
ANSELB = 0; // set all pins as digital
ANSELC = 0; // set all pins as digital
ANSELD = 0; // set all pins as digital
C1ON_bit = 0; // Disable comparator
C2ON_bit = 0; // Disable comparator
Lcd_Init(); // Initalise LCD
delay_ms(100);
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD curser off
Uart2_Init(4800); // Initialize UART module and set board rate
Delay_ms(100);
RC1IE_bit = 1; // Enable USART Receiver interrupt
GIE_bit = 1; // Enable Global interrupt
PEIE_bit = 1; // Enable Peripheral interrupt
ready=0;
i=0;
Lcd_Out(1,1,"LCD test"); //LCD test message
delay_ms(2000); // display for 2 seconds
lcd_Cmd(_LCD_CLEAR); //clear LCD display
while(1)
{
GPS(); //GPS recive data subroutine
}
}