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 support@ccsinfo.com

PIC16f876a is reset when it is received the serial data

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



Joined: 14 Feb 2013
Posts: 9

View user's profile Send private message

PIC16f876a is reset when it is received the serial data
PostPosted: Fri Feb 22, 2013 10:15 pm     Reply with quote

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]);
              }
      }
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

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

PostPosted: Sat Feb 23, 2013 12:53 am     Reply with quote

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:
Code:
while(!bkbhit);
?

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19383

View user's profile Send private message

PostPosted: Sat Feb 23, 2013 2:13 am     Reply with quote

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
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Feb 23, 2013 2:37 am     Reply with quote

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
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