野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17094|回复: 0

STM32 ADC(一个外部,一个内部温度)

[复制链接]
发表于 2014-10-26 14:37:05 | 显示全部楼层 |阅读模式
本帖最后由 tyboy 于 2014-10-26 19:06 编辑

之前第二组读不出值来。。现在好了,贴不删了。。代码可用。和我一样的菜鸟把第二通运算出温度就可以了。。
  1. #include "adcdma.h"
  2. // __IO uint16_t ADCDATA;
  3. //__IO uint16_t ADCDATA[10][2];
  4. __IO u16 ADCDATA[2];
  5. __IO float ADCDONE[2];
  6. void GPIO_Conf()
  7. {
  8.   GPIO_InitTypeDef GPIO_Conf;
  9.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO  | RCC_APB2Periph_ADC1,ENABLE);
  10.   GPIO_Conf.GPIO_Pin =  GPIO_Pin_1;
  11.   GPIO_Conf.GPIO_Mode =  GPIO_Mode_AIN;
  12.   GPIO_Init(GPIOC,&GPIO_Conf);
  13.   GPIO_Conf.GPIO_Pin = GPIO_Pin_9;
  14.   GPIO_Conf.GPIO_Mode = GPIO_Mode_AF_PP;
  15.   GPIO_Conf.GPIO_Speed = GPIO_Speed_50MHz;
  16.   GPIO_Init(GPIOA,&GPIO_Conf);

  17. }

  18. void DMA_Conf()
  19. {
  20.   DMA_InitTypeDef DMA_Conf;
  21.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  22.   DMA_DeInit(DMA1_Channel1);
  23.   DMA_Conf.DMA_PeripheralBaseAddr = (u32)&ADC1->DR;
  24.   DMA_Conf.DMA_MemoryBaseAddr = (u32)ADCDATA;
  25.   DMA_Conf.DMA_DIR = DMA_DIR_PeripheralSRC;
  26.   DMA_Conf.DMA_BufferSize =2;
  27.   DMA_Conf.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  28.   DMA_Conf.DMA_MemoryInc = DMA_MemoryInc_Enable;
  29.   DMA_Conf.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  30.   DMA_Conf.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  31.   DMA_Conf.DMA_Mode = DMA_Mode_Circular;
  32.   DMA_Conf.DMA_Priority = DMA_Priority_High;
  33.   DMA_Conf.DMA_M2M = DMA_M2M_Disable;
  34.   DMA_Init(DMA1_Channel1,&DMA_Conf);
  35.   DMA_Cmd(DMA1_Channel1,ENABLE);
  36. }

  37. void USART_Conf()
  38. {
  39.   USART_InitTypeDef USART_Conf;
  40.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  41.   USART_Conf.USART_BaudRate = 9600;
  42.   USART_Conf.USART_WordLength = USART_WordLength_8b;
  43.   USART_Conf.USART_StopBits = USART_StopBits_1;
  44.   USART_Conf.USART_Parity = USART_Parity_No;
  45.   USART_Conf.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  46.   USART_Conf.USART_Mode = USART_Mode_Tx;
  47.   USART_Init(USART1,&USART_Conf);
  48.   
  49.   USART_Cmd(USART1,ENABLE);
  50.   
  51. }

  52. void ADC_Conf()
  53. {
  54.   ADC_InitTypeDef ADC_Conf;
  55.   
  56.   ADC_Conf.ADC_Mode = ADC_Mode_Independent;
  57.   ADC_Conf.ADC_ScanConvMode = ENABLE;
  58.   ADC_Conf.ADC_ContinuousConvMode = ENABLE;
  59.   ADC_Conf.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  60.   ADC_Conf.ADC_DataAlign = ADC_DataAlign_Right ;
  61.   ADC_Conf.ADC_NbrOfChannel = 2;
  62.   ADC_Init(ADC1,&ADC_Conf);
  63.   
  64.   
  65.   
  66.   RCC_ADCCLKConfig(RCC_PCLK2_DiARM);  
  67.   ADC_RegularChannelConfig(ADC1,ADC_Channel_11,1,ADC_SampleTime_55Cycles5 );
  68.   ADC_RegularChannelConfig(ADC1,ADC_Channel_16,2,ADC_SampleTime_55Cycles5 );
  69.   
  70.   ADC_DMACmd(ADC1,ENABLE);
  71.   ADC_TempSensorVrefintCmd(ENABLE);
  72.   ADC_Cmd(ADC1,ENABLE);
  73.   
  74.   ADC_ResetCalibration(ADC1);
  75.   while(ADC_GetResetCalibrationStatus(ADC1));
  76.   ADC_StartCalibration(ADC1);
  77.   while(ADC_GetCalibrationStatus(ADC1));
  78.   ADC_SoftwareStartConvCmd(ADC1,ENABLE);
  79.   while(ADC_GetSoftwareStartConvStatus(ADC1));
  80.   
  81.   
  82. }

  83. float getvol(u16 advol)
  84. {
  85.   return (float)advol/4096*3.3;
  86. }

  87. void datacomp()
  88. {
  89.   u8 i;
  90.   for(i=0; i<2; i++)
  91.   {

  92.     ADCDONE[i] = getvol(ADCDATA[i]);

  93.   }
  94. }

  95. void CALL_SET()
  96. {
  97.   GPIO_Conf();
  98.   DMA_Conf();
  99.   USART_Conf();
  100.   ADC_Conf();
  101. }
复制代码
回复

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-11-24 16:51 , Processed in 0.036922 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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