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

Loop problem

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



Joined: 05 Aug 2011
Posts: 9
Location: United Kingdom

View user's profile Send private message

Loop problem
PostPosted: Fri Aug 12, 2011 2:40 am     Reply with quote

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
Code:
}while(1);


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: 19535

View user's profile Send private message

PostPosted: Fri Aug 12, 2011 3:32 am     Reply with quote

The simple reason, is that the syntax is wrong.....
Code:

while(1);

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:
Code:

while(1) {
   code
}


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

View user's profile Send private message

Re: Loop problem
PostPosted: Fri Aug 12, 2011 3:33 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 12, 2011 4:27 am     Reply with quote

Thankx Smile

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 .. Surprised

Thank you very much for the help.
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