Infrared detection with LED binary counter
I'm learning the Propeller chip as my first microcontroller and experimenting with building my first simple circuits and writing code for them. Spin is a good language but I prefer assembler so that is what I write. After having trouble grasping some NTSC video concepts I decided I needed to back up and do something less intense. So I wrote this infrared detection with binary LED counters for the demoboard. Comments or suggestions are welcome.
{ Infrared detection with LED binary counter
DemoBoard: IR LED pin #4, IR Receiver pin #5
Show output (number of detections) on led pins 16..23 = LSB..MSB }
CON
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
PUB main
cognew(@ir_init, 0)
DAT
org
{ ============================================ }
ir_init or ctra,_nco ' set nco mode for ir signal
or dira,_alert_leds ' set led's to output
:fraction shl _khz38,#1 ' converted fraction from spin code
cmpsub _fract,_clock wc ' in PE Kit v1.1 squarewave obj
if_c add _khz38,#1
shl _fract,#1
djnz tmp,#:fraction
mov frqa,_khz38 ' ir signal frequency
mov tmp,#1
shl tmp,#16 ' for adding to _objects
mov time,cnt ' setup ir led on delay
add time,_delay
:detect or dira,_ir_led ' turn on ir pin
waitcnt time,_delay
test _ir_receiver,ina wz ' check detection
andn dira,_ir_led ' clear ir pin
if_nz jmp #:detect ' no objects detected
add _objects,tmp ' inc #objects detected
test _objects,_maximum wc ' check for overflow
if_c mov _objects,#0 ' out of leds, rollover
if_nc andn outa,_alert_leds ' clear all leds
if_nc or outa,_objects ' #detections on _alert_leds, 8-bits
' pins 16..23 = LSB..MSB
mov _fract,#511 ' reuse _fract as counter
:chill waitcnt time,_delay
djnz _fract,#:chill
jmp #:detect
{ ============================================ }
_nco long %00100 << 26 | 4 ' nco counter + ir pin
_ir_led long 1 << 4 ' ir led
_ir_receiver long 1 << 5 ' ir receiver
_alert_leds long %1111_1111 << 16 ' motion alert led's (16..23)
_maximum long 1 << 24 ' prevents overflow to other pins
_delay long 32_000 ' ir on delay
_khz38 long 0 ' ir modulation signal
_clock long 80_000_000 ' clock freq
_fract long 76_000 ' 38khz << 1
_objects long 0 ' counter
tmp long 32 ' counter
time res 1 ' uninitialized data
{ ========= } fit { ========= }
