找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6332|回复: 3
收起左侧

STM32音乐盒生日快乐播放源码

[复制链接]
ID:251113 发表于 2017-11-19 16:55 | 显示全部楼层 |阅读模式
stm32电子工艺实习作品
0.png 生日快乐歌.jpg
单片机源程序如下:
  1. /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
  2. * File Name          : main.c
  3. * Author             : MCD Application Team
  4. * Version            : V1.0
  5. * Date               : 10/08/2007
  6. * Description        : Main program body
  7. ********************************************************************************
  8. * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/

  15. /* Includes ------------------------------------------------------------------*/
  16. #include "stm32f10x_lib.h"
  17. #include "math.h"
  18. #include "stdio.h"

  19. int fputc(int ch,FILE *f);

  20. /* Private typedef -----------------------------------------------------------*/
  21. /* Private define ------------------------------------------------------------*/
  22. #define ADC1_DR_Address    ((u32)0x4001244C)

  23. //ADC1 4001 2400H
  24. //ADC_DR偏移地址 4CH

  25. volatile u16 ADC_Ch0_Res, ADC_Ch1_Res, ADC_Ch2_Res, ADC_Ch3_Res;
  26. volatile u16 ADC_RegularConvertedValueTab[1];
  27. u32 ADC_Calibration_DR;
  28. u32 shownum=0;




  29. /* Private macro -------------------------------------------------------------*/
  30. /* Private variables ---------------------------------------------------------*/

  31. ADC_InitTypeDef ADC_InitStructure;

  32. DMA_InitTypeDef DMA_InitStructure;

  33. USART_InitTypeDef USART_InitStructure;

  34. USART_ClockInitTypeDef  USART_ClockInitStructure;

  35. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  36. TIM_OCInitTypeDef  TIM_OCInitStructure;

  37. ErrorStatus HSEStartUpStatus;



  38. /* Private function prototypes -----------------------------------------------*/

  39. void USART_Configuration(void);
  40. void DMA_Configuration(void);
  41. void ADC_Configuration(void);
  42. void RCC_Configuration(void);
  43. void GPIO_Configuration(void);
  44. void NVIC_Configuration(void);
  45. void TIM1_Configuration(void);
  46. void TIM2_Configuration(void);
  47. void Delay(vu32 nCount);

  48. /* Private functions ---------------------------------------------------------*/

  49. /*******************************************************************************
  50. * Function Name  : main
  51. * Description    : Main program
  52. * Input          : None
  53. * Output         : None
  54. * Return         : None
  55. *******************************************************************************/
  56. int main(void)
  57. {
  58. //        u16 i=0;
  59. //        u32 sum=0;
  60.   #ifdef DEBUG
  61.     debug();
  62.   #endif

  63.   /* System Clocks Configuration */
  64.   RCC_Configuration();

  65.   /* NVIC configuration */
  66.   NVIC_Configuration();

  67.   /* Configure the GPIO ports */
  68.   GPIO_Configuration();

  69.   /* USART1 configuration ------------------------------------------------------*/
  70.   USART_Configuration();

  71.   /* DMA Channel1 Configuration ----------------------------------------------*/
  72.    DMA_Configuration();
  73.   
  74.   
  75.   /* ADC1 configuration ------------------------------------------------------*/
  76.   ADC_Configuration();

  77.   TIM1_Configuration();
  78.   TIM2_Configuration();




  79.   
  80.   while(1)
  81.   {                        
  82.    
  83. }

  84.   
  85. }

  86. void TIM1_Configuration(void)
  87. {
  88. /* ---------------------------------------------------------------
  89.   TIM2 Configuration: Output Compare Inactive Mode:
  90.   TIM2CLK = 36 MHz, Prescaler = 35999, TIM2 counter clock = 1 KHz
  91. --------------------------------------------------------------- */
  92.   /* Time base configuration */
  93.   TIM_TimeBaseStructure.TIM_Period = 300-1;           //定时周期
  94.   // T = (TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK
  95.   //   = (499+1)*(35999+1)/36MHz = 0.5s      
  96.   TIM_TimeBaseStructure.TIM_Prescaler = 36000-1;     //预分频因子     
  97.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;     //定时器分频因子
  98.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //定时器计数模式
  99.   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  100.   
  101.   TIM_ARRPreloadConfig(TIM1, ENABLE);//自动装载寄存器使能

  102.   /* TIM IT enable */
  103.   TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

  104.   /* TIM2 enable counter */
  105.   TIM_Cmd(TIM1, ENABLE);  //计数器控制寄存器TIMx->CR1中的计数器使能位(CEN)  

  106. }

  107. void TIM2_Configuration(void)
  108. {
  109. /* ---------------------------------------------------------------
  110.   TIM2 Configuration: Output Compare Inactive Mode:
  111.   TIM2CLK = 36 MHz, Prescaler = 35999, TIM2 counter clock = 1 KHz
  112. --------------------------------------------------------------- */
  113.   /* Time base configuration */
  114.   TIM_TimeBaseStructure.TIM_Period = 2;           //定时周期
  115.   // T = (TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK
  116.   //   = (499+1)*(35999+1)/36MHz = 0.5s      
  117.   TIM_TimeBaseStructure.TIM_Prescaler = 36000-1;     //预分频因子     
  118.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;     //定时器分频因子
  119.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //定时器计数模式
  120.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  121.   
  122.   TIM_ARRPreloadConfig(TIM2, ENABLE);//自动装载寄存器使能

  123.   /* TIM IT enable */
  124.   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

  125.   /* TIM2 enable counter */
  126.   TIM_Cmd(TIM2, ENABLE);  //计数器控制寄存器TIMx->CR1中的计数器使能位(CEN)  

  127. }


  128. void USART_Configuration(void)
  129. {
  130.   /* USART1 configured as follow:
  131.         - BaudRate = 38400 baud
  132.         - Word Length = 8 Bits
  133.         - One Stop Bit
  134.         - No parity
  135.         - Hardware flow control disabled (RTS and CTS signals)
  136.         - Receive and transmit enabled
  137.         - USART Clock disabled
  138.         - USART CPOL: Clock is active low
  139.         - USART CPHA: Data is captured on the second edge
  140.         - USART LastBit: The clock pulse of the last data bit is not output to
  141.                          the SCLK pin
  142.   */
  143.   USART_InitStructure.USART_BaudRate = 38400;//设置波特率为38400
  144.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;// 8位传输
  145.   USART_InitStructure.USART_StopBits = USART_StopBits_1; //1个停止位
  146.   USART_InitStructure.USART_Parity = USART_Parity_No;    //无校验位
  147.   USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  148.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  149.   USART_Init(USART1, &USART_InitStructure);
  150.   // 禁止硬件流控制,禁止RTS和CTS信号
  151.   // 允许接收、发送
  152.   /*
  153.   USART_InitStructure.USART_Clock = USART_Clock_Disable;//串口时钟禁止
  154.   USART_InitStructure.USART_CPOL = USART_CPOL_Low;
  155.   USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
  156.   USART_InitStructure.USART_LastBit = USART_LastBit_Disable;  */
  157.   
  158.     /* Configure the USART1  配置串口的波特率,校验位,停止位和时钟等*/  


  159.     USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  160.     USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  161.     USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge;
  162.     USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  163.     USART_ClockInit(USART1 , &USART_ClockInitStructure);

  164.   /* Enable USART1  使能串口1*/
  165.   USART_Cmd(USART1, ENABLE);
  166.   
  167. }


  168. void DMA_Configuration(void)
  169. {
  170.   DMA_DeInit(DMA1_Channel1);//复位开启DMA1的第一通道
  171.   //DMA对应的外设基地址
  172.   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  173.   
  174.   //序列1转换结果放在ADC_RegularConvertedValueTab[0],序列2……
  175.   DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_RegularConvertedValueTab;  
  176.   //DMA的转换模式:SRC模式,从外设向内存中传送数据
  177.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  178.   DMA_InitStructure.DMA_BufferSize = 1;
  179.   //BufferSize = 4,ADC转换序列有4个通道
  180.   
  181.   //接收一次数据后,设备地址是否后移
  182.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  183.   //接收一次数据后,目标内存地址自动后移,用来采集多个数据
  184.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

  185.   //转换结果的数据大小  
  186.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  187.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  188.   
  189. //转换模式:常用循环缓存模式。Buffer写满后,自动回到初始地址开始传输
  190.   //另外一种Normal模式:不循环,仅一次DMA
  191.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  192.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMA优先级,高
  193.   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//内存到内存模式禁止
  194.   DMA_Init(DMA1_Channel1, &DMA_InitStructure);

  195.   /* Enable DMA channel1 */
  196.   DMA_Cmd(DMA1_Channel1, ENABLE);
  197. }

  198. void ADC_Configuration(void)
  199. {
  200.   /* ADC1 configuration ------------------------------------------------------*/
  201.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//每个ADC独立工作
  202.   //ADC扫描所有规则转换通道ADC_SQRx和注入转换通道ADC_JSQR
  203.   ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  204.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//连续转换模式开启
  205.   
  206.   /*关闭ADC外部触发,即禁止由外部触发模数转换*/
  207.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  208.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//转换数据右对齐
  209.   ADC_InitStructure.ADC_NbrOfChannel = 1;   //开启4个通道
  210.   ADC_Init(ADC1, &ADC_InitStructure);      //调用固件库函数完成初始化

  211.   /* ADC1 regular channel configuration */ //采样周期为239.5个周期
  212.   ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_239Cycles5);

  213.   //转换周期不能太短,否则CPU基本一直处于ADC中断状态

  214.   /* Enable ADC1 DMA */
  215.   ADC_DMACmd(ADC1, ENABLE);/*  使能ADC1  DMA*/

  216.   /* Enable ADC1 ECO*/ //ADC转换完成中断使能
  217. // ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
  218.   
  219.   /* Enable ADC1 */
  220.   ADC_Cmd(ADC1, ENABLE);

  221.     /* Enable ADC1 reset calibaration register */
  222.   ADC_ResetCalibration(ADC1);// ADC1 复位校准
  223.   
  224.   /* Check the end of ADC1 reset calibration register */
  225.   while(ADC_GetResetCalibrationStatus(ADC1)); //等待校准寄存器初始化

  226.   /* Start ADC1 calibaration */
  227.   ADC_StartCalibration(ADC1);//开始校准
  228.   
  229.   /* Check the end of ADC1 calibration */
  230.   while(ADC_GetCalibrationStatus(ADC1));//等到校准完成

  231.   ADC_Calibration_DR = ADC1->DR;//保存校准码

  232.   /* Start ADC1 Software Conversion */
  233.   ADC_SoftwareStartConvCmd(ADC1, ENABLE);//软件启动ADC1进行连续转换

  234.   //ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
  235. }


  236. /*******************************************************************************
  237. * Function Name  : RCC_Configuration
  238. * Description    : Configures the different system clocks.
  239. * Input          : None
  240. * Output         : None
  241. * Return         : None
  242. *******************************************************************************/
  243. void RCC_Configuration(void)
  244. {                                                                                                                               
  245.   /* RCC system reset(for debug purpose) */
  246.   RCC_DeInit();

  247.   /* Enable HSE */

  248. // RCC_HSICmd(ENABLE);
  249.   RCC_HSEConfig(RCC_HSE_ON);
  250. // RCC_LSEConfig(RCC_LSE_OFF);

  251.   while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
  252.   /* Wait till HSE is ready */
  253.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  254.   if(HSEStartUpStatus == SUCCESS)
  255.   {
  256.     /* Enable Prefetch Buffer */
  257.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  258.     /* Flash 2 wait state */
  259.     FLASH_SetLatency(FLASH_Latency_2);

  260.     /* HCLK = SYSCLK */
  261.     RCC_HCLKConfig(RCC_SYSCLK_Div1);

  262.     /* PCLK2 = HCLK */
  263.     RCC_PCLK2Config(RCC_HCLK_Div1);

  264.     /* PCLK1 = HCLK/2 */
  265.     RCC_PCLK1Config(RCC_HCLK_Div2);

  266.     /* PLLCLK = 8MHz * 9 = 72 MHz */
  267.     RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9);

  268.     /* Enable PLL */
  269.     RCC_PLLCmd(ENABLE);

  270.     /* Wait till PLL is ready */
  271.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  272.     {
  273.     }

  274.     /* Select PLL as system clock source */
  275.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  276.     /* Wait till PLL is used as system clock source */
  277.     while(RCC_GetSYSCLKSource() != 0x08)
  278.     {
  279.     }
  280. }

  281.   /* Enable DMA clock */
  282.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);


  283.   /* Enable GPIOA  GPIOB USART1 GPIOC and ADC1 clocks */
  284.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1
  285.                          | RCC_APB2Periph_GPIOC | RCC_APB2Periph_ADC1
  286.                          | RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOD
  287.                                                  | RCC_APB2Periph_TIM1, ENABLE);
  288.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  289.   
  290.   RCC_ADCCLKConfig(RCC_PCLK2_Div6);
  291. }

  292. /*******************************************************************************
  293. * Function Name  : GPIO_Configuration
  294. * Description    : Configures the different GPIO ports.
  295. * Input          : None
  296. * Output         : None
  297. * Return         : None
  298. *******************************************************************************/
  299. void GPIO_Configuration(void)
  300. {
  301.   GPIO_InitTypeDef GPIO_InitStructure;

  302.   /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  303.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  304.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  305.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  306.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  307.   /* Configure USART1 Rx (PA.10) as input floating */
  308.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  309.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  310.   GPIO_Init(GPIOA, &GPIO_InitStructure);


  311.   /* Configure PA4 PA5 PA6 and PA7
  312.   (ADC Channel4 Channel5 Channel6 and Channel7) as analog input */
  313.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  314.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  315.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  316.   

  317.   
  318.   /* GPIOA Configuration: Pin8 in Output */
  319.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  320.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  321.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
  322.   GPIO_Init(GPIOB, &GPIO_InitStructure);  
  323.   
  324.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  325.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  326.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
  327.   GPIO_Init(GPIOC, &GPIO_InitStructure);         
  328.   


  329. }

  330. /*******************************************************************************
  331. * Function Name  : NVIC_Configuration
  332. * Description    : Configures the nested vectored interrupt controller.
  333. * Input          : None
  334. * Output         : None
  335. * Return         : None
  336. *******************************************************************************/
  337. void NVIC_Configuration(void)
  338. {
  339.   NVIC_InitTypeDef NVIC_InitStructure;

  340. #ifdef  VECT_TAB_RAM
  341.   /* Set the Vector Table base location at 0x20000000 */
  342.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  343. #else  /* VECT_TAB_FLASH  */
  344.   /* Set the Vector Table base location at 0x08000000 */
  345.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  346. #endif

  347.     // Enable ADC IRQChannel
  348.   NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel;
  349.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  350.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  351.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  352.   NVIC_Init(&NVIC_InitStructure);

  353.   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
  354.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  355. ……………………

  356. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
生日快乐播放.rar (494.66 KB, 下载次数: 104)
回复

使用道具 举报

ID:402927 发表于 2018-10-31 16:11 | 显示全部楼层
谢谢楼主
回复

使用道具 举报

ID:444864 发表于 2018-12-19 12:22 | 显示全部楼层
怎么我烧进去没有歌播放
回复

使用道具 举报

ID:501736 发表于 2019-12-25 14:38 | 显示全部楼层
怎么会有错误
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表