# Timer1 operation and working of delay in below code PIC12F1822

Discussion in 'Homework Help' started by poojapatel2210, Dec 23, 2015.

1. ### poojapatel2210New Member

Joined:
Nov 5, 2015
Messages:
25
Likes:
0
****************************************
;routine to set 5S time out, use Timer 1
****************************************
Tout5SOn:
movlw 0x67
movwf TMR1H
movlw 0x6A
movwf TMR1L
goto SetToutTMR
****************************************
;routine to set 3S time out, use Timer 1
****************************************
Tout3SOn:
movlw 0xA4 ;[1/(250000/4/8)]*23435=3S ( can you explain how 23435 is calculated & overflow takes place in simple )
movwf TMR1H
movlw 0x73
movwf TMR1L
SetToutTMR:
call SetBank1
bcf PIE1,TMR1IE ;disable TMR1 int
call SetBank0

movlw b'00110100'
movwf T1CON
bsf T1CON,TMR1ON ;turn on timer 1
ClrTMR1F:
bcf PIR1,TMR1IF ;clear TMR1 overflow flag
return
Tout3SOff:
bcf T1CON,TMR1ON ;turn off timer 1
goto ClrTMR1F

Last edited: Dec 23, 2015
2. ### Les JonesWell-Known Member

Joined:
May 15, 2015
Messages:
1,446
Likes:
187
Location:
Lancashire UK
The timer increments every 1/(250000/4/8) seconds = 1/7812.5 =0.000128 seconds So to get 3 second it needs to increment 3/0.000128 = 23437 The value loaded into the timer of 0xA473 = 42099 So this value is counting 65526 (16 bits) - 42099 = 23437 This is not quite the same as the value of 23435 in the example so maybe there was a typing error.

Les.

• Thanks x 1
3. ### jpanhaltWell-Known MemberMost Helpful Member

Joined:
Jun 21, 2006
Messages:
5,899
Likes:
502
Location:
Cleveland, OH, USA
ONLINE
Sorry Les, I was writing when you posted. I get the same answer as you.

I am assuming you see how this is calculated: [1/(250000/4/8)]*23435=3S

1) Your oscillator is at 250,000 Hz
2) Instruction cycle is Fosc/4
3) Bits <4,5> of T1CON set a 1:8 prescale

Therefore each count = 1/((250,000/4)/8), which can also be written, 1/(250,000/(4*8), = 1/7812.5 =128 us per count. Then 3 seconds = 3 s /(128X10^-6) s/count = 23437.5 counts. The integer value of that is 23437 coounts.