下面这段代码有个小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;
[page]
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++);
}
//TIM_Cmd(TIM3, ENABLE);
}
void song()
{
// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_TimeBaseStructure.TIM_Period = reload_time;
// reload_time=FREQH[k]*256+FREQL[k];
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
reload_time=get_time;
TIM_TimeBaseStructure.TIM_Period = reload_time;
TIM_Cmd(TIM3, ENABLE);
delay(time);
}
int main (void)
{
uchar i=0;
RCC_Configuration(); //配置时钟
NVIC_Configuration();
GPIO_Configuration(); //配置IO口
TIM3_Configuration();
GPIO_WriteBit(GPIOD, GPIO_Pin_9, (BitAction)1);
GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)1);
delay1(10);
GPIO_WriteBit(GPIOD, GPIO_Pin_9, (BitAction)0);
GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)0);
while(1)
{
time=1;
i=0;
while(i<75)
{
k=sszymmh[i]+7*sszymmh[i+1]-1;
get_time=FREQH[k]*256+FREQL[k];
time = sszymmh [i+ 2]; //读出时间长度数值
i=i+3;
song();
}
}}
void TIM3_IRQHandler (void)
{
// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_Cmd(TIM3, DISABLE);
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)//判断是否为定时器3溢出中断
{
(bool)beep=!beep;
reload_time=get_time;
GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)beep);
GPIO_WriteBit(GPIOD, GPIO_Pin_9, (BitAction)beep);
}
TIM_ClearFlag(TIM3, TIM_FLAG_Update); //清中断标记
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
// TIM_Cmd(TIM3, DISABLE);
}