|
|
View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Error #int_rda |
Posted: Tue Jul 21, 2009 5:31 pm |
|
|
Hi, I am doing a program to receive multiple string via the UART, of the many texts, only two of them will be accepted like OK, for that I'm using the interruption #int_rda. The problem is that the first time the interruption is generated and is executed routine interruptions, but not the second. The pic does not generate any interrupt, which is what I am doing wrong?
Code: | #include <18F452.h>
#include <string.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)// RS232 Estándar
// CONSTANTES /////////////////////////////////////////////////////////////////
int const lenbuff=32; // Longitud de buffer, Ajustar
// a lo que desees (o te sea posible)
// VARIABLES EN RAM ///////////////////////////////////////////////////////////
int xbuff=0x00; // Índice: siguiente char en cbuff
char cbuff[lenbuff]; // Buffer
char rcvchar=0x00; // último carácter recibido
char flagcommand=0; // Flag para indicar comando disponible
char SMS1_OK= 0;
char SMS2_OK= 0;
char flag = 0;
// Declaración de Funciones ///////////////////////////////////////////////////
void inicbuff(void); // Borra buffer
int addcbuff(char c); // añade carácter recibido al buffer
void echos(char c); // Eco selectivo sobre RS232
void procesa_comando(void); // Procesa comando
// INTERRUPCIONES /////////////////////////////////////////////////////////////
#int_rda
void serial_isr() { // Interrupción recepción serie USART
rcvchar=0x00; // Inicializo carácter recibido
if(kbhit()){ // Si hay algo pendiente de recibir ...
rcvchar=getc(); // lo descargo y ...
addcbuff(rcvchar); // lo añado al buffer y ...
}
}
void inicbuff(void){ // Inicia a \0 cbuff -------------------
int i;
int count=32;
for(i=0;i<count;i++){ // Bucle que pone a 0 todos los
cbuff[i]=0x00; // caracteres en el buffer
}
xbuff=0x00; // Inicializo el índice de siguiente
// carácter
flagcommand = 0;
flag = 0;
}
int addcbuff(char c){ // Añade a cbuff -----------------------
switch(c){
case 0x0A: // Enter -> Habilita Flag para procesar
if (flag == 3 )
flagcommand=1;
else
flag++;
break;
default:
cbuff[xbuff++]=c; // Añade carácter recibido al Buffer
}
}
void Verifica_Trama1(){
char xcommand[13];
flagcommand=0;
strcpy(xcommand,"\r$SMS1:3\r\rOK\r");
if(!strcmp(cbuff,xcommand))
SMS1_OK=1;
else
SMS1_OK=0;
}
void Verifica_Trama2(){
char xcommand[13];
flagcommand=0;
strcpy(xcommand,"\r$SMS2:5\r\rOK\r");
if(!strcmp(cbuff,xcommand))
SMS2_OK=1;
else
SMS2_OK=0;
}
// Programa Principal /////////////////////////////////////////////////////////
void main() {
enable_interrupts(int_rda); // Habilita Interrupción RDA
enable_interrupts(global);
delay_ms(200);
do{
enable_interrupts(int_rda);
SMS1_OK = 0;
inicbuff();
printf("AT+SMS1\r\n");
flag = 0;
delay_ms(100);
while (flagcommand == 0);
Verifica_Trama1();
} while (SMS1_OK == 0);
printf("Mensaje1 Recibido");
do{
SMS2_OK = 0;
inicbuff();
printf("AT+SMS2\r\n");
flag = 0;
delay_ms(100);
while (flagcommand == 0);
Verifica_Trama2();
} while (SMS2_OK == 0);
printf("Mensaje2 Recibido");
while (1);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 21, 2009 6:07 pm |
|
|
Quote: |
int const lenbuff=32;
int addcbuff(char c){ // Aÿñade a cbuff -----------------------
switch(c){
case 0x0A: // Enter -> Habilita Flag para procesar
if (flag == 3 )
flagcommand=1;
else
flag++;
break;
default:
cbuff[xbuff++]=c; // Aÿñade carÿácter recibido al Buffer
}
} |
You don't check if xbuff is incremented past the end of the buffer (cbuff).
This is not safe. It's possible that if the string is longer than 32 bytes,
then your code can overwrite other variables. You need to add a test
to prevent this.
Quote: | #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7) |
If you disable interrupts for several milliseconds, but still received some
characters, the UART could lock up. I don't see any lines in your code
that disable interrupts, but just to be safe, you should add the ERRORS
parameter to the #use rs232() statement.
Quote: | #int_rda
void serial_isr() { // Interrupción recepción serie USART
rcvchar=0x00; // Inicializo carácter recibido
if(kbhit()){ // Si hay algo pendiente de recibir ...
rcvchar=getc(); // lo descargo y ...
addcbuff(rcvchar); // lo añado al buffer y ...
}
} |
If you get an #int_rda interrupt, this means you have a "kbhit".
You don't need to call the kbhit() function. It's not necessary.
Quote: | char xcommand[13];
flagcommand=0;
strcpy(xcommand,"\r$SMS1:3\r\rOK\r"); |
Your string has 13 characters in it. But, an extra byte must be reserved
in the buffer declaration for the string terminator byte of 0x00.
You need a buffer size of at least 14 bytes. Your existing code is probably
over-writing some other variable in your program with a 0x00.
This could cause problems. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Jul 21, 2009 6:35 pm |
|
|
Hi PCM programmer, you are right, Thank |
|
|
|
|
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
|