Hi mackrackit.
Here's the Tx and Rx code as is stands. The Tx program now transmits a data WORD on button press, not just the button press itself.
Tx:
Code:
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
DEFINE OSC 4
DEFINE OSCCAL_1K 1
DEFINE NO_CLRWDT 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
I VAR word ' Delay loop count
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
PACE CON 5 ' Set CHAR transmission speed
' 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 = 0 ' Pull-ups on
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 Inputs detected or it's still light so clear int on change flag
@ SLEEP ' and start snoozin..zzzzzzzzzzz indefinatly
@ NOP
ENDIF
' E_OUT=1 ' Mackrackit, I moved this to (after delay loop)this to save power as I need a long
'PAUSEUS 25 ' Delay loop 30mins so saves Transmitter power
Delay:
FOR I = 1 TO 2 ' Delay loop 2sec test
PAUSe 1000
NEXT I
IF GPIO.3=1 and GPIO.4=1 THEN 'Button pressed then released..carry on
Encode:
E_OUT=1 ' Enable transmitter (+ lights RFEN LED on demo board)
PAUSEUS 25 ' Allow RF stage to stabilize
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 too not from the box
LET VISITS = 300 'Test data number (Bird visits)
SEROUT2 D_OUT,BAUD,PACE,[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.
endif
GOTO Main
END
When the 'Test Data' number arrives at the Rx the code flashes an led three times if DATA is TRUE.
Rx:
Code:
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _PWRTE_ON
DEFINE OSC 4
DEFINE NO_CLRWDT 1 ' Watchdog timer is disabled, so we don't need to reset it
SYMBOL D_IN = PORTC.1 ' data input pin
' Working variables
MATCH VAR BYTE ' Match variable
DAT_OUTB VAR BYTE ' Holds decoded data for output
DAT_IN1 VAR BYTE ' 1st Data byte
DAT_IN2 VAR BYTE ' 2nd Data byte for verify
CHK_SUM VAR BYTE ' Holds checksum received
CheckSum VAR BYTE ' Computed checksum of all bytes received
LOOPS VAR BYTE ' Loop var
VISITS var word ' Test BYTE with a value of 300
X VAR BYTE ' Loop counter value Byte
' Constants
Synch CON "~" ' 01111110 synch byte
N2400 CON 16780 ' 2400 bps inverted
N4800 CON 188 ' 4800 bps inverted
' hardware init
PORTA = 0
TRISA = %00111001 ' RA1 and RA2 are LED outputs
PORTC = 0 ' Clear outputs on power-up
TRISC = %00101110 ' RC1 = RF input
IOC = 0 ' Interrupt on change disabled
VRCON = 0 ' Comparator Vref disabled
CMCON0 = 7 ' Disable comparators
ANSEL = 0 ' disable A/D
OPTION_REG = 128 ' Pull-ups off
INTCON = 0
MATCH = 0 ' Clear match count
MAIN:
' Wait for Synch byte, then get new inbound data & checksum
SERIN2 D_IN,N2400,[WAIT(Synch),DAT_IN1,DAT_IN2,CHK_SUM,VISITS.HIGHBYTE,VISITS.LOWBYTE]
' / **** Begin data validation **** /
' Calculate checksum by adding 2 data bytes together
CheckSum = DAT_IN1 + DAT_IN2
' Test new checksum against one received in CHK_SUM
IF CheckSum != CHK_SUM THEN MAIN ' Failed checksum, return
' Test data bytes for match
IF (DAT_IN1) != (DAT_IN2) THEN MAIN ' Failed data comparison, return
MATCH = MATCH + 1 ' We have a match so increment match count
IF MATCH = 1 THEN DECODE ' Everything matched twice, we're good
GOTO Main ' Else do it all over
' Everything looking good - so place new data on outputs
DECODE:
PORTA = DAT_IN1 & DAT_IN2
DAT_IN1 = !DAT_IN2 ' Destroy the match
MATCH = 0
IF VISITS = 300 THEN LED
GOTO MAIN
lED:
for X = 1 to 3 'Blink led 3 times to show data has arrived (correctly)
HIGH PORTA.1
PAUSE 500
LOW PORTA.1
pause 500
next X
GOTO MAIN
END
Still much to do / change but a good day today. I've sent the Tx / Rx into a continual 'Test' loop
hasn't missed a beat so far, really pleased.
As always thanks for everyone's help, input.
Dave
Bookmarks