Rev Author Line No. Line
1108 miho 1 /* ---------------------------------------------------------------------------
2 * AVR Blink Demo Application
3 * www.mlab.cz miho 2008
4 * ---------------------------------------------------------------------------
5 * Demo program for AVR processors
6 * Simple blinking application for ATtiny13/ATmega8 and similar
7 * ---------------------------------------------------------------------------
8 * 00.00 2008/04/18 First Version
9 * ---------------------------------------------------------------------------
10 */
11  
12  
13 // Configuration
14 // -------------
15  
16  
17 // Define LED port here
18 // Use port available on all devices with no conflict with ISP port
19 #define LED B
20 #define LED_BIT 3
21  
22  
23 // Define CPU clock frequency here
24 // Do not forget to set Clock Oscillator in Low Fuse
25 // Select just one
26 //#define F_CPU 1000000UL // Frequency in Hz
27 #define F_CPU F_CPU_DEFAULT // Default internal RC clock frequency
28 //#define F_CPU F_CPU_MAX // Maximum internal RC clock frequency
29  
30  
31 // Define blink period
32 #define PERIOD 5000 // in ms
33 #define DUTY 20 // in %
34  
35  
36 // End of Configuration
37 // --------------------
38  
39  
40 // Helpers
41 #define GLUE(a,b) a##b
42 #define PORT(a) GLUE(PORT,a)
43 #define PIN(a) GLUE(PIN,a)
44 #define DDR(a) GLUE(DDR,a)
45  
46  
47 // Port definitions
48 #define LED_PORT PORT(LED)
49 #define LED_DDR DDR(LED)
50  
51  
52 // Processor definitions
53 #include <avr/io.h>
54  
55  
56 // Define clock values for selected processors
57  
58 // ATtiny13
59 #if _AVR_IOTN13_H_
60 #define F_CPU_DEFAULT 1200000UL // Default clock 9.6MHz/8
61 #define F_CPU_MAX 9600000UL // Maximal internal clock
62 #endif
63  
64 // ATmega8/16/32/64
65 #if _AVR_IOM8_H_ | _AVR_IOM16_H_ | _AVR_IOM32_H_ | _AVR_IOM64_H_ \
66 | _AVR_IOM88_H_
67 #define F_CPU_DEFAULT 1000000UL // Default clock 1MHz
68 #define F_CPU_MAX 8000000UL // Maximal internal clock
69 #endif
70  
71  
72 // Use delay library
73 #include <util/delay.h>
74  
75  
76 // Define ON and OFF times on PERIOD and DUTY
77 #define TIME_ON (1L*PERIOD*DUTY/100)
78 #define TIME_OFF (1L*PERIOD*(100-DUTY)/100)
79  
80  
81 // Main program
82 int main()
83 {
84  
85 LED_DDR |=1<<LED_BIT; // Set LED port as output
86  
87 for(;;) // Never ending story
88 {
89 LED_PORT |= 1<<LED_BIT; // Set LED port to 1
90 _delay_ms(TIME_ON); // Wait
91 LED_PORT &= ~(1<<LED_BIT); // Clear LED port to 0
92 _delay_ms(TIME_OFF); // Wait
93 }
94  
95 return 0;
96 }