Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30995183
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: C/C++

2018-11-13 15:13:45


点击(此处)折叠或打开

  1. #define BITS_PER_SAMPLE_8 8
  2. #define WAVE_DUMMY_BYTE 0xA5
  3. #define DAC_DHR8R1_Address                              0x40007410
  4. #define BUFFER_SIZE                                                 1024

  5.  __IO uint16_t TIM6ARRValue = 1088;

  6. uint32_t WaveDataLength = 13144;
  7. uint32_t ReadOffset;

  8. uint8_t Wavebuffer[BUFFER_SIZE];
  9. //uint8_t Wavebuffer2[512];


  10. #define SAMPLE_RATE_8000 8000
  11. #define SAMPLE_RATE_11025 11025
  12. #define SAMPLE_RATE_16000                                     16000
  13. #define SAMPLE_RATE_22050 22050
  14. #define SAMPLE_RATE_44100 44100
  15. #define SAMPLE_RATE_48000                                     48000

  16. extern const unsigned char AnsiChar[];


  17. u16 GetARRValue(u16 sample)
  18. {
  19.     u16 arrValue;
  20.     //??OCA????.WAV?????
  21.     switch (sample)
  22.     {
  23.                 case SAMPLE_RATE_8000 :
  24.                         arrValue = (u16)(72000000/8000);
  25.                         break; // 8KHz = 2x36MHz / 9000
  26.                 case SAMPLE_RATE_11025:
  27.                         arrValue = (u16)(72000000/11025);
  28.                         break; // 11.025KHz = 2x36MHz / 6531
  29.                 case SAMPLE_RATE_16000:
  30.                          arrValue = (u16)(7000000/16000);
  31.                         break; // 16KHz = 2x36MHz / 4500
  32.                 case SAMPLE_RATE_22050:
  33.                         arrValue = (u16)(72000000/22050);
  34.                         break; // 22.05KHz = 2x36MHz / 2365
  35.                 case SAMPLE_RATE_44100:
  36.                         arrValue = (u16)(72000000/44100);
  37.                         break; // 44.1KHz = 2x36MHz / 1633
  38.                 case SAMPLE_RATE_48000:
  39.                         arrValue = (u16)(72000000/48000);
  40.                         break; // 48KHz = 2x36MHz / 1500
  41.                 default:
  42.                         arrValue = 0;
  43.                         break;
  44.     }
  45.     return arrValue;
  46. }



  47. void WavRCCConfiguration(void)
  48. {
  49.   //SystemInit();
  50.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA, ENABLE);
  51.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
  52.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
  53.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
  54. }


  55. void WavGPIOConfiguration(void)
  56. {
  57.   GPIO_InitTypeDef GPIO_InitStructure;
  58.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  59.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  60.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  61.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  62.     GPIO_SetBits(GPIOA,GPIO_Pin_4);//PA.4 ???
  63. }

  64. void WavTimerConfiguration(void)
  65. {
  66.      TIM_DeInit(TIM6);
  67.      TIM6ARRValue = GetARRValue(SAMPLE_RATE_8000);
  68.    TIM_SetAutoreload(TIM6, TIM6ARRValue); //TIM6ARRValue???72000000/22050
  69.    TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
  70.    //TIM_Cmd(TIM6, ENABLE);
  71.      TIM_Cmd(TIM6, DISABLE);
  72.     
  73. //     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  74. //    
  75. //     TIM_DeInit(TIM6);
  76. // TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  77. // TIM_TimeBaseStructure.TIM_Period = GetARRValue(SAMPLE_RATE_8000);//72MHz ?CPU,??44.1KHz ???,?????1633
  78. // TIM_TimeBaseStructure.TIM_Prescaler = 0;
  79. // TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
  80. // TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  81. // TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
  82. // // TIM8 TRGO selection
  83. // TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
  84. }

  85. void WavDACConfiguration(void)
  86. {
  87.     DAC_InitTypeDef DAC_InitStructure;
  88.     DAC_DeInit();
  89.   DAC_StructInit(&DAC_InitStructure);
  90.   DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
  91.     DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;//?¨??·??ú?÷????
  92.   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  93.   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //DAC_OutputBuffer_Enable(?ù???ó???à)
  94.     
  95.   DAC_Init(DAC_Channel_1, &DAC_InitStructure);
  96.     
  97.     DAC_SetChannel1Data(DAC_Align_8b_R, 0); //jon ??????
  98.     
  99.   DAC_Cmd(DAC_Channel_1, ENABLE);
  100.   DAC_DMACmd(DAC_Channel_1, ENABLE);
  101. }

  102. void WavNVICConfiguration(void)
  103. {
  104.     
  105.   NVIC_InitTypeDef NVIC_InitStructure;
  106.      /* Enable the TIM6 Interrupt */
  107. // NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
  108. // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  109. // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  110. // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  111. // NVIC_Init(&NVIC_InitStructure);
  112. //    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  113.     
  114.   /* Enable the DMA1_Channel3 Interrupt */
  115.   NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel3_IRQn;
  116.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  117.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  118.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  119.   NVIC_Init(&NVIC_InitStructure);
  120. }

  121. void WavDMAConfiguration(void)
  122. {
  123.     DMA_InitTypeDef DMA_InitStructure;
  124.     DMA_DeInit(DMA2_Channel3);
  125.   DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_Address;
  126.   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&AnsiChar[44];//(uint32_t)&Wavebuffer; //
  127.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  128.   DMA_InitStructure.DMA_BufferSize = (9550-44);//BUFFER_SIZE; //
  129.     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  130.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  131.     
  132.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  133.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

  134.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  135.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  136.   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  137.   DMA_Init(DMA2_Channel3, &DMA_InitStructure);
  138.   DMA_Cmd(DMA2_Channel3, ENABLE);
  139.     DMA_ITConfig(DMA2_Channel3, DMA_IT_TC | DMA_IT_HT, ENABLE);
  140. }

  141. void waveplaydma(int len)
  142. {
  143.     DMA_InitTypeDef DMA_InitStructure;
  144.     DMA_DeInit(DMA2_Channel3);
  145.   DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_Address;
  146.   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&AnsiChar[44];//(uint32_t)&Wavebuffer; //
  147.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  148.   DMA_InitStructure.DMA_BufferSize = len;//BUFFER_SIZE; //
  149.     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  150.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  151.     
  152.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  153.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

  154.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  155.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  156.   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  157.   DMA_Init(DMA2_Channel3, &DMA_InitStructure);
  158.   DMA_Cmd(DMA2_Channel3, ENABLE);
  159.     DMA_ITConfig(DMA2_Channel3, DMA_IT_TC | DMA_IT_HT, ENABLE);
  160. }


  161. void WavePlayer_Init(void)
  162. {
  163.         WavRCCConfiguration();
  164.      WavNVICConfiguration();
  165.         WavGPIOConfiguration();
  166.         WavDACConfiguration();
  167.         WavDMAConfiguration();
  168.         WavTimerConfiguration();
  169. }

  170. int PlayFlg=0;
  171. int PlayPingPong=0;

  172. void WavPlayerStop(void)
  173. {
  174.     //DMA_ITConfig(DMA2_Channel3, DMA_IT_TC | DMA_IT_HT, DISABLE);
  175.     ReadOffset = 0;
  176.     WaveDataLength = 0;
  177.     PlayFlg = 0;
  178.     
  179.     TIM_ITConfig(TIM6, TIM_IT_Update, DISABLE);
  180.     DMA_Cmd(DMA2_Channel3, DISABLE);
  181.     TIM_Cmd(TIM6, DISABLE);
  182.     
  183.     TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
  184.     DMA_ClearITPendingBit(DMA2_IT_HT3|DMA2_IT_TC3);
  185.     
  186. }

  187. void PlayReadBlock(int offset)
  188. {
  189.         int i=0;
  190.      int EndByte=WaveDataLength-ReadOffset;
  191.     
  192.      if(EndByte<512) //?á??
  193.         {
  194.                 WavPlayerStop();
  195.             
  196. //            WaveDataLength = 0;
  197. //            ReadOffset = 0;
  198.   //            WavePlayer_Start(1);
  199.             return ;
  200.         }
  201.     
  202.     
  203.         if(EndByte>=512)
  204.         {
  205.             for(i=offset;i<(offset+512);i++)
  206.             {
  207.                     Wavebuffer[i]=AnsiChar[ReadOffset+i];
  208.             }
  209.             ReadOffset+=512;
  210.         }
  211. //        else
  212. //        {
  213. //            //????????
  214. //            for(i=offset;i<(offset+EndByte);i++)
  215. //            {
  216. //                    Wavebuffer[i]=AnsiChar[ReadOffset+i];
  217. //            }
  218. //            //????????
  219. //            for(i=(offset+EndByte);i<(offset+512);i++)
  220. //            {
  221. //                    Wavebuffer[i]=0x7F;
  222. //            }
  223. //            ReadOffset+=EndByte;
  224. //            
  225. ////            TIM_ITConfig(TIM6, TIM_IT_Update, DISABLE); //??TIM6????
  226. //// TIM_Cmd(TIM6, DISABLE); //??TIM6
  227. ////            WaveDataLength = 0;
  228. ////            ReadOffset = 0;
  229. //        }
  230.         
  231. }


  232. void waveTask(void)
  233. {
  234. //        if(PlayFlg==1)
  235. //        {
  236. //                if(PlayPingPong==1)
  237. //                {    
  238. //                    PlayPingPong = 0;
  239. //                    PlayReadBlock(0);
  240. //                }
  241. //                if(PlayPingPong==2)
  242. //                {    
  243. //                    PlayPingPong = 0;
  244. //                    PlayReadBlock(BUFFER_SIZE>>1);
  245. //                }    
  246. //        }
  247. }



  248. void DMA2_Channel3_IRQHandler(void)
  249. {
  250.          if (DMA_GetITStatus(DMA2_IT_HT3) == SET) //??????°?
  251.      {
  252.          DMA_ClearITPendingBit(DMA2_IT_HT3);
  253.          DMA_ClearFlag(DMA2_FLAG_HT3);
  254.          //?ì?é??°?±ê???????§???ò????±ê???????í?°°???????
  255.              //PlayReadBlock(0);
  256.              PlayPingPong = 1;
  257.      }
  258.         
  259.      //LED3 = !LED3;
  260.      if (DMA_GetITStatus(DMA2_IT_TC3) == SET) //?????ê
  261.      {
  262.          DMA_ClearITPendingBit(DMA2_IT_TC3);
  263.          DMA_ClearFlag(DMA2_FLAG_TC3);
  264.             
  265.              PlayPingPong = 2;
  266.          //?ì?é?ê?? ±ê??,???í?ó°???????
  267.          //PlayReadBlock(BUFFER_SIZE>>1);
  268.             
  269.              WavPlayerStop();
  270.              WavePlayer_Start(1);
  271.             
  272.      }
  273. }


  274. int WavePlayer_Start(int PlayIndex)
  275. {
  276.     int i=0;
  277.   ReadOffset = 44; //????wav?·
  278.     WaveDataLength = 9550;
  279.     PlayFlg = 1;
  280. //    for(i=0;i<1024;i++)
  281. //        Wavebuffer[i] = 0x7f;
  282.     PlayReadBlock(0);
  283.     PlayReadBlock(BUFFER_SIZE>>1);
  284.     
  285.     waveplaydma((9550-44));
  286.     
  287.     DMA_Cmd(DMA2_Channel3, ENABLE);
  288.     TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
  289.   TIM_Cmd(TIM6, ENABLE);
  290.   return 0;
  291. }

  292. /**
  293.   * @brief Stop wave playing
  294.   * @param None
  295.   * @retval None
  296.   */


  297. /**
  298.   * @brief Pause wave playing
  299.   * @param None
  300.   * @retval None
  301.   */
  302. void WavPlayerRePlay(void)
  303. {
  304.      //DMA_ITConfig(DMA2_Channel3, DMA_IT_TC | DMA_IT_HT, ENABLE);
  305.     TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
  306.   TIM_Cmd(TIM6, ENABLE);
  307. }

  308. /**
  309.   * @brief Pause wave playing
  310.   * @param None
  311.   * @retval None
  312.   */
  313. void WavPlayerPause(void)
  314. {
  315.     //DMA_ITConfig(DMA2_Channel3, DMA_IT_TC | DMA_IT_HT, DISABLE);
  316.     TIM_ITConfig(TIM6, TIM_IT_Update, DISABLE);
  317.   TIM_Cmd(TIM6, DISABLE);
  318. }

阅读(3347) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~