找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 16652|回复: 4
打印 上一主题 下一主题
收起左侧

STM32F103 AD转换程序(注释很详细)

[复制链接]
跳转到指定楼层
楼主
ID:366276 发表于 2018-7-6 14:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM32F103 AD转换程序
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f10x.h"
  3. #include "stm32_eval.h"
  4. #include "stm32f10x_conf.h"
  5. #include <stdio.h>



  6. //u* Private typedef -----------------------------------------------------------*/
  7. /* Private define ------------------------------------------------------------*/
  8. #define DR_ADDRESS                  ((uint32_t)0x4001244C) //ADC1 DR寄存器基地址
  9. /* Private macro -------------------------------------------------------------*/
  10. /* Private variables ---------------------------------------------------------*/
  11. USART_InitTypeDef USART_InitStructure;    //串口初始化结构体声明
  12. ADC_InitTypeDef ADC_InitStructure;        //ADC初始化结构体声明
  13. DMA_InitTypeDef DMA_InitStructure;        //DMA初始化结构体声明
  14. __IO uint16_t ADCConvertedValue;     // ADC为12位模数转换器,只有ADCConvertedValue的低12位有效


  15. /* Private function prototypes -----------------------------------------------*/
  16. void ADC_GPIO_Configuration(void);

  17. #ifdef __GNUC__
  18.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  19.      set to 'Yes') calls __io_putchar() */
  20.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  21. #else
  22.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  23. #endif /* __GNUC__ */

  24. /* Private functions ---------------------------------------------------------*/
  25. static void Delay_ARMJISHU(__IO uint32_t nCount)
  26. {
  27.   for (; nCount != 0; nCount--);
  28. }

  29. //==================================================================
  30. //变量定义
  31. double  Str_V,End_V,In_V,Pwm;
  32.   u16 ADCConvertedValueLocal;
  33. int main(void)
  34. {

  35.   
  36. //USAT\RT1===============================================================================
  37.   USART_InitStructure.USART_BaudRate = 115200;
  38.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  39.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  40.   USART_InitStructure.USART_Parity = USART_Parity_No;
  41.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  42.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  43.   STM_EVAL_COMInit(COM1, &USART_InitStructure);

  44.   /* Output a message on Hyperterminal using printf function */
  45.   printf("\n\r\n\r\n\r\n\r");

  46.   printf("\n\r **************华蓝盾公司************************");
  47.   printf("\n\r");
  48.   printf("\n\r ==============直流电机驱动======================= \n\r");
  49.   printf("\n\r");
  50.   
  51.   
  52.   /* Enable DMA1 clock */
  53.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);                 //使能DMA时钟
  54.   
  55.   /* DMA1 channel1 configuration ----------------------------------------------*/
  56.   DMA_DeInit(DMA1_Channel1);                  //开启DMA1的第一通道
  57.   DMA_InitStructure.DMA_PeripheralBaseAddr = DR_ADDRESS;                  //DMA对应的外设基地址
  58.   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;   //内存存储基地址
  59.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;        //DMA的转换模式为SRC模式,由外设搬移到内存
  60.   DMA_InitStructure.DMA_BufferSize = 1;                   //DMA缓存大小,1个
  61.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;        //接收一次数据后,设备地址禁止后移
  62.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;        //关闭接收一次数据后,目标内存地址后移
  63.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;  //定义外设数据宽度为16位
  64.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;  //DMA搬移数据尺寸,HalfWord就是为16位
  65.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;   //转换模式,循环缓存模式。
  66.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;        //DMA优先级高
  67.   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;                  //M2M模式禁用
  68.   DMA_Init(DMA1_Channel1, &DMA_InitStructure);         
  69.   /* Enable DMA1 channel1 */
  70.   DMA_Cmd(DMA1_Channel1, ENABLE);
  71.   
  72.   /* Enable ADC1 and GPIOC clock */
  73.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);          //使能ADC和GPIOC时钟
  74.   /* ADC1 configuration ------------------------------------------------------*/
  75.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;                //独立的转换模式
  76.   ADC_InitStructure.ADC_ScanConvMode = ENABLE;                  //开启扫描模式
  77.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;   //开启连续转换模式
  78.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;        //ADC外部开关,关闭状态
  79.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;   //对齐方式,ADC为12位中,右对齐方式
  80.   ADC_InitStructure.ADC_NbrOfChannel = 1;         //开启通道数,1个
  81.   ADC_Init(ADC1, &ADC_InitStructure);
  82.   /* ADC1 regular channel13 configuration */
  83.   ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_55Cycles5);  ///13
  84.                           //ADC通道组, 第13个通道 采样顺序1,转换时间
  85.   /* Enable ADC1 DMA */
  86.   ADC_DMACmd(ADC1, ENABLE);          //ADC命令,使能
  87.   /* Enable ADC1 */
  88.   ADC_Cmd(ADC1, ENABLE);  //开启ADC1
  89.   
  90.   /* Enable ADC1 reset calibaration register */   
  91.   ADC_ResetCalibration(ADC1);          //重新校准
  92.   /* Check the end of ADC1 reset calibration register */
  93.   while(ADC_GetResetCalibrationStatus(ADC1));  //等待重新校准完成
  94.   /* Start ADC1 calibaration */
  95.   ADC_StartCalibration(ADC1);                //开始校准
  96.   /* Check the end of ADC1 calibration */
  97.   while(ADC_GetCalibrationStatus(ADC1));           //等待校准完成
  98.   /* Start ADC1 Software Conversion */
  99.   ADC_SoftwareStartConvCmd(ADC1, ENABLE);        //连续转换开始,ADC通过DMA方式不断的更新RAM区。
  100.   //=============================================================================================================
  101.          Str_V =0.5;
  102.          End_V =2.5;
  103.   while (1)
  104.   {
  105.     ADCConvertedValueLocal = ADCConvertedValue;

  106.      In_V =         3.3* ADCConvertedValueLocal/4096  ;         //计算等效电平
  107.          printf("\r\n 电压值1:%1.3f",In_V );
  108.         
  109.         if (In_V <= 0.5)
  110.         {
  111.           Pwm =0;
  112.          printf("\r\n 当前脉宽= %0.3f"  ,Pwm );

  113.         }

  114.         if( In_V>=2.5 )
  115.         {
  116.                 Pwm =0.9;

  117.           printf("\r\n 当前脉宽= %0.3f"  ,Pwm );
  118.     }

  119.         if( (In_V <2.5 )         &&  (In_V >0.5))
  120.         {
  121.           Pwm = 0.9*(In_V-0.5)/2        ;                                                                                       
  122.                     printf("\r\n 当前脉宽= %0.3f"  ,Pwm );
  123.     }



  124.     Delay_ARMJISHU(8000000);
  125.   }
  126. }

  127. /**
  128.   * @brief  Retargets the C library printf function to the USART.
  129.   * @param  None
  130.   * @retval None
  131.   */
  132. PUTCHAR_PROTOTYPE
  133. {
  134.   /* Place your implementation of fputc here */
  135.   /* e.g. write a character to the USART */
  136.   USART_SendData(EVAL_COM1, (uint8_t) ch);

  137.   /* Loop until the end of transmission */
  138.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  139.   {}

  140.   return ch;
  141. }

  142. #ifdef  USE_FULL_ASSERT

  143. /**
  144.   * @brief  Reports the name of the source file and the source line number
  145.   *         where the assert_param error has occurred.
  146.   * @param  file: pointer to the source file name
  147.   * @param  line: assert_param error line source number
  148.   * @retval None
  149.   */
  150. void assert_failed(uint8_t* file, uint32_t line)
  151. {
  152.   /* User can add his own implementation to report the file name and line number,
  153.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  154.   /* Infinite loop */
  155.   while (1)
  156.   {
  157.   }
  158. }
  159. #endif

  160. /**
  161.   * @brief  Configures the different GPIO ports.
  162.   * @param  None
  163.   * @retval None
  164.   */
  165. void ADC_GPIO_Configuration(void)                                                  //ADC配置函数
  166. {
  167.   GPIO_InitTypeDef GPIO_InitStructure;

  168.   /* Configure PC.00 (ADC Channel10) as analog input -------------------------*/
  169.   //PC0 作为模拟通道10输入引脚                        
  170.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;     //管脚1
  171.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//输入模式
  172.   GPIO_Init(GPIOC, &GPIO_InitStructure);     //GPIO组
  173. }

  174. /**
  175.   * @}
  176.   */

  177. /**
  178.   * @}
  179.   */

  180. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
复制代码


AD转换.rar

252.64 KB, 下载次数: 99, 下载积分: 黑币 -5

评分

参与人数 1黑币 +3 收起 理由
stu + 3 很给力!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:433804 发表于 2018-12-3 21:44 | 只看该作者
太复杂了,ad不是一个很麻烦的东西吧,这个程序感觉对菜鸟一点都不友好
回复

使用道具 举报

板凳
ID:433680 发表于 2018-12-5 11:49 | 只看该作者

感谢楼主的分享。
回复

使用道具 举报

地板
ID:347379 发表于 2019-1-16 20:20 | 只看该作者
总算是看完了,但是得消化一下
回复

使用道具 举报

5#
ID:87000 发表于 2019-1-17 08:40 | 只看该作者
只可惜时单路的ADC
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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