找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1219|回复: 0
收起左侧

stm32 ADC串口发送源码 通过DMA通道1读取

[复制链接]
ID:409950 发表于 2018-10-15 15:01 | 显示全部楼层 |阅读模式
利用ADC的第14通道对开发板上单圈电位器的电压值作AD转换,采用连续转换模式,转换结果通过DMA通道1读取。ADC转换的结果,每间隔1秒钟向串口发送一次。

单片机源程序如下:
  1. /*******************************************************************************
  2. * File Name          : main.c
  3. * Author             : Wuhan R&D Center, Embest
  4. * Description        : Main program body
  5. ********************************************************************************/

  6. /* Includes ------------------------------------------------------------------*/
  7. #include "stm32f10x_lib.h"
  8. #include"stdio.h"

  9. /************ 用于定义ITM Viewer相关的ITM激励寄存器端口 ************************/
  10. #define ITM_Port8(n)    (*((volatile unsigned char *)(0xE0000000+4*n)))
  11. #define ITM_Port16(n)   (*((volatile unsigned short*)(0xE0000000+4*n)))
  12. #define ITM_Port32(n)   (*((volatile unsigned long *)(0xE0000000+4*n)))

  13. #define DEMCR           (*((volatile unsigned long *)(0xE000EDFC)))
  14. #define TRCENA          0x01000000

  15. /*用于定义是否使用ITM Viewer*/
  16. //#define DBG_ITM   

  17. /* Private typedef -----------------------------------------------------------*/
  18. /* Private define ------------------------------------------------------------*/
  19. #define ADC1_DR_Address    ((u32)0x4001244C)
  20. int AD_value;
  21. static unsigned long ticks;
  22. unsigned char Clock1s;

  23. /* Private macro -------------------------------------------------------------*/
  24. /* Private variables ---------------------------------------------------------*/
  25. ADC_InitTypeDef ADC_InitStructure;
  26. DMA_InitTypeDef DMA_InitStructure;
  27. vu16 ADC_ConvertedValue;
  28. ErrorStatus HSEStartUpStatus;
  29.    
  30. /* Private function prototypes -----------------------------------------------*/
  31. void RCC_Configuration(void);
  32. void GPIO_Configuration(void);
  33. void NVIC_Configuration(void);
  34. void USART_Configuration1(void);
  35. void SetupClock (void);
  36. int fputc(int ch, FILE *f);  
  37. /* Private functions ---------------------------------------------------------*/

  38. /*******************************************************************************
  39. * Function Name  : main
  40. * Description    : Main program
  41. * Input          : None
  42. * Output         : None
  43. * Return         : None
  44. *******************************************************************************/
  45. int main(void)
  46. {
  47. #ifdef DEBUG
  48.   debug();
  49. #endif

  50.   /* System clocks configuration ---------------------------------------------*/
  51.   RCC_Configuration();

  52.   /* NVIC configuration ------------------------------------------------------*/
  53.   NVIC_Configuration();

  54.   /* GPIO configuration ------------------------------------------------------*/
  55.   GPIO_Configuration();
  56.    
  57.    /* Configure the USART1 */
  58.   USART_Configuration1();

  59.   printf("\r\n USART1 print AD_value -------------------------- \r\n");

  60.   /* DMA channel1 configuration ----------------------------------------------*/
  61.   DMA_DeInit(DMA1_Channel1);
  62.   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  63.   DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
  64.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  65.   DMA_InitStructure.DMA_BufferSize = 1;
  66.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  67.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  68.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  69.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  70.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  71.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  72.   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  73.   DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  74.   
  75.   /* Enable DMA channel1 */
  76.   DMA_Cmd(DMA1_Channel1, ENABLE);
  77.      
  78.   /* ADC1 configuration ------------------------------------------------------*/
  79.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  80.   ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  81.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  82.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  83.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  84.   ADC_InitStructure.ADC_NbrOfChannel = 1;
  85.   ADC_Init(ADC1, &ADC_InitStructure);

  86.   /* ADC1 regular channel14 configuration */
  87.   ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_55Cycles5);

  88.   /* Enable ADC1 DMA */
  89.   ADC_DMACmd(ADC1, ENABLE);
  90.   
  91.   /* Enable ADC1 */
  92.   ADC_Cmd(ADC1, ENABLE);

  93.   /* Enable ADC1 reset calibaration register */   
  94.   ADC_ResetCalibration(ADC1);
  95.   /* Check the end of ADC1 reset calibration register */
  96.   while(ADC_GetResetCalibrationStatus(ADC1));

  97.   /* Start ADC1 calibaration */
  98.   ADC_StartCalibration(ADC1);
  99.   /* Check the end of ADC1 calibration */
  100.   while(ADC_GetCalibrationStatus(ADC1));
  101.      
  102.   /* Start ADC1 Software Conversion */
  103.   ADC_SoftwareStartConvCmd(ADC1, ENABLE);    /*使能ADC1软件开始转换*/

  104. while(1)
  105.   {
  106.   AD_value=ADC_GetConversionValue(ADC1);
  107.    if (ticks++ >= 900000) {                  /* Set Clock1s to 1 every 1 second    */
  108.     ticks   = 0;
  109.     Clock1s = 1;
  110.   }

  111.    /* Printf message with AD value to serial port every 1 second             */
  112.     if (Clock1s) {
  113.       Clock1s = 0;
  114.       printf("The current AD value = 0x%04X \r\n", AD_value);
  115.     }
  116.   
  117.   }
  118. }

  119. /*******************************************************************************
  120. * Function Name  : RCC_Configuration
  121. * Description    : Configures the different system clocks.
  122. * Input          : None
  123. * Output         : None
  124. * Return         : None
  125. *******************************************************************************/
  126. void RCC_Configuration(void)
  127. {
  128.   /* RCC system reset(for debug purpose) */
  129.   RCC_DeInit();

  130.   /* Enable HSE */
  131.   RCC_HSEConfig(RCC_HSE_ON);

  132.   /* Wait till HSE is ready */
  133.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  134.   if(HSEStartUpStatus == SUCCESS)
  135.   {
  136.     /* HCLK = SYSCLK */
  137.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
  138.   
  139.     /* PCLK2 = HCLK */
  140.     RCC_PCLK2Config(RCC_HCLK_Div1);

  141.     /* PCLK1 = HCLK/2 */
  142.     RCC_PCLK1Config(RCC_HCLK_Div2);

  143.     /* ADCCLK = PCLK2/4 */
  144.     RCC_ADCCLKConfig(RCC_PCLK2_Div4);
  145.   
  146.     /* Flash 2 wait state */
  147.     FLASH_SetLatency(FLASH_Latency_2);         /*设置代码延时值(参数里的是指2延时周期)*/
  148.     /* Enable Prefetch Buffer */
  149.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);      /*使能或者失能预取指缓存*/

  150.     /* PLLCLK = 8MHz * 9 = 56 MHz */
  151.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  152.     /* Enable PLL */
  153.     RCC_PLLCmd(ENABLE);

  154.     /* Wait till PLL is ready */
  155.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  156.     {
  157.     }

  158.     /* Select PLL as system clock source */
  159.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  160.     /* Wait till PLL is used as system clock source */
  161.     while(RCC_GetSYSCLKSource() != 0x08)
  162.     {
  163.     }
  164.   }

  165. /* Enable peripheral clocks --------------------------------------------------*/
  166.   /* Enable DMA clock */
  167.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  168.   /* Enable ADC1 and GPIOC clock */
  169.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);

  170.   /* Enable USART1 and GPIOA clock */
  171.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  172. }

  173. /*******************************************************************************
  174. * Function Name  : GPIO_Configuration
  175. * Description    : Configures the different GPIO ports.
  176. * Input          : None
  177. * Output         : None
  178. * Return         : None
  179. *******************************************************************************/
  180. void GPIO_Configuration(void)
  181. {
  182.   GPIO_InitTypeDef GPIO_InitStructure;

  183.   /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
  184.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  185.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  186.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  187.    /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  188.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  189.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  190.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  191.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  192.    
  193.   /* Configure USART1 Rx (PA.10) as input floating */
  194.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  195.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  196.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  197. }

  198. /*******************************************************************************
  199. * Function Name  : NVIC_Configuration
  200. * Description    : Configures Vector Table base location.
  201. * Input          : None
  202. * Output         : None
  203. * Return         : None
  204. *******************************************************************************/
  205. void NVIC_Configuration(void)
  206. {
  207. #ifdef  VECT_TAB_RAM  
  208.   /* Set the Vector Table base location at 0x20000000 */
  209.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  210. #else  /* VECT_TAB_FLASH  */
  211.   /* Set the Vector Table base location at 0x08000000 */
  212.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
  213. #endif
  214. }

  215. #ifdef  DEBUG
  216. /*******************************************************************************
  217. * Function Name  : assert_failed
  218. * Description    : Reports the name of the source file and the source line number
  219. *                  where the assert error has occurred.
  220. * Input          : - file: pointer to the source file name
  221. *                  - line: assert error line source number
  222. * Output         : None
  223. * Return         : None
  224. *******************************************************************************/
  225. void assert_failed(u8* file, u32 line)
  226. {
  227.   /* User can add his own implementation to report the file name and line number,
  228.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  229.   /* Infinite loop */
  230.   while (1)
  231.   {
  232.   }
  233. }
  234. #endif
  235. /*******************************************************************************
  236. * Function Name  : USART_Configuration
  237. * Description    : Configures the USART1.
  238. * Input          : None
  239. * Output         : None
  240. * Return         : None
  241. *******************************************************************************/
  242. void USART_Configuration1(void)
  243. {
  244.   USART_InitTypeDef USART_InitStructure;
  245.   USART_ClockInitTypeDef  USART_ClockInitStructure;
  246. /* USART1 configuration ------------------------------------------------------*/
  247.   /* USART1 configured as follow:
  248.         - BaudRate = 115200 baud  
  249.         - Word Length = 8 Bits
  250.         - One Stop Bit
  251.         - No parity
  252.         - Hardware flow control disabled (RTS and CTS signals)
  253.         - Receive and transmit enabled
  254.         - USART Clock disabled
  255.         - USART CPOL: Clock is active low
  256.         - USART CPHA: Data is captured on the middle
  257.         - USART LastBit: The clock pulse of the last data bit is not output to
  258.                          the SCLK pin
  259.   */
  260.   USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  261. USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  262. USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  263. USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  264. /* Configure the USART1 synchronous paramters */
  265. USART_ClockInit(USART1, &USART_ClockInitStructure);

  266. USART_InitStructure.USART_BaudRate = 115200;
  267. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  268. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  269. USART_InitStructure.USART_Parity = USART_Parity_No ;
  270. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;


  271. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  272. /* Configure USART1 basic and asynchronous paramters */
  273. USART_Init(USART1, &USART_InitStructure);
  274.   /* Enable USART1 */
  275.   USART_Cmd(USART1, ENABLE);
  276. }

  277. /*******************************************************************************
  278. * Function Name  : fputc
  279. * Description    : Retargets the C library printf function to the USART or ITM Viewer.
  280. * Input          : None
  281. * Output         : None
  282. * Return         : None
  283. *******************************************************************************/

  284. int fputc(int ch, FILE *f)
  285. {
  286. #ifdef DBG_ITM
  287. /* 将Printf内容发往ITM激励寄存器端口  */
  288.   if (DEMCR & TRCENA) {
  289.     while (ITM_Port32(0) == 0);
  290.     ITM_Port8(0) = ch;
  291.   }
  292. #else  
  293. /* 将Printf内容发往串口 */
  294.   USART_SendData(USART1, (unsigned char) ch);
  295.   while (!(USART1->SR & USART_FLAG_TXE));
  296. #endif  
  297.   return (ch);
  298. }


  299. /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
ADC_test.zip (638.43 KB, 下载次数: 23)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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