Audio.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. int TTSDelayCnt;
  13. unsigned char MuteFlag=1;
  14. /*****************************************************************
  15. *CLK=12Mhz
  16. *(10000,2)==> 12000000/2/10000=600Hz
  17. ******************************************************************/
  18. void newBeepSet(int fre)
  19. {
  20. /*此函数能够自适用SYSCLK来生成频率为freHz,占空比为50%的PWM输出
  21. 公式:
  22. T=(1+TIM_Period)*(1+TIM_Prescaler)/SYSCLK
  23. fre=1/T=SYSCLK/(1+TIM_Period)*(1+TIM_Prescaler)
  24. TIM_Period=(SYSCLK/fre/(1+TIM_Prescaler))-1
  25. 这里SYSCLK,fre是已知的,只要确定TIM_Period和TIM_Prescaler即可
  26. 这里我们先确定TIM_Prescaler,只要保证TIM_Prescaler不超界即可
  27. 我们直接取TIM_Prescaler=SYSCLK/10000,这样就能保证TIM_Prescaler不超界
  28. TIM_Prescaler定下来了,再根据公式计算,那么TIM_Period也就出来了
  29. */
  30. uint16_t tempPrescale;
  31. uint16_t TimerPeriod,ChannelPulse;
  32. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  33. TIM_OCInitTypeDef TIM_OCInitStructure;
  34. tempPrescale = USER_MAIN_SYSCLK / 10000 - 1;//确定TIM_Prescaler
  35. TimerPeriod = USER_MAIN_SYSCLK / (fre * tempPrescale);//根据公式再求TIM_Period
  36. ChannelPulse = (uint16_t) (TimerPeriod/2);//50%占空比
  37. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  38. TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//TIM_CKD_DIV1
  39. TIM_TimeBaseStructure.TIM_Prescaler = tempPrescale;//预分频
  40. TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
  41. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
  42. TIM_TimeBaseStructure.TIM_Period=TimerPeriod-1; //周期
  43. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  44. TIM_OCInitStructure.TIM_Pulse=ChannelPulse;//50%占空比
  45. TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
  46. TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
  47. TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
  48. TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
  49. TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
  50. TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
  51. TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
  52. TIM_OCInitStructure.TIM_Pulse=ChannelPulse;
  53. TIM_OC2Init(TIM2, &TIM_OCInitStructure); //通道2
  54. TIM_Cmd(TIM2, ENABLE);
  55. TIM_CtrlPWMOutputs(TIM2,ENABLE);
  56. }
  57. void SetBeep(int fre,int tick)
  58. {
  59. newBeepSet(fre);
  60. uTimerCreate(UTIMER_BEEP,tick);
  61. uTimerCreate(UTIMER_MIC,tick+8);
  62. }
  63. /**********************************************************************
  64. 1ms在8000~9000之间
  65. ***********************************************************************/
  66. void SetBeepByNoOS(int fre,int ms)
  67. {
  68. //if(newPara.KeySound==0) return;
  69. newBeepSet(fre);
  70. TIM_Cmd(TIM2, ENABLE); //禁止/使能TIM1
  71. uTimerHardDly(ms);
  72. TIM_Cmd(TIM2, DISABLE); //禁止/使能TIM1
  73. }
  74. /********************************************************************************
  75. SpeakerInit
  76. *********************************************************************************/
  77. void SpeakerInit(void)
  78. {
  79. GPIO_InitTypeDef GPIO_InitStructure;
  80. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE );
  81. GPIO_InitStructure.GPIO_Pin = SPK_MUTE_PIN|SPK_BOOST_PIN|SPK_MAIN_MUTE_PIN;
  82. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
  83. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  84. GPIO_Init(SPK_MUTE_PORT, &GPIO_InitStructure);//初始化GPIO
  85. GPIO_SetBits(SPK_BOOST_PROT,SPK_BOOST_PIN);//默认拉高
  86. SPK_MAIN_MUTE_LOW;
  87. SpeakerCtrl(0);
  88. }
  89. /********************************************************************************
  90. SpeakerCtrl
  91. *********************************************************************************/
  92. void SpeakerCtrl(int en)
  93. {
  94. if(en){
  95. //SPK_MUTE_PORT->BRR=SPK_MUTE_PIN;//Speaker Enable
  96. SpeakerEnable();
  97. }else{
  98. //SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;//Speaker Disable
  99. SpeakerDisable();
  100. }
  101. }
  102. void SetRingFreq(int fre)
  103. {
  104. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  105. TIM_OCInitTypeDef TIM_OCInitStructure;
  106. unsigned short arr;
  107. arr=20000000/fre;//6000000
  108. /*====== 初始化TIM4 ======*/
  109. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  110. TIM_TimeBaseStructure.TIM_Prescaler =3; //设置用来作为TIMx时钟频率除数的预分频值
  111. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  112. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  113. TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
  114. TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  115. //====== 初始化TIM4 Channel2 PWM模式 ======/
  116. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
  117. TIM_OCInitStructure.TIM_Pulse=arr/2; //设置占空比时
  118. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  119. //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
  120. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity_Low 输出极性:TIM输出比较极性低
  121. TIM_OC1Init(TIM5, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2
  122. TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable); //使能TIM4在CCR2上的预装载寄存器
  123. TIM_Cmd(TIM5, ENABLE); //禁止/使能TIM4
  124. }
  125. void SPKDelayUs(unsigned short us)
  126. {
  127. unsigned short i;
  128. unsigned short t=us;
  129. while(t--){
  130. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  131. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  132. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  133. }
  134. }
  135. void SpeakerEnable(void)
  136. {
  137. if(MuteFlag==0)return;
  138. SPK_MUTE_PORT->BSRR=SPK_MUTE_PIN;//////
  139. }
  140. /********************************************************************************
  141. * End of Module
  142. *******************************************************************************/