Thanks a lot for replying, I want to make it clear my issue, I saw this kind of code on vector.asm movwf w_temp ; Saves value in register W movf STATUS ; Saves value in register STATUS movwf status_temp movf PCLATH ; Saves value in register PCLATH movwf pclath_temp .. .. .. .. and my target is : state.asm state1 .., ,, ,, return ;how can I put interrupt here based on timer.asm, I called it *A* state2 .. .. .. return timer.asm .. .. .. .. incf COUNT1, f movlw .10 ;Compare count to 100 decimal subwf COUNT1, W ;Store the result back in btfss STATUS, C ;Is the result 0, skip if no goto exittimer clrf COUNT1 ;********************************************************************** ; 100ms timer function calls ;********************************************************************** ;....... 100ms timer counter here..... in here, how can I use it (the counter) as interrupt into my *A* ;.................................... Thanks in advance On 1/07/2013 11:46 PM, Tamas Rudnai wrote: > I think the problem is that you did not see the structure of the interrup= t > handler and the rest of the code. Take a look at the Example 4 again in > > http://www.mikroe.com/chapters/view/12/appendix-b-examples/ > > I have just copied it over here logical pieces and color coded for you > (hope you can read this mail in HTML formatted mail client): > > ;************************ INTERRUPT ROUTINE *****************************= **** > org 0x0004 ; Interrupt vector > movwf w_temp ; Saves value in register W > movf STATUS ; Saves value in register STATUS > movwf status_temp > movf PCLATH ; Saves value in register PCLATH > movwf pclath_temp > > banksel PORTB ; Selects bank containing PORTB > incf PORTB ; Increments register PORTB by 1 > > banksel INTCON ; Selects bank containing INTCON > bcf INTCON,TMR0IF ; Clears interrupt flag TMR0IF > > movf pclath_temp,w ; PCLATH is given its original conten= t > movwf PCLATH > movf status_temp,w ; STATUS is given its original conten= t > movwf STATUS > swapf w_temp,f ; W is given its original content > swapf w_temp,w > > bsf INTCON,GIE ; Global interrupt enabled > retfie ; Return from interrupt routine > > Here the black ('org' directive) describes the location of the interrupt > handler. Not sure what PIC you have but n midrange this is usually should > be on address 4. > > Blue is the Prologue of the ISR (Interrupt Service Routine). That saves t= he > context of the current running environment, in this example the WREG, > STATUS and the PCLATH. > > Purple is the Epilogue, that restores the context, so that the original > running code is not getting confused. You do not need to re-enable the > global interrupt vector, I am not sure if that part is correct. RETFIE > should do that for you. > > Green is just clears the Interrupt flag of the Timer 0 - so you tell the > PIC that you have handled that already. If you do not do that the interru= pt > will be occurred again and again immediately after you return from the IS= R. > > That two red lines are the one you really need. You only need to modify > this part from this example. All you need to do is to use your own counte= r > instead of the port B. So for example you can have a "TimerCounter" or > "TMR0H" or whatever you like. With that one you will know how many times > the Timer 0 interrupt occurred. > > Now let's the the main routine: > > ;************************ MAIN PROGRAM **********************************= **** > main ; Start of the main program > banksel ANSEL ; Bank containing register ANSEL > clrf ANSEL ; Clears registers ANSEL and ANSELH > clrf ANSELH ; All pins are digital > > banksel TRISB ; Selects bank containing register TR= ISB > clrf TRISB ; All port B pins are configured as > outputs > banksel OPTION_REG ; Bank containing register OPTION_REG > bcf OPTION_REG,T0CS ; TMR0 counts pulses from oscillator > bcf OPTION_REG,PSA ; Prescaler is assign to timer TMR0 > > bsf OPTION_REG,PS0 ; Prescaler rate is 1:256 > bsf OPTION_REG,PS1 > bsf OPTION_REG,PS2 > > banksel INTCON ; Bank containing register INTCON > bsf INTCON,TMR0IE ; TMR0 interrupt overflow enabled > bsf INTCON,GIE ; Global interrupt enabled > > banksel PORTB ; Bank containing register PORTB > clrf PORTB ; Clears port Bloop > goto loop ; Remain here > end ; End of program > > So the blue one just initialises the ports of the PIC. I am pretty sure y= ou > already have done that so, basically you tell which port is input or > output, and which one is analogue or digital. > > Purple sets the Timer 0 -- in that example with a 1:256 prescaler, so you > might need to set to 1:64 instead. > > Green enables the interrupt to be occured. As soon as you enable it an > interrupt could occur! In my opinion it would be safer if you clear TMR0 > and TMR0IF right before enabling the interrupt -- so you would not have a= ny > junk before that. > > You need to replace the black one to clear your counter, so that it start= s > from zero. > > The red "loop" is the main code -- currently that is only an infinite loo= p, > but it can be anything. In there you probably need to compare your counte= r > and the current value of the timer. That way you get a 16 bit time value. > Or you may only want to check if the counter become 1 (therefore the Time= r > Interrupt has been occurred). > > When this condition met you clear the counter and then you do whatever yo= u > need to do and then return loop back to wait for the condition again. > > I hope that clarifies this a bit. > > Tamas > > > > > > On 1 July 2013 07:07, Electronic Consultation < > electronic.consultation.au@gmail.com> wrote: > >> the issue is, >> I can't change the code from the scratch, I must use the template I >> provide before, >> the state machine is ready, but the delay between state ( the interrupt >> with timer0) is not yet created properly... >> it's seperated in two files....one for the timer itself and the other >> for state machine, >> >> do you have clue ? >> 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 >> 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 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 >> delay >>> 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 >> 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 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 >>> >> -- >> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive >> View/change your membership options at >> http://mailman.mit.edu/mailman/listinfo/piclist >> > > -- http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .