list p=16f84a
#include p16f84a.inc
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
; OSCILLATOR RC set to 800khz
cblock 0x0C ; start location of user Registers
D1,D2,D3 ; Delay work files
COUNTER ; to count Timer0
endc
org 0x00 ; first location of program memory
goto start
org 0x04 ; location of where the program jumps to when an Interrupt occurs
retfie ; not used here
start bsf STATUS, 5 ; set up the I/O Ports etc.
movlw B'00000000' ; PortA as Outputs
movwf TRISA
movlw B'00001000' ; PortB, bit 3 as input, rest as output
movwf TRISB
movlw B'00000111' ; set up timer0 , bits 0-2 prescaler 1:256, bit3 prescaler to Timer 0, bit5 using main internal clock 800khz/4 = 200khz !
movwf OPTION_REG
bcf STATUS, 5 ; Timer 0 is now running !
clrf PORTA ; turn off all ports
clrf PORTB
clrf COUNTER
; this routine counts on TMR0 then turns on a LED,
; it then does another TMR0 count and turns off the LED
; it then loops back to the begining.
; the timing formula for TMR0
; oscillator 800k / 4 = 200k processor cycle.
; timer0 prescaler 200k / 256 = 781 hz
; timer0 counter 781 / 256 = 3 hz or .33 sec
again clrf TMR0 ; clear Timer0 register so it starts counting from 0
bcf INTCON,T0IF ; Clear the TNR0 full Flag, When its full it SETS its Flag to let the system know the count is up.
tm0wait1 ; just wait for the Timer to count up
btfss INTCON,T0IF ; test to see if Tmr0 is full
goto tm0wait1 ; no loop around
bsf PORTB,4 ; turn on led
clrf TMR0 ; clear Timer0 register so it starts counting from 0
bcf INTCON,T0IF ; Clear the TNR0 full Flag, When its full it SETS its Flag to let the system know the count is up.
tm0wait2 ; just wait for the Timer to count up
btfss INTCON,T0IF ; test to see if Tmr0 is full
goto tm0wait2 ; no loop around
bcf PORTB,4 ; turn off led