用最新库玩转智林STM32开发板(之七)------让开发板发出SOUND 听说库又升级了哈,我的版本以是最新版,所以题目没改,我觉得ST这么升级下去对编程者没太大好处,大家都象智林老大,不用库算了. 唉,还得言归正传 智林板的喇叭接的是第TIM3的第3道,所以得编相应的程序. #include <stm32f10x_lib.h> // STM32F10x Library Definitions
const unsigned int MusicData[7] = {30592,27257,24297,22922,20408,18182,16194}; void DelayNuS(unsigned int i) { unsigned char t = 0; for(;i>0;i--) { for(t=0;t<2;t++) { } } } void DelayNmS(unsigned int i) { for(;i>0;i--) { DelayNuS(1000); } }
void SetBellF(unsigned int SetV) { TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIM_TimeBaseInitStructure.TIM_Period = SetV; TIM_TimeBaseInitStructure.TIM_Prescaler = 0; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = (SetV/2); TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC3Init(TIM3, &TIM_OCInitStructure); }
void Timer3Init(void) { TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIM_TimeBaseInitStructure.TIM_Period = 10000; TIM_TimeBaseInitStructure.TIM_Prescaler = 0; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 5000; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC3Init(TIM3, &TIM_OCInitStructure); } void Timer3OutEnable(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); } void Timer3OutDisable(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOB, &GPIO_InitStructure); } void Timer3Start(void) { TIM_Cmd(TIM3, ENABLE); } void Timer3Stop(void) { TIM_Cmd(TIM3, DISABLE); } void Timer3Test(void) { Timer3Init(); Timer3OutEnable(); Timer3Start(); while(1) { Timer3OutEnable(); SetBellF(MusicData[0]); DelayNmS(400); SetBellF(MusicData[0]); DelayNmS(400); SetBellF(MusicData[4]); DelayNmS(400); SetBellF(MusicData[4]); DelayNmS(400); SetBellF(MusicData[5]); DelayNmS(400); SetBellF(MusicData[5]); DelayNmS(400); SetBellF(MusicData[4]); DelayNmS(400); DelayNmS(400); SetBellF(MusicData[3]); DelayNmS(400); SetBellF(MusicData[3]); DelayNmS(400); SetBellF(MusicData[2]); DelayNmS(400); SetBellF(MusicData[2]); DelayNmS(400); SetBellF(MusicData[1]); DelayNmS(400); SetBellF(MusicData[1]); DelayNmS(400); SetBellF(MusicData[0]); DelayNmS(400); DelayNmS(400); Timer3OutDisable(); DelayNmS(1000); } }
相关链接:https://bbs.21ic.com/upfiles/img/20094/20094148116477.rar |