akoimeexx

Flashing Goggle Attiny13 code

Feb 29th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. /**
  2.  * AVR program to flash LEDs in apair of brazing goggles.
  3.  * Copyright (c) 2012 Akoi Meexx (http://akoimeexx.com/)
  4.  *
  5.  * Chip type: Attiny13
  6.  * Clock frequency: Default internal clock (9.6MHz / 8 prescaler = 1.2MHz)
  7.  *                       +--------+
  8.  *        [        (PB5) |1*     8| (VCC)  Power ]
  9.  *        [        (PB3) |2      7| (PB2)  GREEN ]
  10.  *        [        (PB4) |3      6| (PB1) YELLOW ]
  11.  *        [ Ground (GND) |4      5| (PB0)    RED ]
  12.  *                       +--------+
  13.  */
  14.  
  15. /**
  16.  * AVR-specific defines and includes
  17.  */
  18. #include <avr/io.h>
  19. #define F_CPU 1200000UL // 1.2 MHz
  20. #include <util/delay.h>
  21. #include <avr/eeprom.h>
  22. uint16_t EEMEM nonvolatile_mode;
  23.  
  24. // Some defines that make the code more readable
  25. #define output_low(port,pin) port &= ~(1<<pin)
  26. #define output_high(port,pin) port |= (1<<pin)
  27. #define toggle_pin(port,pin) port ^= (1<<pin)
  28. #define set_input(portdir,pin) portdir &= ~(1<<pin)
  29. #define set_output(portdir,pin) portdir |= (1<<pin)
  30.  
  31. /**
  32.  * Alias our LED pinouts
  33.  */
  34. #define RED PB0
  35. #define YELLOW PB1
  36. #define GREEN PB2
  37.  
  38. /**
  39.  * Define our lighting modes and declare our mode global
  40.  */
  41. #define DEBUG_MODE 0
  42. #define TOGGLE_MODE 1
  43. #define FADE_MODE 2
  44. #define AUDIO_MODE 3
  45. int lighting_mode;
  46.  
  47.  
  48. /**
  49.  * Switches all output pins off then switches on specified pin
  50.  */
  51. void toggle_step(int pin) {
  52.     output_low(PORTB, RED);
  53.     output_low(PORTB, YELLOW);
  54.     output_low(PORTB, GREEN);
  55.      
  56.     output_high(PORTB, pin);
  57. }
  58.  
  59. void init(void) {
  60.     int increment_mode;
  61.    
  62.     lighting_mode = (int) eeprom_read_word(&nonvolatile_mode); // DEBUG_MODE;
  63.     increment_mode = lighting_mode + 1;
  64.    
  65.     if (increment_mode > AUDIO_MODE) {
  66.         increment_mode = 0;
  67.     }
  68.    
  69.     eeprom_write_word(&nonvolatile_mode, increment_mode);
  70.    
  71.     set_output(DDRB, RED);  
  72.     set_output(DDRB, YELLOW);
  73.     set_output(DDRB, GREEN);   
  74. }
  75.  
  76. int main(void) {
  77.     init();
  78.     while(1) {
  79.         switch (lighting_mode) {
  80.             case DEBUG_MODE:
  81.                 toggle_pin(PORTB, RED);
  82.                 _delay_ms(1000);
  83.                 break;
  84.             case TOGGLE_MODE:
  85.                 toggle_step(GREEN);
  86.                 _delay_ms(250);
  87.                 toggle_step(YELLOW);
  88.                 _delay_ms(250);
  89.                 toggle_step(RED);
  90.                 _delay_ms(250);
  91.                 toggle_step(YELLOW);
  92.                 _delay_ms(250);
  93.                 break;
  94.             case FADE_MODE:
  95.                 // TODO: fade_step();
  96.                 lighting_mode = TOGGLE_MODE;
  97.                 break;
  98.             case AUDIO_MODE:
  99.                 // TODO: audio_hell();
  100.                 lighting_mode = TOGGLE_MODE;
  101.                 break;
  102.             default:
  103.                 lighting_mode = DEBUG_MODE;
  104.         }
  105.     }
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment