; Version 1.0 of the PICAXE code for PiPower boards that impliment Pi heartbeat and PICAXE serial comms. ; This version transmits the byte representing the measured voltage value. ; May 26, 2014 symbol voltage = b0 symbol toggleState = b1 symbol test = b2 symbol VTemp = b3 symbol TimeSinceBeat = b4 ; symbol voltageAve = W3 symbol bitCount = b5 symbol serialTXCountdown = b6 rem RPI_Power control is pin C.0 - also the serout rem signal (to RPI) is pin C.2 rem Solar MOSFET is pin C.1 rem ADC is pin C.4 rem signal (from Pi) is pin c.3 TimeSinceBeat=0 toggleState=0 pullup $08 ;; Enable the pullup resistor for pin C.3 main: ;; debug low c.2 ; keep serial TX pin low until needed pause 1000 low c.0 ; turn on the Pi's Power. ; Voltage is measured in the solar control task (start1) if pinc.3 = 0 and toggleState = 0 then gosub toggleToOne: if pinc.3 = 1 and toggleState = 1 then gosub toggleToZero: If TimeSinceBeat > 160 then goto PowerOff: ; We've seen no heatbeat for 160 seconds. The Pi has shut down, crashed or wants a hard reboot. TimeSinceBeat = TimeSinceBeat+1 ; This counter will be kept low by heartbeat transistions serialTXCountdown = serialTXCountdown +1 if serialTXCountdown = 240 then gosub sendSerialData: goto main: toggleToOne: toggleState = 1 TimeSinceBeat = 0 return toggleToZero: toggleState = 0 TimeSinceBeat = 0 return PowerOff: high c.0 ; Relay MOSFET off... TimeSinceBeat=0 pause 30000 ; keep the device powered off for 30 seconds. if voltage > 217 then goto main: ; Put in some battery voltage hysteresis. goto PowerOff: ;; ------- Below is the Serial code. This runs in task 0 (main:) sendSerialData: suspend 1 ;; Suspend the solar charging task to dedicate all CPU to the serial transfer. VTemp = voltage gosub startBit: gosub sendData: serialTXCountdown = 0 low c.2 resume 1 ;; The solar charging process can begin again now. return sendData: for bitCount = 0 to 7 test = VTemp&1 if test = 1 then gosub highBit else gosub lowBit end if VTemp = VTemp/2 ;; Shift the byte one bit to the left. next bitCount return highBit: high c.2 pause 500 return lowBit: low c.2 pause 500 return startBit: high c.2 ;; The Pi is now awake and ready to RX the bit stream. pause 500 return ; Mult-task routine for battery charging. start1: readadc C.4, voltage if voltage < 225 then gosub SolarOn: if voltage > 235 then gosub SolarOff: pause 500 goto start1: SolarOn: high C.1 return SolarOff: low C.1 return