View previous topic :: View next topic |
Author |
Message |
assimhussain
Joined: 05 Aug 2011 Posts: 9 Location: United Kingdom
|
Loop problem |
Posted: Fri Aug 12, 2011 2:40 am |
|
|
I have a strange problem with loop..
for the code below the program does not pick the while loop.. I tried to run in debug mode with animation n the arrow stops at step
it keeps on executing at while(1); but doesnt go into the loop..
it was working perfect untill today... I cant figure it out what happend.. could any1 help pls..
Code: |
#include <16F917.h>
#device ADC=8
//#use delay(clock=4000000)
void main()
{
unsigned int vin0;
int x=0;
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(0);
{
//delay_ms(500);
vin0=read_adc();
Output_B(0xFF);
setup_ccp2(ccp_pwm);
set_pwm2_duty(vin0);
setup_timer_2(T2_DIV_BY_16,255,1);
}while(1);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri Aug 12, 2011 3:32 am |
|
|
The simple reason, is that the syntax is wrong.....
executes the ';' while the value in the brackets is true. You have created a 'while' loop containing nothing.
You want to create a loop containing your code. so either:
which executes the section in brackets 'while' the condition is true, or
Code: |
do {
code
} while(1);
|
Key thing missing, is the 'do' without this, the bracketed section is evaluated once, then the while loop at the end, is executed for ever.
It _is_ executing the loop. It is just that the loop contains nothing...
Best Wishes |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: Loop problem |
Posted: Fri Aug 12, 2011 3:33 am |
|
|
assimhussain wrote: | I have a strange problem with loop..
Code: |
...
{
//delay_ms(500);
vin0=read_adc();
Output_B(0xFF);
setup_ccp2(ccp_pwm);
set_pwm2_duty(vin0);
setup_timer_2(T2_DIV_BY_16,255,1);
}while(1);
...
|
|
Because you've missed out the "do" at the beginning of the loop block. it should look like:
Code: |
do {
// loop code...
}while(1);
|
What you created was a block that executed once followed by a while loop with no code that never terminated.
RF_Developer |
|
|
assimhussain
Joined: 05 Aug 2011 Posts: 9 Location: United Kingdom
|
|
Posted: Fri Aug 12, 2011 4:27 am |
|
|
Thankx
I didn't notice it. I was trying to use other loops and end up in some other problem, and came to conclusion that 'there is some problem with my loops'.
Huh.. hw stupid ..
Thank you very much for the help. |
|
|
|