Audio.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #define THIS_FILE_ID 7
  10. //------------------------------------------------------------------------------
  11. #include "stm32f10x.h"
  12. #include <includes.h>
  13. int cntMeiTiao;
  14. OS_ID idBeepTimer;
  15. OS_ID idRingTimer;
  16. /*****************************************************************
  17. *CLK=12Mhz
  18. *(10000,2)==> 12000000/2/10000=600Hz
  19. ******************************************************************/
  20. void BeepInit(void)
  21. {
  22. GPIO_InitTypeDef GPIO_InitStructure;
  23. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
  24. //设置该引脚为复用输出功能,输出TIM4 CH2的PWM脉冲波形 GPIOB.7
  25. GPIO_InitStructure.GPIO_Pin = BEEP_PIN; //TIM_CH2
  26. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  27. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  28. GPIO_Init(BEEP_PORT, &GPIO_InitStructure);//初始化GPIO
  29. RunMake(THIS_FILE_ID);
  30. idBeepTimer=NULL;
  31. }
  32. void newBeepSet(int fre)
  33. {
  34. /*此函数能够自适用SYSCLK来生成频率为freHz,占空比为50%的PWM输出
  35. 公式:
  36. T=(1+TIM_Period)*(1+TIM_Prescaler)/SYSCLK
  37. fre=1/T=SYSCLK/(1+TIM_Period)*(1+TIM_Prescaler)
  38. TIM_Period=(SYSCLK/fre/(1+TIM_Prescaler))-1
  39. 这里SYSCLK,fre是已知的,只要确定TIM_Period和TIM_Prescaler即可
  40. 这里我们先确定TIM_Prescaler,只要保证TIM_Prescaler不超界即可
  41. 我们直接取TIM_Prescaler=SYSCLK/10000,这样就能保证TIM_Prescaler不超界
  42. TIM_Prescaler定下来了,再根据公式计算,那么TIM_Period也就出来了
  43. */
  44. uint16_t tempPrescale;
  45. uint16_t TimerPeriod,ChannelPulse;
  46. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  47. TIM_OCInitTypeDef TIM_OCInitStructure;
  48. tempPrescale = USER_MAIN_SYSCLK / 10000 - 1;//确定TIM_Prescaler
  49. TimerPeriod = USER_MAIN_SYSCLK / (fre * tempPrescale);//根据公式再求TIM_Period
  50. ChannelPulse = (uint16_t) (TimerPeriod/2);//50%占空比
  51. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
  52. TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
  53. TIM_TimeBaseStructure.TIM_Prescaler = tempPrescale;
  54. TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
  55. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
  56. TIM_TimeBaseStructure.TIM_Period=TimerPeriod-1;
  57. TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
  58. TIM_OCInitStructure.TIM_Pulse=ChannelPulse;//50%占空比
  59. TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
  60. TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
  61. TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
  62. TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
  63. TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
  64. TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
  65. TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
  66. TIM_OCInitStructure.TIM_Pulse=ChannelPulse;
  67. TIM_OC2Init(TIM5, &TIM_OCInitStructure);
  68. TIM_Cmd(TIM5, ENABLE);
  69. TIM_CtrlPWMOutputs(TIM5,ENABLE);
  70. }
  71. void SetBeep(int fre,int tick)
  72. {
  73. if(newPara.KeySound==0) {
  74. SpeakerDisable();
  75. return;
  76. }
  77. newBeepSet(fre);
  78. idBeepTimer=os_tmr_create(tick,TMR_INF_BEEP);
  79. idBeepTimer=os_tmr_create(tick+8,TMR_INF_MIC);//添加一个延时
  80. }
  81. /**********************************************************************
  82. 1ms在8000~9000之间
  83. ***********************************************************************/
  84. void SetBeepByNoOS(int fre,int ms)
  85. {
  86. if(newPara.KeySound==0) return;
  87. newBeepSet(fre);
  88. RunMake(THIS_FILE_ID);
  89. TIM_Cmd(TIM5, ENABLE); //禁止/使能TIM4
  90. DelayMs(ms);
  91. TIM_Cmd(TIM5, DISABLE); //禁止/使能TIM4
  92. }
  93. /********************************************************************************
  94. MicCtrlInit
  95. *********************************************************************************/
  96. void MicrophoneInit(void)
  97. {
  98. GPIO_InitTypeDef GPIO_InitStructure;
  99. GPIO_InitStructure.GPIO_Pin = MIC_MUTE_PIN; //TIM_CH2
  100. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
  101. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  102. GPIO_Init(MIC_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
  103. MicrophoneCtrl(0);
  104. RunMake(THIS_FILE_ID);
  105. }
  106. /********************************************************************************
  107. MicCtrl
  108. *********************************************************************************/
  109. void MicrophoneCtrl(int en)
  110. {
  111. if(en){
  112. MIC_MUTE_PORT->BRR=MIC_MUTE_PIN;//Set Pin Low ,MIC Enalbe
  113. }else{
  114. MIC_MUTE_PORT->BSRR=MIC_MUTE_PIN;//Set Pin High ,MIC MUTE
  115. }
  116. }
  117. /********************************************************************************
  118. SpeakerInit
  119. *********************************************************************************/
  120. void SpeakerInit(void)
  121. {
  122. GPIO_InitTypeDef GPIO_InitStructure;
  123. GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN;
  124. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
  125. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  126. GPIO_Init(SPK_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
  127. SpeakerCtrl(0);
  128. }
  129. /********************************************************************************
  130. SpeakerCtrl
  131. *********************************************************************************/
  132. void SpeakerCtrl(int en)
  133. {
  134. if(en){
  135. //SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;//Speaker Enable
  136. SpeakerEnable();
  137. }else{
  138. //SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;//Speaker Disable
  139. SpeakerDisable();
  140. }
  141. }
  142. void SetRingFreq(int fre)
  143. {
  144. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  145. TIM_OCInitTypeDef TIM_OCInitStructure;
  146. unsigned short arr;
  147. arr=20000000/fre;//6000000
  148. /*====== 初始化TIM4 ======*/
  149. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  150. TIM_TimeBaseStructure.TIM_Prescaler =3; //设置用来作为TIMx时钟频率除数的预分频值
  151. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  152. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  153. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
  154. TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  155. //====== 初始化TIM4 Channel2 PWM模式 ======/
  156. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
  157. TIM_OCInitStructure.TIM_Pulse=arr/2; //设置占空比时
  158. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  159. //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
  160. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity_Low 输出极性:TIM输出比较极性低
  161. TIM_OC1Init(TIM5, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
  162. TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
  163. TIM_Cmd(TIM5, ENABLE); //禁止/使能TIM4
  164. }
  165. const SUT_RING_ELEMENT acusRingStart[]={
  166. //频率,时间
  167. {800,100},
  168. {0,100},
  169. {900,100},
  170. {900,150},
  171. {0,200},
  172. {500,300},
  173. {0,0}//必须以{x,0}作为结束
  174. };
  175. SUT_RING sutRing={0,0,0,0};
  176. void RingStart(SUT_RING_ELEMENT *pRingElement)
  177. {
  178. unsigned short freq,time,num;
  179. num=0;
  180. while(1){
  181. time=pRingElement[num].time;
  182. if(time==0)break;
  183. if(++num>10000)return;//不能太长,太长判断为非法而不处理
  184. }
  185. sutRing.pElement=pRingElement;
  186. sutRing.start=1;
  187. sutRing.handle=0;
  188. freq=sutRing.pElement->freq;
  189. time=sutRing.pElement->time;
  190. SetRingFreq(freq);
  191. idRingTimer=os_tmr_create(time,TMR_INF_RING);
  192. SpeakerDisable();
  193. }
  194. void SPKDelayUs(unsigned short us)
  195. {
  196. unsigned short i;
  197. unsigned short t=us;
  198. while(t--){
  199. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  200. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  201. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  202. }
  203. }
  204. void SpeakerEnable(void)
  205. {
  206. unsigned char i;
  207. unsigned char time;
  208. // if(newPara.SoundMode==0) time=3;
  209. // else time=4;
  210. time=4;
  211. for(i=0;i<time;i++){
  212. SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
  213. SPKDelayUs(5);
  214. SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;
  215. SPKDelayUs(5);
  216. }
  217. SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
  218. SPKDelayUs(5);
  219. }
  220. /********************************************************************************
  221. * End of Module
  222. *******************************************************************************/