View previous topic :: View next topic |
Author |
Message |
sprotch
Joined: 29 Sep 2005 Posts: 7
|
Serial ISR problem |
Posted: Fri Oct 14, 2005 5:12 am |
|
|
Hi,
I've a problem with an interrupt routine for serial communication. When enabling interrupts on INT_RDA, a NUL character is read (ASCII code 0) whithout sending anything to the serial port? I don't understand why?
I'm using PICDEM2 board with CCS compiler latest version (2.236).
Code: |
#include <16F877A.h>
#device *=16
#use delay(clock=4000000)
#include "lcd.c"
#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP
#use rs232(baud=4800, xmit=PIN_C6,rcv=PIN_C7,ERRORS)
char c;
BOOLEAN char_ready_flag = FALSE;
#INT_RDA
void serial_isr() {
c = getc();
char_ready_flag = TRUE;
}
void main(){
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(1){
if(char_ready_flag){
// Display ASCII code of read character
printf(lcd_putc,"%d",c);
char_ready_flag = FALSE;
}
}
}
|
This code outputs a '0' on LCD without receiving anything, just by enabling interrupts... |
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 14, 2005 5:28 am |
|
|
Try clearing the interrupt before enabling it.
Remember that a 'null' is what the chip will see if the logic input is low (the low will be seen as a 'start' bit, followed by all zeros for the data). It is common to find that chips like the MAX232, take a few mSec to fully power up, and garbage can easily be received by the processor in this time. So depending on your external connections, the UART is probably seeing this.
You may have to wait for the input to go high, before clearing the interrupt, and enabling the receive routine.
Best Wishes |
|
|
sprotch
Joined: 29 Sep 2005 Posts: 7
|
|
Posted: Fri Oct 14, 2005 5:53 am |
|
|
Thanks Ttelmah!
I see what you think about and try to modify my code, putting an (exagerated) delay at start and then clearing the interrupt... the result is the same...
Code: |
void main(){
lcd_init();
delay_ms(1000);
clear_interrupt(INT_RDA);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
|
|
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 14, 2005 5:57 am |
|
|
Have you actually looked at the pin with a scope or logic analyser to see what the voltage level is?. What you show now, most certainly should not show this behaviour, unless the signal really is going low for some reason.
Best Wishes |
|
|
sprotch
Joined: 29 Sep 2005 Posts: 7
|
|
Posted: Fri Oct 14, 2005 6:31 am |
|
|
The problem was that my PIC is bound to a GSM modem which is powered off at start time. It's switched on later in the code so the receive pin was seen as floating which causes my troubles...
Thanks for the help Ttelmah |
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 14, 2005 6:38 am |
|
|
As a 'fix', if you haven't already worked out a route round it, consider having a flag called something like 'not_started', then in the main program loop, if this flag is set, poll the input pin. If you see a 'high', clear the flag, and enable the interrupts. This way your PIC code (assuming it has something it can 'do' without the module available), can start working before the unit is seen. :-)
Best Wishes |
|
|
sprotch
Joined: 29 Sep 2005 Posts: 7
|
|
Posted: Fri Oct 14, 2005 6:48 am |
|
|
Thanks for the suggestion Ttelmah but it's the PIC who will power on the modem in my code and as I must catch the AT messages the modem will return me, I'm forced to have the interrupt enabled at once... I'll find a trick to avoid considering this NULL message at start time but was wondering how was that possible (receiving something without having traffic)... |
|
|
|