Code:
'*******************************************************************************
'*******************************************************************************
' Twinair Lights 3
'
' Lights and gear driver for Robbe Twinair
'
' Input is a 3 positions switch, Gear servo signal.
' Outputs 2 positions servo signal 1.1 and 1.9 ms and 8 digital outputs to drive
' position lights, Cabin Lights, Landing lights, Flashes ( 3 sep. ) and Beacons ( 2 Sep. ).
' short push on button ( park pos. ) enables system to restart without power OFF
' long push ( park pos. ) enables gear to go up and lock for transportation.
' 16F1825 or equ. processor with two+ Hard PWM module.
' Use ICD2/3 or PicKit3 as a programmer !!!
' OK On September 8th 2013
' 16F1825 ... 1977 Lines
'
'*******************************************************************************
'*******************************************************************************
' Defines
#DEFINE BEC_First 1 ' Wait for signal @ Power ON
DEFINE OSC 8
DEFINE PULSIN_MAX 6500
'*******************************************************************************
'Config
#CONFIG
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _WRT_ALL & _PLLEN_OFF & _STVREN_OFF & _BORV_HI & _LVP_OFF
#ENDCONFIG
OSCCON = %01110000 ' Internal RC @ 8Mhz
OSCTUNE = %00000000 ' Internal osc Adjustment
INTCON = 0
PIE1 = 0
PIE2 = 0
CM1CON0.7 = 0
CM2CON0.7 = 0
CPSCON0 = 0
CPSCON1 = 0
DACCON0 = 0
FVRCON = 0
CCP1CON = %00001100
CCP2CON = %00001100
CCP3CON = %00000000
CCP4CON = %00000000
'*******************************************************************************
'ADC parameters
ADCON0 = 0 ' all digital. A/D disabled
ADCON1 = %10000000
ANSELA = %00000000
ANSELC = %00000000
'*******************************************************************************
' Port parameters & Aliases
OPTION_REG.7 = 0 ' Global Pull Up Enable
PORTA = %00000000
PORTC = %00000000
WPUA = %00000000
TRISA = %00101000
TRISC = %00010000
WPUC = %00010000 ' Enable Weak Pull Up for Manual Gear drive
Poslight_ VAR PORTA.0 ' Position Lights
CabinLight_ VAR PORTA.1 ' Cabin Lights
Landlight_ VAR PORTA.2 ' Landing Lights
Signal_ VAR PORTA.3 ' Gear Signal Input
Gearout_ VAR PORTA.4 ' Gear Signal Output
Batt_ VAR PORTA.5 ' Battery level Input ( 4.3v / 4.7v )
Flashlight1_ VAR PORTC.0 ' FlashLight #1
Flashlight2_ VAR PORTC.1 ' FlashLight #2
Flashlight3_ VAR PORTC.2 ' FlashLight #3
Beacon1_ VAR PORTC.3 ' Beacon #1 need CCP2 Output !
Manual_ VAR PORTC.4 ' Manual Gear retract input
Beacon2_ VAR PORTC.5 ' Beacon #2 need CCP1 Output !
'*******************************************************************************
' Variables
Servoval VAR WORD
ServovalOld VAR WORD
Servomes VAR WORD
Gearval VAR WORD
Sample VAR BYTE
I VAR BYTE
J VAR BYTE
Bad VAR BYTE
Delay VAR BYTE
HPWMVal1 VAR BYTE
HPWMVal2 VAR BYTE
HPWMVal3 VAR BYTE
HPWMValue1 VAR BYTE
HPWMValue2 VAR BYTE
Battlo VAR BIT
Beacons VAR BIT
FlashLight VAR BIT
FirstPass VAR BIT
'*******************************************************************************
' Constants
Beacon1 CON 1 ' Alias for HPWM ch.1
Beacon2 CON 2 ' Alias for HPWM ch.2
Stabsample CON 10 ' Nb of samples before command being accepted
GearDown CON 220 ' Down Gear Pulse ( x5 ms )
GearUp CON 380 ' Up Gear Pulse ( x5 ms )
Losig CON 21400 ' 22500 - Geardown*5
Upsig CON 20600 ' 22500 - GearUp*5
'*******************************************************************************
CLEAR
PORTA = %00100000 ' Batt prepos. to 1 ( Ok level )
'*******************************************************************************
'*******************************************************************************
First: ' Wait for Signal and Verify Voltage: lock if low
#IF BEC_First ' Wait for R/C Signal if Power ON First
PULSIN Signal_,1,servomes
IF ( servomes < 160 ) OR ( servomes > 440 ) THEN First
#ENDIF
PAUSE 100 ' Voltage settling time ...
IF !Batt_ OR Battlo THEN
Battlo = 1
LOW Gearout_
GOTO FIRST
ENDIF
'*******************************************************************************
' is there Someone alive here ???
Alive:
PULSIN Signal_,1,servomes
IF ( servomes < 160 ) OR ( servomes > 440 ) THEN First ' just to be sure ...
IF Servomes <= 330 THEN ' No ... Gear Pulse is NOT High
' do Place Gear switch to HIGH Gear
I = (I + 1)// 25
IF !I THEN ' Flash LandLights until High Gear
HIGH LandLight_
ELSE
LOW LandLight_
ENDIF
GOTO Alive
ENDIF
LOW LandLight_ ' Yes, Switch in the good position let's go on
I = 0
'*******************************************************************************
' Send GearUP signal to unlock gear
FOR I = 1 to 10 ' Blink PosLights for 6.8 sec
HIGH PosLight_
FOR J = 1 to 15 ' 22.5 ms*15
LOW GearOut_
PULSOUT GearOut_, GearUp
PAUSEUS Upsig
NEXT J
LOW PosLight_
FOR J = 1 to 15 ' 22.5 ms*15
LOW GearOut_
PULSOUT GearOut_, GearUp
PAUSEUS Upsig
NEXT J
NEXT I
Gearval = GearUp ' initialize Gearval
'*******************************************************************************
'Switch is in UP position and Gear is UP and unlocked ...
' We can place it down now ... without any unwanted movement.
'*******************************************************************************
'*******************************************************************************
Main:
WHILE !Manual_
WEND
FirstPass = 1
WHILE 1
'*******************************************************************************
start:
'*******************************************************************************
IF !Batt_ OR Battlo THEN
Battlo = 1
GOTO Nosignal ' Endless loop if Batt LOW
ENDIF
Start1:
PULSIN Signal_,1,servomes
Sample = sample + 1
IF ( servomes < 160 ) OR ( servomes > 440 ) THEN
Bad = ( Bad + 1 ) MIN 16 ' stop if Bad.4 = 1
IF Bad.4 THEN
GOTO nosignal ' 16 false pulses ... warning !!!
ELSE
GOTO Start ' Check closer for EACH frame ...
ENDIF
' before processing.
ENDIF
IF Sample = 0 THEN Bad = 0 ' Clear Bad only if 256 good frames (~ 5-6 secs )
' for better glitch catching !!!
Servoval = Servomes ' Signal is then good ...
IF ABS(Servomes - ServovalOld) < 3 AND I => Stabsample THEN Fast
' But we need 10 samples to avoid switching on transients !
' Let's allow +/- 10 µs jitter
IF I = 0 THEN ServovalOld = Servoval ' let's initialize Refvalue
IF ABS( Servoval - ServovalOld ) > 2 THEN ' Too different ... let's restart sampling
I = 0
LOW Gearout_
PULSOUT Gearout_, Gearval ' Continue sending last servo pulses
GOTO Start
ENDIF
I = I + 1
IF I < Stabsample THEN ' Not enough samples !
LOW GearOut_
PULSOUT Gearout_, Gearval ' Continue sending last servo pulses
GOTO Start
ENDIF
'*******************************************************************************
' We have 10 "equal" Samples: We can continue and process the command
Fast:
SELECT CASE Servoval
Case is > 330 ' Hi pos = Flight
IF FirstPass THEN ' Save lights Batt on ground
HIGH CabinLight_ ' Just CabinLights ON
GOTO Powersave
ENDIF
Beacons = 1 ' Beacons ON
FlashLight = 1 ' FlashLight ON
HIGH Poslight_ ' Position Lights ON
LOW CabinLight_ ' Cabins Lights OFF
LOW Landlight_ ' Landing Lights OFF
Powersave:
Gearval = GearUp ' Gear UP
Case is => 270 ' Mid Pos @ 300 = TakeOff
FirstPass = 0 ' Enable Lights
Beacons = 1 ' Beacons ON
FlashLight = 1 ' FlashLight ON
HIGH Poslight_ ' Position Lights ON
HIGH Landlight_ ' Landing Lights ON
LOW CabinLight_ ' CabinsLights OFF
Gearval = GearDown ' Gear Down
Case else ' Lo Pos = Parking
FirstPass = 0 ' Enable Lights
Beacons = 0 ' Beacons OFF
FlashLight = 0 ' Flashlights OFF
LOW Poslight_ ' POSITION Lights OFF
HIGH CabinLight_ ' CabinsLights ON
LOW Landlight_ ' Landing Lights OFF
Gearval = GearDown ' Gear LOW
' Check for Manual Gear UP
Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, GearUP_
END SELECT
Not_so_sure:
LOW Gearout_
PULSOUT Gearout_, Gearval
IF Beacons THEN ' Let's run the Beacons @ +/- 1Hz ( 45 x 22.5 ms )
LOOKUP HPWMVal1,[0,0,0,0,0,0,0,0,0,1,2,5,8,10,15,40,70,100,130,160,195,230,237,255 _
,237,230,195,160,130,100,70,40,15,10,8,5,2,1,0,0,0,0,0,0,0],HPWMValue1
LOOKUP HPWMVal2,[0,0,0,0,0,0,0,0,0,1,2,5,8,10,15,40,70,100,130,160,195,230,237,255 _
,237,230,195,160,130,100,70,40,15,10,8,5,2,1,0,0,0,0,0,0,0],HPWMValue2
HPWMVal1 = ( HPWMVal1 + 1 )//45
HPWMVal2 = ( HPWMVal2 + 1 )//43
HPWMVal3 = ( HPWMVal1 + 1 )//42
HPWM Beacon1 ,HPWMValue1,5000 ' Beacon1 Control
HPWM Beacon2 ,HPWMValue2,5000 ' Beacon2 Control
ELSE
HPWMVal1 = 0
HPWMVal2 = 0
HPWMVal3 = 0
HPWM Beacon1,0,5000
HPWM Beacon2,0,5000
ENDIF
IF FlashLight THEN ' Let's run the 3 double Flashlights ** @ +/- 1Hz
IF ( HPWMVal1 => 15 AND HPWMVal1 < 20 ) OR ( HPWMVal1 => 27 AND HPWMVal1 < 32 ) THEN
HIGH Flashlight1_
ELSE
LOW Flashlight1_
ENDIF
IF ( HPWMVal2 => 19 AND HPWMVal2 < 24 ) OR ( HPWMVal2 => 31 AND HPWMVal2 < 36 ) THEN
HIGH Flashlight2_
ELSE
LOW Flashlight2_
ENDIF
IF ( HPWMVal3 => 25 AND HPWMVal3 < 30 ) OR ( HPWMVal3 => 37 AND HPWMVal3 < 42 ) THEN
HIGH Flashlight3_
ELSE
LOW Flashlight3_
ENDIF
ELSE
LOW Flashlight1_
LOW Flashlight2_
LOW Flashlight3_
ENDIF
WEND
END
'*******************************************************************************
nosignal: ' Land lights Blink @ 2.5 Hz , Gear Down , others OFF
'*******************************************************************************
LOW PosLight_
LOW CabinLight_
LOW Flashlight1_
LOW Flashlight2_
LOW Flashlight3_
IF FirstPass THEN next3 ' If signal cut Gear stays UP
HPWM 1,0,500
HPWM 2,0,500
LOW Beacon1_
LOW Beacon2_
HPWMVal1 = 0
HPWMVal2 = 0
HPWMVal3 = 0
WHILE 1 ' Endless loop
HIGH LandLight_
FOR I = 1 to 10 ' ~ 200 ms
LOW Gearout_
PULSOUT Gearout_, Geardown ' Gear Down !
PAUSEUS losig
NEXT I
LOW LandLight_
FOR I = 1 to 10 ' ~ 200 ms
LOW Gearout_
PULSOUT Gearout_, Geardown ' Gear Down !
PAUSEUS losig
NEXT I
WEND
'*******************************************************************************
GearUP_: ' Button has to be pressed for more than 7 seconds to place gear UP
LOW Cabinlight_ ' Just PosLights !
' let's verify if we are really sure ...
FOR I = 1 to 3 ' Blink Poslights for 2 sec
HIGH PosLight_
FOR J = 1 to 15
LOW GearOut_
PULSOUT GearOut_, GearDown
PAUSEUS Losig
NEXT J
LOW PosLight_
FOR J = 1 to 15
LOW GearOut_
PULSOUT GearOut_, GearDown
PAUSEUS Losig
NEXT J
NEXT I
Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, pushed ' Test if Button still pushed
released: ' Let Gear down and wait
WHILE 1 ' send DownGear Signal Forever
LOW Gearout_
PULSOUT Gearout_, GearDown ' Let Gear Down !
PAUSEUS Losig
' Rearm system w/Button, no Power OFF needed... or let Gear Down if power OFF !
'
IF !Manual_ THEN ' Wait for Button release before continue
PAUSE 1000 ' Time to release button
GOTO Main
ENDIF
WEND ' NO we Continue Looping.
pushed:
FOR I = 1 to 7 ' Blink Poslights for 4.8 sec
HIGH PosLight_
FOR J = 1 to 15
LOW GearOut_
PULSOUT GearOut_, GearDown
PAUSEUS Losig
NEXT J
LOW PosLight_
FOR J = 1 to 15
LOW GearOut_
PULSOUT GearOut_, GearDown
PAUSEUS Losig
NEXT J
Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, nextI ' Test Button
I = 0 ' IF YES ... jump to nextI
GOTO Not_so_sure ' IF NO ... return
nextI:
NEXT I
next1:' is Button still pushed ???
Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, next2 ' YES ... jump to next2
GOTO Not_so_sure ' NO ... return
next2:' still pushed ??? ... you really sure ???
Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, next3 ' YES ... we proceed !!!
GOTO Not_so_sure ' NO ... return
next3:
' Switch OFF all Lights
'LOW PosLight_ ' already being done !!!
'LOW Cabinlight_ ' already being done !!!
LOW LandLight_
LOW Flashlight1_
LOW Flashlight2_
LOW Flashlight3_
HPWM Beacon1,0,5000
HPWM Beacon2,0,5000
LOW Beacon1_
LOW Beacon2_
HPWMVal1 = 0
HPWMVal2 = 0
HPWMVal3 = 0
WHILE 1 ' send UpGear Signal Forever
LOW Gearout_
PULSOUT Gearout_, GearUP ' Gear UP !
PAUSEUS Upsig
WEND
'*******************************************************************************
END
But this works more than fine ...
Bookmarks