View previous topic :: View next topic |
Author |
Message |
Gregus
Joined: 26 Oct 2004 Posts: 2
|
Problem with serial timeout routine (Modbus related) |
Posted: Tue Oct 26, 2004 12:36 pm |
|
|
I'm trying to implement the Modbus protocol on my PIC18F452. I am using code I found on this forum as a guide but am having a lot of trouble with the the RS232 timeout routine. Basically when a byte is received it resets the timer to a predefined value (I'm using 0 for testing purposes) and then enables the timer overflow interrupt which will trigger once the flow of data on the RS232 stops.
The problem i'm having is no matter how many bytes I send at a time I always trigger the overflow interrupt after the first byte, and after that it works as expected, triggering the overflow interrupt after the timeout period following the final byte.
Does anyone have any idea what's going on here? I tried multiple chips, and multiple sources of RS232 data (Straight from PC, and from one PIC to another).
Here is the very simple code i'm using for reference. Please help me!
Code: |
#include <18f452.h>
#fuses HS, PUT, NOBROWNOUT, NOPROTECT, NOLVP, NOWDT
#use delay (clock = 20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
byte inByte = 0x00;
//Receive routine
#INT_RDA
void RDA_isr()
{
inByte = getc();
set_timer1(0); // Reset timer1 to maximum timeout value
enable_interrupts(INT_TIMER1); // Enable Timer1 so timeout will occur when data stops
}
//COMM1 Receive Complete timer Interrupt
#INT_TIMER1
void TIMER1_isr()
{
disable_interrupts(INT_TIMER1); // Disable Timer1 until next message received.
}
void main()
{
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(true);
} |
Greg |
|
|
yerpa
Joined: 19 Feb 2004 Posts: 58 Location: Wisconsin
|
|
Posted: Tue Oct 26, 2004 2:47 pm |
|
|
Maybe you should clear the timer flag before you enable interrupts. By the time you enable the timer interrupt, the flag is probably already true from a previous timer overflow (underflow?), so an immediate interrupt would be generated. I don't use the PIC 18 series (yet), so I could be way off base, but this same situation has occured for me with other microprocessors. |
|
|
Guest
|
|
|
Gregus
Joined: 26 Oct 2004 Posts: 2
|
|
Posted: Wed Oct 27, 2004 6:16 am |
|
|
You hit the nail right on the head... set the Timer1 Overflow bit to 0 before enabling interrupts and it worked perfect.
THANKS SO MUCH!!!! |
|
|
|