The OP
Published on 2023-8-11 16:33
Only look at the author
This post is from stm32/stm8
Latest reply
The following figure shows the circuit diagram of the connection between the buzzer and STM32. The "PBeep" in the program is the macro definition of the "BEEP" IO port in the figure, which is usually defined in the header file:
#define PBeep PBout(8)
[attach]860506[/attach]
First, find a score online, look carefully, and figure out two arrays, one for pitch and one for note length.
Check the score, as follows:
[attach]860505[/attach]
When it comes to converting the musical notation to an array, you can see the benefit of choosing a nursery rhyme. All the tones are in the tone[] array, that is, between the bass 7 and the treble 5. Let's look at this array again:
// Low 7 1 2 3 4 5 6 7 High 1 High 2 High 3 High 4 High 5 Silent
uc16 tone[] = {247,262,294,330,349,392,440,494,523,587,659,698,784,1000}; // Audio data table
#include "beep.h"
int melody[] = {50, 50, 50, 50, 200, 200, 200, 400, 400, 500, 500, 500};
void BEEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //Enable port A clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Speed 50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //Initialize GPIOD3,6
GPIO_SetBits(GPIOB,GPIO_Pin_5);
}
void Sound(u16 frq)
{
u32 time;
if(frq != 1000)
{
time = 500000/((u32)frq);
PBeep = 1;
delay_us(time);
PBeep = 0;
delay_us(time);
}else
delay_us (1000);
}
void Sound2(u16 time)
{
PBeep = 1;
delay_ms(time);
PBeep = 0;
delay_ms(time);
}
void play_successful(void)
{
int id=0;
for(id = 0;id < 12 ;id++)
{
Sound2(melody[id]);
}
}
void play_failed(void)
{
int id=0;
for(id = 11 ;id >=0 ;id--)
{
Sound2(melody[id]);
}
}
void play_music(void)
{
// low 7 1 2 3 4 5 6 7 High 1 High 2 High 3 High 4 High 5 Silent
uc16 tone[] = {247,262,294,330,349,392,440,294,523,587,659,698,784,1000}; //Audio data table
//Love song in the world
u8 music[] = {5,5,6, 8,7,6,5,6,13,13,//tone
5,5,6,8,7,6,5,3,13,13,
2,2,3,5,3,5,6 ,3,2,1,
6,6,5,6,5,3,6,5,13,13,
5,5,6,8,7,6,5,6,13,13,
5,5,6,8,7,6,5,3,13,13,
2,2,3,5,3, 5,6,3,2,1,
6,6,5,6,5,3,6,1,
//
time 2,4,2,2,2,2,2,8,4 ,
4,
2,4,2,4,2,2,4,2,2,8
,
2,4,2,2,2,2,2,8,4
,4,
2,4,2,2,2,2,2,8,4, 4,
2,4,2,2,2,2,2,8,4, 4,
2,4,2,4,2, 2,4,2,2,8,
2,4,2,2,2,2,2,8,
4, 2,2,2, 4, 2,2,2, 2,2,8,
4, 2,2,2,4,2,2,2,2,2,8,
4, 2,2, 2,4,2,2,5,2,6,2,4,
2,2 ,2,4,2,4,2,2,12};
u32 yanshi;
u16 i,e;
yanshi = 10;
for(i=0;i<sizeof(music)/sizeof(music[0]);i++){
for(e=0;e<((u16)time)*tone[music ]/yanshi;e++){
Sound((u32)tone[music]);
}
}
}
#ifndef __BEEP__H
#define __BEEP__H
#include "sys.h"
#include "stdlib.h"
#include "delay.h"
//Define GPIOB bit address variable macro, bit input macro, output macro
#define PBeep PBout(5)
void BEEP_Init(void);
void Sound(u16 frq);
void Sound2(u16 time);
void play_music(void);
void play_successful(void);
void play_failed(void);
#endif
Details
Published on 2024-11-10 00:53
| ||
|
||
wangerxian
Currently offline
|
2
Published on 2023-8-11 16:54
Only look at the author
This post is from stm32/stm8
Comments | |
|
||
|
3
Published on 2023-8-11 17:00
Only look at the author
This post is from stm32/stm8
| ||
|
||
|
4
Published on 2023-8-11 17:05
Only look at the author
| |
|
|
|
5
Published on 2023-8-11 17:07
Only look at the author
| |
|
|
|
This post is from stm32/stm8
Comments
I don't know if there is a tool that can write code to make sounds, so that you can test the sound corresponding to the code you wrote. I don't think there is a direct conversion tool.
Details
Published on 2023-8-14 09:10
| ||
|
||
|
This post is from stm32/stm8
| ||
|
||
|
8
Published on 2023-8-12 13:17
Only look at the author
This post is from stm32/stm8
| ||
|
||
|
秦天qintian0303
Currently offline
|
9
Published on 2023-8-12 20:48
Only look at the author
This post is from stm32/stm8
| |
Personal signature
在爱好的道路上不断前进,在生活的迷雾中播撒光引 |
||
|
wangerxian
Currently offline
|
10
Published on 2023-8-14 09:10
Only look at the author
This post is from stm32/stm8
| |
|
||
|
11
Published on 2023-8-16 11:44
Only look at the author
| |
|
|
|
lugl4313820
Currently offline
|
12
Published on 2023-8-24 10:03
Only look at the author
|
|
|
|
digital2Fox
Currently offline
|
13
Published on 2024-4-30 14:47
Only look at the author
This post is from stm32/stm8
| |
|
||
|
14
Published on 2024-9-27 11:10
Only look at the author
This post is from stm32/stm8
| ||
|
||
|
15
Published on 2024-9-29 14:29
Only look at the author
This post is from stm32/stm8
| ||
Personal signature
默认摸鱼,再摸鱼。2022、9、28 |
||
|
16
Published on 2024-10-5 12:31
Only look at the author
This post is from stm32/stm8
| ||
|
||
|
17
Published on 2024-11-10 00:53
Only look at the author
This post is from stm32/stm8
| ||
|
||
|
论坛测评队员
EEWorld Datasheet Technical Support
EEWorld
subscription
account
EEWorld
service
account
Automotive
development
circle
About Us Customer Service Contact Information Datasheet Sitemap LatestNews
Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190