Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright 2012 D Westaby
- ----------------------------------------------------------------------
- Generic Pattern for Attiny2313
- ----------------------------------------------------------------------
- Title: Generic_Lighting.c
- Author: Dustin Westaby
- Date Created: September 8, 2012
- Date Modified: March 21, 2013
- Compiled with AVR-GCC WinAVR
- Connection reference by Alex Weber:
- http://tinkerlog.com/2009/06/18/microcontroller-cheat-sheet/
- ----------------------------------------------------------------------
- Fuses:
- ----------------------------------------------------------------------
- BrownOut Disabled
- CKDIV8
- Int RC Osc 8Mhz + 64ms
- ----------------------------------------------------------------------
- Inputs:
- ----------------------------------------------------------------------
- pin i/o port circuit trace notes
- ----------------------------------------------------------------------
- 1 0 PA2 = RESET Momentary Switch to ground
- 10 GND = Ground
- 20 VCC = +V Battery
- 12 0 PB0 = B0 Momentary Switch to ground
- Note: Momentary switches need pullup 10k resisters
- ----------------------------------------------------------------------
- Ouputs:
- ----------------------------------------------------------------------
- pin i/o port circuit trace notes
- ----------------------------------------------------------------------
- 2 1 PD0 = LED1 *1
- 3 1 PD1 = LED2 *1
- 4 1 PA1 = LED3 *1
- 5 1 PA0 = LED4 *1
- 6 1 PD2 = LED5
- 7 1 PD3 = LED6
- 8 1 PD4 = LED7
- 9 1 PD5 = LED8
- 11 1 PD6 = LED9
- 13 1 PB1 = LED10
- 14 1 PB2 = LED11
- 17 1 PB5 = LED12 *1
- 18 1 PB6 = LED13 *1
- 19 1 PB7 = LED14 *1
- Note *1 - LED connected will disable pin's alternate functions
- = external osc
- = serial
- = programming
- Use a resister to isolate these.
- */
- //--------------------------------------
- // Global Variables |
- //--------------------------------------
- // 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
- // One CPU Cycle = 1us
- #define F_CPU 8000000UL/8
- #define LED1_PD0 (0)
- #define LED2_PD1 (1)
- #define LED3_PA1 (1)
- #define LED4_PA0 (0)
- #define LED5_PD2 (2)
- #define LED6_PD3 (3)
- #define LED7_PD4 (4) //These 4 are new to the program
- #define LED8_PD5 (5)
- #define LED9_PD6 (6)
- #define LED10_PB1 (1)
- #define LED11_PB2 (2)
- #define LED12_PB5 (5)
- #define LED13_PB6 (6)
- #define LED14_PB7 (7)
- #define INPUT_PB0 (0)
- #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)
- #define OFF (0)
- #define ON (1)
- #define LED_ON(led_position) (ON<<led_position)
- #define LED_OFF(led_position) (OFF<<led_position)
- #define INPUT_SW (0)
- #define OUTPUT_LED (1)
- 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;
- }
- }
- //--------------------------------------
- // Common 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);
- }
- //--------------------------------------
- // Switch Debounce |
- //--------------------------------------
- int button_is_pressed()
- {
- // the button is pressed when bit is clear
- if (bit_is_clear(PINB, PB0))
- {
- delay_ms(25);
- if (bit_is_clear(PINB, PB0)) return 1;
- }
- return 0;
- }
- //--------------------------------------
- // Specific Subroutines |
- //--------------------------------------
- void program_left_turn_mode(void) {
- /* Opposite '~', actually turns the following LEDs OFF */
- PORTA &= ~(LED_ON(LED3_PA1) | LED_ON(LED4_PA0));
- PORTA &= 0xFF & (LED_OFF(LED3_PA1) | LED_OFF(LED4_PA0));
- PORTB &= ~(LED_ON(LED14_PB7) | LED_ON(LED12_PB5));
- PORTD &= ~(LED_ON(LED6_PD3) | LED_ON(LED5_PD2) | LED_ON(LED2_PD1) | LED_ON(LED1_PD0));
- }
- void program_brake_mode(void) {
- /* Opposite '~', actually turns the following LEDs OFF */
- PORTA &= ~(LED_ON(LED3_PA1) | LED_ON(LED4_PA0));
- }
- void program_right_turn_mode (void) {
- /* Opposite '~', actually turns the following LEDs OFF */
- PORTA &= ~(LED_ON(LED3_PA1) | LED_ON(LED4_PA0));
- PORTB &= ~(LED_ON(LED14_PB7) | LED_ON(LED13_PB6) | LED_ON(LED12_PB5) | LED_ON(LED11_PB2));
- PORTD &= ~(LED_ON(LED6_PD3) | LED_ON(LED2_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
- // DDRA = 0b00000011; //A0, A1
- // DDRB = 0b11111111;
- // DDRD = 0b11111111;
- DDRA = (OUTPUT_LED<<LED4_PA0) | (OUTPUT_LED<<LED3_PA1);
- DDRB = (INPUT_SW<<INPUT_PB0) | (OUTPUT_LED<<LED10_PB1) | (OUTPUT_LED<<LED11_PB2) | (OUTPUT_LED<<LED12_PB5) | (OUTPUT_LED<<LED13_PB6) | (OUTPUT_LED<<LED14_PB7);
- DDRD = (OUTPUT_LED<<LED1_PD0) | (OUTPUT_LED<<LED2_PD1) | (OUTPUT_LED<<LED5_PD2) | (OUTPUT_LED<<LED6_PD3) | (OUTPUT_LED<<LED7_PD4) | (OUTPUT_LED<<LED8_PD5) | (OUTPUT_LED<<LED9_PD6);
- 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