PUB Toggle(Pin, Delay, Count)
{{Toggle Pin, Count times with Delay clock cycles in between.}}
dira[Pin]~~ 'Set I/O pin to output direction
repeat Count 'Repeat for Count iterations
!outa[Pin] ' Toggle I/O Pin
waitcnt(Delay + cnt) ' Wait for Delay cycles
PUB Toggle2(Pin, Delay, Count)
{{Toggle Pin, Count times with Delay clock cycles in between.}}
dira[Pin]~~ 'Set I/O pin to output direction
!outa[Pin]
!outa[Pin]
!outa[Pin]
!outa[Pin]
!outa[Pin]
!outa[Pin]
!outa[Pin]
This produces a train of pulses with 9.20-us width and 18.40-us
period. Again, this is not fast enough to achieve 1-us timing.
PUB Toggle3(Pin)
'Use cog's counter module to toggle at clock speed
dira[Pin]~~ 'Set I/O pin to output direction
' mode PLL BPIN APIN
ctra := %00100_000 << 23 + 1 << 9 + Pin 'Establish mode and APIN
(BPIN is ignored)
frqa := $8000_0000 'Set FRQA so PHSA[31] toggles every clock
'repeat 'infinite loop, so counter
PUB Toggle4
'toggle with assembly
cognew(@ex01A, 0)
DAT
ORG 0
ex01A
MOV DIRA, #$80 '(Cell 0) Make P7 output
MOV pattern, #0 '(Cell 1) Initialize output parameter
loop
MOV OUTA, pattern '(Cell 2) Output the pattern to P7
ADD pattern, #$80 '(Cell 3) Toggle P7 output
JMP #loop '(Cell 4) repeat loop
pattern LONG $AAAAAAAA '(Cell 5)
FIT 496
PUB Toggle5
'toggle with assembly
cognew(@HardCode, 0)
DAT
ORG 0
HardCode
MOV DIRA, #$80 '(Cell 0) Make P7 output
MOV OUTA, #0 'init to 0
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
XOR OUTA, #$80 'toggle
'now stop
COGID thisCog
COGSTOP thisCog
thisCog LONG 5 '0=SPIN, 1=keyboard, 2=mouse, 3=VGA, 4=VGA, 5=This
FIT 496
InitClock
'get initial clock time
MOV StartCNT,CNT
'Start period loop
CycleLoop
NOP 'to syncronize cycle and burst loops
NOP
BurstLoop
MOV pattern1, DMaskT0 '#$00 're-init output pattern
MOV thisCNT, CNT 'get current clock time
SUB thisCNT, StartCNT 'calc offset from period start time
'see if it is time to start pulse
CMP thisCNT, DelayA wz, wc
IF_AE OR pattern1,DMaskA
CMP thisCNT, DelayB wz, wc
IF_AE OR pattern1,DMaskB
CMP thisCNT, DelayC wz, wc
IF_AE OR pattern1,DMaskC
'see if time to end pulse
CMP thisCNT,WidthA wz, wc
IF_AE XOR pattern1,DMaskA
CMP thisCNT,WidthB wz, wc
IF_AE XOR pattern1,DMaskB
CMP thisCNT,WidthC wz, wc
IF_AE XOR pattern1,DMaskC
'output pattern
MOV OUTA, pattern1
'see if time to end period
CMP thisCNT,Period wz, wc
IF_B JMP #CycleLoop
'start new period
MOV StartCNT, CNT
DJNZ NBurst,#BurstLoop
Sadly, can only control 3 channels this way
(was hoping for 4)
Could probably do more channels with
more elaborate code, but this way is nice and simple.
Have Implemented this and posted the "Delay
Generator Demo" to user forum. Can also be found here in the
programming section..
Copyright 2007 Raymond Allen