Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright 2012 D Westaby
- ----------------------------------------------------------------------
- Car Lights for Attiny2313
- ----------------------------------------------------------------------
- Title: Car_Lighting.D
- Author: Dustin Westaby
- Date Created: September 8, 2012
- Date Modified: September 24, 2012
- Compiled with AVR-GCC WinAVR
- ----------------------------------------------------------------------
- Fuses:
- ----------------------------------------------------------------------
- BrownOut Disabled
- CKDIV8
- Int RC Osc 8Mhz + 64ms
- ----------------------------------------------------------------------
- Inputs:
- ----------------------------------------------------------------------
- None
- ----------------------------------------------------------------------
- Ouputs:
- ----------------------------------------------------------------------
- pin i/o port circuit trace connection
- ----------------------------------------------------------------------
- 4 1 PA1 = R_LED1
- 5 1 PA0 = L_LED2
- 2 1 PD0 = R_LED3
- 3 1 PD1 = R_LED4
- 6 1 PD2 = R_LED5
- 7 1 PD3 = R_LED6
- 14 1 PB2 = L_LED7
- 17 1 PB5 = L_LED8
- 18 1 PB6 = L_LED9
- 19 1 PB7 = L_LED10
- */
- //--------------------------------------
- // Global Variables |
- //--------------------------------------
- // 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
- // One CPU Cycle = 1us
- #define F_CPU 8000000UL/8
- #define R_LED1_PA1 (1)
- #define L_LED2_PA0 (0)
- #define R_LED3_PD0 (0)
- #define R_LED4_PD1 (1)
- #define R_LED5_PD2 (2)
- #define R_LED6_PD3 (3)
- #define L_LED7_PB2 (2)
- #define L_LED8_PB5 (5)
- #define L_LED9_PB6 (6)
- #define L_LED10_PB7 (7)
- #define CONST_DIM_DELAY_ON (200)
- #define CONST_DIM_DELAY_OFF (4000)
- /* This value is used to count us of delay and convert to ms
- Due to other cpu processing: "1ms of delay" < "1000us of delay" */
- //#define TIME_CONVERSION_VAL (50) //actual number should be 150 (am running faster)
- #define TIME_CONVERSION_VAL (150)
- int program_ms_counter;
- int program_counter_us; //used for tracking time elapsed
- //--------------------------------------
- // Includes |
- //--------------------------------------
- #include <avr/io.h>
- #include <util/delay.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) {
- program_ms_counter+=ms;
- while ( ms )
- {
- _delay_ms(1);
- ms--;
- }
- }
- void delay_us(uint16_t us) {
- program_counter_us+=us;
- while ( us )
- {
- _delay_us(1);
- us--;
- }
- while(program_counter_us>=TIME_CONVERSION_VAL)
- {
- program_ms_counter++;
- program_counter_us-=TIME_CONVERSION_VAL;
- }
- }
- //--------------------------------------
- // Misc Subroutines |
- //--------------------------------------
- void reset_ms_us_counters(void) {
- program_ms_counter=0;
- program_counter_us=0;
- }
- void all_LEDs_ON(void) {
- PORTA = 0xFF;
- PORTB = 0xFF;
- PORTD = 0xFF;
- }
- void all_LEDs_OFF(void) {
- PORTA = ~(0x03);
- PORTB = ~(0xFF);
- PORTD = ~(0xFF);
- }
- void program_left_turn_mode(void) {
- PORTA &= ~((1<<R_LED1_PA1)+(1<<L_LED2_PA0));
- PORTB &= ~((1<<L_LED10_PB7)+(1<<L_LED8_PB5));
- PORTD &= ~((1<<R_LED6_PD3)+(1<<R_LED5_PD2)+(1<<R_LED4_PD1)+(1<<R_LED3_PD0));
- }
- void program_brake_mode(void) {
- PORTA &= ~((1<<R_LED1_PA1)+(1<<L_LED2_PA0));
- }
- void program_right_turn_mode (void) {
- PORTA &= ~((1<<R_LED1_PA1)+(1<<L_LED2_PA0));
- PORTB &= ~((1<<L_LED10_PB7)+(1<<L_LED9_PB6)+(1<<L_LED8_PB5)+(1<<L_LED7_PB2));
- PORTD &= ~((1<<R_LED6_PD3)+(1<<R_LED4_PD1));
- }
- void program_all_dim_loop (int timer_val) {
- while(program_ms_counter < timer_val)
- {
- all_LEDs_ON();
- delay_us(CONST_DIM_DELAY_ON);
- all_LEDs_OFF();
- delay_us(CONST_DIM_DELAY_OFF);
- }
- }
- void program_break_loop (int timer_val) {
- while(program_ms_counter < timer_val)
- {
- all_LEDs_ON();
- delay_us(CONST_DIM_DELAY_ON);
- program_brake_mode();
- delay_us(CONST_DIM_DELAY_OFF);
- }
- }
- void program_left_turn_loop (int timer_val) {
- while(program_ms_counter < timer_val)
- {
- all_LEDs_ON();
- delay_us(CONST_DIM_DELAY_ON);
- program_left_turn_mode();
- delay_us(CONST_DIM_DELAY_OFF);
- }
- }
- void program_right_turn_loop (int timer_val) {
- while(program_ms_counter < timer_val)
- {
- all_LEDs_ON();
- delay_us(CONST_DIM_DELAY_ON);
- program_right_turn_mode();
- delay_us(CONST_DIM_DELAY_OFF);
- }
- }
- //--------------------------------------
- // Main |
- //--------------------------------------
- int main (void)
- {
- /* ---------------------------------------------------------------- */
- /* Initialization */
- /* ---------------------------------------------------------------- */
- //Initialize all ports as outputs
- DDRA = 0b00000011; //A0, A1
- DDRB = 0b11111111;
- DDRD = 0b11111111;
- all_LEDs_OFF();
- /* ---------------------------------------------------------------- */
- /* Main Loop */
- /* ---------------------------------------------------------------- */
- while(1)
- {
- //1) Dim
- //1 - All dim nothing happens (resting natural state)
- reset_ms_us_counters();
- program_all_dim_loop(6000);
- //2) Left Turn
- //2 - left turn, all dim, 2 will blink at ½ sec for 10 sec.
- reset_ms_us_counters();
- while(program_ms_counter < 10000)
- {
- program_left_turn_loop(program_ms_counter+500);
- program_all_dim_loop (program_ms_counter+500);
- }
- //then dim for 1 sec
- program_all_dim_loop(11000);
- //3) Dim (4 sec)
- //4 - All dim nothing happens (resting natural state)
- reset_ms_us_counters();
- program_all_dim_loop(4000);
- //4) Brakes
- //3 – brakes get bright (8 LED’s) for 4 sec then dim for 1sec for 12 seconds
- reset_ms_us_counters();
- while(program_ms_counter < 12000)
- {
- program_break_loop (program_ms_counter+4000);
- program_all_dim_loop(program_ms_counter+1000);
- }
- //5) Dim (4 Sec)
- //4 - All dim nothing happens (resting natural state)
- reset_ms_us_counters();
- program_all_dim_loop(4000);
- //6) Right Turn
- //5 – right turn, all dim, 2 will blink at ½ sec for 10 sec.
- reset_ms_us_counters();
- while(program_ms_counter < 10000)
- {
- program_right_turn_loop(program_ms_counter+500);
- program_all_dim_loop (program_ms_counter+500);
- }
- //then dim for 1 sec.
- program_all_dim_loop(11000);
- //7) Dim (4 sec)
- //4 - All dim nothing happens (resting natural state)
- reset_ms_us_counters();
- program_all_dim_loop(4000);
- //8) Brakes
- //6 – brakes get bright (8 LED’s) for 4 sec then dim for 1sec for 12 seconds
- reset_ms_us_counters();
- while(program_ms_counter < 12000)
- {
- program_break_loop (program_ms_counter+4000);
- program_all_dim_loop(program_ms_counter+1000);
- }
- //9) Loop
- }
- while(1); // Ending infinite loop (just in case)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement