|
|
View previous topic :: View next topic |
Author |
Message |
Joris H
Joined: 24 Sep 2003 Posts: 14
|
RS232-PIC is locking up |
Posted: Wed Sep 24, 2003 8:22 pm |
|
|
Hi,
In my project I use a PIC16f877-20/P. I try to communicate with a GSM-module. When I run the code then the LCD display: -AT+CMGR= xxx
(this is code that I send) after that the PIC is locking up. The displayed time is stopped.
I tryed to put the " printf(lcd_putc,"\%S",receved_from_modem);"line directly in the "get_string(receved_from_modem,int max)" and then I got the complete responce from the module in one long string. I tryed other AT-Commands with the same result.
Why is the PIC locking up?
Please HELP
CCS: 3.091
some code:
#include <16F877.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOLVP,NOPROTECT
#use fixed_io(c_outputs=pin_C6)
#use rs232(baud=9600,parity=n,bits=8,xmit=PIN_C6,rcv=PIN_C7)
#int_RDA
RDA_isr() {
get_string(receved_from_modem,160);
bit_set(ModemFlag,RecMess);
}
void main() {
int b;
rtc_init();
lcd_init();
init_ext_eeprom();
enable_interrupts(INT_RDA);
enable_interrupts(global);
strcpy(OK, "OK");
do
{Display_MainMenu();}
while(TRUE);
}
void get_string(receved_from_modem,int max) {
int len;
char c;
max--;
len=0;
do {
c=getc();
if(c==0x0A ||c==0x0D) { //DEL first CR and LF
if(len<2) {
len--;
c='z';
}
} else if ((c>=' ')&&(c<='~'))
if(len<max) {
len=len+1;
receved_from_modem[len]=c;
}
} while(c!=0x0A ); //(c!=LF)||(c!=CR)||
receved_from_modem[len]=0;
//printf(lcd_putc,"\fyy%S",receved_from_modem);
}
run_manual()
{lcd_putc("\fGSM ");
disable_interrupts(global);
printf("AT+CMGR=1\n");
delay_us( 1000 );
enable_interrupts(INT_RDA);
enable_interrupts(global);
do {
Display_Time(); //display the real time hh:mm:ss
if (bit_test(ModemFlag,RecMess))
{bit_clear(ModemFlag,RecMess);
printf(lcd_putc,"\%S",receved_from_modem);
printf(lcd_putc," xxx ");}
}
while (TRUE); |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Sep 25, 2003 7:34 am |
|
|
You are not handeling errors in your USART. If you recieve more than a single byte while performing a task like formatting a string the recieve buffer will over run.
Code: |
#use rs232(baud=9600,parity=n,bits=8,xmit=PIN_C6,rcv=PIN_C7)
|
Try this change.
Code: |
#use rs232(baud=9600,parity=n,bits=8,xmit=PIN_C6,rcv=PIN_C7,errors)
|
|
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Sep 25, 2003 9:30 am |
|
|
Joris
Try this way, don't tested but it will work. Just to show how I will do it.
Let us know if this aproach is usefull for you.
Best wishes,
Humberto
Code: |
#include <16F877.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOLVP,NOPROTECT
//#use fixed_io(c_outputs=pin_C6) // >>> Not needed
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7, ERRORS)
// Define a buffer to store incoming chars
#define BUFFER_SIZE 20 // Adjust this value
#define HeaderString 'x' // Where x = Fisrt char expected in the string
receved_from_modem[BUFFER_SIZE];
//#int_RDA
//RDA_isr() {
//get_string(receved_from_modem,160); Not recommend to call a function inside the ISR.
//bit_set(ModemFlag,RecMess);
//}
#int_RDA
RDA_isr()
{
char c;
c=getc();
if(c==HeaderString)
{
len = 0;
receved_from_modem[len] = c;
len++;
}
else if ((c>=' ')&&(c<='~'))
{
receved_from_modem[len] = c;
len++;
}
if(c==0x0A ||c==0x0D) // I gues End Of String
{
bit_set(ModemFlag,RecMess); // All OK
}
if(len >= BUFFER_SIZE)
{
bit_set(ModemFlag,RecErr); // Something wrong !!
disable_interrupts(INT_RDA); // Get out & ovoid reentrance
}
}
void main() {
int b;
rtc_init();
lcd_init();
init_ext_eeprom();
enable_interrupts(INT_RDA);
enable_interrupts(global);
strcpy(OK, "OK");
while(1)
{
Display_MainMenu();
run_manual()
lcd_putc("\fGSM ");
disable_interrupts(global);
printf("AT+CMGR=1\n");
delay_us( 1000 );
// enable_interrupts(INT_RDA);
enable_interrupts(global);
Display_Time(); //display the real time hh:mm:ss
if (bit_test(ModemFlag,RecMess))
{
bit_clear(ModemFlag,RecMess);
printf(lcd_putc,"\%S",receved_from_modem);
printf(lcd_putc," xxx ");}
}
} // while
} // Main
|
|
|
|
Joris H
Joined: 24 Sep 2003 Posts: 14
|
RS232 |
Posted: Sun Sep 28, 2003 11:49 am |
|
|
Thanks for the help
I reworked a bit the sample code send by Humberto
For your info here the code for the interrupt
RDA_isr() {
int len;
char c;
do{
c=getc();
if(c==LF ) //DEL first CR and LF from the mess.
{if (len<=2)
Thanks again
Joris
{len=0;
c=0;
}}
else if (c>=' '&& c<='~')
{
receved_from_modem[len]=c;
len++;
len_mess=len; //find the # of caracters
}
if(c==LF ); //(c!=LF)||(c!=CR)|| //End of mess.
{
bit_set(ModemFlag,RecMess);
receved_from_modem[len]=0;
disable_interrupts(INT_RDA); //get out of interrupt
}
if (len>=BUFFER_SIZE) //someting went wrong
{
bit_set(ModemFlag,RecErr);
disable_interrupts(INT_RDA); //get out
}}
while (c!=LF );
len=0;
} |
|
|
densimitre
Joined: 21 Dec 2004 Posts: 45
|
|
Posted: Tue Oct 18, 2005 7:46 pm |
|
|
Hi
If I have a 16F877 in asincronous and full duplex mode, when I send a "AT <CR>" to modem, what happend?
1.- The modem answer after "A" with a "A" but PIC don�t listen up to finish send all "AT <CR>" comand???
2.- The PIC read that "A" and after send the "T" and so on?..
How can i prevent lost data?
Thank you very much |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|