dirsb = %100000 '1=Output 0=input
dirsc = %110000 '1=Output 0=input
symbol UpperPot = c.1 'Top tilt limit Pot
symbol LowerPot = c.0 'Lower tilt limit Pot
symbol TiltLed = c.7 'Tilt Led (Blue)
symbol EnabledLed = c.6 'Enabled Led (Yellow)
symbol ArmedLed = b.7 'Armed Led (Green)
symbol Tiltswitch = Pinb.6 'Tilt switch for main unit
Symbol xa = w0
Symbol xa.lsb = b0
Symbol xa.msb = b1
Symbol ya = w1
Symbol ya.lsb = b2
Symbol ya.msb = b3
Symbol za = w2
Symbol za.lsb = b4
Symbol za.msb = b5
Symbol Xaxis = w3
Symbol Yaxis = w4
symbol Upperlimit = w5 'Top tilt limit
symbol Lowerlimit = w6 'Lower tilt limit
symbol Xaverage = w7 'Average value from the GYRO
symbol Yaverage = w8
Symbol Counter = b18
symbol Xlimit = b20 'Limit flag
symbol Ylimit = b21
symbol TiltCalc = w11 'Used to calculate the tilt limits
Symbol Holdoff = b24 'Hold off timer
Holdoff=0 'When the unit turns off the output, this stops the output toggling quickly
symbol displaybank = b25
symbol fontlookup = b26
symbol subcounter = b27
symbol pixels = b19
; save Font codes in EEPROM
eeprom 0,(124,130,130,130,124) ; 0 (ASCII 48)
'The rest of the eeprom alphabet has been deleted here to keep the post under 10000 characters
hi2csetup i2cmaster, $D0, i2cfast_8, i2cbyte 'set up the GYRO
pause 1000
init:
'set clock source
'register 107 0x6B
'reset / sleep / cycle / NA / temperature disable / [2,1,0] clock source
hi2cout $6B, (%00000000)'internal 8MHz
'hi2cout $6B, (%00000001)'clk source from X gyro
'hi2cout $6B, (%00000010)'clk source from Y gyro
'hi2cout $6B, (%00000011)'clk source from Z gyro
'hi2cout $6B, (%00000100)'external 32.768kHz
'hi2cout $6B, (%00000101)'external 19.2MHz
pause 100
'gyro full scale
'register 27 0x1B
'x-test / y-test / z-test / [5,4] full scale / NA / NA / NA
hi2cout $1B, (%00000000)'+-250 degrees/s
'hi2cout $1B, (%00001000)'+-500 degrees/s
'hi2cout $1B, (%00010000)'+-1000 degrees/s
'hi2cout $1B, (%00011000)'+-2000 degrees/s
pause 100
'accel full scale
'register 28 0x1C
'x-test / y-test / z-test / [5,4] full scale / NA / NA / NA
hi2cout $1C, (%00000000)'+-2g
'hi2cout $1B, (%00001000)'+-4g
'hi2cout $1B, (%00010000)'+-8g
'hi2cout $1B, (%00011000)'+-16g
pause 100
; initialise i2c for display (ID 0x78) and then send power up instructions
hi2csetup i2cmaster,%01111000,i2cfast,i2cbyte
hi2cout 0x80,(0xAE,0x00,0x00,0xB0,0x20,0x00,0xD5,0x80,0xA8,0x1F,0xD3,0x00,0x8D, _
0x14,0xA1,0x00,0xC8,0xDA,0x02,0x81,0xCF,0xD9,0xF1, 0xDB,0x30,0xA4,0xA6)
gosub sendtodisplay ; send 4 blank lines to clear display
let displaybank = 0 ; flag rows 0-3 for display
hi2cout 0x80,(0xAF) ; turn display on ; turn display on
high TiltLed
pause 200
low TiltLed
pause 200
high EnabledLed
pause 200
low EnabledLed
pause 200
high ArmedLed
pause 200
low ArmedLed
PAUSE 100
Readadc Upperpot,Upperlimit ;Read the endstop pots
Readadc Lowerpot,Lowerlimit
;convert angles here
poke 28,"T","i","l","t"," ","d","o","w","n"," ","a","n","g","l","e"," "
poke 44," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "
poke 60,"T","i","l","t"," ","u","p"," ","a","n","g","l","e"," "," "," "
poke 76," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "
displaybank = 1 - displaybank ; toggle lines to display
gosub sendtodisplay ; load message to display memory and point display at it
PAUSE 1500
poke 28," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," " ;data to send to display (4 * 16)
poke 44," "," "," "," "," ","R","E","A","D","Y"," "," "," "," "," "," "
poke 60," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "
poke 76," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "
displaybank = 1 - displaybank ; toggle lines to display
gosub sendtodisplay ; load message to display memory and point display at it
hi2csetup i2cmaster, $D0, i2cfast_8, i2cbyte 'Switch to the GYRO
'_________________________________________________________________________________________________________________________________________________
main:
if Tiltswitch=1 then ;Loop here if unit inverted
low TiltLed
;Switch off the relay here
goto main
endif
Readadc Upperpot,Upperlimit ;Read the endstop pots
Readadc Lowerpot,Lowerlimit
for Counter=1 to 30 'Sample the gyro 30 times and average
'0x3B is accel X first register
hi2cin $3B, (xa.msb, xa.lsb, ya.msb, ya.lsb, za.msb, za.lsb)
'0x41 is temperature first
'hi2cin $41, (temp.msb, temp.lsb)
'0x3B is gyro X first register
'hi2cin $43, (xg.msb, xg.lsb, yg.msb, yg.lsb, zg.msb, zg.lsb)
Xaxis=xa/1000
Yaxis=ya/1000
Xaverage=Xaverage+Xaxis
Yaverage=Yaverage+Yaxis
next
Xaxis=Xaverage/30
Yaxis=Yaverage/30
Xaverage=0;
Yaverage=0;
Xaxis=Xaxis*10
Yaxis=Yaxis*10
'debug
if Holdoff>0 then
Holdoff=Holdoff-1
endif
if Xaxis>99 and Xaxis<200 then ;If out of X axis range, then turn off LED
Xlimit=0
Holdoff=4
endif
if Xaxis<100 and Xaxis>0 then ;Within range
Xlimit=1
endif
if Xaxis>500 and Xaxis<600 then ;If out of X axis range, then turn off LED
Xlimit=0
Holdoff=4
endif
if Xaxis<700 and Xaxis>600 then ;Within range
Xlimit=1
endif
if Yaxis>Upperlimit and Yaxis<300 then ;If out of Y axis range, then turn off LED
Ylimit=0
Holdoff=4
endif
if Yaxis<Upperlimit and Yaxis>0 then ;Within range
Ylimit=1
endif
TiltCalc=655-Lowerlimit
if Yaxis<TiltCalc and Yaxis>400 then ;If out of Y axis range, then turn off LED
Ylimit=0
Holdoff=4
endif
TiltCalc=655-Lowerlimit ;Within range
if Yaxis<655 and Yaxis>TiltCalc then
Ylimit=1
endif
if Xlimit=1 and Ylimit=1 and Holdoff=0 then ;If within limits, switch the output on
high TiltLed
;Switch on the relay here
else
low TiltLed
;Switch off the relay here
endif
goto main
'_________________________________________________________________________________________________________________________________________________
sendtodisplay: ; sends characters in memory locations 28-91 to the display
for Counter = 28 to 91
PEEK Counter, fontlookup ; load character to output
select case fontlookup
case 48 to 57 ; 0-9
fontlookup = fontlookup - 48 * 5
case 65 to 90 ; A-Z
fontlookup = fontlookup -65 * 5 + 50
case 97 to 122 ; a-z output as A-Z
fontlookup = fontlookup - 97 * 5 + 50
else
fontlookup = 244 ; all other characters will display a blank
endselect
hi2cout 0x40,(0) ; one blank column
for subcounter = 0 to 4
read fontlookup, pixels
hi2cout 0x40,(pixels) ; followed by 5 columns per character
fontlookup = fontlookup + 1
next subcounter
hi2cout 0x40,(0,0) ; two blank columns
next Counter ; this completes the 8x8 character output
If displaybank = 0 then
hi2cout 0x80,(0x40) ; point display to lines 0-3 of display memory
else hi2cout 0x80,(0x60) ; point display to lines 4-7 of display memory
Endif
return