1 | // cpu: ATMega8
|
2 | // speed: 2 mhz
|
3 | // rom: 8192 bytes (0x0000 .. 0x1fff)
|
4 | // ram: 1024 bytes (0x0060 .. 0x045f)
|
5 | // eeprom: 512 bytes (0x0000 .. 0x01ff)
|
6 | // generates Pulsating Beep with 3,7kHz
|
7 |
|
8 | //#include <avr/io.h>
|
9 | #include <avr/interrupt.h>
|
10 | #define RAMSTART 0x0060
|
11 | #define RAMSIZE (RAMEND-RAMSTART+1)
|
12 | #define T_ON 4
|
13 | #define T_OFF 20
|
14 |
|
15 | // Function Prototypes
|
16 | void PORT_INIT(void);
|
17 | void TIMER0_INIT(void);
|
18 | void TIMER1_INIT(void);
|
19 | void TIMER0_START(void);
|
20 | void TIMER1_START(void);
|
21 | void TIMER1_STOP(void);
|
22 |
|
23 | //Global Variable Declaration
|
24 | int Tick10Hz=0; //Used in ISR TIMER0
|
25 |
|
26 | ISR(TIMER0_OVF_vect)
|
27 | {
|
28 | Tick10Hz = 1;
|
29 | TCNT0 = 61;
|
30 | }
|
31 |
|
32 | int main(void)
|
33 | {
|
34 | int icount=0;
|
35 | PORT_INIT();
|
36 | TIMER0_INIT();
|
37 | TIMER1_INIT();
|
38 | sei();
|
39 |
|
40 | TIMER0_START();
|
41 |
|
42 | while (1)
|
43 | {
|
44 | if (Tick10Hz == 1)
|
45 | {
|
46 | icount++;
|
47 | if ((icount > 0) && (icount < T_ON))
|
48 | TIMER1_START();
|
49 | else if (icount == T_ON)
|
50 | TIMER1_STOP();
|
51 | else if (icount == (T_ON + T_OFF))
|
52 | icount=0;
|
53 | Tick10Hz = 0;
|
54 | }
|
55 | }
|
56 | }
|
57 |
|
58 | void PORT_INIT()
|
59 | {
|
60 | //DDRB = 0x02; //PB1=Out
|
61 | DDRB = 0x03; // 0=Out 1=Out 2=In 3=In 4=In 5=In 6=In 7=In
|
62 | PORTB = 0x00; // 0=Low 1=Low 2=Low 3=Low 4=Low 5=Low 6=Low 7=Low
|
63 | }
|
64 |
|
65 | void TIMER0_INIT (void)
|
66 | {
|
67 | TCNT0 = 61;
|
68 | TIMSK |= (1<<TOIE0);
|
69 | }
|
70 |
|
71 | void TIMER0_START (void)
|
72 | {
|
73 | TCCR0 = (1<<CS02)|(1<<CS00);
|
74 | }
|
75 |
|
76 | void TIMER1_INIT()
|
77 | {
|
78 | TCCR1B = (1<<WGM12); //Reset counter @ compare level
|
79 | OCR1AH = 1; //Compare Register High
|
80 | OCR1AL = 244; //Compare Register Low
|
81 | //OCR1AH OCR1AL f out
|
82 | // 0 180 5,5kHz
|
83 | // 1 14 3,7kHz
|
84 | // 1 244 2,0kHz
|
85 | }
|
86 |
|
87 | void TIMER1_START()
|
88 | {
|
89 | TCCR1A = (1<<COM1A0); //No PWM, Toggle OC1(PB1)
|
90 | TCCR1B |= (1<<CS10); //Start Timer1 by setting Prescaler @ clk/1
|
91 | }
|
92 |
|
93 | void TIMER1_STOP()
|
94 | {
|
95 | TCCR1B &= ~((1<<CS10)|(1<<CS11)|(1<<CS12)); //Clear all Prescaler Bits to Stop Timer1
|
96 | TCCR1A &= ~(1<<COM1A0); //Stop Toggling to prevent undefined State of PB1
|
97 | }
|