; ---------------------------------------------------------- ; -- AIR COMMAND - Water Rocket Flight Software V1.4 -- ; -- Copyright (c) 2007 GKDesign Engineering -- ; -- Author: George Katz 10/6/07 -- ; ---------------------------------------------------------- ; PORT A ; desig. bit dir pin function ; RA0 0 in 17 'Arm' button. Arms flight computer ready for launch. ; RA1 1 in 18 'Burnout Detect' switch. Activated when the rocket booster's internal pressure drops to near zero. ; RA2 2 in 1 'Program' button. When in standby mode, a new program sequence can be selected. ; RA3 3 out 2 - reserved for analog apogee sensor - not used in this version. ; RA4 4 out 3 - not used ; RA5 5 in 4 - used as master reset ; RA6 6 out 15 - staging actuator ; RA7 7 out 16 - parachute deploy actuator. ; PORT B ; desig. bit dir pin function ; RB0 0 out 6 7 segment LED display - 'c' segment ; RB1 1 out 7 7 segment LED display - 'b' segment ; RB2 2 out 8 7 segment LED display - 'a' segment ; RB3 3 out 9 7 segment LED display - 'f' segment ; RB4 4 out 10 7 segment LED display - 'g' segment ; RB5 5 out 11 7 segment LED display - 'e' segment ; RB6 6 out 12 7 segment LED display - 'd' segment ; RB7 7 out 13 ARMED LED LIST P=16F628A, R=DEC ; Use the PIC16F628 and decimal system #include "P16F628A.INC" ; Include header file errorlevel -302 __config _INTRC_OSC_NOCLKOUT & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF & _MCLRE_OFF CBLOCK 0x20 ; Declare variable addresses starting at 0x20 Loop1 Loop2 Loop3 Program ; The program index - which program to run. (The delay index) Armed ; bit 7 of this byte represents whether the FC is armed or not. (all other bits should be 0) 1 = armed. 0 = standby Message ; Used for displaying other status information on the display ENDC ; defines ARM_BTN EQU 0 LAUNCH_DETECT_BTN EQU 1 PROGRAM_BTN EQU 2 ; ; ----------- ; INITIALIZE ; ----------- ; ORG 0x000 ; Program starts at 0x000 CLRF PORTA ; Initialize port A CLRF PORTB ; Initialize port B BSF STATUS,RP0 ; switch to RAM bank 1 MOVLW 07h ; set bits RA0, RA1 and RA2 as inputs, the rest outputs MOVWF TRISA CLRF TRISB ; All pins port B output BCF STATUS,RP0 ; switch to RAM bank 0 ; ; ------------------------ ; FUNCTION OF PORT A PINS ; ------------------------ ; MOVLW 7 MOVWF CMCON ; Comparators off, all pins digital I/O GOTO Start ; jump over the table ; ; ----------------------- ; LED conversion table ; ----------------------- ; dec2led ADDWF PCL,1 ; dpdegfabc RETLW b'01101111' ; 0 0.0 sec RETLW b'00000011' ; 1 0.25 RETLW b'01110110' ; 2 0.5 RETLW b'01010111' ; 3 0.75 RETLW b'00011011' ; 4 1.0 RETLW b'01011101' ; 5 1.25 RETLW b'01111101' ; 6 1.5 RETLW b'00000111' ; 7 1.75 RETLW b'01111111' ; 8 2.0 RETLW b'01011111' ; 9 2.25 RETLW b'00111111' ; A 2.5 RETLW b'01111001' ; b 2.75 RETLW b'01101100' ; C 3.0 RETLW b'01110011' ; d 3.25 RETLW b'01111100' ; E 3.5 RETLW b'00111100' ; F 3.75 RETLW b'01101101' ; G 4.0 RETLW b'00111001' ; h 4.25 RETLW b'00000001' ; i 4.5 RETLW b'01100011' ; J 4.75 RETLW b'00111000' ; k 5.0 RETLW b'01101000' ; L 5.25 RETLW b'00110101' ; m 5.5 RETLW b'00110001' ; n 5.75 RETLW b'01110001' ; o 6.0 RETLW b'00111110' ; p 6.25 RETLW b'00011111' ; q 6.5 RETLW b'00110000' ; r 6.75 RETLW b'00011001' ; s 7.0 RETLW b'01111000' ; t 7.25 RETLW b'01101011' ; u 7.5 RETLW b'01100001' ; v 7.75 RETLW b'01100101' ; w 8.0 RETLW b'01101000' ; L - Launch delay //33 RETLW b'00010000' ; '-' - Parachute deploy less drain on battery //34 RETLW b'01010100' ; '///' - staging //35 ; ; ---------- ; MAIN LOOP ; ---------- ; Start CLRF Program ; initialise the Program variable to be 0 CLRF Message ; initialise the Message variable to 0 CLRF Armed ; clear the armed bit CALL LEDOut ; output current Program setting to display CALL DeployClose ; set the parachute release actuator to the default position CALL MotorDly ; leave a short wait CALL StagingClose ; set the staging actuator to the default position prgbtn_up BTFSS PORTA, ARM_BTN ; check if the ARM button is down (normally high) GOTO LaunchArmed ; ARM button was pressed BTFSC PORTA, PROGRAM_BTN ; check if we are pressing the program change button (normally high) GOTO prgbtn_up ; no we are not so check buttons again prgbtn_down BTFSS PORTA, ARM_BTN ; check if the launch button is down (normally high) GOTO LaunchArmed ; ARM button was pressed BTFSS PORTA, PROGRAM_BTN ; check if we are letting go of the program change button GOTO prgbtn_down ; nope still holding it down INCF Program,1 ; Program button has been down and now up, so we increment the Program variable MOVLW 33 ; check for overflow and wrap after 33 SUBWF Program,w SKPNC CLRF Program CALL LEDOut ; output the new program index to the display GOTO prgbtn_up ; go back to listening for keys LaunchArmed BSF Armed,7 ; Turn on the Armed flag. CALL LEDOut ; Turn on 'Armed' LED to indicate system is armed. ; Rocket waits for the burn-out detect event LnchDetect BTFSC PORTA, LAUNCH_DETECT_BTN ; See if launch burn-out switch goes low since it is normally high. GOTO LnchDetect ; loop endlesly until burn-out is detected ; indicate staging activation MOVLW 35 ; 35 is the '///' symbol MOVWF Message ; store that value in the message variable CALL LEDOut2 ; display it on screen ; as soon as we detect burn-out, we tell the staging actuator to start activating CALL StagingOpen ; note that the staging motor runs for up to two seconds ; therefore the minimum amount of time until parachute deployment, is this delay. ; staging would have now occured.... ; now we wait while the booster coasts to apogee ; display the fact the computer is in the chute deploy delay MOVLW 33 ; 33 is the 'L' symbol MOVWF Message ; store that value in the message variable CALL LEDOut2 ; display it on screen ; Wait the appropriate delay to deploy the chute setting the correct delay of 0 seconds + delay index (Program) MOVLW 1 ; We wait a minimum of 0 seconds ADDWF Program,0 ; Add the program offset to the 0 seconds already in the W register MOVWF Loop3 ; store the total number of loops we have to go through Inhibit CALL MotorDly DECFSZ Loop3,F GOTO Inhibit ; Finished our delay so now we should deploy CLRF Armed ; Turn off the 'Armed' LED MOVLW 34 ; 34 is the '-' symbol MOVWF Message ; store that value in the message variable CALL LEDOut2 ; display it on screen ; deploy the parachute by driving the output for a fixed period to give the motor ; enough time to run properly. deploy CALL DeployOpen ; deploy the parachute CALL MotorDly ; wait 1 second before going back to the start CALL MotorDly CALL MotorDly CALL MotorDly GOTO Start ; go back and await instruction to start again, when returning the actuators will also return to default positions ; --------------- ; Motor driving DELAY 250 MSEC ; --------------- MotorDly MOVLW 250 ; Set the right amount of time. MOVWF Loop1 MOuter MOVLW 200 MOVWF Loop2 MInner NOP NOP DECFSZ Loop2,F GOTO MInner ; Inner loop = 5 usec. DECFSZ Loop1,F GOTO MOuter RETURN ; --------------- ; LED output sub routine. Calling this routine displays ; the status of the Program index as well as the Armed bit status ; --------------- LEDOut MOVF Program,0 ; load the Program index into W CALL dec2led ; convert the Program index to LED bits IORWF Armed,0 ; OR with the Armed bit (dp) MOVWF PORTB ; write value out to port B RETURN ; --------------- ; LED output sub routine. Calling this routine displays ; the status of the Message index as well as the Armed bit status ; --------------- LEDOut2 MOVF Message,0 ; load the Message index into W CALL dec2led ; convert the Program index to LED bits IORWF Armed,0 ; OR with the Armed bit (dp) MOVWF PORTB ; write value out to port B RETURN ; ------------------------------ ; PWM for servo open DeployOpen MOVLW 100 ; 2 seconds worth of pulses MOVWF Loop3 PWMOn2 BSF PORTA,7 ; pulse on CALL Wait2ms BCF PORTA,7 ; pulse off CALL Wait20ms DECFSZ Loop3,F GOTO PWMOn2 RETURN DeployClose MOVLW 100 ; 2 seconds worth of pulses MOVWF Loop3 PWMOn1 BSF PORTA,7 ; pulse on CALL Wait1ms BCF PORTA,7 ; pulse off CALL Wait20ms DECFSZ Loop3,F GOTO PWMOn1 RETURN ;-------------------------------------------- StagingOpen MOVLW 100 ; 2 seconds worth of pulses MOVWF Loop3 PWMOn3 BSF PORTA,6 ; pulse on CALL Wait2ms BCF PORTA,6 ; pulse off CALL Wait20ms DECFSZ Loop3,F GOTO PWMOn3 RETURN StagingClose MOVLW 100 ; 2 seconds worth of pulses MOVWF Loop3 PWMOn4 BSF PORTA,6 ; pulse on CALL Wait1ms BCF PORTA,6 ; pulse off CALL Wait20ms DECFSZ Loop3,F GOTO PWMOn4 RETURN ;-------------------------------------------- Wait20ms MOVLW 80 ; 20 milisecond delay GOTO StartDly Wait2ms MOVLW 8 ; 2 milisecond delay GOTO StartDly Wait1ms MOVLW 4 ; 1 milisecond delay StartDly MOVWF Loop1 OuterDly MOVLW 50 MOVWF Loop2 InnerDly NOP NOP DECFSZ Loop2,F GOTO InnerDly ; Inner loop = 5 usec. DECFSZ Loop1,F GOTO OuterDly RETURN END