Audio.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*******************************************************************************
  2. * File Name: Audio.c
  3. * Function Describe:device for the Keys
  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. OS_ID idBeepTimer;
  14. /*****************************************************************
  15. *CLK=12Mhz
  16. *(10000,2)==> 12000000/2/10000=600Hz
  17. ******************************************************************/
  18. void BeepInit(void)
  19. {
  20. GPIO_InitTypeDef GPIO_InitStructure;
  21. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  22. TIM_OCInitTypeDef TIM_OCInitStructure;
  23. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器4时钟
  24. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | 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. /*====== 初始化TIM4 ======*/
  32. TIM_TimeBaseStructure.TIM_Period = 10000; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  33. TIM_TimeBaseStructure.TIM_Prescaler =3; //设置用来作为TIMx时钟频率除数的预分频值
  34. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  35. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  36. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
  37. TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  38. //====== 初始化TIM4 Channel2 PWM模式 ======/
  39. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
  40. TIM_OCInitStructure.TIM_Pulse=5000; //设置占空比时
  41. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  42. //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
  43. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity_Low 输出极性:TIM输出比较极性低
  44. TIM_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
  45. TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
  46. // TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM4
  47. RunMake(THIS_FILE_ID);
  48. idBeepTimer=NULL;
  49. }
  50. void SetBeep(int fre,int tick)
  51. {
  52. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  53. TIM_OCInitTypeDef TIM_OCInitStructure;
  54. unsigned short arr;
  55. arr=6000000/fre;
  56. /*====== 初始化TIM4 ======*/
  57. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  58. TIM_TimeBaseStructure.TIM_Prescaler =3; //设置用来作为TIMx时钟频率除数的预分频值
  59. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  60. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  61. TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复寄存器,用于自动更新pwm占空比
  62. TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  63. //====== 初始化TIM4 Channel2 PWM模式 ======/
  64. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
  65. TIM_OCInitStructure.TIM_Pulse=arr/2; //设置占空比时
  66. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  67. //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
  68. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity_Low 输出极性:TIM输出比较极性低
  69. TIM_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
  70. TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
  71. #if 0
  72. //SpeakerEnable();
  73. TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM4
  74. os_dly_wait(tick);
  75. TIM_Cmd(TIM4, DISABLE); //禁止/使能TIM4
  76. //SpeakerDisable();
  77. #else
  78. //SpeakerEnable();
  79. //RunMake(THIS_FILE_ID);
  80. TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM4
  81. idBeepTimer=os_tmr_create(tick,TMR_INF_BEEP);
  82. //RunMake(THIS_FILE_ID);
  83. //SlwTrace(INF,"os_tmr_create\r\n");
  84. //printf("[Beep%ul]\r\n",os_time_get());
  85. #endif
  86. }
  87. /**********************************************************************
  88. 1ms在8000~9000之间
  89. ***********************************************************************/
  90. void DelayMs(unsigned short ms)
  91. {
  92. unsigned short i;
  93. unsigned short t=ms;
  94. RunMake(THIS_FILE_ID);
  95. IWDG_ReloadCounter();
  96. while(t--){
  97. for(i=0;i<6000;i++);
  98. IWDG_ReloadCounter();
  99. }
  100. RunMake(THIS_FILE_ID);
  101. }
  102. void SetBeepByNoOS(int fre,int ms)
  103. {
  104. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  105. TIM_OCInitTypeDef TIM_OCInitStructure;
  106. unsigned short arr;
  107. RunMake(THIS_FILE_ID);
  108. arr=6000000/fre;
  109. /*====== 初始化TIM4 ======*/
  110. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  111. TIM_TimeBaseStructure.TIM_Prescaler =3; //设置用来作为TIMx时钟频率除数的预分频值
  112. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  113. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  114. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
  115. TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  116. RunMake(THIS_FILE_ID);
  117. //====== 初始化TIM4 Channel2 PWM模式 ======/
  118. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
  119. TIM_OCInitStructure.TIM_Pulse=arr/2; //设置占空比时
  120. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  121. //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
  122. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity_Low 输出极性:TIM输出比较极性低
  123. TIM_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
  124. TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
  125. RunMake(THIS_FILE_ID);
  126. TIM_Cmd(TIM4, ENABLE); //禁止/使能TIM4
  127. DelayMs(ms);
  128. TIM_Cmd(TIM4, DISABLE); //禁止/使能TIM4
  129. }
  130. /********************************************************************************
  131. MicCtrlInit
  132. *********************************************************************************/
  133. void MicrophoneInit(void)
  134. {
  135. GPIO_InitTypeDef GPIO_InitStructure;
  136. GPIO_InitStructure.GPIO_Pin = MIC_MUTE_PIN; //TIM_CH2
  137. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
  138. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  139. GPIO_Init(MIC_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
  140. MicrophoneCtrl(1);
  141. RunMake(THIS_FILE_ID);
  142. }
  143. /********************************************************************************
  144. MicCtrl
  145. *********************************************************************************/
  146. void MicrophoneCtrl(int en)
  147. {
  148. if(en){
  149. MIC_MUTE_PORT->BRR=MIC_MUTE_PIN;//Set Pin Low ,MIC Enalbe
  150. }else{
  151. MIC_MUTE_PORT->BSRR=MIC_MUTE_PIN;//Set Pin High ,MIC MUTE
  152. }
  153. }
  154. /********************************************************************************
  155. SpeakerInit
  156. *********************************************************************************/
  157. void SpeakerInit(void)
  158. {
  159. GPIO_InitTypeDef GPIO_InitStructure;
  160. GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN;
  161. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
  162. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  163. GPIO_Init(SPK_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
  164. SpeakerCtrl(0);
  165. }
  166. /********************************************************************************
  167. SpeakerCtrl
  168. *********************************************************************************/
  169. void SpeakerCtrl(int en)
  170. {
  171. if(en){
  172. //SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;//Speaker Enable
  173. SpeakerEnable();
  174. }else{
  175. //SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;//Speaker Disable
  176. SpeakerDisable();
  177. }
  178. }
  179. /********************************************************************************
  180. * End of Module
  181. *******************************************************************************/