; ---------------------------------------------------------- ; -- AIR COMMAND - Water Rocket Flight Software V1.3.2 -- ; -- Copyright (c) 2007 GKDesign Engineering -- ; -- Author: George Katz 22/7/07 -- ; ---------------------------------------------------------- ; PORT A ; desig. bit dir pin function ; RA0 0 in 17 'Arm' button. Arms flight computer ready for launch. ; RA1 1 in 18 'Launch Detect' button. Activated when the rocket experiences +ve Gs. ; 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 - not used ; 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 7 segment LED display - 'dp' segment 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 ; TODO: check each of the above configuration settings to make sure they match what is needed. ; check the Brown out detection whether that was causing the reset. 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'00000001' ; 0 3.0 sec RETLW b'00000010' ; 1 3.25 RETLW b'00000011' ; 2 3.5 RETLW b'00000100' ; 3 3.75 RETLW b'00000101' ; 4 4.0 RETLW b'00000110' ; 5 4.25 RETLW b'00000111' ; 6 4.5 RETLW b'00001000' ; 7 4.75 RETLW b'00001001' ; 8 5.0 RETLW b'00001010' ; 9 5.25 RETLW b'00001011' ; A 5.5 RETLW b'00001100' ; b 5.75 RETLW b'00001101' ; C 6.0 RETLW b'00001110' ; d 6.25 RETLW b'00001111' ; E 6.5 RETLW b'00111100' ; F 6.75 RETLW b'01101101' ; G 7.0 RETLW b'00111001' ; h 7.25 RETLW b'00000001' ; i 7.5 RETLW b'01100011' ; J 7.75 RETLW b'00111000' ; k 8.0 RETLW b'01101000' ; L 8.25 RETLW b'00110101' ; m 8.5 RETLW b'00110001' ; n 8.75 RETLW b'01110001' ; o 9.0 RETLW b'00111110' ; p 9.25 RETLW b'00011111' ; q 9.5 RETLW b'00110000' ; r 9.75 RETLW b'00011001' ; s 10.0 RETLW b'01111000' ; t 10.25 RETLW b'01101011' ; u 10.5 RETLW b'01100001' ; v 10.75 RETLW b'01100101' ; w 11.0 RETLW b'01101000' ; L - Launch delay //33 RETLW b'00000100' ; '-' - Parachute deploy less drain on battery //34 RETLW b'01000000' ; '_' - Initialising //35 ; ; ---------- ; MAIN LOOP ; ---------- ; Start CLRF Program ; initialise the Program variable to be 0 CLRF Message ; initialise the Message variable to 0 CLRF Armed MOVLW 35 ; 35 is the '_' symbol - initialising on ground MOVWF Message ; store that value in the message variable CALL LEDOut2 ; output current Program setting to display CALL DeployClose ; set motor into default position CALL LEDOut ; display the '0' 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,4 ; Turn on the Armed flag. CALL LEDOut ; Turn on 'Armed' LED to indicate system is armed. ; Rocket waits for the launch detect event LnchDetect BTFSC PORTA, LAUNCH_DETECT_BTN ; See if launch detect button goes low since it is normally high. GOTO LnchDetect ; loop endlesly until launch detect is triggered ; launch was detected ; 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 fire the chute setting the correct delay of 3 seconds + delay index (Program) MOVLW 12 ; We wait a minimum of 3 seconds 4 x 3 since we increment in 1/4 seconds ADDWF Program,0 ; Add the program offset to the 3 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. ; ------- new RC motor deploy ------------------------ deploy CALL DeployOpen CALL MotorDly CALL MotorDly CALL MotorDly CALL MotorDly ; ------- new RC motor deploy ------------------------ GOTO Start ; go back and await instruction to start again ; --------------- ; 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 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