-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton_buzzer.c
83 lines (64 loc) · 1.32 KB
/
button_buzzer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdbool.h>
#include <stdint.h>
#include <avr/io.h>
#define F_CPU 1000000UL // 1 MHz
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
bool rising_edge()
{
//PA0 is connected with button
static bool button_is_on = 0;
if (((PINA & (1 << PA0)) == 0) && (!button_is_on))
{
button_is_on = 1;
return 1;
}
else if ((button_is_on == 1) && (PINA & (1 << PA0) == 1))
{
button_is_on = 0;
return 0;
}
else
{
return 0;
}
}
ISR(TIMER1_OVF_vect)
{
static uint8_t frequency_const[] = {8, 8, 8, 11, 6, 8, 11, 6, 8, 6, 6, 6, 5, 6, 9, 11, 6, 8};
static uint8_t fq_len = 18;
static uint8_t counter = 0;
if(rising_edge())
{
OCR0 = frequency_const[counter % fq_len] - 5;
counter++;
}
}
void setup_io()
{
//PA0 - input, PullUp
DDRA &= ~(1 << PA0);
PORTA |= (1 << PA0);
//PB3 - output
DDRB |= (1 << PB3);
PORTB |= (1 << PB3);
}
void setup_timers()
{
TCCR0 = (1 << CS00) | (0 << CS01) | (1 << CS02) | (0 << WGM00) | (1 << WGM01) | (1 << COM00) | (0 << COM01);
TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0) | (0 << WGM11) | (0 << WGM10);
TCCR1B = (0 << WGM13) | (0 << WGM12) | (0 << CS12) | (0 << CS11) | (1 << CS10);
TIMSK |= (1 << TOIE1);
}
int main(void)
{
setup_io();
setup_timers();
sei();
sleep_enable();
while (true)
{
sleep_cpu();
}
}