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.c
- Author: Dustin Westaby
- Date Created: September 8, 2012
- Date Modified: September 21, 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<<1)
- #define L_LED2_PA0 (1<<0)
- #define R_LED3_PD0 (1<<0)
- #define R_LED4_PD1 (1<<1)
- #define R_LED5_PD2 (1<<2)
- #define R_LED6_PD3 (1<<3)
- #define L_LED7_PB2 (1<<2)
- #define L_LED8_PB5 (1<<5)
- #define L_LED9_PB6 (1<<6)
- #define L_LED10_PB7 (1<<7)
- //delay wait values, measured in us
- #define DIM_DELAY_ON (200)
- #define DIM_DELAY_OFF (200)
- /* 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)
- 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--;
- }
- //This loop will continue to convert us into ms until the "us counter" is
- // below the conversion val
- while(program_counter_us>=TIME_CONVERSION_VAL)
- {
- program_ms_counter++; //increment "ms counter"
- program_counter_us-=TIME_CONVERSION_VAL; //subtract "us counter"
- }
- }
- //--------------------------------------
- // Program Subroutines |
- //--------------------------------------
- void reset_ms_us_counters(void) {
- //set "ms counter" and "us counter" to 0
- program_ms_counter=0;
- program_counter_us=0;
- }
- void all_LEDs_BRT(void) {
- //Sets all PORT outputs to 1, which is a high (+5V) on the pin
- PORTA = 0xFF; // = 0b11111111
- PORTB = 0xFF;
- PORTD = 0xFF;
- }
- void all_LEDs_OFF(void) {
- //Sets all PORT outputs to 0, which is a low (GND) on the pin
- Porta &= ~( R_Led1_Pa1 //Turn Off Port A, Pin 1
- + L_Led2_Pa0); //Turn Off Port A, Pin 0
- Portb &= ~( L_Led10_Pb7 //Turn Off Port B, Pin 7
- + L_Led9_Pb6 //Turn Off Port B, Pin 6
- + L_Led8_Pb5 //Turn Off Port B, Pin 5
- + L_Led7_Pb2); //Turn Off Port B, Pin 2
- Portd &= ~( R_Led6_Pd3 //Turn Off Port D, Pin 3
- + R_Led5_Pd2 //Turn Off Port D, Pin 2
- + R_Led4_Pd1 //Turn Off Port D, Pin 1
- + R_Led3_Pd0); //Turn Off Port D, Pin 0
- }
- void program2_LEDs_to_DIM(void) {
- PORTA &= ~( R_LED1_PA1 //Turn Off PORT A, pin 1
- + L_LED2_PA0); //Turn Off PORT A, pin 0
- PORTB &= ~( L_LED10_PB7 //Turn Off PORT B, pin 7
- + L_LED8_PB5); //Turn Off PORT B, pin 5
- PORTD &= ~( R_LED6_PD3 //Turn Off PORT D, pin 3
- + R_LED5_PD2 //Turn Off PORT D, pin 2
- + R_LED4_PD1 //Turn Off PORT D, pin 1
- + R_LED3_PD0); //Turn Off PORT D, pin 0
- }
- void program3_6_LEDs_to_DIM(void) {
- PORTA &= ~( R_LED1_PA1 //Turn Off PORT A, pin 1
- + L_LED2_PA0); //Turn Off PORT A, pin 0
- }
- void program5_LEDs_to_DIM (void) {
- PORTA &= ~( R_LED1_PA1 //Turn Off PORT A, pin 1
- + L_LED2_PA0); //Turn Off PORT A, pin 0
- PORTB &= ~( L_LED10_PB7 //Turn Off PORT B, pin 7
- + L_LED9_PB6 //Turn Off PORT B, pin 6
- + L_LED8_PB5 //Turn Off PORT B, pin 5
- + L_LED7_PB2); //Turn Off PORT B, pin 2
- PORTD &= ~( R_LED6_PD3 //Turn Off PORT D, pin 3
- + R_LED4_PD1); //Turn Off PORT D, pin 1
- }
- /* Software PWM explaination:
- The dim LED effect is performed by a software PWM. Each of the loops in the
- main() function turn on all LEDs, then turn off only the LEDs we want as DIM.
- The LEDs that remain on will appear BRIGHT. The LEDs that are rapid turned
- on and off will appear DIM.
- DIM_DELAY_ON DIM_DELAY_ON
- +5V ______________ _______________
- | | | |
- GND _______________| |_______________| |______________
- DIM_DELAY_OFF DIM_DELAY_OFF DIM_DELAY_OFF
- */
- void program_NORMAL_MODE(void) {
- all_LEDs_BRT(); //Turn on all LEDs
- delay_us(DIM_DELAY_ON); //Time to keep all LEDs on
- all_LEDs_OFF(); //Turn off all LEDs
- delay_us(DIM_DELAY_OFF); //Time to keep all LEDs off
- }
- void program_LEFT_TURN(void) {
- all_LEDs_BRT(); //Turn on all LEDs
- delay_us(DIM_DELAY_ON); //Time to keep all LEDs on
- program2_LEDs_to_DIM(); //Turn off program 2 LEDs
- delay_us(DIM_DELAY_OFF); //Time to keep program 2 LEDs off
- }
- void program_BRAKE_MODE(void) {
- all_LEDs_BRT(); //Turn on all LEDs
- delay_us(DIM_DELAY_ON); //Time to keep all LEDs on
- program3_6_LEDs_to_DIM(); //Turn off program 3 LEDs
- delay_us(DIM_DELAY_OFF); //Time to keep program 3 LEDs off
- }
- void program_RIGHT_TURN (void) {
- all_LEDs_BRT(); //Turn on all LEDs
- delay_us(DIM_DELAY_ON); //Time to keep all LEDs on
- program5_LEDs_to_DIM(); //Turn off program 5 LEDs
- delay_us(DIM_DELAY_OFF); //Time to keep program 5 LEDs off
- }
- //--------------------------------------
- // Main |
- //--------------------------------------
- int main (void)
- {
- /* ---------------------------------------------------------------- */
- /* Initialization */
- /* ---------------------------------------------------------------- */
- //Initialize all ports as outputs
- DDRA = 0b00000011; //A0, A1
- DDRB = 0b11111111;
- DDRD = 0b11111111;
- //Begin with all LEDs off
- all_LEDs_OFF();
- /* ---------------------------------------------------------------- */
- /* Main Loop */
- /* ---------------------------------------------------------------- */
- while(1)
- {
- /* ---------------------------- */
- /* Program 1 - NORMAL */
- /* ---------------------------- */
- reset_ms_us_counters();
- while(program_ms_counter < 6000)
- {
- program_NORMAL_MODE();
- }
- /* ---------------------------- */
- /* Program 3 - BRAKES */
- /* ---------------------------- */
- reset_ms_us_counters();
- while(program_ms_counter < 500)
- {
- program_BRAKE_MODE();
- }
- while(program_ms_counter < 1000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 1500)
- {
- program_BRAKE_MODE();
- }
- while(program_ms_counter < 2000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 2500)
- {
- program_BRAKE_MODE();
- }
- while(program_ms_counter < 3000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 3500)
- {
- program_BRAKE_MODE();
- }
- while(program_ms_counter < 4000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 4500)
- {
- program_BRAKE_MODE();
- }
- while(program_ms_counter < 5000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 5500)
- {
- program_BRAKE_MODE();
- }
- while(program_ms_counter < 6000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 6500)
- {
- program_BRAKE_MODE();
- }
- while(program_ms_counter < 7000)
- {
- program_NORMAL_MODE();
- }
- /* ------------------------------- */
- /* Program 2 - LEFT TURN */
- /* ------------------------------- */
- reset_ms_us_counters();
- while(program_ms_counter < 3000)
- {
- program_LEFT_TURN();
- }
- while(program_ms_counter < 4000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 6500)
- {
- program_LEFT_TURN();
- }
- while(program_ms_counter < 7000)
- {
- program_NORMAL_MODE();
- }
- /* ---------------------------- */
- /* Program 1 - NORMAL */
- /* ---------------------------- */
- reset_ms_us_counters();
- while(program_ms_counter < 6000)
- {
- program_NORMAL_MODE();
- }
- /* ------------------------------------ */
- /* Program 4 - NORMAL FLASHING */
- /* ------------------------------------ */
- reset_ms_us_counters();
- while(program_ms_counter < 500)
- {
- all_LEDs_OFF();
- }
- while(program_ms_counter < 1000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 1500)
- {
- all_LEDs_OFF();
- }
- while(program_ms_counter < 2000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 2500)
- {
- all_LEDs_OFF();
- }
- while(program_ms_counter < 3000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 3500)
- {
- all_LEDs_OFF();
- }
- while(program_ms_counter < 4000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 4500)
- {
- all_LEDs_OFF();
- }
- while(program_ms_counter < 5000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 5500)
- {
- all_LEDs_OFF();
- }
- while(program_ms_counter < 6000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 6500)
- {
- all_LEDs_OFF();
- }
- while(program_ms_counter < 7000)
- {
- program_NORMAL_MODE();
- }
- /* ------------------------------- */
- /* Program 2 - LEFT TURN */
- /* ------------------------------- */
- reset_ms_us_counters();
- while(program_ms_counter < 3000)
- {
- program_LEFT_TURN();
- }
- while(program_ms_counter < 4000)
- {
- program_NORMAL_MODE();
- }
- while(program_ms_counter < 6500)
- {
- program_LEFT_TURN();
- }
- while(program_ms_counter < 7000)
- {
- program_NORMAL_MODE();
- }
- }
- while(1); // Ending infinite loop (just in case)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement