Audio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*******************************************************************************
  2. * File Name: Audio.c
  3. * Function Describe:device for the audio
  4. * Relate Module:
  5. * Explain: Hardware version is HS120
  6. * Writer: ShiLiangWen
  7. * Date: 2015.6.15
  8. *******************************************************************************/
  9. //------------------------------------------------------------------------------
  10. #include "includes.h"
  11. int cntMeiTiao;
  12. /*****************************************************************
  13. *CLK=12Mhz
  14. *(10000,2)==> 12000000/2/10000=600Hz
  15. ******************************************************************/
  16. void BeepInit(void)
  17. {
  18. GPIO_InitTypeDef GPIO_InitStructure;
  19. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
  20. GPIO_InitStructure.GPIO_Pin = BEEP_PIN; //TIM4_CH4
  21. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  22. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  23. GPIO_Init(BEEP_PORT, &GPIO_InitStructure);//初始化GPIO
  24. //idBeepTimer=NULL;
  25. }
  26. void newBeepSet(int fre)
  27. {
  28. /*此函数能够自适用SYSCLK来生成频率为freHz,占空比为50%的PWM输出
  29. 公式:
  30. T=(1+TIM_Period)*(1+TIM_Prescaler)/SYSCLK
  31. fre=1/T=SYSCLK/(1+TIM_Period)*(1+TIM_Prescaler)
  32. TIM_Period=(SYSCLK/fre/(1+TIM_Prescaler))-1
  33. 这里SYSCLK,fre是已知的,只要确定TIM_Period和TIM_Prescaler即可
  34. 这里我们先确定TIM_Prescaler,只要保证TIM_Prescaler不超界即可
  35. 我们直接取TIM_Prescaler=SYSCLK/10000,这样就能保证TIM_Prescaler不超界
  36. TIM_Prescaler定下来了,再根据公式计算,那么TIM_Period也就出来了
  37. */
  38. uint16_t tempPrescale;
  39. uint16_t TimerPeriod,ChannelPulse;
  40. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  41. TIM_OCInitTypeDef TIM_OCInitStructure;
  42. tempPrescale = USER_MAIN_SYSCLK / 1000000 - 1;//确定TIM_Prescaler //10000
  43. TimerPeriod = USER_MAIN_SYSCLK / (fre * tempPrescale);//根据公式再求TIM_Period
  44. //newPara.VolumLv 占空比 声音 5-50% 占空比
  45. //ChannelPulse = (uint16_t) (TimerPeriod/2);//50%占空比
  46. ChannelPulse = (uint16_t) (TimerPeriod*(newPara.VolumLv)*1)/100;
  47. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  48. TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
  49. TIM_TimeBaseStructure.TIM_Prescaler = tempPrescale;
  50. TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
  51. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
  52. TIM_TimeBaseStructure.TIM_Period=TimerPeriod-1;
  53. TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  54. TIM_OCInitStructure.TIM_Pulse=ChannelPulse;//50%占空比
  55. TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
  56. TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
  57. TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
  58. TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
  59. TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
  60. TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
  61. TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
  62. TIM_OCInitStructure.TIM_Pulse=ChannelPulse;
  63. TIM_OC4Init(TIM4, &TIM_OCInitStructure); //CH4
  64. TIM_Cmd(TIM4, ENABLE);
  65. TIM_CtrlPWMOutputs(TIM4,ENABLE);
  66. }
  67. void SetBeep(int fre,int tick)
  68. {
  69. // return;
  70. // if(newPara.KeySound==0) {
  71. // SpeakerDisable();
  72. // return;
  73. // }
  74. newBeepSet(fre);
  75. uTimerCreate(UTIMER_BEEP,tick);
  76. uTimerCreate(UTIMER_MIC,tick+8);
  77. }
  78. /**********************************************************************
  79. 1ms在8000~9000之间
  80. ***********************************************************************/
  81. void SetBeepByNoOS(int fre,int ms)
  82. {
  83. if(newPara.KeySound==0) return;
  84. newBeepSet(fre);
  85. TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM1
  86. uTimerHardDly(ms);
  87. TIM_Cmd(TIM4, DISABLE); //禁止/使能TIM1
  88. }
  89. /********************************************************************************
  90. SpeakerInit
  91. *********************************************************************************/
  92. void SpeakerInit(void)
  93. {
  94. GPIO_InitTypeDef GPIO_InitStructure;
  95. GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN;
  96. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
  97. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  98. GPIO_Init(SPK_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
  99. SpeakerCtrl(0);
  100. }
  101. /********************************************************************************
  102. SpeakerCtrl
  103. *********************************************************************************/
  104. void SpeakerCtrl(int en)
  105. {
  106. if(en){
  107. //SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;//Speaker Enable
  108. SpeakerEnable();
  109. }else{
  110. //SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;//Speaker Disable
  111. SpeakerDisable();
  112. }
  113. }
  114. void SetRingFreq(int fre)
  115. {
  116. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  117. TIM_OCInitTypeDef TIM_OCInitStructure;
  118. unsigned short arr;
  119. arr=20000000/fre;//6000000
  120. /*====== 初始化TIM4 ======*/
  121. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  122. TIM_TimeBaseStructure.TIM_Prescaler =3; //设置用来作为TIMx时钟频率除数的预分频值
  123. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  124. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  125. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
  126. TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  127. //====== 初始化TIM4 Channel2 PWM模式 ======/
  128. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
  129. TIM_OCInitStructure.TIM_Pulse=arr/2; //设置占空比时
  130. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  131. //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
  132. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity_Low 输出极性:TIM输出比较极性低
  133. TIM_OC1Init(TIM5, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
  134. TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
  135. TIM_Cmd(TIM5, ENABLE); //禁止/使能TIM4
  136. }
  137. void SPKDelayUs(unsigned short us)
  138. {
  139. unsigned short i;
  140. unsigned short t=us;
  141. while(t--){
  142. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  143. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  144. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  145. }
  146. }
  147. void SpeakerEnable(void)
  148. {
  149. unsigned char i;
  150. unsigned char time;
  151. #if 1
  152. time=4;
  153. if(sutPocStatus.HeadSetPtt)return;
  154. for(i=0;i<time;i++){
  155. SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
  156. SPKDelayUs(7);
  157. SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;
  158. SPKDelayUs(7);
  159. }
  160. #endif
  161. SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
  162. }
  163. void SpeakerDisable()
  164. {
  165. if(Test_EnableSpk==0)SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;
  166. }
  167. /********************************************************************************
  168. * End of Module
  169. *******************************************************************************/