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.

Timers in PIC

Status
Not open for further replies.

VRL_15

New Member
Is it possible to use the same timer for example TMR1 for different constant delays?

if I use TMR1 for 0.5 sec how can I use a register as a timer for obtaining 1 sec constant delay?

If anyone has an idea please give your advice

Thank you
 
Change the prescaler value for each delay. The prescaler changes how many instruction clock cycles have to happen before the timer register gets incremented.

A 1:1 prescaler will increment the timer register on each instruction clock cycle. A 1:16 prescaler assignment will increment the timer register every 16 instruction clock cycles, etc etc.

Another way to do it would be to set up a register as a "counter" register that gets decrimented by 1 each time the timer interrupt flag gets set (the interrupt flag is set upon the timer register rolling over from 0xFF back to 0x00), then branch upon the counter register being decrimented to zero. By loading the counter register with different values you should be able to get different delays. This is essentially what you're doing when you use a nested delay loop, only you're decrimenting a GP register rather than using the timer.
 
Last edited:
I have TMR0 which is 10 ms timer

I wish to use GP register as a 1 second timer using TMR0 timer of 10ms

I tried the following code but didnt get the result
btfsc INTCON,2
decf count2,0 // count2 is 100 in decimal
btfsc STATUS,Z
'''''''''''remaining code

after each time the TMR0 overflows i decremented count2 and this will repeat 100 times which should have given 1 second

Please let me know your ideas on this
 
I have TMR0 which is 10 ms timer

I wish to use GP register as a 1 second timer using TMR0 timer of 10ms

I tried the following code but didnt get the result
btfsc INTCON,2
decf count2,0 // count2 is 100 in decimal
btfsc STATUS,Z
'''''''''''remaining code

after each time the TMR0 overflows i decremented count2 and this will repeat 100 times which should have given 1 second

Please let me know your ideas on this

First off...you need to change the designator on your decriment instruction to 1 or F. With the 0 there, it's saving the decrimented result in W while keeping the number in COUNT2 the same so it will never equal zero, and thus will never branch. If you change it to a 1 or F, it will store the decrimented result back into COUNT2 instead.

Second off, the TMR0 interrupt flag (T0IF in INTCON) must be cleared in software everytime it gets set.

Furthermore, if you use "decfsz" instead of "decf" and "btfss STATUS,Z", you won't have to check the Z flag as decfsz means DECriment File proceeding instruction and Skip next instruction if file equals Zero. The F after the comma which proceeds the file address/label tells it to store the decrimented result back into the file.

So it should look like this -

Code:
Delay
		clrf		TMR0			;start TMR0 counting at 0
		bcf		INTCON,T0IF		;clear TMR0 interrupt flag
		movlw		d'100'			;set the timer interrupt counter
		movwf		count2			;to decimal 100
W8		btfsc 		INTCON,T0IF		;check TMR0 interrupt flag
		goto		W8			;branch if set
		bcf		INTCON,T0IF		;clear TMR0 interrupt flag
		decfsz 		count2,F 		;decriment interrupt counter by 1
		goto		W8			;branch if zero
		return

Instead of this -

Code:
btfsc INTCON,2
decf count2,0 // count2 is 100 in decimal
btfsc STATUS,Z

The basic flowchart for my code would be -

**broken link removed**

With the return instruction, you can call that as a subroutine labeled "Delay" anytime you wish to implement the 1 second delay with a simple "call Delay" instruction.

Again you can use the timer prescaler to slow down the timer count.
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top