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

Where is the error in this loop?

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



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

Where is the error in this loop?
PostPosted: Fri Sep 01, 2017 11:13 am     Reply with quote

Hi,

I am using the interrupts of the uart to give me options to get out of the loop, but for more that I introduce the correct character I can not get out of the loop, my goal is if I enter any of the characters ('N', 'b', 'h' 'H', 'P') must be out of the loop ... someone can tell me what my mistake is ...

Code:
#include <18F4620.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)// RS232 Estándar

char Keypress=' ';

#int_rda
void serial_isr() {

   Keypress=0x00;
   if(kbhit()){
      Keypress=getc();
      if(Keypress!=0x00){
         putchar(keypress);
         keypress=0x00;
      }
   }
}

void main() {

   enable_interrupts(global);
   enable_interrupts(int_rda);

   printf("\r\n\Listen on RS232 \r\n");

   do {
           

   } while ((Keypress != 'N') && (Keypress != 'b') && (Keypress != 'h') && (Keypress != 'H') && (Keypress != 'P'));

printf("\r\n\The End\r\n");

while(1);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 01, 2017 11:51 am     Reply with quote

Quote:
void serial_isr() {

Keypress=0x00;
if(kbhit()){
Keypress=getc();
if(Keypress!=0x00){
putchar(keypress);
keypress=0x00;
}
}
}

You get a key, such as 'N', but then you set 'Keypress' to 0x00 before
you leave the isr, as shown in bold above.

The PIC then exits the #int_rda function with 'Keypress' = 0x00, so
you stay in the do-while loop.

Try doing this instead:
Code:
#int_rda
void serial_isr()
{
Keypress=getc();
putc(keypress);
}

Also, you don't need kbhit(). If you are in the #int_rda function, it is
guaranteed that you have a kbhit.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Fri Sep 01, 2017 1:58 pm     Reply with quote

Thank you!
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Sep 01, 2017 3:18 pm     Reply with quote

Hi,

A more important point is that to be a successful programmer you've got to learn to troubleshoot your own code! In this case, a print statement in your 'Do' loop showing the value of 'Keypress' would have given you the clues immediately to solve this issue yourself!
_________________
John

If it's worth doing, it's worth doing in real hardware!
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