Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright 2011 Dustin L. Westaby
- ----------------------------------------------------------------------
- TacPad Suit Controller for Attiny2313
- ----------------------------------------------------------------------
- Title: tacpad.c
- Author: Dustin Westaby
- Date Created: 3/27/11
- Last Modified: 3/27/11
- Purpose: Membrane display and control of prop suit systems. Lights,
- Fans, etc...
- Compiled with AVR-GCC WinAVR
- Revisions List:
- 3/27/11 Initial build of prototype circuit
- ----------------------------------------------------------------------
- Fuses:
- ----------------------------------------------------------------------
- BrownOut Disabled
- CKDIV8
- Int RC Osc 8Mhz + 64ms
- ----------------------------------------------------------------------
- Inputs:
- ----------------------------------------------------------------------
- Power up
- ----------------------------------------------------------------------
- Ouputs:
- ----------------------------------------------------------------------
- pin i/o port circuit trace connection
- ----------------------------------------------------------------------
- 2 0 PD0 = OUT_L_SUIT Transistor Drive
- 3 0 PD1 = OUT_F_SUIT Transistor Drive
- 4 1 PA1 = IN_L_HELM Micro Switch
- 5 1 PA0 = IN_MAIN_POWER Micro Switch
- 6 0 PD2 = OUT_MAIN_POWER Transistor Drive
- 7 1 PD3 = IN_L_SUIT Micro Switch
- 14 1 PB2 = IN_F_SUIT Micro Switch
- 17 1 PB5 = IN_F_HELM Micro Switch
- 18 0 PB6 = OUT_F_HELM Transistor Drive
- 19 0 PB7 = OUT_L_HELM Transistor Drive
- ----------------------------------------------------------------------
- Notes:
- ----------------------------------------------------------------------
- -Program is free to modify, but please keep my name on the author list.
- */
- //--------------------------------------
- // Global Variables |
- //--------------------------------------
- // 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
- // One CPU Cycle = 1us
- #define F_CPU 8000000UL/8
- enum { up, down };
- enum { on, off };
- //--------------------------------------
- // Includes |
- //--------------------------------------
- #include <avr/io.h>
- #include <util/delay.h>
- //#include <avr/sleep.h>
- #include <inttypes.h>
- //--------------------------------------
- // Delay Subroutines |
- //--------------------------------------
- //These functions are from the delay.h include, the calls to delay functions
- //are re-written here to allow for longer waits.
- void delay_ms(uint16_t ms) {
- while ( ms )
- {
- _delay_ms(1);
- ms--;
- }
- }
- void delay_us(uint16_t us) {
- while ( us )
- {
- _delay_us(1);
- us--;
- }
- }
- //--------------------------------------
- // Button Input Subroutines |
- //--------------------------------------
- int l_helm_is_pressed()
- {
- // the button is pressed when bit is clear
- if (bit_is_clear(PINA, PA1))
- {
- delay_us(800);
- if (bit_is_clear(PINA, PA1)) return 1;
- }
- return 0;
- }
- int l_suit_is_pressed()
- {
- // the button is pressed when bit is clear
- if (bit_is_clear(PIND, PD3))
- {
- delay_us(800);
- if (bit_is_clear(PIND, PD3)) return 1;
- }
- return 0;
- }
- int f_helm_is_pressed()
- {
- // the button is pressed when bit is clear
- if (bit_is_clear(PINB, PB5))
- {
- delay_us(800);
- if (bit_is_clear(PINB, PB5)) return 1;
- }
- return 0;
- }
- int f_suit_is_pressed()
- {
- // the button is pressed when bit is clear
- if (bit_is_clear(PINB, PB2))
- {
- delay_us(800);
- if (bit_is_clear(PINB, PB2)) return 1;
- }
- return 0;
- }
- int power_main_is_pressed()
- {
- // the button is pressed when bit is clear
- if (bit_is_clear(PINA, PA0))
- {
- delay_us(800);
- if (bit_is_clear(PINA, PA0)) return 1;
- }
- return 0;
- }
- //--------------------------------------
- // Main |
- //--------------------------------------
- int main (void)
- {
- /* ---------------------------------------------------------------- */
- /* Initialization */
- /* ---------------------------------------------------------------- */
- int L_HELM_STATUS, L_SUIT_STATUS, F_HELM_STATUS, F_SUIT_STATUS, MAIN_POWER_STATUS;
- int animation_delay;
- //PWM fade variables
- int direction, time_on, time_off, max_value, min_value, rate_of_change;
- MAIN_POWER_STATUS = 0;
- L_HELM_STATUS = 0;
- L_SUIT_STATUS = 0;
- F_HELM_STATUS = 0;
- F_SUIT_STATUS = 0;
- animation_delay = 80;
- //Initialize ports
- //A0, A1 are inputs
- DDRA = 0b00000000;
- PORTA = 0b11111111;
- //B2, B5 are inputs, B6, B7 are outputs
- DDRB = 0b11000000;
- PORTB = 0b00111111;
- //D3 is input, D0, D1, D2 are outputs
- DDRD = 0b00000111;
- PORTD = 0b11111000;
- while(1)
- {
- //Reset Fade In and Out for Standby Animation
- max_value=500; //Max of 500us for LEDs ON
- min_value=5; //Max of 5us for LEDs OFF
- time_on=min_value; //Set Starting Time ON to 5us
- time_off=max_value; //Set Starting Time OFF to 500us
- rate_of_change=2; //This is the speed that the fade goes between min and max
- direction=down; //Direction is defined for the direction of the fade (in or out)
- //debounce input, only continue if all buttons are released
- while(l_helm_is_pressed()||l_suit_is_pressed()
- ||f_helm_is_pressed()||f_suit_is_pressed()
- ||power_main_is_pressed());
- //debounce input, only continue if a button is pressed
- while(!l_helm_is_pressed()&&!l_suit_is_pressed()
- &&!f_helm_is_pressed()&&!f_suit_is_pressed()
- &&!power_main_is_pressed())
- {
- //are we in standby?
- if (!L_HELM_STATUS&&!F_HELM_STATUS&&!L_SUIT_STATUS&&!F_SUIT_STATUS)
- {
- //Turn ON Power LEDs
- PORTD = (L_SUIT_STATUS<<PD0)|(F_SUIT_STATUS<<PD1)|(MAIN_POWER_STATUS<<PD2);
- delay_us(time_on);
- //Turn OFF Power LEDs
- PORTD = (L_SUIT_STATUS<<PD0)|(F_SUIT_STATUS<<PD1)|(0<<PD2);
- delay_us(time_off);
- if (direction==up)
- {
- //In the UP direction the Time ON increases while the Time OFF decreases.
- //The result is that the LEDs get brighter
- time_on+=rate_of_change;
- time_off-=rate_of_change;
- //When the end of the fade is reached, switch directions
- if (time_on>max_value)
- direction=down;
- }
- else
- {
- //In the DOWN direction the Time ON decreases while the Time OFF increases.
- //The result is that the LEDs get dimmer
- time_on-=rate_of_change;
- time_off+=rate_of_change;
- //When the end of the fade is reached, switch directions
- if (time_on<=min_value)
- direction=up;
- }
- }
- }
- //are we powered on?
- if( power_main_is_pressed()&&MAIN_POWER_STATUS==0)
- {
- MAIN_POWER_STATUS = 1;
- //power on light animation
- PORTD = (0<<PD0)|(0<<PD1)|(1<<PD2);
- delay_ms(animation_delay);
- PORTD = (1<<PD0)|(0<<PD1)|(1<<PD2);
- delay_ms(animation_delay);
- PORTB = (1<<PB7)|(0<<PB6);
- delay_ms(animation_delay);
- PORTB = (1<<PB7)|(1<<PB6);
- delay_ms(animation_delay);
- PORTD = (1<<PD0)|(1<<PD1)|(1<<PD2);
- delay_ms(animation_delay);
- }
- else if ( power_main_is_pressed()&&MAIN_POWER_STATUS==1)
- {
- MAIN_POWER_STATUS = 0;
- L_HELM_STATUS = 0;
- L_SUIT_STATUS = 0;
- F_HELM_STATUS = 0;
- F_SUIT_STATUS = 0;
- }
- //only enter if powered on
- if( MAIN_POWER_STATUS==1)
- {
- //find out which button was pressed and toggle the status
- if( l_helm_is_pressed()&&L_HELM_STATUS==0)
- L_HELM_STATUS = 1;
- else if ( l_helm_is_pressed()&&L_HELM_STATUS==1)
- L_HELM_STATUS = 0;
- if( l_suit_is_pressed()&&L_SUIT_STATUS==0)
- L_SUIT_STATUS = 1;
- else if ( l_suit_is_pressed()&&L_SUIT_STATUS==1)
- L_SUIT_STATUS = 0;
- if( f_helm_is_pressed()&&F_HELM_STATUS==0)
- F_HELM_STATUS = 1;
- else if ( f_helm_is_pressed()&&F_HELM_STATUS==1)
- F_HELM_STATUS = 0;
- if( f_suit_is_pressed()&&F_SUIT_STATUS==0)
- F_SUIT_STATUS = 1;
- else if ( f_suit_is_pressed()&&F_SUIT_STATUS==1)
- F_SUIT_STATUS = 0;
- }
- //output status
- PORTB = (L_HELM_STATUS<<PB7)|(F_HELM_STATUS<<PB6);
- PORTD = (L_SUIT_STATUS<<PD0)|(F_SUIT_STATUS<<PD1)|(MAIN_POWER_STATUS<<PD2);
- }
- while(1); // Ending infinite loop (just in case)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement