Code:
LIST P=16F506 ; DIRECTIVE TO DEFINE PROCESSOR
;--------------------------------------------------------------------------------------------------------------------------------------
; FILE DESCRIPTION
;--------------------------------------------------------------------------------------------------------------------------------------
;
; EVERY TIMER PERIOD THE MAIN CODE EXECUTES FIRST. THEN THE PROGRAM WILL WAIT FOR THE TMR0 VALUE TO REACH 255 SO THAT IT WILL KNOW
; WHEN THE TMR0 ROLLOVER IS ABOUT TO HAPPEN AND SYNCHRONIZES ON THAT ROLLOVER TO REACH INSTRUCTION CYCLE PRECISION.
;
; THE FOLLOWING CODE DEMONSTRATES HOW TIMED EVENTS CAN BE CREATED USING TIMER0, THE PERIOD IS FIXED AND IS THE PERIOD OF THE TIMER0
; ROLLOVER (DIFFERENT PRESCALER VALUES MAY BE USED)
; TIMER0 MAY BE PRELOADED FROM FOR DIFFERENT TIMING (PLEASE SEE NOTE *ADVANCED * BELOW)
;
; THE MAIN LOOP WITH USER CODE DOES NOT HAVE TO HAVE ISOCHRONOUS LOOPS ! ANYTHING UP TO 65276 INSTRUCTION CYCLES WILL DO !
;
; THIS VARIATION OF THE CODE DOES NOT USE THE PCL REGISTER AND CAN THEREFORE BE USED ANYWHERE IN THE PROGRAM MEMORY
;
;--------------------------------------------------------------------------------------------------------------------------------------
; VARIABLE DEFINITION
;--------------------------------------------------------------------------------------------------------------------------------------
DELAYCOUNT EQU H'0D' ; USED IN A FIXED DELAY
;--------------------------------------------------------------------------------------------------------------------------------------
; PROGRAM CODE
;--------------------------------------------------------------------------------------------------------------------------------------
ORG H'0000' ; RESET VECTOR
INITIALIZE MOVLW B'11000111' ; CONFIGURE TIMER0 TO RUN FROM INSTRUCTION CLOCK WITH 1:256 PRESCALER
OPTION ; WE WILL HAVE ROLLOVERS EVERY 65.5 MS (15.3 HZ) @ FOSC = 4 MHZ
MOVLW D'254' ; PRELOAD TIMER0 FOR FAST STARTUP
MOVWF TMR0 ;
;------- -----------------------------------------------------------------------------------------------
SYNCHRONIZE COMF TMR0,W ; ROUGH SYNCHRONISATION: WAIT FOR TIMER0 TO REACH 255
SKPZ ; IS TMR0 NOW 255 ?
GOTO SYNCHRONIZE ; NO, GO LOOK AGAIN
; TIMER0 NOW HAS VALUE 255, THE PRESCALER VALUE IS NOW 3,4,5 OF 6
;------- -----------------------------------------------------------------------------------------------
; HERE THERE MUST BE A DELAY OF EXACTLY 248 INSTRUCTION CYCLES WHEN USING A 1:256 PRESCALER
; (CAN BE ANY USEFUL CODE)
MOVLW D'82' ; 247 INSTRUCTION CYCLES
MOVWF DELAYCOUNT ;
DECFSZ DELAYCOUNT,F ;
GOTO $-1 ;
GOTO $+1 ; 2 INSTRUCTION CYCLES
;------- -----------------------------------------------------------------------------------------------
; FINE SYNCHRONISATION: WE WILL ARRIVE HERE JUST BEFORE THE PRESCALER ROLLS OVER (AND
; INCREMENTS TIMER0), HERE WE WILL COUNT HOW MANY INSTRUCTION CYCLES WE ARRIVED BEFORE THE
; ACTUAL ROLLOVER - WHICH GIVES US THE TIME WE NEED TO DELAY
MOVLW D'3' ; ASSUME WE WILL HAVE TO SKIP THE DELAY (JUMP THREE INSTRUCTIONS)
ADDWF TMR0,W ; W = UNCHANGED OR W = W - 1 (ADD TO THE DELAY DEPENDING ON HOW MUCH TO SOON WE ARRIVED)
ADDWF TMR0,W ; W = UNCHANGED OR W = W - 1 (ADD TO THE DELAY DEPENDING ON HOW MUCH TO SOON WE ARRIVED)
ADDWF TMR0,W ; W = UNCHANGED OR W = W - 1 (ADD TO THE DELAY DEPENDING ON HOW MUCH TO SOON WE ARRIVED)
; W IS NOW 0,1,2 OF 3 (0 = THREE CYCLES DELAY, 3 = NO DELAY)
MOVWF DELAYCOUNT ; DELAY AS MUCH AS NEEDED
BTFSC DELAYCOUNT,1 ;
GOTO $+3 ;
GOTO $+1 ;
NOP ;
BTFSS DELAYCOUNT,0 ;
GOTO $+1 ;
;------- -----------------------------------------------------------------------------------------------
PSEUDO_INT ; THE TIMER0 VALUE IS NOW 0, THE PRESCALER VALUE WILL ALWAYS BE 7
; WE WILL GET BACK HERE EVERY 65536 INSTRUCTIONS (1:256 PRESCALER)
; WE MAY USE UP TO 65273 INSTRUCTION CYCLES HERE (1:256 PRESCALER)
;MOVLW D'32' ; * ADVANCED *
;MOVWF TMR0 ; NOTE:TO CHANGE THE PERIOD, WE CAN PRELOAD TIMER0 HERE, OR, WE CAN DELAY A BIT FIRST TO CHANGE
; THE PRESCALER VALUE, AND THEN PRELOAD TIMER0)
; * ADVANCED *
NOP ; DO SOMETHING
NOP ; DO SOMETHING MORE
; ETC...
; ETC...
; ETC...
;------- -----------------------------------------------------------------------------------------------
GOTO SYNCHRONIZE ;
;--------------------------------------------------------------------------------------------------------------------------------------