Audio.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. unsigned char Earcheck=0;
  15. OS_ID idBeepTimer;
  16. OS_ID idRingTimer;
  17. /*****************************************************************
  18. *CLK=12Mhz
  19. *(10000,2)==> 12000000/2/10000=600Hz
  20. ******************************************************************/
  21. void BeepInit(void)
  22. {
  23. GPIO_InitTypeDef GPIO_InitStructure;
  24. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
  25. //设置该引脚为复用输出功能,输出TIM4 CH2的PWM脉冲波形 GPIOB.7
  26. GPIO_InitStructure.GPIO_Pin = BEEP_PIN; //TIM_CH2
  27. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  28. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  29. GPIO_Init(BEEP_PORT, &GPIO_InitStructure);//初始化GPIO
  30. RunMake(THIS_FILE_ID);
  31. idBeepTimer=NULL;
  32. }
  33. void newBeepSet(int fre)
  34. {
  35. int cntMeiTiao;
  36. uint16_t TimerPeriod,ChannelPulse = 0;
  37. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  38. TIM_OCInitTypeDef TIM_OCInitStructure;
  39. cntMeiTiao=SystemCoreClock/(SystemCoreClock/10000);
  40. TimerPeriod = cntMeiTiao/fre;
  41. // printf("cntMeiTiao==%d,TimerPeriod=%d\r\n",cntMeiTiao,TimerPeriod);
  42. //sutProductPara.VolLv
  43. ChannelPulse = sutProductPara.VolLv;//(uint16_t) (TimerPeriod*(sutProductPara.VolLv)*1)/100;
  44. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
  45. TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock/10000-1; //48000
  46. TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
  47. TIM_TimeBaseStructure.TIM_Period=TimerPeriod;
  48. TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
  49. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
  50. TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
  51. TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
  52. TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
  53. TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
  54. TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
  55. TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
  56. TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
  57. TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
  58. TIM_OCInitStructure.TIM_Pulse=ChannelPulse;
  59. TIM_OC2Init(TIM5, &TIM_OCInitStructure);
  60. TIM_Cmd(TIM5, ENABLE);
  61. TIM_CtrlPWMOutputs(TIM5,ENABLE);
  62. }
  63. void SetBeep(int fre,int tick)
  64. {
  65. newBeepSet(fre);
  66. idBeepTimer=os_tmr_create(tick,TMR_INF_BEEP);
  67. //idBeepTimer=os_tmr_create(tick+8,TMR_INF_MIC);//添加一个延时
  68. }
  69. void SetBeepByNoOS(int fre,int ms)
  70. {
  71. newBeepSet(fre);
  72. TIM_Cmd(TIM5, ENABLE); //禁止/使能TIM4
  73. DelayMs(ms);
  74. TIM_Cmd(TIM5, DISABLE); //禁止/使能TIM4
  75. }
  76. /********************************************************************************
  77. MicCtrlInit
  78. *********************************************************************************/
  79. void MicrophoneInit(void)
  80. {
  81. }
  82. /********************************************************************************
  83. MicCtrl
  84. *********************************************************************************/
  85. void MicrophoneCtrl(int en)
  86. {
  87. if(en){
  88. }else{
  89. }
  90. }
  91. void SpeakerEnable()
  92. {
  93. SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
  94. }
  95. void SpeakerDisable()
  96. {
  97. SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;
  98. }
  99. /********************************************************************************
  100. SpeakerInit
  101. *********************************************************************************/
  102. void SpeakerInit(void)
  103. {
  104. GPIO_InitTypeDef GPIO_InitStructure;
  105. GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN;//|SPK_EX_PIN;
  106. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
  107. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  108. GPIO_Init(SPK_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
  109. SpeakerCtrl(0);
  110. }
  111. /********************************************************************************
  112. SpeakerCtrl
  113. *********************************************************************************/
  114. void SpeakerCtrl(int en)
  115. {
  116. if(en){
  117. //SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;//Speaker Enable
  118. SpeakerEnable();
  119. }else{
  120. //SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;//Speaker Disable
  121. SpeakerDisable();
  122. }
  123. }
  124. void SetRingFreq(int fre)
  125. {
  126. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  127. TIM_OCInitTypeDef TIM_OCInitStructure;
  128. unsigned short arr;
  129. arr=20000000/fre;//6000000
  130. /*====== 初始化TIM4 ======*/
  131. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  132. TIM_TimeBaseStructure.TIM_Prescaler =3; //设置用来作为TIMx时钟频率除数的预分频值
  133. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  134. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  135. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
  136. TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  137. //====== 初始化TIM4 Channel2 PWM模式 ======/
  138. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
  139. TIM_OCInitStructure.TIM_Pulse=arr/2; //设置占空比时
  140. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  141. //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
  142. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity_Low 输出极性:TIM输出比较极性低
  143. TIM_OC1Init(TIM5, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
  144. TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
  145. TIM_Cmd(TIM5, ENABLE); //禁止/使能TIM4
  146. }
  147. const SUT_RING_ELEMENT acusRingStart[]={
  148. //频率,时间
  149. {800,100},
  150. {0,100},
  151. {900,100},
  152. {900,150},
  153. {0,200},
  154. {500,300},
  155. {0,0}//必须以{x,0}作为结束
  156. };
  157. SUT_RING sutRing={0,0,0,0};
  158. void RingStart(SUT_RING_ELEMENT *pRingElement)
  159. {
  160. unsigned short freq,time,num;
  161. num=0;
  162. while(1){
  163. time=pRingElement[num].time;
  164. if(time==0)break;
  165. if(++num>10000)return;//不能太长,太长判断为非法而不处理
  166. }
  167. sutRing.pElement=pRingElement;
  168. sutRing.start=1;
  169. sutRing.handle=0;
  170. freq=sutRing.pElement->freq;
  171. time=sutRing.pElement->time;
  172. SetRingFreq(freq);
  173. idRingTimer=os_tmr_create(time,TMR_INF_RING);
  174. SpeakerDisable();
  175. }
  176. void Ear_check(char Update)
  177. {
  178. static signed char LastEarStatue=-1;
  179. if(GPIO_ReadInputDataBit(EAR_CHEK_PORT,EAR_CHEK_PIN)==SET)
  180. {
  181. Earcheck=1;
  182. }else {
  183. Earcheck=0;
  184. }
  185. if(LastEarStatue!=Earcheck||Update){
  186. if(modemInitReady>1){
  187. LastEarStatue=Earcheck;
  188. if(Earcheck==0)ModemSendAT("AT+POC=608&2\r\n");
  189. else ModemSendAT("AT+POC=608&1\r\n");
  190. }
  191. }
  192. }
  193. /********************************************************************************
  194. * End of Module
  195. *******************************************************************************/