单片机蜂鸣器实验
单片机STM32F407开发板DMF407电机开发板平台keil V5.31HSE 为8MHZHSI为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; }播放简单音乐哆来咪以下是常见中音阶频率参考哆C262Hz来D294Hz咪E330Hz发F349Hz嗦G392Hz拉A440Hz西B494Hzunsigned short temp1000000/262; unsigned short time0; unsigned short step0; while(1) { if(step0) { if(timeDelay) { LED0(0); BEEP(0); delay_us(temp); LED0(1); BEEP(1); delay_us(temp); time; } else { time0; step1; temp1000000/294; } } if(step1) { if(timeDelay) { LED0(0); BEEP(0); delay_us(temp); LED0(1); BEEP(1); delay_us(temp); time; } else { time0; step2; temp1000000/330; } } if(step2) { if(timeDelay) { LED0(0); BEEP(0); delay_us(temp); LED0(1); BEEP(1); delay_us(temp); time; } else { time0; step3; temp1000000/349; } } if(step3) { if(timeDelay) { LED0(0); BEEP(0); delay_us(temp); LED0(1); BEEP(1); delay_us(temp); time; } else { time0; step4; temp1000000/392; } } if(step4) { if(timeDelay) { LED0(0); BEEP(0); delay_us(temp); LED0(1); BEEP(1); delay_us(temp); time; } else { time0; step5; temp1000000/440; } } if(step5) { if(timeDelay) { LED0(0); BEEP(0); delay_us(temp); LED0(1); BEEP(1); delay_us(temp); time; } else { time0; step6; temp1000000/494; } } if(step6) { if(timeDelay) { LED0(0); BEEP(0); delay_us(temp); LED0(1); BEEP(1); delay_us(temp); time; } else { time0; step0; temp1000000/262; } } }Program Size: Code6086 RO-data450 RW-data28 ZI-data1388优化一下unsigned short temp1000000/262; unsigned short time0; unsigned short step0; while(1) { if(step6) { if(timeDelay) { LED0(0); BEEP(0); delay_us(temp); LED0(1); BEEP(1); delay_us(temp); time; } else { time0; if(step0) { step1; temp1000000/294; } else if(step1) { step2; temp1000000/330; } if(step2) { step3; temp1000000/349; } if(step3) { step4; temp1000000/392; } if(step4) { step5; temp1000000/440; } if(step5) { step6; temp1000000/494; } if(step6) { step0; temp1000000/262; } } } }Program Size: Code5614 RO-data450 RW-data28 ZI-data1388继续优化unsigned short temp[]{1000000/262,1000000/294,1000000/330,1000000/349,1000000/392,1000000/440,1000000/494}; unsigned short time0; unsigned short step0; while(1) { if(step6) { if(timeDelay) { LED0(0); BEEP(0); delay_us(temp[step]); LED0(1); BEEP(1); delay_us(temp[step]); time; } else { time0; step; } } else { step0; } }Program Size: Code5568 RO-data464 RW-data28 ZI-data1388AI优化/* 宏定义 */ #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: Code5582 RO-data462 RW-data28 ZI-data1388