rem PicaxeMeter.bas
rem John Saunders 3/13/2008
rem Project ID = 0 puts analog data from the DAU Controller to the meter.
rem format for PicaxeDAU, every 2 sec on C setting
rem Each record starts with a "Picaxe" then analog data in 8-bit binary
rem Order is BNC-left, BNC-Right, Diff, Yellow, Green,Temp, 200mv
rem Selector positions 1 - 7 put these on to the meter.
rem Project ID = 1 outputs the three analog inputs from DA9-P, pins 8,9 & 1
rem serial 9600 every second
rem Project ID = 2 outputs the three analog inputs from DA9-P, pins 8,9 & 1
rem to the Sd writer every minute
rem The analog inputs go to the meter for selector positions 9, 10, & 11
rem Selector position 12 goes full-scale for setting the meter with the pot at back.
#picaxe 18X
rem variables
rem persistent
symbol ProjID = b0 'The rear panel selector switch
symbol SigSelect = b1 'The position of the front panel switch
symbol Cycles = b2 'Controls analog data conversion and serial output
symbol Minutes = b3 'Increments each output record
rem re-usable
symbol Mask = b4 'Selected value for the meter
symbol BitPos = b5 'Used in for loops
symbol MeterData = b6 'Used for shifting in and out,also temperature
symbol Dummy = b7 'Read from the RS-232 input when not selected
symbol Scale = b8 'Full-scale input volts for scaling to meter
symbol Temp = w5 'For convenience in calculations
symbol Calc = w6 'For convenience in calculations
rem outputs
symbol LED = 0 'Light to indicate 3 range is in use
symbol Amp = 2 'High puts in a 5/3 gain to the digital pot
symbol SRLoad = 3 'Control Shift Register load
symbol DigPotData = 5 'Digital Pot data
symbol ShiftCLK = 6 'Control Shift Register & Digital pot clock
symbol DigPotEn = 7 'Digital Pot Not Enable
rem inputs
symbol PinNine = 1 'Pin 9 on the RS-232 connectors
symbol Pin8 = 2 'Red Jack on front panel
symbol SERData = 0 'Serial RS-232 Data, use 8 MHz
symbol SRData = INPUT6 'Serial Control Shift Register data
rem data
DATA 0,(128,64,32,16,8,4,2,1) 'Mask of bits for Shifting
symbol FileNo = 8
DATA 8,(50)
startup:
SETFREQ m8
LOW ShiftCLK
LOW LED
HIGH DigPotEn
HIGH SRLoad
LET MeterData = 0
READ FileNo, Dummy
INC Dummy
WRITE FileNo, Dummy
main:
GOTO ReadControl
readanalogs:
GOTO GetAnalogs
selectproject:
SELECT ProjID 'Project input, parameter select, and scaling
CASE 7 'The PicaxeDAU with binary serial output
BRANCH SigSelect,(Par0,Par1,Par2,Par3,Par4,Par5,Par6)
Par0:
SERIN SERData,N9600_8,("Picaxe"),MeterData,Dummy,Dummy,Dummy,Dummy,Dummy,Dummy
LET Scale = 5
GOTO GetParFSD
Par1:
SERIN SERData,N9600_8,("Picaxe"),Dummy,MeterData,Dummy,Dummy,Dummy,Dummy,Dummy
LET Scale = 5
GOTO GetParFSD
Par2:
SERIN SERData,N9600_8,("Picaxe"),Dummy,Dummy,MeterData,Dummy,Dummy,Dummy,Dummy
LET Scale = 5
GOTO GetParFSD
Par3:
SERIN SERData,N9600_8,("Picaxe"),Dummy,Dummy,Dummy,MeterData,Dummy,Dummy,Dummy
LET Scale = 5
GOTO GetParFSD
Par4:
SERIN SERData,N9600_8,("Picaxe"),Dummy,Dummy,Dummy,Dummy,MeterData,Dummy,Dummy
LET Scale = 20
GOTO GetParFSD
Par5:
SERIN SERData,N9600_8,("Picaxe"),Dummy,Dummy,Dummy,Dummy,Dummy,MeterData,Dummy
LET Scale = 10
GOTO GetParFSD
Par6:
SERIN SERData,N9600_8,("Picaxe"),Dummy,Dummy,Dummy,Dummy,Dummy,Dummy,MeterData
LET Scale = 2
GOTO GetParFSD
CASE 6 'Serial output once per second
IF Cycles > 4 THEN
LET Cycles = 0
ENDIF
IF Cycles = 1 THEN
SERTXD (#Minutes)
FOR Dummy = 86 TO 90 STEP 2
PEEK Dummy, WORD Temp
SERTXD (",",#Temp)
NEXT
SERTXD (10,13)
INC Minutes
ENDIF
PAUSE 293
CASE 5 'Writes to SD writer once per minute
PAUSE 1961
IF Cycles > 59 THEN
LET Cycles = 0
ENDIF
BRANCH Cycles,(SDTimeout,Openfile,WriteFile,CloseFile)
ELSE
LET MeterData = 125
END SELECT
GetParFSD:
LOOKUP SigSelect,(0,0,0,0,0,0,0,0,86,88,90,250),Dummy 'To meter analogs and calibration
IF Dummy > 12 THEN
IF Dummy < 100 THEN
PEEK Dummy,WORD Temp
LET MeterData = Temp / 20
LET Scale = 5
ELSE
LET MeterData = Dummy
LET Scale = 10
ENDIF
ENDIF
LOW LED
LOW Amp
SELECT Scale
CASE 2 '+ and - 200mv
HIGH Amp
IF MeterData < 100 THEN 'Negative, Light the LED
LET MeterData = 100 - MeterData
HIGH LED
ELSE
LET MeterData = MeterData - 100
ENDIF
CASE 5 'Both BNC, Diff, and Yellow
IF MeterData < 151 THEN
HIGH Amp 'Count of 150 gives full-scale
HIGH LED 'Shows the 0-3 scale is in use
ELSE
LET MeterData = MeterData/2
ENDIF
rem CASE 10 'Temperature: requires no mod 250 count is 100 deg F
CASE 20 'Green
LET Calc = 2 * MeterData
IF MeterData > 125 THEN 'Use 0-3 scale for > 10V
HIGH LED
LET MeterData = Calc/3
ELSE
LET MeterData = Calc
ENDIF
END SELECT
GOTO LoadDigPot
END
ReadControl: 'shifts the command byte then 8 bits LSB first from MeterData
PULSOUT SRLoad,1 'Load the control data into the '165
LET ProjID = 0
FOR BitPos = 4 TO 7 '3 ID bits, G is MSB
IF SRData = 0 THEN NotOne
READ BitPos,Mask
LET ProjID = ProjID | Mask
NotOne:
PULSOUT ShiftCLK,1
NEXT
LET SigSelect = 0 'Code is 0 - 5 for pos 1 - 6 & 8 - 13 for 7 - 12
FOR BitPos = 4 TO 7 '4 select bits, D is MSB
IF SRData = 0 THEN NotaOne
READ BitPos,Mask
LET SigSelect = SigSelect | Mask
NotaOne:
PULSOUT ShiftCLK,1
NEXT
IF SigSelect < 6 THEN readanalogs
LET SigSelect = SigSelect - 2
GOTO readanalogs
GetAnalogs: 'Sum five samples to give 50mv/count and store for output
IF Cycles = 0 THEN
HIGH 1
PEEK 80,WORD Temp
POKE 86,WORD Temp
PEEK 82,WORD Temp
POKE 88,WORD Temp
PEEK 84,WORD Temp
POKE 90,WORD Temp
POKE 80,0,0,0,0,0,0
LOW 1
ENDIF
IF Cycles < 5 THEN
READADC10 Pin8,Calc
PEEK 80,WORD Temp
LET Temp = Calc + Temp
POKE 80,WORD Temp
READADC10 PinNine,Calc
PEEK 82,WORD Temp
LET Temp = Calc + Temp
POKE 82,WORD Temp
READADC10 SERDATA,Calc
PEEK 84,WORD Temp
LET Temp = Calc + Temp
POKE 84,WORD Temp
ENDIF
INC Cycles
GOTO selectproject
SDTimeout:
sertxd ("S 1 5",13)
goto GetParFSD
Openfile: 'Opens the file
Read FileNo,Dummy
sertxd ("O 1 A /W",#Dummy,".CSV",13)
goto GetParFSD
WriteFile: 'Writes the data or heading
sertxd ("W 1 21",13,#Minutes)
FOR Dummy = 86 TO 90 STEP 2
PEEK Dummy, WORD Temp
SERTXD (",",#Temp)
NEXT
sertxd (13,10)
INC Minutes
goto GetParFSD
CloseFile: 'Closes the file
sertxd ("C 1",13)
INC Minutes
goto GetParFSD
LoadDigPot: 'Loads MeterData into the digital potentiometer
LOW DigPotEn
LOW DigPotData
FOR BitPos = 4 TO 7 '4 select bits, D is MSB
IF BitPos < 7 THEN FirstCmdOne
HIGH DigPotData
FirstCmdOne:
PULSOUT ShiftCLK,1
NEXT
LOW DigPotData
FOR BitPos = 4 TO 7 '4 select bits, D is MSB
IF BitPos < 7 THEN SecondCmdOne
HIGH DigPotData
SecondCmdOne:
PULSOUT ShiftCLK,1
NEXT
FOR BitPos = 0 TO 7 '8 data bits MSB first
READ BitPos,Mask
LET Mask = MeterData & Mask
IF Mask = 0 THEN
HIGH DigPotData
ELSE
LOW DigPotData
ENDIF
PULSOUT ShiftCLK,1
NEXT
High DigPotEn
Goto main