|
|
View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Where is the error in this loop? |
Posted: Fri Sep 01, 2017 11:13 am |
|
|
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
|
|
Posted: Fri Sep 01, 2017 11:51 am |
|
|
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
|
|
Posted: Fri Sep 01, 2017 1:58 pm |
|
|
Thank you! |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Fri Sep 01, 2017 3:18 pm |
|
|
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! |
|
|
|
|
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
|