hippy
Ex-Staff (retired)
This code shows how to use the on-chip timers on a PICAXE-28X2 to count external pulses.
Note that both Timer 1 and Timer 3 are activated by an incoming pulse on input pin C.0, Timer 5 is activated by an incoming pulse on input pin C.2
Note that both Timer 1 and Timer 3 are activated by an incoming pulse on input pin C.0, Timer 5 is activated by an incoming pulse on input pin C.2
Code:
; *****************************************************************
; * HARDWARE *
; *****************************************************************
#Picaxe 28X2
#No_Table
#No_Data
#Terminal 9600
; *****************************************************************
; * CIRCUIT *
; *****************************************************************
; Timer 1 Timer 3 Timer 5
; -.- _ -.- _ -.-
; | _|_|_ | | _|_|_ | | |
; `---O O-->| 11 C.0 `---O O-->| 11 C.0 | _ | 11 C.0
; | 12 C.1 | 12 C.1 | _|_|_ | 12 C.1
; | 13 C.2 | 13 C.2 `---O O-->| 13 C.2
; | 14 C.3 | 14 C.3 | 14 C.3
; `------- `------- `-------
; *****************************************************************
; * SFR USAGE *
; *****************************************************************
; .---------------------------------------------------------------.
; | SFR Adresses |
; `---------------------------------------------------------------'
Symbol TMR1H = $CF
Symbol TMR1L = $CE
Symbol T1CON = $CD
Symbol T1GCON = $CC
Symbol T3GCON = $B4
Symbol TMR3H = $B3
Symbol TMR3L = $B2
Symbol T3CON = $B1
Symbol TMR5H = $50
Symbol TMR5L = $4F
Symbol T5CON = $4E
Symbol T5GCON = $4D
; .---------------------------------------------------------------.
; | Select timer being used |
; `---------------------------------------------------------------'
#Define TIMER 1
#If TIMER = 1
Symbol TMRxH = TMR1H
Symbol TMRxL = TMR1L
Symbol TxCON = T1CON
Symbol TxGCON = T1GCON
#EndIf
#If TIMER = 3
Symbol TMRxH = TMR3H
Symbol TMRxL = TMR3L
Symbol TxCON = T3CON
Symbol TxGCON = T3GCON
#EndIf
#If TIMER = 5
Symbol TMRxH = TMR5H
Symbol TMRxL = TMR5L
Symbol TxCON = T5CON
Symbol TxGCON = T5GCON
#EndIf
; .---------------------------------------------------------------.
; | TxCON Bits |
; `---------------------------------------------------------------'
Symbol TMRxCS1_BIT = bit7
Symbol TMRxCS0_BIT = bit6
Symbol TxCKPS1_BIT = bit5
Symbol TxCKPS0_BIT = bit4
Symbol TxOSCEN_BIT = bit3
Symbol TxSYNC_BIT = bit2
Symbol TxRD16_BIT = bit1
Symbol TMRxON_BIT = bit0
; .---------------------------------------------------------------.
; | TxGCON Bits |
; `---------------------------------------------------------------'
Symbol TMRxGE_BIT = bit7
Symbol TxGPOL_BIT = bit6
Symbol TxGTM_BIT = bit5
Symbol TxGSPM_BIT = bit4
Symbol TxGGO_BIT = bit3
Symbol TxGVAL_BIT = bit2
Symbol TxGSS1_BIT = bit1
Symbol TxGSS0_BIT = bit0
; *****************************************************************
; * VARIABLES USED *
; *****************************************************************
Symbol reserveW0 = w0 ; b1:b0
Symbol counter = w1 ; b3:b2
Symbol counter.lsb = b2
Symbol counter.msb = b3
; *****************************************************************
; * POWER ON RESET INITIALISATION *
; *****************************************************************
PoweOnReset:
; Stop the counter
PokeSfr TxCON, 0
; Clear the counter
PokeSfr TMRxL, 0
PokeSfr TMRxH, 0
; Comfigure gating
TMRxGE_BIT = 0 ; Gating not enabled / always count
TxGPOL_BIT = 1 ; Polarity is active high
TxGTM_BIT = 0 ; Toggle Mode disabled
TxGSPM_BIT = 0 ; Single-Pulse Mode is disabled
TxGGO_BIT = 1 ; Activate gating
TxGVAL_BIT = 0 ; Gate value, read-only
TxGSS1_BIT = 0 ; Use gate pin
TxGSS0_BIT = 0 ; Use gate pin
PokeSfr TxGCON, b0
; Start the counter running
TMRxCS1_BIT = 1 ; External clock
TMRxCS0_BIT = 0 ; External clock
TxCKPS1_BIT = 0 ; Divide by 1 Pre-Scaler
TxCKPS0_BIT = 0 ; Divide by 1 Pre-Scaler
TxOSCEN_BIT = 0 ; External LP Oscillator Not Enabled
TxSYNC_BIT = 0 ; Don't Synchronise Clock
TxRD16_BIT = 0 ; Read as two consecutive 8-bit bytes
TMRxON_BIT = 1 ; Counter Enabled
PokeSfr TxCON, b0
; *****************************************************************
; * MAIN PROGRAM LOOP *
; *****************************************************************
MainLoop:
Do
PeekSfr TMRxL, counter.lsb ; Must read LSB then MSB
PeekSfr TMRxH, counter.msb
SerTxd( #counter, " " )
Pause 500
Loop