Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

HiTec PIC C

Status
Not open for further replies.

vdd

New Member
Hi guys,

I have a problem with using HiTec PIC C. I programmed a Pic16F84 with some simple codes such as turning on (scrolling) LEDs on portb0 to portb7 and stop. but somehow the code seems looping itself even I didn't ask it to. Please can any one help? :confused:
 
I am not familiar with HiTech C but I am guessing, something is not right with ur while loop. Check your while statements.. Unless you "break" it, the code inside a "while (1)" loop will repeat itself.
Why don't you post your code so that we can have a look.
 
In ALL C compilers, if the code even reaches the end of the main(){}, the part will reset and the code will run again.

If you want to do a job once upon power-up and then never do anything again (rare outside of the testbench), then you must have:
main(){
... do stuff...
while(1); //loop forever
}
if you have the WDT enabled, you must account for that:
main(){
... do stuff...
while(1){
CLRWDT;
} //loop forever
}
 
MrNobody said:
I am not familiar with HiTech C but I am guessing, something is not right with ur while loop. Check your while statements.. Unless you "break" it, the code inside a "while (1)" loop will repeat itself.
Why don't you post your code so that we can have a look.


I think you might have missed the question. I think the guy said he didn't ask for looping. So in his code, probably there's no while(1){ } in it but it loops anyway.
 
Oznog said:
In ALL C compilers, if the code even reaches the end of the main(){}, the part will reset and the code will run again.

If you want to do a job once upon power-up and then never do anything again (rare outside of the testbench), then you must have:
main(){
... do stuff...
while(1); //loop forever
}

Thanks, I think I nearly got it, but still little confused.
that's the problem I'm having, the code runs to the end and reset and run again. That's what I don't want. All I want is the program will stop in what ever it told to do last and Not going back to the beginning and start again.
How do I do that?
 
It sounds like you may have the WDT (watch dog timer) enabled. Have a search of the manual for WDT.

Mike.
 
vdd said:
Thanks, I think I nearly got it, but still little confused.
that's the problem I'm having, the code runs to the end and reset and run again. That's what I don't want. All I want is the program will stop in what ever it told to do last and Not going back to the beginning and start again.
How do I do that?
Read again. That's exactly what I told you to do.
while(1); //means to keep doing this loop forever- but there's NO instructions in the loop; it does not mean to repeat code lines elsewhere. All it does is the instruction is a GOTO to itself. Since the code is unconditionally stuck on this line forever then it will never get to the end of the main(){}

The second example is a single instruction in the loop- clear the WDT over and over and over.

This is an acceptable use of "while", although the original purpose was more like:
Code:
 while(LATB!=0b00001111){
    clock_pin=0;
    delay();
    clock_pin=1;
    delay();
}
which will loop and make a clock pulse until port B is 0b00001111 then exit and run the next line of code outside the loop. But if you say while(1); "1" is inherently true so no false condition will ever occur and it is an endless, empty loop.

It is also legitimate to say:
Code:
while(1){
    clock_pin=0;
    delay();
    clock_pin=1;
    delay();
    if(LATB==0b00001111){
        break; //forcefully exit the loop
    }
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top