We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I'm trying to pause at while (TL0 <= 10) {} until I get a count of 10. When I run the program, the program doesn't pause at while (TL0 <= 10) {}. It's like the while is false. Can any one see something I don't? // Setup TH0 Timer0 for 16-bit counter mode TMOD = (TMOD & 0xF0) | 0x05; //Set the counter0 Run control bit. TR0 = 1; while (1) { //reset Tiimer0 (counter) TL0 = 0; // Toggle P3.4 increment the count while (TL0 <= 10) {} //The rest of the code }
Did you try to debug your code step-by-step? Sometimes it helps alot (= Your code seems correct. Maybe is it from hardware? You know, simple button generates alot of pulses when pushed. Do you use software/hardware protection against a jar? Good days!
maybe try: while( TL0 <= 10); ...though I don't recall if using {} is any different.
it would be a good idea to add TH0 and TH1 to the code, there is no 'automatic reset' when settong TR, that would kill 99% of the timer code out there. Erik
Hi, typicaly timers are clocked with 1/12 of OSC periods. So in the best case your while loop (TL0<=10) pause will be for time of ten commands' execution. So why do you thing it does not work? Maybe it works but pause is very short. Good days!
Hi, it would be a good idea to add TH0 and TH1 to the code, there is no 'automatic reset' ... Here they use while_loop based on TL0 (low byte of the timer in 16-bit mode) so it is not need to set TH0. The increasing of TL0 is not affected by value of TH0 because ticks come from OSC/12 to TL0 and then to TH0. As result TL0 always increases 0->255->... as long as TR0 is set.
Hi Oleg, you could be right, however in this case you didn't realize that the timer 0 was clocked not by oscillator but by toggling pin T0 (P3.4). "When I run the program, the program doesn't pause at while (TL0 <= 10) {}." My question: Does the program jump over "while condition" 1st - right after reset or 2nd - after "first touching" the pin T0? I guess it is the 2nd case, am I right? In my opinion the most important thing here is debouncing contact used for toggling T0 !!!
Hi Glenn, is your problem concerning real device or debugger? The program is certainly ok and it works well in real device provided that contact toggling T0 pin is debounced. It's easy to demonstrate it if you display the TL0 content on a port with appropriately connected LEDs. Of course, you must stop timer 0 immediately after while(..). It always shows 11, ie. 0x0B. In this demonstration even no contact debouncing is needed. In debugger, when the program is running @ full speed, it really works as you described. When stepping, it works well! A strange thing, in my opinion, is that timer 0 counts when toggling T0 even when program is stopped. I think it should be incremented only after at least one instruction has been executed. I suspect it can have a connection with the problem. I am convinced that this is a bug in the debugger. Eric