Advertisement
dlwestab

Car Lights 1.4

Sep 24th, 2012
73
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.D
  8. Author:     Dustin Westaby
  9. Date Created:   September 8, 2012
  10. Date Modified:   September 24, 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. // 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
  48. // One CPU Cycle = 1us
  49. #define F_CPU 8000000UL/8
  50.  
  51. #define R_LED1_PA1  (1)
  52. #define L_LED2_PA0  (0)
  53. #define R_LED3_PD0  (0)
  54. #define R_LED4_PD1  (1)
  55. #define R_LED5_PD2  (2)
  56. #define R_LED6_PD3  (3)
  57. #define L_LED7_PB2  (2)
  58. #define L_LED8_PB5  (5)
  59. #define L_LED9_PB6  (6)
  60. #define L_LED10_PB7 (7)
  61.  
  62.  
  63. #define CONST_DIM_DELAY_ON  (200)
  64. #define CONST_DIM_DELAY_OFF (4000)
  65.  
  66. /* This value is used to count us of delay and convert to ms
  67.    Due to other cpu processing: "1ms of delay" < "1000us of delay" */
  68. //#define TIME_CONVERSION_VAL (50) //actual number should be 150 (am running faster)
  69. #define TIME_CONVERSION_VAL (150)
  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. void delay_us(uint16_t us) {
  94.   program_counter_us+=us;
  95.   while ( us )
  96.   {
  97.     _delay_us(1);
  98.     us--;
  99.   }
  100.  
  101.   while(program_counter_us>=TIME_CONVERSION_VAL)
  102.   {
  103.           program_ms_counter++;
  104.           program_counter_us-=TIME_CONVERSION_VAL;
  105.   }
  106. }
  107.  
  108. //--------------------------------------
  109. //          Misc Subroutines          |
  110. //--------------------------------------
  111.  
  112. void reset_ms_us_counters(void) {
  113.         program_ms_counter=0;
  114.         program_counter_us=0;
  115. }
  116.  
  117. void all_LEDs_ON(void) {
  118.         PORTA = 0xFF;
  119.         PORTB = 0xFF;
  120.         PORTD = 0xFF;
  121. }
  122.  
  123. void all_LEDs_OFF(void) {
  124.         PORTA = ~(0x03);
  125.         PORTB = ~(0xFF);
  126.         PORTD = ~(0xFF);
  127. }
  128.  
  129. void program_left_turn_mode(void) {
  130.         PORTA &= ~((1<<R_LED1_PA1)+(1<<L_LED2_PA0));
  131.         PORTB &= ~((1<<L_LED10_PB7)+(1<<L_LED8_PB5));
  132.         PORTD &= ~((1<<R_LED6_PD3)+(1<<R_LED5_PD2)+(1<<R_LED4_PD1)+(1<<R_LED3_PD0));
  133. }
  134.  
  135. void program_brake_mode(void) {
  136.         PORTA &= ~((1<<R_LED1_PA1)+(1<<L_LED2_PA0));
  137. }
  138.  
  139. void program_right_turn_mode (void) {
  140.         PORTA &= ~((1<<R_LED1_PA1)+(1<<L_LED2_PA0));
  141.         PORTB &= ~((1<<L_LED10_PB7)+(1<<L_LED9_PB6)+(1<<L_LED8_PB5)+(1<<L_LED7_PB2));
  142.         PORTD &= ~((1<<R_LED6_PD3)+(1<<R_LED4_PD1));
  143. }
  144.  
  145. void program_all_dim_loop (int timer_val) {
  146.  
  147.    while(program_ms_counter < timer_val)
  148.    {
  149.       all_LEDs_ON();
  150.       delay_us(CONST_DIM_DELAY_ON);
  151.       all_LEDs_OFF();
  152.       delay_us(CONST_DIM_DELAY_OFF);
  153.    }
  154.  
  155. }
  156.  
  157. void program_break_loop (int timer_val) {
  158.  
  159.    while(program_ms_counter < timer_val)
  160.    {
  161.       all_LEDs_ON();
  162.       delay_us(CONST_DIM_DELAY_ON);
  163.       program_brake_mode();
  164.       delay_us(CONST_DIM_DELAY_OFF);
  165.    }
  166.  
  167. }
  168.  
  169. void program_left_turn_loop (int timer_val) {
  170.  
  171.    while(program_ms_counter < timer_val)
  172.    {
  173.       all_LEDs_ON();
  174.       delay_us(CONST_DIM_DELAY_ON);
  175.       program_left_turn_mode();
  176.       delay_us(CONST_DIM_DELAY_OFF);
  177.    }
  178.  
  179. }
  180.  
  181. void program_right_turn_loop (int timer_val) {
  182.  
  183.    while(program_ms_counter < timer_val)
  184.    {
  185.       all_LEDs_ON();
  186.       delay_us(CONST_DIM_DELAY_ON);
  187.       program_right_turn_mode();
  188.       delay_us(CONST_DIM_DELAY_OFF);
  189.    }
  190.  
  191. }
  192.  
  193.  
  194. //--------------------------------------
  195. //               Main                  |
  196. //--------------------------------------
  197. int main (void)
  198. {
  199.  
  200.   /* ---------------------------------------------------------------- */
  201.   /*                            Initialization                        */
  202.   /* ---------------------------------------------------------------- */
  203.  
  204.   //Initialize all ports as outputs
  205.   DDRA =  0b00000011; //A0, A1
  206.   DDRB =  0b11111111;
  207.   DDRD =  0b11111111;
  208.  
  209.   all_LEDs_OFF();
  210.  
  211. /* ---------------------------------------------------------------- */
  212. /*                              Main Loop                           */
  213. /* ---------------------------------------------------------------- */
  214.  
  215.   while(1)
  216.   {
  217.  
  218. //1)    Dim
  219.       //1 - All dim nothing happens (resting natural state)
  220.       reset_ms_us_counters();
  221.       program_all_dim_loop(6000);
  222.  
  223. //2)    Left Turn
  224.       //2 - left turn, all dim, 2 will blink at ½ sec for 10 sec.
  225.       reset_ms_us_counters();
  226.       while(program_ms_counter < 10000)
  227.       {
  228.          program_left_turn_loop(program_ms_counter+500);
  229.          program_all_dim_loop  (program_ms_counter+500);
  230.       }
  231.       //then dim for 1 sec
  232.       program_all_dim_loop(11000);
  233.  
  234. //3)    Dim (4 sec)
  235.       //4 - All dim nothing happens (resting natural state)
  236.       reset_ms_us_counters();
  237.       program_all_dim_loop(4000);
  238.  
  239. //4)    Brakes
  240.       //3 – brakes get bright (8 LED’s) for 4 sec then dim for 1sec for 12 seconds
  241.       reset_ms_us_counters();
  242.       while(program_ms_counter < 12000)
  243.       {
  244.          program_break_loop  (program_ms_counter+4000);
  245.          program_all_dim_loop(program_ms_counter+1000);
  246.       }
  247.  
  248. //5)    Dim (4 Sec)
  249.       //4 - All dim nothing happens (resting natural state)
  250.       reset_ms_us_counters();
  251.       program_all_dim_loop(4000);
  252.  
  253. //6)    Right Turn
  254.       //5 – right turn, all dim, 2 will blink at ½ sec for 10 sec. 
  255.       reset_ms_us_counters();
  256.       while(program_ms_counter < 10000)
  257.       {
  258.          program_right_turn_loop(program_ms_counter+500);
  259.          program_all_dim_loop   (program_ms_counter+500);
  260.       }
  261.       //then dim for 1 sec.
  262.       program_all_dim_loop(11000);
  263.  
  264. //7)    Dim (4 sec)
  265.       //4 - All dim nothing happens (resting natural state)
  266.       reset_ms_us_counters();
  267.       program_all_dim_loop(4000);
  268.  
  269. //8)    Brakes
  270.       //6 – brakes get bright (8 LED’s) for 4 sec then dim for 1sec for 12 seconds
  271.       reset_ms_us_counters();
  272.       while(program_ms_counter < 12000)
  273.       {
  274.          program_break_loop  (program_ms_counter+4000);
  275.          program_all_dim_loop(program_ms_counter+1000);
  276.       }
  277.  
  278. //9)    Loop
  279.  
  280. }
  281.  
  282.   while(1);                 // Ending infinite loop (just in case)
  283.  
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement