Some timer code
tell me how i can activat a timer in pic
This is code for a PIC18F452 but the 16F877A is similar. I had to put dots in there to try and format the code for readability. There's probably a better way to format the code in this post but I don't know how to do that yet:
movlw 0x88
movwf T0CON ....... ; enable 16-bit Timer0, internal clock, no prescale (1:1)
movlw 0x05 .......... ; enable 16-bit Timer1, inverter off, 1:1 prescale
movwf T1CON ....... ; internal instruction clock
movlw 0x8F ........... ; disable PORTB pullups, TMR0 internal clock, IRQ falling edge RB0
movwf T0CON ........ ; 1:256 prescaler assigned to WDT
Ok, I found some old 16F877A code from the same project before migrating to the 18F452. This code sets everything up:
movlw 0x05 ........... ; enable 16-bit timer1, inverter off, 1:1 prescale
movwf T1CON ........ ; internal instruction clock
movlw 0x8F ............ ; disable PORTB pullups, TMR0 internal clock, IRQ falling edge RB0
movwf OPTION_REG ; 1:256 prescaler assigned to WDT
This code actually uses the timer:
Delay .......................... ; wait for TIMER1 overflow, 50/sec (20mSec) @ 12.288 MHz
;
; CLRWDT ............................ ; kick the dog (if you're using the watchdog timer)
movwf temp2 .................... ; save W
bcf TMR1ON,0 .................... ; stop TIMER1
clrf TMR1L
movlw 0x10 ....................... ; was $10
movwf TMR1H .................... ; seed TIMER1 to $1000 to start for 20 msec delay
bsf TMR1ON,0 .................... ; start TIMER1
bcf PIR1,TMR1IF ................. ; clear overflow flag for test
delaylp btfss PIR1,TMR1IF . ; did TIMER1 overflow occur?
goto delaylp ...................... ; no, continue polling
bcf PIR1,TMR1IF ................. ; yes, clear flag and return to caller
decf timecode,F ................. ; decrement timeout counter and check for zero (timeout)
btfsc STATUS,Z .................. ; check for zero value (timeout)
bsf timeout ....................... ; timeout occurred, set flag and reset
incf ticks,F ......................... ; increment tick counter
movf ticks,W
xorlw 0x32 ........................ ; see if 50 ticks have passed
btfss STATUS,Z
goto $+3
incf seconds,F ................... ; increment seconds timer
clrf ticks ............................ ; reset tick counter
movf temp2,W .................. ; restore W
return
This code is pretty well documented but you'll probably need to read the relevant sections of the PIC16F877A datasheet in order to understand what's going on. Hope this helps.
- Phil