Code:
'****************************************************************
'* Name : Altimeter_12F683 *
'* Author : Daniel Rascio *
'* Date : 21/01-2012 *
'* Version : *
'* Notes : *
'****************************************************************
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
' OSC1 e OSC2 are I/O, MCLR off
'*******************************************************************************
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3 'internal osc
DEFINE ADC_SAMPLEUS 250
DEFINE OSC 4 'internal 4MHz osc
'*******************************************************************************
OSCCON = %01100111 '110 4mhz / osts internal osc / HFINTOSC stable
TRISIO = %00001111 ' GPIO.0,1,2,3 input, GPIO.4,5 output
ANSEL = %00010001 'GPIO.0 - A/D in ,rest are digital /fosc/8 /right justify
WPU = 0 ' Internal pull-ups = off
CMCON0 = 7 'comparators off
ADCON0 = %10000001 ' right justify/vref=vdd/input on AN0/
GPIO = %0001111
'*******************************************************************************
n var byte 'counter for-next
B0 var byte 'open button
B1 var byte 'close button
B2 var byte 'start rs232 button
k var byte 'eeprom eraser counter
adin var word 'raw data at gpio.0
altitude var word 'actual height
anterior var word 'previous height
maxima var word 'maximum height
inicial var word 'initial height at ground
disparo var word 'deployment height
m0 var byte 'stores data to write eeprom
m1 var byte 'stores data to write eeprom
contador var byte 'eeprom counter
contador2 var byte 'initial height counter
t var word 'eeprom address counter
dado var word 'eeprom read data
'******************************************************************************
pause 500 'stabilization time at startup
Main:
button gpio.1,0,255,10,B0,1,Abrir 'opens parachute hatch
button gpio.3,0,255,10,B1,1,Fechar 'closes parachute hatch
button gpio.2,0,255,10,B2,1,Serial 'starts rs232 com.
ADCIN 0, adin 'sensor reading at AN.0
if adin > 833 then
adin=833
endif 'avoids negative values for height
if adin < 206 then
adin=206
endif
altitude = (((835-adin)*104)+35)/10 'height conversion
pause 200 'time to do calculations
if contador2 < 2 then
inicial = altitude 'stores ground height
contador2=contador2+1
endif
if (altitude-inicial) > 40 and t < 254 then
'writes eeprom if actual height is 40m higher than ground
m0 = altitude.byte0
m1 = altitude.byte1
write t,m0
write t+1,m1
t=t+2
endif
if altitude > anterior and altitude > maxima then
maxima=altitude 'stores max height if actual height is higher than
disparo=altitude-50 'previous height and maximum
write 254,maxima.byte0 'deploy height is maximum - 50
write 255,maxima.byte1
endif
if altitude < disparo then
goto Abrir 'opens parachute hatch if actual height is lower than maximum-50
endif
anterior = altitude 'stores previous height
goto Main
'******************************************************************************
'routine for open parachute
Abrir:
maxima=0
disparo=0
anterior=0
for n=1 to 2 step 1
pulsout gpio.5,150 '1,5ms keeps servo at central position (opened)
pause 22 '22ms interval between pulses
next
goto Main
'routine for close parachute
Fechar:
maxima=0
disparo=0
anterior=0
for n=1 to 2 step 1
pulsout gpio.5,100 '1ms pulse keeps servo at -90° (closed)
pause 22 '22ms interval between pulses
next
goto Main
'RS232 com. routine
Serial:
for n=0 to 254 step 2
read n,m0 'reads eeprom
read n+1,m1
dado.byte0 = m0
dado.byte1 = m1
serout gpio.4,4,[1,#n,2," ",3,#dado,4," ",13] 'prints address and value
next
t=0 'resets eeprom writer counter
serout gpio.4,4,[1,"PRESS AGAIN TO ERASE!",13]
for n=1 to 25 step 1 'waits button to be pushed to erase eeprom
pause 200
if gpio.2=0 then
serout gpio.4,4,[1,"ERASING MEMORY...PLEASE WAIT",13]
for k=0 to 255 step 1
write k,0
next
serout gpio.4,4,[1,"EEPROM ERASED!",13]
serout gpio.4,4,[1,"BACK TO MAIN PROGRAM....",13]
goto Main
endif
next
serout gpio.4,4,[1,"BACK TO MAIN PROGRAM....",13]'if button is not pushed...
goto Main
Bookmarks