Advertisement
dlwestab

Carter TacPad Prop Suit Controller Circuit

Mar 27th, 2011
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.44 KB | None | 0 0
  1. /*
  2. Copyright 2011 Dustin L. Westaby
  3.  
  4. ----------------------------------------------------------------------
  5.     TacPad Suit Controller for Attiny2313
  6. ----------------------------------------------------------------------
  7. Title:      tacpad.c
  8. Author:     Dustin Westaby
  9. Date Created:   3/27/11
  10. Last Modified:  3/27/11
  11. Purpose:  Membrane display and control of prop suit systems. Lights,
  12.           Fans, etc...
  13.  
  14. Compiled with AVR-GCC WinAVR
  15.  
  16. Revisions List:
  17. 3/27/11 Initial build of prototype circuit
  18.  
  19. ----------------------------------------------------------------------
  20.     Fuses:
  21. ----------------------------------------------------------------------
  22.  BrownOut Disabled
  23.  CKDIV8
  24.  Int RC Osc 8Mhz + 64ms
  25.  
  26. ----------------------------------------------------------------------
  27.     Inputs:
  28. ----------------------------------------------------------------------
  29.  Power up
  30.  
  31. ----------------------------------------------------------------------
  32.     Ouputs:
  33. ----------------------------------------------------------------------
  34.  pin  i/o  port    circuit trace       connection
  35. ----------------------------------------------------------------------
  36.   2    0   PD0 =   OUT_L_SUIT          Transistor Drive
  37.   3    0   PD1 =   OUT_F_SUIT          Transistor Drive
  38.   4    1   PA1 =   IN_L_HELM           Micro Switch
  39.   5    1   PA0 =   IN_MAIN_POWER       Micro Switch
  40.   6    0   PD2 =   OUT_MAIN_POWER      Transistor Drive
  41.   7    1   PD3 =   IN_L_SUIT           Micro Switch
  42.  14    1   PB2 =   IN_F_SUIT           Micro Switch
  43.  17    1   PB5 =   IN_F_HELM           Micro Switch
  44.  18    0   PB6 =   OUT_F_HELM          Transistor Drive
  45.  19    0   PB7 =   OUT_L_HELM          Transistor Drive
  46.  
  47. ----------------------------------------------------------------------
  48.     Notes:
  49. ----------------------------------------------------------------------
  50.  -Program is free to modify, but please keep my name on the author list.
  51.  
  52. */
  53.  
  54. //--------------------------------------
  55. //          Global Variables           |
  56. //--------------------------------------
  57. // 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
  58. // One CPU Cycle = 1us
  59. #define F_CPU 8000000UL/8
  60.  
  61. enum { up, down };
  62. enum { on, off };
  63.  
  64. //--------------------------------------
  65. //              Includes               |
  66. //--------------------------------------
  67. #include <avr/io.h>
  68. #include <util/delay.h>
  69. //#include <avr/sleep.h>
  70. #include <inttypes.h>
  71.  
  72.  
  73. //--------------------------------------
  74. //          Delay Subroutines          |
  75. //--------------------------------------
  76. //These functions are from the delay.h include, the calls to delay functions
  77. //are re-written here to allow for longer waits.
  78. void delay_ms(uint16_t ms) {
  79.   while ( ms )
  80.   {
  81.     _delay_ms(1);
  82.     ms--;
  83.   }
  84. }
  85.  
  86. void delay_us(uint16_t us) {
  87.   while ( us )
  88.   {
  89.     _delay_us(1);
  90.     us--;
  91.   }
  92. }
  93.  
  94.  
  95. //--------------------------------------
  96. //      Button Input Subroutines       |
  97. //--------------------------------------
  98. int l_helm_is_pressed()
  99. {
  100.  
  101.     // the button is pressed when bit is clear
  102.     if (bit_is_clear(PINA, PA1))
  103.     {
  104.         delay_us(800);
  105.         if (bit_is_clear(PINA, PA1)) return 1;
  106.     }
  107.  
  108.     return 0;
  109. }
  110.  
  111.  
  112. int l_suit_is_pressed()
  113. {
  114.  
  115.     // the button is pressed when bit is clear
  116.     if (bit_is_clear(PIND, PD3))
  117.     {
  118.         delay_us(800);
  119.         if (bit_is_clear(PIND, PD3)) return 1;
  120.     }
  121.  
  122.     return 0;
  123. }
  124.  
  125. int f_helm_is_pressed()
  126. {
  127.  
  128.     // the button is pressed when bit is clear
  129.     if (bit_is_clear(PINB, PB5))
  130.     {
  131.         delay_us(800);
  132.         if (bit_is_clear(PINB, PB5)) return 1;
  133.     }
  134.  
  135.     return 0;
  136. }
  137.  
  138.  
  139. int f_suit_is_pressed()
  140. {
  141.  
  142.     // the button is pressed when bit is clear
  143.     if (bit_is_clear(PINB, PB2))
  144.     {
  145.         delay_us(800);
  146.         if (bit_is_clear(PINB, PB2)) return 1;
  147.     }
  148.  
  149.     return 0;
  150. }
  151.  
  152. int power_main_is_pressed()
  153. {
  154.  
  155.     // the button is pressed when bit is clear
  156.     if (bit_is_clear(PINA, PA0))
  157.     {
  158.         delay_us(800);
  159.         if (bit_is_clear(PINA, PA0)) return 1;
  160.     }
  161.  
  162.     return 0;
  163. }
  164.  
  165.  
  166.  
  167.  
  168. //--------------------------------------
  169. //               Main                  |
  170. //--------------------------------------
  171. int main (void)
  172. {
  173.  
  174.   /* ---------------------------------------------------------------- */
  175.   /*                            Initialization                        */
  176.   /* ---------------------------------------------------------------- */
  177.   int L_HELM_STATUS, L_SUIT_STATUS, F_HELM_STATUS, F_SUIT_STATUS, MAIN_POWER_STATUS;
  178.   int animation_delay;
  179.  
  180.   //PWM fade variables
  181.   int direction, time_on, time_off, max_value,  min_value, rate_of_change;
  182.  
  183.   MAIN_POWER_STATUS = 0;
  184.   L_HELM_STATUS = 0;
  185.   L_SUIT_STATUS = 0;
  186.   F_HELM_STATUS = 0;
  187.   F_SUIT_STATUS = 0;
  188.  
  189.   animation_delay = 80;
  190.  
  191.   //Initialize ports
  192.   //A0, A1 are inputs
  193.   DDRA =  0b00000000;
  194.   PORTA = 0b11111111;
  195.  
  196.   //B2, B5 are inputs, B6, B7 are outputs
  197.   DDRB =  0b11000000;
  198.   PORTB = 0b00111111;
  199.  
  200.   //D3 is input, D0, D1, D2 are outputs
  201.   DDRD =  0b00000111;
  202.   PORTD = 0b11111000;
  203.  
  204.  
  205.  
  206.   while(1)
  207.   {
  208.  
  209.     //Reset Fade In and Out for Standby Animation
  210.     max_value=500;              //Max of 500us for LEDs ON
  211.     min_value=5;                //Max of   5us for LEDs OFF
  212.     time_on=min_value;          //Set Starting Time ON  to 5us
  213.     time_off=max_value;         //Set Starting Time OFF to 500us
  214.     rate_of_change=2;           //This is the speed that the fade goes between min and max
  215.     direction=down;             //Direction is defined for the direction of the fade (in or out)
  216.  
  217.     //debounce input, only continue if all buttons are released
  218.     while(l_helm_is_pressed()||l_suit_is_pressed()
  219.         ||f_helm_is_pressed()||f_suit_is_pressed()
  220.         ||power_main_is_pressed());
  221.  
  222.     //debounce input, only continue if a button is pressed
  223.     while(!l_helm_is_pressed()&&!l_suit_is_pressed()
  224.         &&!f_helm_is_pressed()&&!f_suit_is_pressed()
  225.         &&!power_main_is_pressed())
  226.     {
  227.    
  228.         //are we in standby?
  229.         if (!L_HELM_STATUS&&!F_HELM_STATUS&&!L_SUIT_STATUS&&!F_SUIT_STATUS)
  230.         {
  231.             //Turn ON Power LEDs
  232.             PORTD = (L_SUIT_STATUS<<PD0)|(F_SUIT_STATUS<<PD1)|(MAIN_POWER_STATUS<<PD2);
  233.             delay_us(time_on);
  234.  
  235.             //Turn OFF Power LEDs
  236.             PORTD = (L_SUIT_STATUS<<PD0)|(F_SUIT_STATUS<<PD1)|(0<<PD2);
  237.             delay_us(time_off);
  238.  
  239.             if (direction==up)
  240.             {
  241.                 //In the UP direction the Time ON increases while the Time OFF decreases.
  242.                 //The result is that the LEDs get brighter
  243.               time_on+=rate_of_change;
  244.               time_off-=rate_of_change;
  245.  
  246.               //When the end of the fade is reached, switch directions
  247.               if (time_on>max_value)
  248.                 direction=down;
  249.             }
  250.             else
  251.             {
  252.                 //In the DOWN direction the Time ON decreases while the Time OFF increases.
  253.                 //The result is that the LEDs get dimmer
  254.               time_on-=rate_of_change;
  255.               time_off+=rate_of_change;
  256.  
  257.               //When the end of the fade is reached, switch directions
  258.               if (time_on<=min_value)
  259.                 direction=up;
  260.             }
  261.         }
  262.     }
  263.    
  264.     //are we powered on?
  265.     if( power_main_is_pressed()&&MAIN_POWER_STATUS==0)
  266.     {
  267.         MAIN_POWER_STATUS = 1;
  268.  
  269.         //power on light animation
  270.         PORTD = (0<<PD0)|(0<<PD1)|(1<<PD2);
  271.         delay_ms(animation_delay);
  272.         PORTD = (1<<PD0)|(0<<PD1)|(1<<PD2);
  273.         delay_ms(animation_delay);
  274.         PORTB = (1<<PB7)|(0<<PB6);
  275.         delay_ms(animation_delay);
  276.         PORTB = (1<<PB7)|(1<<PB6);
  277.         delay_ms(animation_delay);
  278.         PORTD = (1<<PD0)|(1<<PD1)|(1<<PD2);
  279.         delay_ms(animation_delay);
  280.  
  281.     }
  282.     else if ( power_main_is_pressed()&&MAIN_POWER_STATUS==1)
  283.     {
  284.         MAIN_POWER_STATUS = 0;
  285.         L_HELM_STATUS = 0;
  286.         L_SUIT_STATUS = 0;
  287.         F_HELM_STATUS = 0;
  288.         F_SUIT_STATUS = 0;
  289.     }
  290.  
  291.     //only enter if powered on
  292.     if( MAIN_POWER_STATUS==1)
  293.     {
  294.    
  295.         //find out which button was pressed and toggle the status
  296.         if( l_helm_is_pressed()&&L_HELM_STATUS==0)
  297.             L_HELM_STATUS = 1;
  298.         else if ( l_helm_is_pressed()&&L_HELM_STATUS==1)
  299.             L_HELM_STATUS = 0;
  300.  
  301.         if( l_suit_is_pressed()&&L_SUIT_STATUS==0)
  302.             L_SUIT_STATUS = 1;
  303.         else if ( l_suit_is_pressed()&&L_SUIT_STATUS==1)
  304.             L_SUIT_STATUS = 0;
  305.  
  306.         if( f_helm_is_pressed()&&F_HELM_STATUS==0)
  307.             F_HELM_STATUS = 1;
  308.         else if ( f_helm_is_pressed()&&F_HELM_STATUS==1)
  309.             F_HELM_STATUS = 0;
  310.  
  311.         if( f_suit_is_pressed()&&F_SUIT_STATUS==0)
  312.             F_SUIT_STATUS = 1;
  313.         else if ( f_suit_is_pressed()&&F_SUIT_STATUS==1)
  314.             F_SUIT_STATUS = 0;
  315.  
  316.  
  317.     }
  318.  
  319.     //output status
  320.     PORTB = (L_HELM_STATUS<<PB7)|(F_HELM_STATUS<<PB6);
  321.     PORTD = (L_SUIT_STATUS<<PD0)|(F_SUIT_STATUS<<PD1)|(MAIN_POWER_STATUS<<PD2);
  322.  
  323.  
  324.   }
  325.  
  326.  
  327.  
  328.   while(1);                 // Ending infinite loop (just in case)
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement