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.

Timer1 operation and working of delay in below code PIC12F1822

Status
Not open for further replies.

poojapatel2210

New Member
****************************************
;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:
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.
 
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.


Is there a small adjustment for code overhead?

0xFFFF - 0xA473 = 0x5B8C = d.23436 . It rolls over at 23437 counts.

John
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top