使用stm32f103实现蜂鸣器播放歌曲天外来物

时间: 2023-07-20 09:12:24 浏览: 64
使用STM32F103实现蜂鸣器播放歌曲《天外来物》的代码如下: ```c #include "stm32f10x.h" // 定义蜂鸣器引脚 #define BUZZER_PIN GPIO_Pin_0 #define BUZZER_PORT GPIOA // 定义音符频率 #define C4 262 #define D4 294 #define E4 330 #define F4 349 #define G4 392 #define A4 440 #define B4 494 // 定义每个音符的持续时间 #define QUA 500 #define HAF 1000 #define EIG 125 // 定义歌曲 int song[] = {E4, F4, G4, A4, G4, F4, E4, E4, F4, G4, A4, G4, F4, E4, E4, F4, G4, A4, B4, A4, G4, F4, E4, E4, E4, F4, G4, A4, G4, F4, E4, E4, F4, G4, A4, G4, F4, E4, E4, F4, G4, A4, B4, A4, G4, F4, E4}; // 定义歌曲的时值 int duration[] = {QUA, QUA, QUA, HAF, QUA, QUA, HAF, QUA, QUA, QUA, HAF, QUA, QUA, HAF, QUA, QUA, QUA, HAF, QUA, QUA, HAF, QUA, QUA, QUA, QUA, QUA, QUA, QUA, HAF, QUA, QUA, HAF, QUA, QUA, QUA, HAF, QUA, QUA, HAF, QUA, QUA, QUA, HAF, QUA, QUA, HAF, QUA, QUA, QUA, QUA, QUA, QUA, QUA, HAF}; // 播放音符 void play_note(int note, int duration) { int i, t; t = 1000000 / note; for (i = 0; i < duration * 1000 / (t * 2); i++) { GPIO_SetBits(BUZZER_PORT, BUZZER_PIN); Delay_us(t); GPIO_ResetBits(BUZZER_PORT, BUZZER_PIN); Delay_us(t); } } // 延时函数 void Delay_us(u32 nCount) { u32 i; for (i = 0; i < nCount * 8; i++); } int main(void) { int i; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = BUZZER_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BUZZER_PORT, &GPIO_InitStructure); while (1) { for (i = 0; i < sizeof(song) / sizeof(song[0]); i++) { play_note(song[i], duration[i]); Delay_us(EIG * 1000); } } } ``` 代码中将歌曲《天外来物》的音符与时值分别存储在数组song和duration中,通过play_note函数实现了蜂鸣器的播放功能,并通过Delay_us函数实现了不同音符之间的时间间隔。需要注意的是,蜂鸣器的引脚需要通过GPIO_Init函数进行初始化,才能使蜂鸣器正常工作。

相关推荐

最新推荐

recommend-type

启明欣欣stm32f103rct6开发板原理图

启明欣欣stm32f103rct6开发板原理图 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈...
recommend-type

stm32f103数据手册

完全免积分!!!!! 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈...
recommend-type

STM32F103单片机系统时钟部分归纳

——时钟控制(RCC) 三种不同的时钟源可用作系统时钟(SYSCLOCK): HIS振荡器时钟(由芯片内部RC振荡器提供) HSE振荡器时钟(由芯片外部晶体振荡器提供) ... LSE外部32.768kHz低速外部输入时钟,用于驱动RTC
recommend-type

STM32F103RET6TR中文数据手册.pdf

STM32F103RET6TR中文数据手册,同样适用于STM32F103RE 、 STM32F103ZE 、 STM32F103VE、STM32F103RD、 STM32F103VD、 STM32F103ZD、STM32F103RC、 STM32F103VC、 STM32F103ZC
recommend-type

嵌入式实验报告 stm32f103 跑马灯实验 GPIO口操作

熟悉编译环境,尝试独立进行工程的创建、编译、下载。通过阅读 STM32 芯片手册了解关于 GPIO 的相关内容,并且通过编程实现 LED 流水灯控制。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。