On Mon, Jul 01, 2013 at 07:29:00PM +0800, Electronic Consultation wrote: > Dear Friends, >=20 > What I'm trying to do is creating 1s interrupt on the background with=20 > Timer0, I have set Timer0 to B2 and I need a counter for it. > It's state machine turn on and turn off LED in one second, but the "one=20 > second" comes from Timer0 interrupt, > Do you get what I mean ? I do. Now I'm going to delete all of your code and let's look at the problem. The two important parameters of timers and counters are the size the timer in bits and the number of counts required to get the necessary time. Unless you use an external time source (such as 32.768 Khz crystal), the slowest input clock to all timers, including timer zero is the instruction clock of Fosc/4. Your Fosc is 20 Mhz. So your instruction clock is 5 Mhz. Since the clock ticks 5 million times a second, you need to count 5 million to delay 1 second. This will take 23 bits of counter to do. Timer zero is an 8 bit counter with a 6 bit max prescaler. That's 14 bits. So there is no way for Timer 0 to have a 1 second interrupt. Period. That discussion is over. The question now becomes what can we divide the 5000000 counts by with the counter to get the slowest interrupt rate that we can use to get to 1 second. 5000000/64 =3D 78125. Since this is still an integer, we can use th= e 1:64 prescaler, leaving us to count 78125 ticks each second. This still requires 17 bits, which again TMR0 does not have. Let's keep it simple for now and use what we can of timer0 to divide the remainder. It just so happens that 78125 is 5 to the 7th power. So that means that we can only divide evenly by powers of 5 (ie 5,25,125,625, etc.) in the previous list the largest power of 5 less than 8 bits is 125. So we set TMR0 to count up from -125 and roll over and generate the interrupt. This will give us 625 interrupts per second. We also still need 10 bits to count the remaining 625. So we'll have to track 2 counting variables. The most important thing is that unlike the timers, when you count by hand, YOU ALWAYS COUNT DOWN! PICs (like other microcontrollers) will set the Z flag when something is zero. the DECFSZ instruction is specifically designed to skip only when the decrement gets to zero. So unlike your counting up and comparing code that I delete, set the counts to 625 and count down until it is zero. So now you have it. Set the timer 0 to 1:64 and it's count to -125. It'll generate 625 interrupts per second. Count those interrupts down until they get to zero. Raise a flag somewhere and reset the counters to 625 for the next tick. Now I would advise reading the above carefully, throw out all your old code and start from scratch. Your timer setup and interrupt setup looked OK. Just be aware there is no way to get a 1s interrupt with Timer 0. Work with what you have to get there. And don't worry about intermediate 10 mS, 100 mS etc. ticks. If you need 1S then code only for 1S. Hope this helps, BAJ --=20 Byron A. Jeff Chair: Department of Computer Science and Information Technology College of Information and Mathematical Sciences Clayton State University http://faculty.clayton.edu/bjeff --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .