Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

16F877A code help looping problem

Status
Not open for further replies.

shinigami.alv

Newbie level 4
Joined
Apr 20, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,334
for some reasons, my code isnt running as how it is suppose to run. can help me to figure out where is the problem? the program suppose to run like this: my PIC receives data from another PIC and pin Dx will become high if the corresponding correct bit received. and if RC2 is not low, port D will be cleared and become low. however, the pin Ds wont be all high even if i received all 4 correct bits. and also, when i put RC2 to high, it wont jump out from the while loop unless it receives another bit.
PHP:
while(1)                            
       {
                                
        if   (OERR==0)
       {
         CREN=1; 

          while (RC2==0)
         {
            no=read_packet();             
          if      (no==0b00001010)
             {
             RD2 = 1; 
             }
           else if (no==0b00001001)
            {
             RD3 = 1;
             }
            else if (no==0b00000110)
            {
            RD4 = 1;
             }
            else if (no==0b00001111)
            {
            RD5 = 1;
          }
          else
          {
           RD2 = RD2;
            RD3 = RD3;
            RD4 = RD4;
            RD5 = RD5; 
            }
          }       
          
         RD2 = 0;
         RD3 = 0;
         RD4 = 0;
         RD5 = 0; 
            
       }    
        else      CREN=0;                     //if overrun error, disable continuous receive
         }
 

may be your read_packet(); will be waiting for a byte (or a packet, I don't know how you implemented it) to receive. So if you are changing RC2 at that time, it willl not break the while loop, because the while loop will check it's condition only ones at starting of each cycle inside the loop...

while(RC2 == 0) {
while(1);
}

In the above case if you enter the firs't while loop and if you change RC2 to 1, will it exit the loop?
 

well, if it loops into 2nd while loop, it is impossible to exit to 1st while loop, right? i found that the problem is in my data receiving function, but things still don't work even if i put (RC2==0) in the read_packet function.

PHP:
unsigned char uart_rec(void)   //receive uart value
{
   unsigned char rec_data;
   while(RCIF==0);            //wait for data
   rec_data = RCREG;
   return rec_data;         //return the received data 
}



unsigned char read_packet(void)
{
   
   // Buffer for received byte.
   unsigned char received_byte;
   
   // Counter to indicate the current position of the received data packet. 
   static unsigned char counter = 0;
   
   // Buffers for the data and checksum.
   unsigned char data;
   unsigned char checksum;
  
	
   // We loop until the checksum is correct.
   do 
	{
      if (RC2==0)
         { 
            RD2 = RD2;
            RD3 = RD3;
            RD4 = RD4;
            RD5 = RD5; 
            } 
     else
      {
       RD2 = 0;
         RD3 = 0;
         RD4 = 0;
         RD5 = 0; 
            
       }     
	  // We will ignore the sync data and assume the header byte is the start of packet.
      // Keep reading until the header byte is received.
      while (uart_rec() != HEADER);
      
      // The following byte shoulde be the data byte.
      data =    uart_rec();
      
      // Then the last byte is the checksum.
      checksum =    uart_rec();
   } 
	while (checksum != (unsigned char)(HEADER + data));
   
   // If the checksum is correct, return the data.
   return data;
}

i think of an alternative way by the way. since it will always wait to receive data, all i need to do is to continuously send data packets to it. and it works. :-|
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top