CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with serial timeout routine (Modbus related)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gregus



Joined: 26 Oct 2004
Posts: 2

View user's profile Send private message

Problem with serial timeout routine (Modbus related)
PostPosted: Tue Oct 26, 2004 12:36 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Oct 26, 2004 2:47 pm     Reply with quote

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








PostPosted: Wed Oct 27, 2004 5:49 am     Reply with quote

Gregus,

Take a look there:
http://www.ccsinfo.com/forum/viewtopic.php?t=20802

I wrote a part of code that you should find interresting Wink

Stefan.
Gregus



Joined: 26 Oct 2004
Posts: 2

View user's profile Send private message

PostPosted: Wed Oct 27, 2004 6:16 am     Reply with quote

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!!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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