please show elegoo arduino board setup using a passive buzzer. heres the code below:
#define
F_CPU
16000000
UL
// CPU Frequency
#include
<avr/io.h>
#include
<util/delay.h>
volatile
uint8_t
notePlaying =
0
;
void
setup
()
{
DDRB |=
(
1
<< PB6
)
;
// Set PB6 as output
TCCR1A =
0
;
// Clear Timer1 registers
TCCR1B =
0
;
TIMSK1 =
0
;
OCR1A =
0
;
}
void
playNote
(
uint16_t
frequency)
{
if
(
notePlaying
)
{
TCCR1B =
0
;
// Stop the timer
notePlaying =
0
;
}
else
{
// Calculate the prescaler value
uint16_t
prescaler =
1
;
while
((
F_CPU /
(
prescaler * frequency *
2
))
>
65535
)
{
prescaler <<=
1
;
}
// Configure Timer1
TCCR1B =
(
1
<< WGM12
)
|
(
prescaler & 0x
07
)
;
// Mode 4, prescaler
OCR1A =
(
F_CPU /
(
prescaler * frequency *
2
))
-
1
;
// Output compare value
TIMSK1 =
(
1
<< OCIE1A
)
;
// Enable compare match interrupt
notePlaying =
1
;
// Start the timer
TCNT1 =
0
;
TCCR1B |=
(
1
<< CS10
)
|
((
prescaler >>
1
)
& 0x
03
)
;
// Start timer
}
}
ISR
(
TIMER1_COMPA_vect
)
{
PORTB ^=
(
1
<< PB6
)
;
// Toggle PB6 state
}
void
loop
()
{
if
(
Serial
.
available
()
>
0
)
{
char
key =
Serial
.
read
()
;
switch
(
key
)
{
case
'A':
playNote
(
440
)
;
break
;
case
'B':
playNote
(
494
)
;
break
;
case
'C':
playNote
(
523
)
;
break
;
case
'D':
playNote
(
587
)
;
break
;
case
'E':
playNote
(
659
)
;
break
;
case
'F':
playNote
(
698
)
;
break
;
case
'G':
playNote
(
784
)
;
break
;
case
'q':
TCCR1B =
0
;
// Stop the timer
notePlaying =
0
;
break
;
}
}
}
#define
F_CPU
16000000
UL
// CPU Frequency
#include
<avr/io.h>
#include
<util/delay.h>
volatile
uint8_t
notePlaying =
0
;
void
setup
()
{
DDRB |=
(
1
<< PB6
)
;
// Set PB6 as output
TCCR1A =
0
;
// Clear Timer1 registers
TCCR1B =
0
;
TIMSK1 =
0
;
OCR1A =
0
;
}
void
playNote
(
uint16_t
frequency)
{
if
(
notePlaying
)
{
TCCR1B =
0
;
// Stop the timer
notePlaying =
0
;
}
else
{
// Calculate the prescaler value
uint16_t
prescaler =
1
;
while
((
F_CPU /
(
prescaler * frequency *
2
))
>
65535
)
{
prescaler <<=
1
;
}
// Configure Timer1
TCCR1B =
(
1
<< WGM12
)
|
(
prescaler & 0x
07
)
;
// Mode 4, prescaler
OCR1A =
(
F_CPU /
(
prescaler * frequency *
2
))
-
1
;
// Output compare value
TIMSK1 =
(
1
<< OCIE1A
)
;
// Enable compare match interrupt
notePlaying =
1
;
// Start the timer
TCNT1 =
0
;
TCCR1B |=
(
1
<< CS10
)
|
((
prescaler >>
1
)
& 0x
03
)
;
// Start timer
}
}
ISR
(
TIMER1_COMPA_vect
)
{
PORTB ^=
(
1
<< PB6
)
;
// Toggle PB6 state
}
void
loop
()
{
if
(
Serial
.
available
()
>
0
)
{
char
key =
Serial
.
read
()
;
switch
(
key
)
{
case
'A':
playNote
(
440
)
;
break
;
case
'B':
playNote
(
494
)
;
break
;
case
'C':
playNote
(
523
)
;
break
;
case
'D':
playNote
(
587
)
;
break
;
case
'E':
playNote
(
659
)
;
break
;
case
'F':
playNote
(
698
)
;
break
;
case
'G':
playNote
(
784
)
;
break
;
case
'q':
TCCR1B =
0
;
// Stop the timer
notePlaying =
0
;
break
;
}
}
}
#define
F_CPU
16000000
UL
// CPU Frequency
#include
<avr/io.h>
#include
<util/delay.h>
volatile
uint8_t
notePlaying =
0
;
void
setup
()
{
DDRB |=
(
1
<< PB6
)
.