/******************************************************************************* * File Name: Audio.c * Function Describe:device for the audio * Relate Module: * Explain: Hardware version is HS120 * Writer: ShiLiangWen * Date: 2015.6.15 *******************************************************************************/ //------------------------------------------------------------------------------ #include "includes.h" int cntMeiTiao; /***************************************************************** *CLK=12Mhz *(10000,2)==> 12000000/2/10000=600Hz ******************************************************************/ void BeepInit(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟 GPIO_InitStructure.GPIO_Pin = BEEP_PIN; //TIM4_CH4 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BEEP_PORT, &GPIO_InitStructure);//初始化GPIO //idBeepTimer=NULL; } void newBeepSet(int fre) { /*此函数能够自适用SYSCLK来生成频率为freHz,占空比为50%的PWM输出 公式: T=(1+TIM_Period)*(1+TIM_Prescaler)/SYSCLK fre=1/T=SYSCLK/(1+TIM_Period)*(1+TIM_Prescaler) TIM_Period=(SYSCLK/fre/(1+TIM_Prescaler))-1 这里SYSCLK,fre是已知的,只要确定TIM_Period和TIM_Prescaler即可 这里我们先确定TIM_Prescaler,只要保证TIM_Prescaler不超界即可 我们直接取TIM_Prescaler=SYSCLK/10000,这样就能保证TIM_Prescaler不超界 TIM_Prescaler定下来了,再根据公式计算,那么TIM_Period也就出来了 */ uint16_t tempPrescale; uint16_t TimerPeriod,ChannelPulse; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; tempPrescale = USER_MAIN_SYSCLK / 1000000 - 1;//确定TIM_Prescaler //10000 TimerPeriod = USER_MAIN_SYSCLK / (fre * tempPrescale);//根据公式再求TIM_Period //newPara.VolumLv 占空比 声音 5-50% 占空比 //ChannelPulse = (uint16_t) (TimerPeriod/2);//50%占空比 ChannelPulse = (uint16_t) (TimerPeriod*(newPara.VolumLv)*1)/100; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_Prescaler = tempPrescale; TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter=0; TIM_TimeBaseStructure.TIM_Period=TimerPeriod-1; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_Pulse=ChannelPulse;//50%占空比 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset; TIM_OCInitStructure.TIM_Pulse=ChannelPulse; TIM_OC4Init(TIM4, &TIM_OCInitStructure); //CH4 TIM_Cmd(TIM4, ENABLE); TIM_CtrlPWMOutputs(TIM4,ENABLE); } void SetBeep(int fre,int tick) { newBeepSet(fre); uTimerCreate(UTIMER_BEEP,tick); uTimerCreate(UTIMER_MIC,tick+8); } /********************************************************************** 1ms在8000~9000之间 ***********************************************************************/ void SetBeepByNoOS(int fre,int ms) { if(newPara.KeySound==0) return; newBeepSet(fre); TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM1 uTimerHardDly(ms); TIM_Cmd(TIM4, DISABLE); //禁止/使能TIM1 } /******************************************************************************** SpeakerInit *********************************************************************************/ void SpeakerInit(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPK_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO SpeakerCtrl(0); } /******************************************************************************** SpeakerCtrl *********************************************************************************/ void SpeakerCtrl(int en) { if(en){ //SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;//Speaker Enable SpeakerEnable(); }else{ //SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;//Speaker Disable SpeakerDisable(); } } void SetRingFreq(int fre) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; unsigned short arr; arr=20000000/fre;//6000000 /*====== 初始化TIM4 ======*/ TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =3; //设置用来作为TIMx时钟频率除数的预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比 TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 //====== 初始化TIM4 Channel2 PWM模式 ======/ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2 TIM_OCInitStructure.TIM_Pulse=arr/2; //设置占空比时 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity_Low 输出极性:TIM输出比较极性低 TIM_OC1Init(TIM5, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2 TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器 TIM_Cmd(TIM5, ENABLE); //禁止/使能TIM4 } void SPKDelayUs(unsigned short us) { unsigned short i; unsigned short t=us; while(t--){ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); } } void SpeakerEnable(void) { unsigned char i; unsigned char time; #if 1 if(sutPocStatus.HeadSetPtt)return ; //耳机FM 外放免提 time=4; for(i=0;iBSRR=SPK_MUTE_PIN; SPKDelayUs(7); SPK_MUTE_PORT->BRR=SPK_MUTE_PIN; SPKDelayUs(7); } #endif SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN; } void SpeakerDisable() { if(Test_EnableSpk==0)SPK_MUTE_PORT->BRR=SPK_MUTE_PIN; } /******************************************************************************** * End of Module *******************************************************************************/