Advertisement
dlwestab

Car Lights 1.2

Sep 21st, 2012
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Copyright 2012 D Westaby
  3.  
  4. ----------------------------------------------------------------------
  5.    Car Lights for Attiny2313
  6. ----------------------------------------------------------------------
  7. Title:      Car_Lighting.c
  8. Author:     Dustin Westaby
  9. Date Created:   September 8, 2012
  10. Date Modified:   September 21, 2012
  11.  
  12. Compiled with AVR-GCC WinAVR
  13.  
  14. ----------------------------------------------------------------------
  15.     Fuses:
  16. ----------------------------------------------------------------------
  17.  BrownOut Disabled
  18.  CKDIV8
  19.  Int RC Osc 8Mhz + 64ms
  20.  
  21. ----------------------------------------------------------------------
  22.     Inputs:
  23. ----------------------------------------------------------------------
  24.  None
  25.  
  26. ----------------------------------------------------------------------
  27.     Ouputs:
  28. ----------------------------------------------------------------------
  29.  pin  i/o  port    circuit trace       connection
  30. ----------------------------------------------------------------------
  31.   4    1   PA1 =   R_LED1
  32.   5    1   PA0 =   L_LED2
  33.   2    1   PD0 =   R_LED3
  34.   3    1   PD1 =   R_LED4
  35.   6    1   PD2 =   R_LED5
  36.   7    1   PD3 =   R_LED6
  37.  14    1   PB2 =   L_LED7
  38.  17    1   PB5 =   L_LED8
  39.  18    1   PB6 =   L_LED9
  40.  19    1   PB7 =   L_LED10
  41.  
  42. */
  43.  
  44. /*--------------------------------------
  45.             Global Variables           |
  46. ---------------------------------------- */
  47.  
  48. /* 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
  49.    One CPU Cycle = 1us */
  50. #define F_CPU 8000000UL/8
  51.  
  52. #define R_LED1_PA1  (1<<1)
  53. #define L_LED2_PA0  (1<<0)
  54. #define R_LED3_PD0  (1<<0)
  55. #define R_LED4_PD1  (1<<1)
  56. #define R_LED5_PD2  (1<<2)
  57. #define R_LED6_PD3  (1<<3)
  58. #define L_LED7_PB2  (1<<2)
  59. #define L_LED8_PB5  (1<<5)
  60. #define L_LED9_PB6  (1<<6)
  61. #define L_LED10_PB7 (1<<7)
  62.  
  63.   //delay wait values, measured in us
  64. #define DIM_DELAY_ON  (200)
  65. #define DIM_DELAY_OFF (200)
  66.  
  67. /* This value is used to count us of delay and convert to ms
  68.    Due to other cpu processing: "1ms of delay" < "1000us of delay" */
  69. #define TIME_CONVERSION_VAL (50)
  70.  
  71. int program_ms_counter;
  72. int program_counter_us; //used for tracking time elapsed
  73.  
  74. //--------------------------------------
  75. //              Includes               |
  76. //--------------------------------------
  77. #include <avr/io.h>
  78. #include <util/delay.h>
  79.  
  80. //--------------------------------------
  81. //          Delay Subroutines          |
  82. //--------------------------------------
  83. //These functions are from the delay.h include, the calls to delay functions
  84. //are re-written here to allow for longer waits.
  85. void delay_ms(uint16_t ms) {
  86.         program_ms_counter+=ms;
  87.         while ( ms )
  88.         {
  89.                 _delay_ms(1);
  90.                 ms--;
  91.         }
  92. }
  93.  
  94. void delay_us(uint16_t us) {
  95.   program_counter_us+=us;
  96.   while ( us )
  97.   {
  98.     _delay_us(1);
  99.     us--;
  100.   }
  101.  
  102.   //This loop will continue to convert us into ms until the "us counter" is
  103.   //   below the conversion val
  104.   while(program_counter_us>=TIME_CONVERSION_VAL)
  105.   {
  106.           program_ms_counter++;                      //increment "ms counter"
  107.           program_counter_us-=TIME_CONVERSION_VAL;   //subtract "us counter"
  108.   }
  109. }
  110.  
  111. //--------------------------------------
  112. //         Program Subroutines         |
  113. //--------------------------------------
  114.  
  115. void reset_ms_us_counters(void) {
  116.    //set "ms counter" and "us counter" to 0
  117.    program_ms_counter=0;
  118.    program_counter_us=0;
  119. }
  120.  
  121. void all_LEDs_BRT(void) {
  122.    //Sets all PORT outputs to 1, which is a high (+5V) on the pin
  123.    PORTA = 0xFF; // = 0b11111111
  124.    PORTB = 0xFF;
  125.    PORTD = 0xFF;
  126. }
  127.  
  128. void all_LEDs_OFF(void) {
  129.    //Sets all PORT outputs to 0, which is a low (GND) on the pin
  130.    Porta &= ~(  R_Led1_Pa1   //Turn Off Port A, Pin 1
  131.               + L_Led2_Pa0); //Turn Off Port A, Pin 0
  132.    Portb &= ~(  L_Led10_Pb7  //Turn Off Port B, Pin 7
  133.               + L_Led9_Pb6   //Turn Off Port B, Pin 6
  134.               + L_Led8_Pb5   //Turn Off Port B, Pin 5
  135.               + L_Led7_Pb2); //Turn Off Port B, Pin 2
  136.    Portd &= ~(  R_Led6_Pd3   //Turn Off Port D, Pin 3
  137.               + R_Led5_Pd2   //Turn Off Port D, Pin 2
  138.               + R_Led4_Pd1   //Turn Off Port D, Pin 1
  139.               + R_Led3_Pd0); //Turn Off Port D, Pin 0
  140. }
  141.  
  142. void program2_LEDs_to_DIM(void) {
  143.    PORTA &= ~(  R_LED1_PA1   //Turn Off PORT A, pin 1
  144.               + L_LED2_PA0); //Turn Off PORT A, pin 0
  145.    PORTB &= ~(  L_LED10_PB7  //Turn Off PORT B, pin 7
  146.               + L_LED8_PB5); //Turn Off PORT B, pin 5
  147.    PORTD &= ~(  R_LED6_PD3   //Turn Off PORT D, pin 3
  148.               + R_LED5_PD2   //Turn Off PORT D, pin 2
  149.               + R_LED4_PD1   //Turn Off PORT D, pin 1
  150.               + R_LED3_PD0); //Turn Off PORT D, pin 0
  151. }
  152.  
  153. void program3_6_LEDs_to_DIM(void) {
  154.    PORTA &= ~(  R_LED1_PA1   //Turn Off PORT A, pin 1
  155.               + L_LED2_PA0); //Turn Off PORT A, pin 0
  156. }
  157.  
  158. void program5_LEDs_to_DIM (void) {
  159.    PORTA &= ~(  R_LED1_PA1   //Turn Off PORT A, pin 1
  160.               + L_LED2_PA0); //Turn Off PORT A, pin 0
  161.    PORTB &= ~(  L_LED10_PB7  //Turn Off PORT B, pin 7
  162.               + L_LED9_PB6   //Turn Off PORT B, pin 6
  163.               + L_LED8_PB5   //Turn Off PORT B, pin 5
  164.               + L_LED7_PB2); //Turn Off PORT B, pin 2
  165.    PORTD &= ~(  R_LED6_PD3   //Turn Off PORT D, pin 3
  166.               + R_LED4_PD1); //Turn Off PORT D, pin 1
  167. }
  168.  
  169. /* Software PWM explaination:
  170.  
  171.    The dim LED effect is performed by a software PWM.  Each of the loops in the
  172.    main() function turn on all LEDs, then turn off only the LEDs we want as DIM.
  173.    The LEDs that remain on will appear BRIGHT.  The LEDs that are rapid turned
  174.    on and off will appear DIM.
  175.  
  176.                       DIM_DELAY_ON                 DIM_DELAY_ON
  177.                 +5V  ______________               _______________
  178.                      |            |               |             |
  179.  GND  _______________|            |_______________|             |______________
  180.        DIM_DELAY_OFF                DIM_DELAY_OFF                DIM_DELAY_OFF
  181. */
  182.  
  183. void program_NORMAL_MODE(void) {
  184.    all_LEDs_BRT();           //Turn on all LEDs
  185.    delay_us(DIM_DELAY_ON);   //Time to keep all LEDs on
  186.    all_LEDs_OFF();           //Turn off all LEDs
  187.    delay_us(DIM_DELAY_OFF);  //Time to keep all LEDs off
  188. }
  189.  
  190. void program_LEFT_TURN(void) {
  191.    all_LEDs_BRT();           //Turn on all LEDs
  192.    delay_us(DIM_DELAY_ON);   //Time to keep all LEDs on
  193.    program2_LEDs_to_DIM();   //Turn off program 2 LEDs
  194.    delay_us(DIM_DELAY_OFF);  //Time to keep program 2 LEDs off
  195. }
  196.  
  197. void program_BRAKE_MODE(void) {
  198.    all_LEDs_BRT();           //Turn on all LEDs
  199.    delay_us(DIM_DELAY_ON);   //Time to keep all LEDs on
  200.    program3_6_LEDs_to_DIM();   //Turn off program 3 LEDs
  201.    delay_us(DIM_DELAY_OFF);  //Time to keep program 3 LEDs off
  202. }
  203.  
  204. void program_RIGHT_TURN (void) {
  205.    all_LEDs_BRT();           //Turn on all LEDs
  206.    delay_us(DIM_DELAY_ON);   //Time to keep all LEDs on
  207.    program5_LEDs_to_DIM();   //Turn off program 5 LEDs
  208.    delay_us(DIM_DELAY_OFF);  //Time to keep program 5 LEDs off
  209. }
  210.  
  211.  
  212. //--------------------------------------
  213. //               Main                  |
  214. //--------------------------------------
  215. int main (void)
  216. {
  217.  
  218.   /* ---------------------------------------------------------------- */
  219.   /*                            Initialization                        */
  220.   /* ---------------------------------------------------------------- */
  221.  
  222.   //Initialize all ports as outputs
  223.   DDRA =  0b00000011; //A0, A1
  224.   DDRB =  0b11111111;
  225.   DDRD =  0b11111111;
  226.  
  227.   //Begin with all LEDs off
  228.   all_LEDs_OFF();
  229.  
  230. /* ---------------------------------------------------------------- */
  231. /*                              Main Loop                           */
  232. /* ---------------------------------------------------------------- */
  233.  
  234.   while(1)
  235.   {
  236.  
  237.         /* ---------------------------- */
  238.         /*      Program 1 - NORMAL      */
  239.         /* ---------------------------- */
  240.         reset_ms_us_counters();
  241.  
  242.         while(program_ms_counter < 6000)
  243.         {
  244.            program_NORMAL_MODE();
  245.         }
  246.  
  247.         /* ---------------------------- */
  248.         /*      Program 3 - BRAKES      */
  249.         /* ---------------------------- */
  250.         reset_ms_us_counters();
  251.  
  252.         while(program_ms_counter < 500)
  253.         {
  254.            program_BRAKE_MODE();
  255.         }
  256.         while(program_ms_counter < 1000)
  257.         {
  258.            program_NORMAL_MODE();
  259.         }
  260.         while(program_ms_counter < 1500)
  261.         {
  262.            program_BRAKE_MODE();
  263.         }
  264.         while(program_ms_counter < 2000)
  265.         {
  266.            program_NORMAL_MODE();
  267.         }
  268.         while(program_ms_counter < 2500)
  269.         {
  270.            program_BRAKE_MODE();
  271.         }
  272.         while(program_ms_counter < 3000)
  273.         {
  274.            program_NORMAL_MODE();
  275.         }
  276.         while(program_ms_counter < 3500)
  277.         {
  278.            program_BRAKE_MODE();
  279.         }
  280.         while(program_ms_counter < 4000)
  281.         {
  282.            program_NORMAL_MODE();
  283.         }
  284.         while(program_ms_counter < 4500)
  285.         {
  286.            program_BRAKE_MODE();
  287.         }
  288.         while(program_ms_counter < 5000)
  289.         {
  290.            program_NORMAL_MODE();
  291.         }
  292.         while(program_ms_counter < 5500)
  293.         {
  294.            program_BRAKE_MODE();
  295.         }
  296.         while(program_ms_counter < 6000)
  297.         {
  298.            program_NORMAL_MODE();
  299.         }
  300.         while(program_ms_counter < 6500)
  301.         {
  302.            program_BRAKE_MODE();
  303.         }
  304.         while(program_ms_counter < 7000)
  305.         {
  306.            program_NORMAL_MODE();
  307.         }
  308.  
  309.         /* ------------------------------- */
  310.         /*      Program 2 - LEFT TURN      */
  311.         /* ------------------------------- */
  312.         reset_ms_us_counters();
  313.  
  314.         while(program_ms_counter < 3000)
  315.         {
  316.            program_LEFT_TURN();
  317.         }
  318.         while(program_ms_counter < 4000)
  319.         {
  320.            program_NORMAL_MODE();
  321.         }
  322.         while(program_ms_counter < 6500)
  323.         {
  324.            program_LEFT_TURN();
  325.         }
  326.         while(program_ms_counter < 7000)
  327.         {
  328.            program_NORMAL_MODE();
  329.         }
  330.  
  331.         /* ---------------------------- */
  332.         /*      Program 1 - NORMAL      */
  333.         /* ---------------------------- */
  334.         reset_ms_us_counters();
  335.  
  336.         while(program_ms_counter < 6000)
  337.         {
  338.            program_NORMAL_MODE();
  339.         }
  340.  
  341.         /* ------------------------------------ */
  342.         /*      Program 4 - NORMAL FLASHING     */
  343.         /* ------------------------------------ */
  344.         reset_ms_us_counters();
  345.  
  346.         while(program_ms_counter < 500)
  347.         {
  348.            all_LEDs_OFF();
  349.         }
  350.         while(program_ms_counter < 1000)
  351.         {
  352.            program_NORMAL_MODE();
  353.         }
  354.         while(program_ms_counter < 1500)
  355.         {
  356.            all_LEDs_OFF();
  357.         }
  358.         while(program_ms_counter < 2000)
  359.         {
  360.            program_NORMAL_MODE();
  361.         }
  362.         while(program_ms_counter < 2500)
  363.         {
  364.            all_LEDs_OFF();
  365.         }
  366.         while(program_ms_counter < 3000)
  367.         {
  368.            program_NORMAL_MODE();
  369.         }
  370.         while(program_ms_counter < 3500)
  371.         {
  372.            all_LEDs_OFF();
  373.         }
  374.         while(program_ms_counter < 4000)
  375.         {
  376.            program_NORMAL_MODE();
  377.         }
  378.         while(program_ms_counter < 4500)
  379.         {
  380.            all_LEDs_OFF();
  381.         }
  382.         while(program_ms_counter < 5000)
  383.         {
  384.            program_NORMAL_MODE();
  385.         }
  386.         while(program_ms_counter < 5500)
  387.         {
  388.            all_LEDs_OFF();
  389.         }
  390.         while(program_ms_counter < 6000)
  391.         {
  392.            program_NORMAL_MODE();
  393.         }
  394.         while(program_ms_counter < 6500)
  395.         {
  396.            all_LEDs_OFF();
  397.         }
  398.         while(program_ms_counter < 7000)
  399.         {
  400.            program_NORMAL_MODE();
  401.         }
  402.  
  403.         /* ------------------------------- */
  404.         /*      Program 2 - LEFT TURN      */
  405.         /* ------------------------------- */
  406.         reset_ms_us_counters();
  407.  
  408.         while(program_ms_counter < 3000)
  409.         {
  410.            program_LEFT_TURN();
  411.         }
  412.         while(program_ms_counter < 4000)
  413.         {
  414.            program_NORMAL_MODE();
  415.         }
  416.         while(program_ms_counter < 6500)
  417.         {
  418.            program_LEFT_TURN();
  419.         }
  420.         while(program_ms_counter < 7000)
  421.         {
  422.            program_NORMAL_MODE();
  423.         }
  424. }
  425.  
  426.   while(1);                 // Ending infinite loop (just in case)
  427.  
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement