123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- /*******************************************************************************
- * File Name: Audio.c
- * Function Describe:device for the Keys
- * Relate Module:
- * Explain: Hardware version is HS120
- * Writer: ShiLiangWen
- * Date: 2015.6.15
- *******************************************************************************/
- #define THIS_FILE_ID 7
- //------------------------------------------------------------------------------
- #include "stm32f10x.h"
- #include <includes.h>
- OS_ID idBeepTimer;
- /*****************************************************************
- *CLK=12Mhz
- *(10000,2)==> 12000000/2/10000=600Hz
- ******************************************************************/
- void BeepInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器4时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
- //设置该引脚为复用输出功能,输出TIM4 CH2的PWM脉冲波形 GPIOB.7
- GPIO_InitStructure.GPIO_Pin = BEEP_PIN; //TIM_CH2
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(BEEP_PORT, &GPIO_InitStructure);//初始化GPIO
- RunMake(THIS_FILE_ID);
- /*====== 初始化TIM4 ======*/
- TIM_TimeBaseStructure.TIM_Period = 10000; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
- 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(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
- //====== 初始化TIM4 Channel2 PWM模式 ======/
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
- TIM_OCInitStructure.TIM_Pulse=5000; //设置占空比时
- 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_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
- TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
- // TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM4
- RunMake(THIS_FILE_ID);
- idBeepTimer=NULL;
- }
- void SetBeep(int fre,int tick)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- unsigned short arr;
- arr=6000000/fre;
- /*====== 初始化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(TIM4, &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_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
- TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
- #if 0
- //SpeakerEnable();
- TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM4
- os_dly_wait(tick);
- TIM_Cmd(TIM4, DISABLE); //禁止/使能TIM4
- //SpeakerDisable();
-
- #else
-
- //SpeakerEnable();
- //RunMake(THIS_FILE_ID);
- TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM4
- idBeepTimer=os_tmr_create(tick,TMR_INF_BEEP);
- //RunMake(THIS_FILE_ID);
- //SlwTrace(INF,"os_tmr_create\r\n");
- //printf("[Beep%ul]\r\n",os_time_get());
- #endif
- }
- /**********************************************************************
- 1ms在8000~9000之间
- ***********************************************************************/
- void DelayMs(unsigned short ms)
- {
- unsigned short i;
- unsigned short t=ms;
- RunMake(THIS_FILE_ID);
- IWDG_ReloadCounter();
- while(t--){
- for(i=0;i<6000;i++);
- IWDG_ReloadCounter();
- }
- RunMake(THIS_FILE_ID);
- }
- void SetBeepByNoOS(int fre,int ms)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- unsigned short arr;
-
- RunMake(THIS_FILE_ID);
- arr=6000000/fre;
- /*====== 初始化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(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
-
- RunMake(THIS_FILE_ID);
-
- //====== 初始化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_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
- TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
- RunMake(THIS_FILE_ID);
-
- TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM4
- DelayMs(ms);
- TIM_Cmd(TIM4, DISABLE); //禁止/使能TIM4
- }
- /********************************************************************************
- MicCtrlInit
- *********************************************************************************/
- void MicrophoneInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = MIC_MUTE_PIN; //TIM_CH2
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(MIC_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
- MicrophoneCtrl(1);
- RunMake(THIS_FILE_ID);
- }
- /********************************************************************************
- MicCtrl
- *********************************************************************************/
- void MicrophoneCtrl(int en)
- {
- if(en){
- MIC_MUTE_PORT->BRR=MIC_MUTE_PIN;//Set Pin Low ,MIC Enalbe
- }else{
- MIC_MUTE_PORT->BSRR=MIC_MUTE_PIN;//Set Pin High ,MIC MUTE
-
- }
- }
- /********************************************************************************
- SpeakerInit
- *********************************************************************************/
- void SpeakerInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- 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();
- }
- }
- /********************************************************************************
- * End of Module
- *******************************************************************************/
|