123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- /*******************************************************************************
- * File Name: Audio.c
- * Function Describe:device for the audio
- * Relate Module:
- * Explain: Hardware version is HS120
- * Writer: ShiLiangWen
- * Date: 2015.6.15
- *******************************************************************************/
- #define THIS_FILE_ID 7
- //------------------------------------------------------------------------------
- #include "stm32f10x.h"
- #include <includes.h>
- int cntMeiTiao;
- unsigned char Earcheck=0;
- OS_ID idBeepTimer;
- OS_ID idRingTimer;
- /*****************************************************************
- *CLK=12Mhz
- *(10000,2)==> 12000000/2/10000=600Hz
- ******************************************************************/
- void BeepInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | 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);
- idBeepTimer=NULL;
- }
- void newBeepSet(int fre)
- {
- int cntMeiTiao;
- uint16_t TimerPeriod,ChannelPulse = 0;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- cntMeiTiao=SystemCoreClock/(SystemCoreClock/10000);
- TimerPeriod = cntMeiTiao/fre;
-
- // printf("cntMeiTiao==%d,TimerPeriod=%d\r\n",cntMeiTiao,TimerPeriod);
- //sutProductPara.VolLv
- ChannelPulse = sutProductPara.VolLv;//(uint16_t) (TimerPeriod*(sutProductPara.VolLv)*1)/100;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
- TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock/10000-1; //48000
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period=TimerPeriod;
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
- TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
- 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(TIM5, &TIM_OCInitStructure);
- TIM_Cmd(TIM5, ENABLE);
- TIM_CtrlPWMOutputs(TIM5,ENABLE);
- }
- void SetBeep(int fre,int tick)
- {
- newBeepSet(fre);
- idBeepTimer=os_tmr_create(tick,TMR_INF_BEEP);
- //idBeepTimer=os_tmr_create(tick+8,TMR_INF_MIC);//添加一个延时
- }
- void SetBeepByNoOS(int fre,int ms)
- {
- newBeepSet(fre);
- TIM_Cmd(TIM5, ENABLE); //禁止/使能TIM4
- DelayMs(ms);
- TIM_Cmd(TIM5, DISABLE); //禁止/使能TIM4
- }
- /********************************************************************************
- MicCtrlInit
- *********************************************************************************/
- void MicrophoneInit(void)
- {
- }
- /********************************************************************************
- MicCtrl
- *********************************************************************************/
- void MicrophoneCtrl(int en)
- {
- if(en){
- }else{
- }
- }
- void SpeakerEnable()
- {
- SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
- }
- void SpeakerDisable()
- {
- SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
- }
- /********************************************************************************
- SpeakerInit
- *********************************************************************************/
- void SpeakerInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN;//|SPK_EX_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
- }
- const SUT_RING_ELEMENT acusRingStart[]={
- //频率,时间
- {800,100},
- {0,100},
- {900,100},
- {900,150},
- {0,200},
- {500,300},
- {0,0}//必须以{x,0}作为结束
- };
- SUT_RING sutRing={0,0,0,0};
- void RingStart(SUT_RING_ELEMENT *pRingElement)
- {
- unsigned short freq,time,num;
- num=0;
- while(1){
- time=pRingElement[num].time;
- if(time==0)break;
- if(++num>10000)return;//不能太长,太长判断为非法而不处理
- }
- sutRing.pElement=pRingElement;
- sutRing.start=1;
- sutRing.handle=0;
- freq=sutRing.pElement->freq;
- time=sutRing.pElement->time;
- SetRingFreq(freq);
- idRingTimer=os_tmr_create(time,TMR_INF_RING);
- SpeakerDisable();
- }
- void Ear_check(char Update)
- {
- static signed char LastEarStatue=-1;
- if(GPIO_ReadInputDataBit(EAR_CHEK_PORT,EAR_CHEK_PIN)==SET)
- {
- Earcheck=1;
- }else {
- Earcheck=0;
- }
- if(LastEarStatue!=Earcheck||Update){
-
-
- if(modemInitReady>1){
- LastEarStatue=Earcheck;
- if(Earcheck==0)ModemSendAT("AT+POC=608&2\r\n");
- else ModemSendAT("AT+POC=608&1\r\n");
- }
- }
- }
- /********************************************************************************
- * End of Module
- *******************************************************************************/
|