Ok,
Time to stick my neck out and say I think (I think) I've finished the TX / RX programs. I haven't connected them to the sensor / LDR to test for operation but I'd be very interested as to whether you can see any gaping errors in the code, here goes, TX first:
Code:
'****************************************************************
'* Name : rfPIC_TX.BAS *
'* Author : B. Reynolds 90% LEDave 10%...Ok 5% *
'* Notice : Copyright (c) 2007 Reynolds Electronics *
'* : All Rights Reserved *
'* Date : 10/09/2007 *
'* Version : 1.0 *
'* Notes : 2-BIT Encoder for remote control with the *
'* : rfPIC12F675 on Microchip rfPIC demo board *
'****************************************************************
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
DEFINE OSC 4
DEFINE NO_CLRWDT 1
DEFINE OSCCAL_1K 1
SYMBOL D_OUT = GPIO.2 ' RF serial data output
SYMBOL E_OUT = GPIO.5 ' RF transmitter enable output
VISITS VAR word ' Holds total daily bird visits
DAT_OUT VAR BYTE ' Holds 8-bit data byte
DAT_OUT2 VAR BYTE ' Holds 8-bit test byte
LOOPS VAR BYTE ' Loop var
CHK_SUM VAR BYTE ' Holds calculated checksum of outbound data
COUNTER VAR BYTE ' Holds WDT count values
BAUD VAR WORD ' Holds baud rate
' Constants
PreAmble CON $A5 ' 10100101 preamble
Synch CON "~" ' 01111110 synch byte
N4800 CON 188 ' 4800 bps
N2400 CON 16780 ' 2400 bps
GUARD CON 5 ' 5mS guard time pause
' hardware init
GPIO = 0 ' Everything off on boot
' GPIO.2=data out, GPIO.5=RF enable, GPIO.3,4 = Bird_visit_count / It's dark..maybe
TRISIO = %00011011 '
ANSEL = 0 ' Disable A/D
INTCON = %00001000 ' Enable port change wakeup from sleep
WPU = %00010000 ' pull-up on for GPIO.4 (external pull-up already on GPIO.3)
IOC = %00011000 ' Wakeup on change enabled for GPIO.3,4
VRCON = 0 ' Disable internal Vref
CMCON = 7 ' Disable comparators
OPTION_REG = %00001111 'Watch_Dog_Timer ON with a 128 prescaler
D_OUT = 0 ' Idles low for inverted serial output
E_OUT = 0 ' Enable for RF transmitter=0 (transmiter off)
BAUD = N2400 ' Set baud rate here
Main:
' Read port to clear missmatch, if with no buttons pressed, then
' clear int-on-change flag & snooze until wake up on change. This
' conserves battery power on the demo board with 3V coin cell.
IF GPIO.3= 1 AND GPIO.4= 1 THEN ' Nothing has happened so SLEEP
E_OUT=0 ' Disable transmitter
INTCON.0 = 0 ' No Bird detected or it's still light so clear int on change flag
@ SLEEP ' and start snoozin..zzzzzzzzzzz indefinatly
@ NOP ' Do nothing for 1st instruction on wake-up
ELSE
IF GPIO.3=0 THEN GOSUB COUNTS 'Bird detected at box - GOTO LABEL COUNTS
IF GPIO.4=0 THEN GOSUB DARK 'It's got dark? or just cloudy - GOTO LABEL DARK
ENDIF
GOTO MAIN 'Back to sleep / continue counting
COUNTS:
VISITS = VISITS +1 'Bird at box so increment counter
INTCON.0 = 0 ' Clear int on change flag
RETURN ' Then back to sleep....
DARK:
INTCON.0 = 0 ' Clear int on change flag
E_OUT=0 ' Disable transmitter
' It could be night time...It might only be a cloud though...
' so back to sleep / wake / sleep for half an hour..zwzwzw
COUNTER=0 ' clear before entry
WHILE COUNTER < 781 'Loop for 30 mins
@ SLEEP
COUNTER=COUNTER+1
IF GPIO.3= 0 THEN COUNTS 'Bird at box so continue counting
WEND
if GPIO.4= 0 then ENCODE ' It really ia dark so carry on and transmit VISITS
IF GPIO.4= 1 THEN RETURN ' It's not dark after all so carry_on_sleeping_counting
Encode:
E_OUT=1 ' Turn transmitter on
pauseus 25 ' Let transmitter settle
DAT_OUT = %10101001
INTCON.0 = 0 ' Clear int on change flag
' Build checksum of 2 data bytes
CHK_SUM = (DAT_OUT * 2)
Transmit:
LET VISITS = VISITS / 2 'Just VISITS to not from the box
SEROUT2 D_OUT,BAUD,[PreAmble,Synch,DAT_OUT,DAT_OUT,CHK_SUM,VISITS.highbyte,VISITS.lowbyte]
PAUSE GUARD ' 5mS guard time gives decoder time to respond,calculate,change,etc.
GOTO Main
END
Dave
Bookmarks