123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /*******************************************************************************
- * 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;
- int TTSDelayCnt;
- unsigned char MuteFlag=1;
- /*****************************************************************
- *CLK=12Mhz
- *(10000,2)==> 12000000/2/10000=600Hz
- ******************************************************************/
- 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 / 10000 - 1;//确定TIM_Prescaler
- TimerPeriod = USER_MAIN_SYSCLK / (fre * tempPrescale);//根据公式再求TIM_Period
- ChannelPulse = (uint16_t) (TimerPeriod/2);//50%占空比
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
-
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//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(TIM2, &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_OC2Init(TIM2, &TIM_OCInitStructure); //通道2
- TIM_Cmd(TIM2, ENABLE);
- TIM_CtrlPWMOutputs(TIM2,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(TIM2, ENABLE); //禁止/使能TIM1
- uTimerHardDly(ms);
- TIM_Cmd(TIM2, DISABLE); //禁止/使能TIM1
- }
- /********************************************************************************
- SpeakerInit
- *********************************************************************************/
- void SpeakerInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE );
- GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN|SPK_BOOST_PIN|SPK_MAIN_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
- GPIO_SetBits(SPK_BOOST_PROT,SPK_BOOST_PIN);//默认拉高
- SPK_MAIN_MUTE_LOW;
- 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)
- {
-
- if(MuteFlag==0)return;
- SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;//////
- }
- /********************************************************************************
- * End of Module
- *******************************************************************************/
|