augustinetez
Active Member
Would someone be kind enough to check my test code below and see if something stands out that I'm not seeing please.
The problem:-
This is the timing loop to be used for frequency measuring in a project I'm messing about with, it calls a subroutine to check for TMR0 overflow and adjusts the various storage registers as needed. I make this subroutine to be 13 cycles long so it should be the same length as 11 NOP's + a RETURN.
But the sim (Oshonsoft) says no - timings shown in the code:
The problem:-
This is the timing loop to be used for frequency measuring in a project I'm messing about with, it calls a subroutine to check for TMR0 overflow and adjusts the various storage registers as needed. I make this subroutine to be 13 cycles long so it should be the same length as 11 NOP's + a RETURN.
But the sim (Oshonsoft) says no - timings shown in the code:
Code:
list n=0 ; Turn off page breaks in the List file
; *******************************************************************************
; * Timer Loop test *
; *******************************************************************************
ERRORLEVEL -302 ;Get rid of 'not in bank 0' warning in assembly
list p=16f628a
#include p16f628a.inc
; Set configuration bits
__CONFIG _HS_OSC & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_OFF
;
cblock 20h
CounterA
CounterB
CounterC
byte2
byte3
endc
;******** MAIN PROGRAM *********
org 0x00
goto Start
org 0x04
goto Start
Start
clrf PORTA ; clear both port registers to zero
clrf PORTB
movlw 0x07
movwf CMCON ; Turn comparators OFF on PortA
banksel TRISA ; Configure PIC I/O pins
movlw b'00000001'
movwf TRISA ; PORTA 0 input, 1:7 outputs
clrf TRISB ; PORTB all outputs
movlw b'10110111'
; Port B weak pull-ups disabled
; INTDEG Don't care
; Count RA4/T0CKI
; Count on falling edge
; Prescaler to Timer/counter
; Prescaler = /256
movwf OPTION_REG
banksel 0x00 ; Bank0
bcf INTCON,T0IF ; Clear TMR0 interupt flag to be sure
nop ; NOP's in program loop are for breakpoint usage
nop ; in simulator (Oshonsoft).
call ms_1000 ; FIRST BREAKPOINT
nop ; SECOND BREAKPOINT
nop
goto $-5
ms_1000 ; 1 second timing loop with PIC running at 10MHz
btfss PORTA,0 ; Select 1 second or 100mS timing
goto ms_100
movlw d'10'
movwf CounterC
ms_100 ; 100mS timing loop with PIC running at 10MHz
movlw D'54'
movwf CounterB
movlw D'255'
movwf CounterA
loop
call ovcount
decfsz CounterA,1
goto loop
decfsz CounterB,1
goto loop
movlw D'59'
movwf CounterA
loop2
call ovcount
decfsz CounterA,1
goto loop2
nop
nop
nop
nop
btfss PORTA,0
retlw 0 ; End 100mS timing
decfsz CounterC,1
goto ms_100
movlw d'7'
movwf CounterC
loop3
decfsz CounterC,1
goto loop3
nop
retlw 0 ; End 1 second timing
ovcount ; Fake routine for simulating, real code below
nop ; 1
nop ; 1
nop ; 1
nop ; 1
nop ; 1 Using this, timing is:-
nop ; 1 100,000uS = 100mS
nop ; 1 or
nop ; 1 1,000,000uS = 1 Sec
nop ; 1
nop ; 1
nop ; 1
return ; 2
; 13 CYCLES
;;
;;********* COUNT TMR0 OVERFLOWS *** 13 CYCLES **********************************
;;
;; Using this that is theoretically the same 13 cycle count, timing is:-
;;
;; 105552.80uS for 100mS
;; or
;; 1055528uS for 1 Sec
;;
;ovcount ; cycles
; btfsc INTCON,T0IF ; 1 or 2 if test=true
; goto ov1 ; 2 ; TMR0 overflow flag
; nop ; 1 ; No overflow.
; nop ; 1 ; Trim to match slowest flow through
; nop ; 1 ; subroutine: ovcount -> ov2
; nop ; 1
; nop ; 1
; nop ; 1
; nop ; 1
; nop ; 1
; nop ; 1
; nop ; 1
; return ; 2
; ; 13 cycles
;ov1
; incf byte2,f ; 1 ; TMR0 overflow, add 1 to highest byte
; btfsc STATUS,Z ; 1 or 2 if test=true
; goto ov2 ; 2 ; Has h_byte overflowed
; bcf INTCON,T0IF ; 1 ; Reset flag
; nop ; 1
; nop ; 1
; nop ; 1 ; Trim timing
; nop ; 1
; return ; 2
; ; 13 cycles
;
;ov2
; incf byte3,f ; 1 ; Add 1 to highest byte
; bcf INTCON,T0IF ; 1 ; Reset flag
; return ; 2
;; ; 13 cycles
end