Here's a neat trick to get 1MHz with a 20MHz oscillator. This takes 5 instruction cycles to toggle the pin, and Timer1 keeps track of the toggle count for you.
You don't need to use any incrementing or decremeting loops or variables.
1. Set T1CKI pin, and make the pin an output.
2. Load Timer1 low & high registers with 65,536 - the number of clocks to output & count.
3. Setup Timer1 for external clock, 1:1 prescaler, and turn it on.
T1CKI outputs your clock while Timer1 count increments on every low-to-high transition on T1CKI.
Code:
PORTC.0 = 1 ' set pin so 1st low-to-high increments count
TRISC.0 = 0 ' make pin an output
Main:
TMR1H = $F0 ' 65,536 - 4096 = 61,440 = $F000
TMR1L = $00 ' so 4096 toggles will set TMR1IF
T1CON = %00000011 ' 1:1 prescaler, external clock, Timer1 on
ASM
Pulse
BCF PORTC,0 ; clear T1CKI pin
BSF PORTC,0 ; set T1CKI pin
BTFSS PIR1,TMR1IF ; when TMR1 overflows, count is complete
GOTO Pulse ; loop until Timer1 overflow
BCF PIR1,TMR1IF ; clear TMR1 overflow flag bit
ENDASM
When PIR1,TMR1IF = 1 you have 4096 clocks. I tested this on a 16F877A, so just change the T1CKI pin to whatever it is on your PIC type.
Bookmarks