赞
踩
一:功能介绍
1、采用stm32单片机+LCD1602+按键+蜂鸣器+LED,制作一个单片机PWM输出蜂鸣器频率变声器;
2、通过按键改变PWM值,并且将值显示到LCD1602上面;
3、蜂鸣器会随着PWM值的变化,其声音也会产生不同的变化
4、LCD1602显示PWM调节的数值;
二:仿真演示视频+程序简要讲解:(程序有中文注释,新手容易看懂)
123-基于stm32单片机PWM输出蜂鸣器频率变声器Proteus仿真+程序源码+讲解视频
三:设计软件介绍
本设计使用C语言编程设计,程序代码采用keil5编写,程序有中文注释,新手容易看懂,仿真采用Proteus软件进行仿真,演示视频使用的是Proteus8.9版本;资料包里有相关软件包,可自行下载安装。
四:程序打开方法
特别注意:下载资料包以后一定要先解压!!!(建议解压到桌面上,文件路径太深会导致程序打开异常),解压后再用keil5打开。
- 程序部分展示,有中文注释,新手容易看懂
- void RCC_Configration(void)
- {
- ErrorStatus HSEStartUpStatus; /* 定义枚举类型变量 HSEStartUpStatus */
-
-
- /* 复位系统时钟设置*/
- RCC_DeInit();
- /* 开启HSE*/
- RCC_HSEConfig(RCC_HSE_ON);
- /* 等待HSE起振并稳定*/
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- /* 判断HSE起是否振成功,是则进入if()内部 */
- if(HSEStartUpStatus == SUCCESS)
- {
- /* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- /* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* 选择PCLK1时钟源为 HCLK(AHB) 8分频 9M*/
- RCC_PCLK1Config(RCC_HCLK_Div1);
- // /* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为4,则PLL输出频率为 8MHz * 4 = 32MHz */
- // RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_4);
- // /* 使能PLL */
- // RCC_PLLCmd(ENABLE);
- // /* 等待PLL输出稳定 */
- // while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
- /* 选择SYSCLK时钟源为HSE */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
- /* 等待HSE成为SYSCLK时钟源 */
- while(RCC_GetSYSCLKSource() != 0x04);
- }
- }
- void EXTI_Configration()
- {
- EXTI_InitTypeDef EXTI_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
- //调用GPIO_EXTILineConfig函数分配引脚
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource13);
- //调用EXTI_Init函数配置外部中断
- EXTI_InitStructure.EXTI_Line=EXTI_Line0|EXTI_Line13;
- EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd=ENABLE;
- EXTI_Init(&EXTI_InitStructure);
-
- }
- /*配置中断优先级*/
- void NVIC_Configration ( )
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
-
-
- void SoftTimerInit(void)
- {
- bsp_StartAutoTimer(Key_Scan_TMR_ID,10);
- }
- /*****************引脚配置********************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- //LCD1602 管脚
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11| GPIO_Pin_12| GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 |GPIO_Pin_6|GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
-
-
- int main()
- { int num;
- RCC_Configration();
- Key_Configration();
- NVIC_Configration();
- bsp_InitSysTick();
- SoftTimerInit();
- EXTI_Configration();
- TIM2_Configuration(5000);
- USART1_Configration();
- GPIO_Configuration();//初始化
- Init1602();
- WrByte1602(0,1,'F'); //字符显示
- WrByte1602(0,2,'r');
- WrByte1602(0,3,'e');
- WrByte1602(0,4,'=');
- WrByte1602(0,9,'H');
- WrByte1602(0,10,'z');
- while(1)
- {
- WrByte1602(0,5,AsciiCode[1000000/(TIM2->ARR)%10000/1000]);//显示频率
- WrByte1602(0,6,AsciiCode[1000000/(TIM2->ARR)%1000/100]);
- WrByte1602(0,7,AsciiCode[1000000/(TIM2->ARR)%100/10]);
- WrByte1602(0,8,AsciiCode[1000000/(TIM2->ARR)%10]);
- if( bsp_CheckTimer(Key_Scan_TMR_ID) )//识别按键
- {
- Key_Value = Key_Prosse();
- }
- if(gKey.KeyStatu == 1)//频率+
- {
- if(Key_Value == 1)
- {
- Key_Value=0;
-
- if(TIM2->ARR < 10000)
- {
- TIM2->ARR += 500;
- }
- else if(TIM2->ARR >= 10000)
- {
- // TIM2->ARR = 10000;
- printf("超过范围\r\n");
- }
- }
- else if(Key_Value == 2)//频率-
- {
- Key_Value=0;
-
- if(TIM2->ARR > 500)
- {
- TIM2->ARR -= 500;
- }
- else if(TIM2->ARR <= 500)
- {
- // TIM2->ARR = 500;
- printf("超过范围\r\n");
- }
- }
- TIM2->CCR2=(TIM2->ARR)/2;
- }
五:仿真文件(采用Proteus打开)
六:资料清单展示(文件中包含的相关资料)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。