|
|
View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
Exit for loop |
Posted: Fri Jun 02, 2017 2:06 am |
|
|
Hi ,
I'm trying to exit for loop using push button. However my code did not work as i plan.
I need it to give result as below:
0
1
2
3
0
1
2
3
art "when I push button"
However, my code output is:
0
1
2
3
art
0
1
2
3
art
Please show me how to do it.Here is my code
Code: |
#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
void main()
{
char c;
int i ,r;
unsigned char d;
unsigned char key;
usb_init_cs();
while (TRUE)
{
usb_task();
if (usb_cdc_kbhit())
{
c=usb_cdc_getc();
if (c=='\n') {putc('\r'); putc('\n');}
if (c=='\r') {putc('\r'); putc('\n');}
while(true)
{
ART:
d = usb_cdc_getc();
if(d=='A') // push A
{
while (key!=32) // push SPACE BAR to stop
{
if(usb_cdc_kbhit())
{key=usb_cdc_getc();}
init_ext_eeprom();
for(y=0;y<=1000;y++)
{
printf(usb_cdc_putc,"%d\r" y);
printf(usb_cdc_putc,"%d\r" y+1);
printf(usb_cdc_putc,"%d\r" y+2);
printf(usb_cdc_putc,"%d\r" y+3);
if(input(PIN_B6)); //push button
{
break;
}
}
printf(usb_cdc_putc,"art\r");
} key=0; // to initialize 'key' not as SPACE BAR
}
}
}
}
}
|
|
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Fri Jun 02, 2017 2:26 am |
|
|
Try moving your printf to before the break command.
Every time the for loop completes it will print as there are no testing of a condition. |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Fri Jun 02, 2017 8:35 am |
|
|
Hi alan;
i've rearrange the code as you advised,but it still give the same result.
When i compile, it shows message that below line "Code has no effect"
Code: |
if(input(PIN_B6)); //push button
|
How to solve this problem ?
Kindly please reply |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Fri Jun 02, 2017 8:59 am |
|
|
Read a C textbook....
In C, a 'statement', is a line of code, ending in a semi-colon, or a block of lines enclosed in {} brackets.
The IF statement executes the following statement.
You have a semi-colon after it (a statement...).
So what gets executed?. |
|
|
|
|
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
|