找回密码
 注册

QQ登录

只需一步,快速开始

搜索

STM32 ADC多通道 关键代码

[复制链接]
coolice 发表于 2021-8-13 18:30:53 | 显示全部楼层 |阅读模式
  1. #==========================================================

  2. define ADC1_DR_Address    ((u32)0x4001244C)

  3. vu16 AD_Value[2];


  4. ==============================================================

  5. 关键代码

  6. void ADC1_Configuration(void)
  7. {
  8.     ADC_InitTypeDef ADC_InitStructure;

  9.     ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  10.     ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  11.     ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换开启
  12.     ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  13.     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  14.     ADC_InitStructure.ADC_NbrOfChannel = 2;     //设置转换序列长度为2
  15.     ADC_Init(ADC1, &ADC_InitStructure);
  16.    
  17.     //ADC内置温度传感器使能(要使用片内温度传感器,切忌要开启它)
  18.     ADC_TempSensorVrefintCmd(ENABLE);
  19.    
  20.     //常规转换序列1:通道10
  21.     ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_13Cycles5);
  22.     //常规转换序列2:通道16(内部温度传感器),采样时间>2.2us,(239cycles)
  23.     ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 2, ADC_SampleTime_239Cycles5);
  24.    
  25.     // Enable ADC1
  26.     ADC_Cmd(ADC1, ENABLE);
  27.     // 开启ADC的DMA支持(要实现DMA功能,还需独立配置DMA通道等参数)
  28.     ADC_DMACmd(ADC1, ENABLE);
  29.    
  30.     // 下面是ADC自动校准,开机后需执行一次,保证精度
  31.     // Enable ADC1 reset calibaration register
  32.     ADC_ResetCalibration(ADC1);
  33.     // Check the end of ADC1 reset calibration register
  34.     while(ADC_GetResetCalibrationStatus(ADC1));

  35.     // Start ADC1 calibaration
  36.     ADC_StartCalibration(ADC1);
  37.     // Check the end of ADC1 calibration
  38.     while(ADC_GetCalibrationStatus(ADC1));
  39.     // ADC自动校准结束---------------
  40.    
  41. }

  42. /*******************************************************************************
  43. * Function Name : DMA_Configuration
  44. * Description    : DMA设置:从ADC模块自动读转换结果至内存
  45. * Input          : None
  46. * Output         : None
  47. * Return         : None
  48. *******************************************************************************/
  49. void DMA_Configuration(void)
  50. {
  51.     DMA_InitTypeDef DMA_InitStructure;
  52.    
  53.     DMA_DeInit(DMA1_Channel1);
  54.     DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  55.     DMA_InitStructure.DMA_MemoryBaseAddr = (u32)AD_Value;  //AD_Value为数组地址
  56.     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  57.     //BufferSize=2,因为ADC转换序列有2个通道
  58.     //如此设置,使序列1结果放在AD_Value[0],序列2结果放在AD_Value[1]
  59.     DMA_InitStructure.DMA_BufferSize = 2;
  60.     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  61.     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  62.     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  63.     DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  64.     //循环模式开启,Buffer写满后,自动回到初始地址开始传输
  65.     DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  66.     DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  67.     DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  68.     DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  69.     //配置完成后,启动DMA通道
  70.     DMA_Cmd(DMA1_Channel1, ENABLE);
  71. }
复制代码


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

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-26 22:12

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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