Audio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_GPIOB | 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. newBeepSet(fre);
  70. uTimerCreate(UTIMER_BEEP,tick);
  71. uTimerCreate(UTIMER_MIC,tick+8);
  72. }
  73. /**********************************************************************
  74. 1ms在8000~9000之间
  75. ***********************************************************************/
  76. void SetBeepByNoOS(int fre,int ms)
  77. {
  78. if(newPara.KeySound==0) return;
  79. newBeepSet(fre);
  80. TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM1
  81. uTimerHardDly(ms);
  82. TIM_Cmd(TIM4, DISABLE); //禁止/使能TIM1
  83. }
  84. /********************************************************************************
  85. SpeakerInit
  86. *********************************************************************************/
  87. void SpeakerInit(void)
  88. {
  89. GPIO_InitTypeDef GPIO_InitStructure;
  90. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
  91. GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN;
  92. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
  93. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  94. GPIO_Init(SPK_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
  95. SpeakerCtrl(0);
  96. }
  97. /********************************************************************************
  98. SpeakerCtrl
  99. *********************************************************************************/
  100. void SpeakerCtrl(int en)
  101. {
  102. if(en){
  103. //SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;//Speaker Enable
  104. SpeakerEnable();
  105. }else{
  106. //SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;//Speaker Disable
  107. SpeakerDisable();
  108. }
  109. }
  110. void SetRingFreq(int fre)
  111. {
  112. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  113. TIM_OCInitTypeDef TIM_OCInitStructure;
  114. unsigned short arr;
  115. arr=20000000/fre;//6000000
  116. /*====== 初始化TIM4 ======*/
  117. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  118. TIM_TimeBaseStructure.TIM_Prescaler =3; //设置用来作为TIMx时钟频率除数的预分频值
  119. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  120. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  121. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
  122. TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  123. //====== 初始化TIM4 Channel2 PWM模式 ======/
  124. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
  125. TIM_OCInitStructure.TIM_Pulse=arr/2; //设置占空比时
  126. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  127. //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
  128. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity_Low 输出极性:TIM输出比较极性低
  129. TIM_OC1Init(TIM5, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
  130. TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
  131. TIM_Cmd(TIM5, ENABLE); //禁止/使能TIM4
  132. }
  133. void SPKDelayUs(unsigned short us)
  134. {
  135. unsigned short i;
  136. unsigned short t=us;
  137. while(t--){
  138. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  139. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  140. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  141. }
  142. }
  143. void SpeakerEnable(void)
  144. {
  145. unsigned char i;
  146. unsigned char time;
  147. #if 1
  148. if(sutPocStatus.HeadSetPtt)return ; //耳机FM 外放免提
  149. time=4;
  150. for(i=0;i<time;i++){
  151. SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
  152. SPKDelayUs(7);
  153. SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;
  154. SPKDelayUs(7);
  155. }
  156. #endif
  157. SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
  158. }
  159. void SpeakerDisable()
  160. {
  161. if(Test_EnableSpk==0)SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;
  162. }
  163. /********************************************************************************
  164. * End of Module
  165. *******************************************************************************/