STM32 —— 多路ADC采集

来源:互联网 发布:淘宝详情页尺寸多少 编辑:程序博客网 时间:2024/06/16 02:31

[cpp] view plain copy
 print?
  1. #ifndef _ADC_H_  
  2. #define _ADC_H_  
  3.   
  4. #include "type.h"  
  5. #include "debug.h"  
  6.   
  7. void ADCInit(void);  
  8. uint16_t ADCGetData(uint8_t channel);  
  9. uint16_t ADCGetAverage(uint8_t channel, uint8_t times);  
  10.   
  11. #ifndef CONFIG_CAN  
  12. void adc_test(void);  
  13. #endif  
  14.   
  15. #endif /* _ADC_H_ */  
[cpp] view plain copy
 print?
  1. // input1~5 -- 模拟量IO口; input6~10 -- 普通IO口用  
  2. #define ADC_CHANNEL        ADC1  
  3.   
  4. #define AD_INPUT1_GRP      GPIOC  
  5. #define AD_INPUT1_INDEX    GPIO_Pin_0  
  6. #define AD_INPUT2_GRP      GPIOC  
  7. #define AD_INPUT2_INDEX    GPIO_Pin_1  
  8. #define AD_INPUT3_GRP      GPIOC  
  9. #define AD_INPUT3_INDEX    GPIO_Pin_2  
  10. #define AD_INPUT4_GRP      GPIOC  
  11. #define AD_INPUT4_INDEX    GPIO_Pin_3  
  12. #define AD_INPUT5_GRP      GPIOA  
  13. #define AD_INPUT5_INDEX    GPIO_Pin_2  
  14.   
  15. #if 0  
  16. #define AD_INPUT6_GRP      GPIOA  
  17. #define AD_INPUT6_INDEX    GPIO_Pin_3  
  18. #define AD_INPUT7_GRP      GPIOC  
  19. #define AD_INPUT7_INDEX    GPIO_Pin_4  
  20. #define AD_INPUT8_GRP      GPIOC  
  21. #define AD_INPUT8_INDEX    GPIO_Pin_5  
  22. #define AD_INPUT9_GRP      GPIOB  
  23. #define AD_INPUT9_INDEX    GPIO_Pin_0  
  24. #define AD_INPUT10_GRP     GPIOB  
  25. #define AD_INPUT10_INDEX   GPIO_Pin_1  
  26. #endif  
  27. #define AD_INPUT_CONFIG(gpio, pos)  GPIOConfig(gpio, pos, GPIO_Mode_AIN)  

[cpp] view plain copy
 print?
  1. #include "adc.h"  
  2. #include "stm32f10x.h"  
  3. #include "delay.h"  
  4. #include "target.h"  
  5.   
  6. #define ADC_CHANNEL_NUM  5  
  7.   
  8. static uint16_t ad_value[ADC_CHANNEL_NUM] = {0};  
  9.   
  10. static void adc_gpio_clk_init(void)  
  11. {  
  12.   RCC_ADCCLKConfig(RCC_PCLK2_Div6);  
  13. }  
  14.   
  15. static void adc_gpio_init(void)  
  16. {  
  17.   adc_gpio_clk_init();  
  18.   
  19.   AD_INPUT_CONFIG(AD_INPUT1_GRP, AD_INPUT1_INDEX | AD_INPUT2_INDEX | AD_INPUT3_INDEX | AD_INPUT4_INDEX);  
  20.   AD_INPUT_CONFIG(AD_INPUT5_GRP, AD_INPUT5_INDEX);  
  21.   ADC_DeInit(ADC_CHANNEL);  
  22. }  
  23.   
  24. static void adc_dma_init(void)  
  25. {  
  26.   DMA_InitTypeDef DMA_InitStructure;  
  27.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);  
  28.   
  29.   DMA_DeInit(DMA1_Channel1);  
  30.   
  31.   DMA_InitStructure.DMA_PeripheralBaseAddr  = (u32) & (ADC1->DR);  
  32.   DMA_InitStructure.DMA_MemoryBaseAddr      = (u32)&ad_value;  
  33.   DMA_InitStructure.DMA_DIR                 = DMA_DIR_PeripheralSRC;  
  34.   DMA_InitStructure.DMA_M2M                 = DMA_M2M_Disable;  
  35.   DMA_InitStructure.DMA_PeripheralDataSize  = DMA_PeripheralDataSize_HalfWord;  
  36.   DMA_InitStructure.DMA_MemoryDataSize      = DMA_MemoryDataSize_HalfWord;  
  37.   DMA_InitStructure.DMA_BufferSize          = ADC_CHANNEL_NUM;  
  38.   DMA_InitStructure.DMA_MemoryInc           = DMA_MemoryInc_Enable;  
  39.   DMA_InitStructure.DMA_PeripheralInc       = DMA_PeripheralInc_Disable;  
  40.   DMA_InitStructure.DMA_Mode                = DMA_Mode_Circular;  
  41.   DMA_InitStructure.DMA_Priority            = DMA_Priority_High;  
  42.   DMA_Init(DMA1_Channel1, &DMA_InitStructure);  
  43. }  
  44.   
  45. static void adc_init()  
  46. {  
  47.   ADC_InitTypeDef ADC_InitStructure;  
  48.   
  49.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  
  50.   ADC_InitStructure.ADC_ScanConvMode = ENABLE;  
  51.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  
  52.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  
  53.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;  
  54.   ADC_InitStructure.ADC_NbrOfChannel = ADC_CHANNEL_NUM;  
  55.   ADC_Init(ADC_CHANNEL, &ADC_InitStructure);  
  56.   
  57.   ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);  
  58.   ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);  
  59.   ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);  
  60.   ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);  
  61.   ADC_RegularChannelConfig(ADC_CHANNEL, ADC_Channel_4, 5, ADC_SampleTime_55Cycles5);  
  62.   
  63.   ADC_DMACmd(ADC_CHANNEL, ENABLE);  
  64.   
  65.   ADC_Cmd(ADC_CHANNEL, ENABLE);  
  66.   
  67.   ADC_ResetCalibration(ADC_CHANNEL);  
  68.   
  69.   while(ADC_GetResetCalibrationStatus(ADC_CHANNEL));  
  70.   
  71.   ADC_StartCalibration(ADC_CHANNEL);  
  72.   
  73.   while(ADC_GetCalibrationStatus(ADC_CHANNEL));  
  74. }  
  75.   
  76. static void adc_start(void)  
  77. {  
  78.   ADC_SoftwareStartConvCmd(ADC_CHANNEL, ENABLE); // start convert  
  79.   DMA_Cmd(DMA1_Channel1, ENABLE);  
  80. }  
  81.   
  82. void  ADCInit(void)  
  83. {  
  84.   adc_gpio_init();  
  85.   adc_dma_init();  
  86.   adc_init();  
  87.   adc_start();  
  88. }  
  89.   
  90. uint16_t ADCGetData(uint8_t channel)  
  91. {  
  92.   uint16_t ret = 0;  
  93.   switch(channel)  
  94.   {  
  95.   case ADC_Channel_0:  
  96.     ret = ad_value[0];  
  97.     break;  
  98.   case ADC_Channel_1:  
  99.     ret = ad_value[1];  
  100.     break;  
  101.   case ADC_Channel_2:  
  102.     ret = ad_value[2];  
  103.     break;  
  104.   case ADC_Channel_3:  
  105.     ret = ad_value[3];  
  106.     break;  
  107.   case ADC_Channel_4:  
  108.     ret = ad_value[4];  
  109.     break;  
  110.   }  
  111.   
  112.   return ret;  
  113. }  
  114.   
  115. uint16_t ADCGetAverage(uint8_t channel, uint8_t times)  
  116. {  
  117.   uint16_t value;  
  118.   int i;  
  119.   
  120.   for(i = 0; i < times; ++i)  
  121.   {  
  122.     value += ADCGetData(channel);  
  123.   }  
  124.   
  125.   return (value / times);  
  126. }  
  127.   
  128. #ifndef CONFIG_ADC  
  129. void adc_test(void)  
  130. {  
  131.   uint16_t buffer[ADC_CHANNEL_NUM];  
  132.   float temp[ADC_CHANNEL_NUM];  
  133.   
  134.   buffer[0] = ADCGetData(ADC_Channel_0);  
  135.   temp[0] = (float)buffer[0] * 3.3 / 4096;  
  136.   printf("temp[0]: %f\r\n", temp[0]);  
  137.   
  138.   buffer[1] = ADCGetData(ADC_Channel_1);  
  139.   temp[1] = (float)buffer[1] * 3.3 / 4096;  
  140.   printf("temp[1]: %f\r\n", temp[1]);  
  141.   
  142.   buffer[2] = ADCGetData(ADC_Channel_2);  
  143.   temp[2] = (float)buffer[2] * 3.3 / 4096;  
  144.   printf("temp[2]: %f\r\n", temp[2]);  
  145.   
  146.   buffer[3] = ADCGetData(ADC_Channel_1);  
  147.   temp[3] = (float)buffer[3] * 3.3 / 4096;  
  148.   printf("temp[1]: %f\r\n", temp[3]);  
  149.   
  150.   buffer[4] = ADCGetData(ADC_Channel_4);  
  151.   temp[4] = (float)buffer[4] * 3.3 / 4096;  
  152.   printf("temp[2]: %f\r\n", temp[4]);  
  153. }  
  154. #endif  

from: http://blog.csdn.net/wqx521/article/details/69466440


原创粉丝点击