微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32单片机TIM3蜂鸣器播放音乐

STM32单片机TIM3蜂鸣器播放音乐

时间:11-25 来源:互联网 点击:
下面这段代码有个小bug,软件仿真PORTB.5 beep 波形正常,如下图所示


但下到我的开发板上,beep没有声音,若看到此程序,有兴趣的朋友可以帮忙验证一下,欢迎一起探讨!

#include // STM32F10x Library Definitions

#include "STM32_Init.h" // STM32 Initialization

#include

#define uchar unsigned char

bool beep;

uchar k;

long reload_time=0,time=0;

long get_time;

uchar sszymmh[]={

6,2,3,5,2,1,3,2,2,5,2,2,1,3,2,6,2,1,5,2,1,

6,2,4,3,2,2,5,2,1,6,2,1,5,2,2,3,2,2,1,2,1,

6,1,1,5,2,1,3,2,1,2,2,4,2,2,3,3,2,1,5,2,2,

5,2,1,6,2,1,3,2,2,2,2,2,1,2,4,5,2,3,3,2,1,

2,2,1,1,2,1,6,1,1,1,2,1,5,1,6,0,0,0 };

// 音阶频率表 高八位

uchar FREQH[]={0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,

0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i

0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,

0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,} ;

// 音阶频率表 低八位

uchar FREQL[]={0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,

0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i

0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,

0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,};

void RCC_Configuration(void)

{

ErrorStatus HSEStartUpStatus;

//使能外部晶振

RCC_HSEConfig(RCC_HSE_ON);

//等待外部晶振稳定

HSEStartUpStatus = RCC_WaitForHSEStartUp();

//如果外部晶振启动成功,则进行下一步操作

if(HSEStartUpStatus==SUCCESS)

{

//设置HCLK(AHB时钟)=SYSCLK

RCC_HCLKConfig(RCC_SYSCLK_Div1);

//PCLK1(APB1) = HCLK/2

RCC_PCLK1Config(RCC_HCLK_Div2);

//PCLK2(APB2) = HCLK

RCC_PCLK2Config(RCC_HCLK_Div1);

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_4);//4倍频

//启动PLL

RCC_PLLCmd(ENABLE);

//等待PLL稳定

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

//系统时钟SYSCLK来自PLL输出

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

//切换时钟后等待系统时钟稳定

while(RCC_GetSYSCLKSource()!=0x08);

}

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOB, ENABLE); //给GPIOD提供时钟

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //定时器3提供时钟

}

void TIM3_Configuration(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Prescaler = 2; //2分频 22.1184M

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数模式

TIM_TimeBaseStructure.TIM_Period = reload_time; //自动装载

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

TIM_Cmd(TIM3, ENABLE);

}

void NVIC_Configuration(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);

#else

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

#endif

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //抢占式优先级别, 无抢占优先级

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

}

void delay1(uchar i)

{

long j;

for(;i>0;i--)

{

for(j=0;j<6000;j++);

}

}

void delay(uchar i)

{

unsigned long j;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_Cmd(TIM3, DISABLE);

for(;i>0;i--)

{

for(j=0;j<60000;j++);

}

//T

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top