单片机蜂鸣器实验

单片机:STM32F407

开发板:DMF407电机开发板

平台:keil V5.31

HSE 为8MHZ

HSI为16MHZ

原理图:

复制代码
void beep_init(void)
{
    GPIO_InitTypeDef gpio_init_struct;
    BEEP_GPIO_CLK_ENABLE();                             /* BEEP时钟使能 */

    gpio_init_struct.Pin = BEEP_GPIO_PIN;               /* 蜂鸣器引脚 */
    gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP;        /* 推挽输出 */
    gpio_init_struct.Pull = GPIO_PULLUP;                /* 上拉 */
    gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH;      /* 高速 */
    HAL_GPIO_Init(BEEP_GPIO_PORT, &gpio_init_struct);   /* 初始化蜂鸣器引脚 */

    BEEP(0);                                            /* 关闭蜂鸣器 */
}

播放连续变化的声音:

复制代码
    while(1)
    {
        LED0(0);
        BEEP(0);
        delay_ms(temp);
        LED0(1);
        BEEP(1);
        delay_ms(temp);
	    temp++;
    }

播放简单音乐哆来咪:

以下是常见中音阶频率参考:

  • 哆(C):‌262Hz

  • 来(D):‌294Hz

  • 咪(E):‌330Hz

  • 发(F):‌349Hz

  • 嗦(G):‌392Hz

  • 拉(A):‌440Hz

  • 西(B):‌494Hz

    复制代码
      unsigned short temp=1000000/262;
      unsigned short time=0;
      unsigned short step=0;
      while(1)
      {
      		if(step==0)
      		{
      				if(time<Delay)
      				{
      					LED0(0);
      					BEEP(0);
      					delay_us(temp);
      					LED0(1);
      					BEEP(1);
      					delay_us(temp);
      					time++;
      				}
      				else
      				{
      					time=0;
      					step=1;
      					temp=1000000/294;
      				}
      		}
      		if(step==1)
      		{
      				if(time<Delay)
      				{
      					LED0(0);
      					BEEP(0);
      					delay_us(temp);
      					LED0(1);
      					BEEP(1);
      					delay_us(temp);
      					time++;
      				}
      				else
      				{
      					time=0;
      					step=2;
      					temp=1000000/330;
      				}
      		}
      		if(step==2)
      		{
      				if(time<Delay)
      				{
      					LED0(0);
      					BEEP(0);
      					delay_us(temp);
      					LED0(1);
      					BEEP(1);
      					delay_us(temp);
      					time++;
      				}
      				else
      				{
      					time=0;
      					step=3;
      					temp=1000000/349;
      				}
      		}	
      		if(step==3)
      		{
      				if(time<Delay)
      				{
      					LED0(0);
      					BEEP(0);
      					delay_us(temp);
      					LED0(1);
      					BEEP(1);
      					delay_us(temp);
      					time++;
      				}
      				else
      				{
      					time=0;
      					step=4;
      					temp=1000000/392;
      				}
      		}	
      		if(step==4)
      		{
      				if(time<Delay)
      				{
      					LED0(0);
      					BEEP(0);
      					delay_us(temp);
      					LED0(1);
      					BEEP(1);
      					delay_us(temp);
      					time++;
      				}
      				else
      				{
      					time=0;
      					step=5;
      					temp=1000000/440;
      				}
      		}	
      		if(step==5)
      		{
      				if(time<Delay)
      				{
      					LED0(0);
      					BEEP(0);
      					delay_us(temp);
      					LED0(1);
      					BEEP(1);
      					delay_us(temp);
      					time++;
      				}
      				else
      				{
      					time=0;
      					step=6;
      					temp=1000000/494;
      				}
      		}	
      		if(step==6)
      		{
      				if(time<Delay)
      				{
      					LED0(0);
      					BEEP(0);
      					delay_us(temp);
      					LED0(1);
      					BEEP(1);
      					delay_us(temp);
      					time++;
      				}
      				else
      				{
      					time=0;
      					step=0;
      					temp=1000000/262;
      				}
      		}			
      }

Program Size: Code=6086 RO-data=450 RW-data=28 ZI-data=1388

优化一下:

复制代码
	unsigned short temp=1000000/262;
	unsigned short time=0;
	unsigned short step=0;

    while(1)
    {
			if(step<6)
			{
					if(time<Delay)
					{
						LED0(0);
						BEEP(0);
						delay_us(temp);
						LED0(1);
						BEEP(1);
						delay_us(temp);
						time++;
					}
					else
					{
						time=0;
						if(step==0)
						{
							step=1;
							temp=1000000/294;
						}
						else if(step==1)
						{
							step=2;
							temp=1000000/330;
						}
						if(step==2)
						{
							step=3;
							temp=1000000/349;
						}	
						if(step==3)
						{
							step=4;
							temp=1000000/392;
						}
						if(step==4)
						{
							step=5;
							temp=1000000/440;
						}	
						if(step==5)
						{
							step=6;
							temp=1000000/494;
						}	
						if(step==6)
						{
							step=0;
							temp=1000000/262;
						}						
					}
			}
		}

Program Size: Code=5614 RO-data=450 RW-data=28 ZI-data=1388

继续优化:

复制代码
	unsigned short temp[]={1000000/262,1000000/294,1000000/330,1000000/349,1000000/392,1000000/440,1000000/494};
	unsigned short time=0;
	unsigned short step=0;

    while(1)
    {
			if(step<=6)
			{
					if(time<Delay)
					{
						LED0(0);
						BEEP(0);
						delay_us(temp[step]);
						LED0(1);
						BEEP(1);
						delay_us(temp[step]);
						time++;
					}
					else
					{
						time=0;
						step++;						
					}
			}
			else
			{
				step=0;
			}
		}

Program Size: Code=5568 RO-data=464 RW-data=28 ZI-data=1388

AI优化:

复制代码
/* 宏定义 */
#define NOTE_DURATION_CYCLES    50  // 每个音符播放的周期数
#define NOTES_COUNT             7   // 音符总数
/* 音符频率定义 (中音 C4 到 B4) */
const uint16_t noteFrequencies[NOTES_COUNT] = {
    262,  // C4
    294,  // D4
    330,  // E4
    349,  // F4
    392,  // G4
    440,  // A4
    494   // B4
};
int main(void)
{
    HAL_Init();                                 /* 初始化HAL库 */
    sys_stm32_clock_init(336, 8, 2, 7);         /* 设置时钟,168Mhz */
    delay_init(168);                            /* 延时初始化 */
    led_init();                                 /* 初始化LED */
    beep_init();                                /* 初始化蜂鸣器 */

    /* 变量定义 */
    uint8_t currentNoteIndex = 0;               // 当前播放的音符索引
    uint16_t cyclesPlayed = 0;                  // 当前音符已播放的周期数
    uint16_t halfPeriodUs = 1000000/noteFrequencies[0]; // 当前音符的半周期(微秒)
		
/* 主循环 */
    while (1) {
        /* 播放当前音符的一个完整周期 */
        if (cyclesPlayed < NOTE_DURATION_CYCLES) {
            LED0(0);
            BEEP(0);
            delay_us(halfPeriodUs);
            
            LED0(1);
            BEEP(1);
            delay_us(halfPeriodUs);
            
            cyclesPlayed++;
        } 
        /* 当前音符播放完毕,切换到下一个音符 */
        else {
            cyclesPlayed = 0;
            
            /* 更新到下一个音符(循环播放) */
            currentNoteIndex = (currentNoteIndex + 1) % NOTES_COUNT;
            
            /* 计算新音符的半周期 */
            halfPeriodUs = 1000000 / noteFrequencies[currentNoteIndex];
        }
    }		
}

Program Size: Code=5582 RO-data=462 RW-data=28 ZI-data=1388

相关推荐
俊基科技2 小时前
A-59U工业级多模语音处理模块在矿山矿井通信与呼叫报警系统中的应用
嵌入式硬件·嵌入式开发·硬件开发·ai降噪·回音消除·拾音降噪
周周记笔记3 小时前
GD32F450xx MCU Datasheet 解读:硬件测试工程师的测试方法论
单片机·嵌入式硬件
潘潘的嵌入式日记5 小时前
I²C 从机接收总被覆盖?双缓冲要在 STOP 时交接
c语言·开发语言·单片机
AOI小白新手上路7 小时前
江科协51单片机16-1 AD/DA ADC 三通道综合显示(LCD1602 版)改造说明
单片机·嵌入式硬件·51单片机
HUI-4747 小时前
EG1135|SSOP‑16 同步整流降压控制器,大功率外置 MOS 降压优选
单片机·嵌入式硬件·硬件工程
单片机仿真设计8 小时前
【proteus仿真】基于 STM32 单片机恒温箱控制系统设计(仿真图+程序)
stm32·单片机·嵌入式硬件·proteus·毕设
国科安芯8 小时前
星载CAN总线通信网络中抗辐射MCU的通信可靠性设计分析
网络·人工智能·分布式·单片机·嵌入式硬件·架构
新晨单片机设计10 小时前
S003A-基于STM32单片机温度计(数码管显示)【Proteus仿真+Keil程序+原理图】
stm32·单片机·stm32单片机功能定制
AOI小白新手上路10 小时前
江科协51单片机16-1 DA数模转换实验(PWM 呼吸灯)
单片机·嵌入式硬件·51单片机
hongmai66688810 小时前
告别Wi-Fi,专注Mesh:ESP32-H2-MINI-1U-H2S模组规格与应用
单片机·嵌入式硬件·物联网·5g·h.265