On Mon, Jul 01, 2013 at 10:07:16PM +0800, Electronic Consultation wrote: > the issue is, > I can't change the code from the scratch, I must use the template I=20 > provide before, What I meant about that was specifically the Timer 0 and interrupt code. I'm taking you at your word that you need a 1 second interrupt driven by timer 0. That means you ditch all the 1 mS, 10 mS, and 100 mS ticks, all the counting up code, and implement exactly what you need from scratch leaving the rest of the state machine code that has nothing to do with the timer and interrupt alone. > the state machine is ready, but the delay between state ( the interrupt=20 > with timer0) is not yet created properly... > it's seperated in two files....one for the timer itself and the other=20 > for state machine, >=20 > do you have clue ? I really do not, because you still are giving the entire picture in bits and pieces. What is the purpose of the state machine? What is the true top level project that you are trying to get done? It's almost always a mistake to try to adapt something else to solve a specific problem, especially when you are unclear on how all the pieces interoperate. But it's difficult to help you out when the true problem that is to be solved is unknown. BAJ > thanks again > On 1/07/2013 9:47 PM, Byron Jeff wrote: > > On Mon, Jul 01, 2013 at 07:29:00PM +0800, Electronic Consultation wrote= : > >> Dear Friends, > >> > >> What I'm trying to do is creating 1s interrupt on the background with > >> 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 "on= e > >> 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 ti= mer > > in bits and the number of counts required to get the necessary time. Un= less > > you use an external time source (such as 32.768 Khz crystal), the slowe= st > > input clock to all timers, including timer zero is the instruction cloc= k 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 de= lay > > 1 second. This will take 23 bits of counter to do. Timer zero is an 8 b= it > > 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 us= e the > > 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 onl= y > > divide evenly by powers of 5 (ie 5,25,125,625, etc.) in the previous li= st > > 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 6= 25 > > interrupts per second. We also still need 10 bits to count the remainin= g > > 625. So we'll have to track 2 counting variables. > > > > The most important thing is that unlike the timers, when you count by h= and, > > 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 t= hey > > get to zero. Raise a flag somewhere and reset the counters to 625 for t= he > > 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, 1= 00 > > mS etc. ticks. If you need 1S then code only for 1S. > > > > Hope this helps, > > > > BAJ > > >=20 > --=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 --=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 .