akoimeexx icon

Flashing Goggle Attiny13 code

akoimeexx | PRO | 02/29/12 03:08:18 PM UTC | 0 ⭐ | 271 👁️ | Never ⏰ | []
C |

2.45 KB

|

None

|

0 👍

/

0 👎

/**
 * AVR program to flash LEDs in apair of brazing goggles.
 * Copyright (c) 2012 Akoi Meexx (http://akoimeexx.com/)
 * 
 * Chip type: Attiny13
 * Clock frequency: Default internal clock (9.6MHz / 8 prescaler = 1.2MHz)
 *                       +--------+
 *        [        (PB5) |1*     8| (VCC)  Power ]
 *        [        (PB3) |2      7| (PB2)  GREEN ]
 *        [        (PB4) |3      6| (PB1) YELLOW ]
 *        [ Ground (GND) |4      5| (PB0)    RED ]
 *                       +--------+
 */
 
/**
 * AVR-specific defines and includes
 */
#include <avr/io.h>
#define F_CPU 1200000UL // 1.2 MHz
#include <util/delay.h>
#include <avr/eeprom.h>
uint16_t EEMEM nonvolatile_mode;
 
// Some defines that make the code more readable
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define toggle_pin(port,pin) port ^= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)
 
/**
 * Alias our LED pinouts
 */
#define RED PB0
#define YELLOW PB1
#define GREEN PB2
 
/**
 * Define our lighting modes and declare our mode global
 */
#define DEBUG_MODE 0
#define TOGGLE_MODE 1
#define FADE_MODE 2
#define AUDIO_MODE 3
int lighting_mode;
 
 
/**
 * Switches all output pins off then switches on specified pin
 */
void toggle_step(int pin) {
    output_low(PORTB, RED); 
    output_low(PORTB, YELLOW); 
    output_low(PORTB, GREEN);
     
    output_high(PORTB, pin); 
}
 
void init(void) {
    int increment_mode;
    
    lighting_mode = (int) eeprom_read_word(&nonvolatile_mode); // DEBUG_MODE;
    increment_mode = lighting_mode + 1;
    
    if (increment_mode > AUDIO_MODE) {
        increment_mode = 0;
    }
    
    eeprom_write_word(&nonvolatile_mode, increment_mode);
    
    set_output(DDRB, RED);  
    set_output(DDRB, YELLOW);
    set_output(DDRB, GREEN);    
}
 
int main(void) {
    init();
    while(1) {
        switch (lighting_mode) {
            case DEBUG_MODE:
                toggle_pin(PORTB, RED);
                _delay_ms(1000);
                break;
            case TOGGLE_MODE:
                toggle_step(GREEN);
                _delay_ms(250);
                toggle_step(YELLOW);
                _delay_ms(250);
                toggle_step(RED);
                _delay_ms(250);
                toggle_step(YELLOW);
                _delay_ms(250);
                break;
            case FADE_MODE:
                // TODO: fade_step();
                lighting_mode = TOGGLE_MODE;
                break;
            case AUDIO_MODE:
                // TODO: audio_hell();
                lighting_mode = TOGGLE_MODE;
                break;
            default:
                lighting_mode = DEBUG_MODE;
        }
    }
    return 0;
}

Comments