View previous topic :: View next topic |
Author |
Message |
chandrshekar411
Joined: 14 Feb 2013 Posts: 9
|
PIC16f876a is reset when it is received the serial data |
Posted: Fri Feb 22, 2013 10:15 pm |
|
|
I'm trying rs485 communication between two pic micro controllers that is simply sending of data to the receiver.
Transmitter: PIC16f886
Receiver: PIC16f876A
When I received the data my controller is reset. I'm continuously sending and receiving the data so my receiver continuously resetting. I received the data through INT_RDA function.
I enabled Watch Dog timer in fuses and kept Setup_wdt(WDT_2304MS);
restart_wdt(); also.
Following is the code for receiving the data
Code: |
#INT_RDA
void serial_isr()
{
int8 c;
buffer[next_in]=fgetc(RS485);
c = next_in;
next_in = (next_in+1) % sizeof(buffer);
if(next_in == next_out)
next_in=c; // Buffer full !!
}
unsigned int8 bgetc()
{
unsigned int8 c;
while(!bkbhit);
c = buffer[next_out];
next_out = (next_out+1)%sizeof(buffer);
return c;
}
void RECVNG()
{
for(i=0;i<6;i++)
{
ch[i] = bgetc();
lcd_gotoxy(1,2);
printf(lcd_putc,"d:%c%c%c",ch[0],ch[1],ch[2]);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Sat Feb 23, 2013 12:53 am |
|
|
That's not all your code, let alone all your comms code. Still, you say you have enabled the watchdog. What do you think is going to happen with this code:?
And a comment - modulo and divide operations are notoriously expensive. Your code like: Code: | next_in = (next_in + 1) % sizeof(buffer); | could be better written as: Code: | if (++next_in == sizeof(buffer)) next_in = 0; | Much shorter, and importantly for interrupts, much faster. _________________ Andrew |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19383
|
|
Posted: Sat Feb 23, 2013 2:13 am |
|
|
It is worth just adding, that the compiler, is smart enough to _not_ use the division/modulus, if the buffer size if a binary multiple. Unfortunately, CCS use modulus in their 'examples', but do not point this out. As such their code will work OK, 'as shown', but will become very bad coding, if you change the buffer size without understanding this limitation. The test version andrewg posts, is one instruction slower with a binary buffer size, but dozens of instructions faster (and avoids a potential problem if division is used in the main code), for a non binary size.
Much better.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Feb 23, 2013 2:37 am |
|
|
This is yet another duplicate of a thread which was locked yesterday.
You've already been told about using the WDT at this stage in the development process.
If your code is resetting you need to do tests to find out why, then do something about it.
In other words, learn to debug.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
|