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
- 12 0 PB0 = B0 Momentary Switch to ground
- 20 VCC = +V Battery
- 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
- 15 1 PB3 = LED12
- 16 1 PB4 = LED13
- 17 1 PB5 = LED14 *1
- 18 1 PB6 = LED15 *1
- 19 1 PB7 = LED16 *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)
- #define LED8_PD5 (5)
- #define LED9_PD6 (6)
- #define LED10_PB1 (1)
- #define LED12_PB3 (3)
- #define LED13_PB4 (4)
- #define LED11_PB2 (2)
- #define LED14_PB5 (5)
- #define LED15_PB6 (6)
- #define LED16_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 (ground)
- if (bit_is_clear(PINB, PB0))
- {
- delay_ms(25);
- if (bit_is_clear(PINB, PB0)) return 1;
- }
- return 0;
- }
- //--------------------------------------
- // Specific Subroutines |
- //--------------------------------------
- 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_outer_leds (int circle) {
- /* The outer circle of LEDs are made up of the following eight:
- LED9_PD6 1 00000001
- LED10_PB1 2 00000010
- LED11_PB2 3 00000100
- LED12_PB3 4 00001000
- LED13_PB4 5 00010000
- LED14_PB5 6 00100000
- LED15_PB6 7 01000000
- LED16_PB7 8 10000000
- */
- //start with circle off
- PORTB |= LED_OFF(LED10_PB1) | LED_OFF(LED11_PB2) | LED_OFF(LED12_PB3) |
- LED_OFF(LED13_PB4) | LED_OFF(LED14_PB5) | LED_OFF(LED15_PB6) |
- LED_OFF(LED16_PB7);
- PORTD |= LED_OFF(LED9_PD6);
- //turn on the leds requested in the byte passed in
- for ( int i=0; circle != 0, i < 8; i++ )
- {
- if ( circle && (1<<i) )
- {
- switch (i)
- {
- case 1
- PORTD |= LED_ON(LED9_PD6);
- break;
- case 2
- PORTB |= LED_ON(LED10_PB1);
- break;
- case 3
- PORTB |= LED_ON(LED11_PB2);
- break;
- case 4
- PORTB |= LED_ON(LED12_PB3);
- break;
- case 5
- PORTB |= LED_ON(LED13_PB4);
- break;
- case 6
- PORTB |= LED_ON(LED14_PB5);
- break;
- case 7
- PORTB |= LED_ON(LED15_PB6);
- break;
- case 8
- PORTB |= LED_ON(LED16_PB7);
- break;
- default
- //should never hit
- circle = 0;
- break;
- } //end switch
- } // end if
- } // end loop
- }
- void program_inner_leds (int circle) {
- /* The inner circle of LEDs are made up of the following eight:
- LED1_PD0 1 00000001
- LED2_PD1 2 00000010
- LED3_PA1 3 00000100
- LED4_PA0 4 00001000
- LED5_PD2 5 00010000
- LED6_PD3 6 00100000
- LED7_PD4 7 01000000
- LED8_PD5 8 10000000
- */
- //start with circle off
- PORTD |= LED_OFF(LED1_PD0) | LED_OFF(LED2_PD1) | LED_OFF(LED5_PD2) |
- LED_OFF(LED6_PD3) | LED_OFF(LED7_PD4) | LED_OFF(LED8_PD5);
- PORTA |= LED_OFF(LED3_PA1) | LED_OFF(LED4_PA0);
- //turn on the leds requested in the byte passed in
- for ( int i=0; circle != 0, i < 8; i++ )
- {
- if ( circle && (1<<i) )
- {
- switch (i)
- {
- case 1
- PORTD |= LED_ON(LED1_PD0);
- break;
- case 2
- PORTD |= LED_ON(LED2_PD1);
- break;
- case 3
- PORTA |= LED_ON(LED3_PA1);
- break;
- case 4
- PORTA |= LED_ON(LED4_PA0);
- break;
- case 5
- PORTD |= LED_ON(LED5_PD2);
- break;
- case 6
- PORTD |= LED_ON(LED6_PD3);
- break;
- case 7
- PORTD |= LED_ON(LED7_PD4);
- break;
- case 8
- PORTD |= LED_ON(LED8_PD5);
- break;
- default
- //should never hit
- circle = 0;
- break;
- } //end switch
- } // end if
- } // end loop
- }
- void program_turn_on_sequence_inner(int speed) {
- int circle_placeholder=0;
- //turn on each led in circle, in sequence
- for(int i=0; i<16, circle_placeholder<=0b11111111; i++)
- {
- circle_placeholder |= 1<<i;
- program_inner_leds(circle_placeholder);
- delay_us(speed);
- }
- }
- void program_turn_on_sequence_outer(int speed) {
- int circle_placeholder=0;
- //turn on each led in circle, in sequence
- for(int i=0; i<16, circle_placeholder<=0b11111111; i++)
- {
- circle_placeholder |= 1<<i;
- program_outer_leds(circle_placeholder);
- delay_us(speed);
- }
- }
- //--------------------------------------
- // Main |
- //--------------------------------------
- int main (void)
- {
- /* ---------------------------------------------------------------- */
- /* Initialization */
- /* ---------------------------------------------------------------- */
- //Initialize all ports
- DDRA = (OUTPUT_LED<<LED4_PA0) | (OUTPUT_LED<<LED3_PA1);
- DDRB = (INPUT_SW<<INPUT_PB0) | (OUTPUT_LED<<LED10_PB1) | (OUTPUT_LED<<LED11_PB2) |
- (OUTPUT_LED<<LED14_PB5) | (OUTPUT_LED<<LED15_PB6) | (OUTPUT_LED<<LED16_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);
- //clean state
- all_LEDs_OFF();
- reset_ms_us_counters();
- //Turn on each led in sequence to ignite, 20ms between animations
- program_turn_on_sequence_inner(20000);
- program_turn_on_sequence_outer(20000);
- //dim down after powerup, 6ms
- program_all_dim_loop(46000);
- /* ---------------------------------------------------------------- */
- /* Main Loop */
- /* ---------------------------------------------------------------- */
- while(1)
- {
- reset_ms_us_counters();
- // do this loop while waiting for button press
- while(!button_is_pressed())
- {
- //minimize delays in here, delays could miss a button press
- //Dim (0.4 seconds)
- program_all_dim_loop(400);
- }
- //button was pressed
- //do stuff
- }
- while(1); // Ending infinite loop (just in case)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement