#include "adc.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//±¾3ìDòÖ»1©Ñ§Ï°ê1óã¬Î′¾-×÷ÕßDí¿é£¬2»μÃóÃóúÆäËüèÎoÎóÃí¾
//ALIENTEK STM32F407¿a·¢°å
//ADC Çy¶ˉ′úÂë
//ÕyμãÔ-×ó@ALIENTEK
//¼¼êõÂÛì3:
www.openedv.com
//′′½¨èÕÆú:2014/5/6
//°æ±¾£oV1.0
//°æè¨ËùóD£¬μá°æ±Ø¾¿¡£
//Copyright(C) 1ãÖYêDDÇòíμç×ó¿Æ¼¼óDÏT1«Ë¾ 2014-2024
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
/* define ---------------------------------------------------------------------*/
/* 񄠨 ----------------------------------------------------------------------*/
__IO uint16_t ADC1ConvertedValue[10][3];
//3õê¼»ˉADC
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure; //ADC3õê¼»ˉ½á11ìå
DMA_InitTypeDef DMA_InitStructure; //DMA3õê¼»ˉ½á11ìå
GPIO_InitTypeDef GPIO_InitStructure; //GPIO3õê¼»ˉ½á11ìå
/* ê1Äü ADC1, DMA2 oí GPIO ê±Öó ****************************************/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOA, ENABLE);
/* DMA1 Stream1 channe1 ÅäÖÃ **************************************/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_BufferSize=120; //»o′æêy¾Y¸öêy
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC; //òÔíaéèÎaêy¾YÔ′
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable; //2»ê1óÃÄú′æμ½Äú′æ
DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)ADC1ConvertedValue;; //Äú′æÖD′æ′¢DMA′«êä1yà′μÄêy¾Y»ùμØÖ·
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord; //Äú′æêy¾YÎa°ë×Ö 16루ADC¾«¶èÎa12룩
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable; //Äú′æμØÖ·òaÔö¼ó
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular; //DMA1¤×÷ÔúÑ-»·Ä£ê½
DMA_InitStructure.DMA_PeripheralBaseAddr= (uint32_t)&ADC1->DR; //êy¾YÔ′μØÖ· ADCêy¾Y¼Ä′æÆ÷μØÖ·
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord; //DMAíaéèêy¾Y¿í¶è
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable; //íaéèμØÖ·2»Ôö¼ó
DMA_InitStructure.DMA_Priority=DMA_Priority_High; //DMAóÅÏ輶¸ß
DMA_Init(DMA1_Channel1, &DMA_InitStructure); //DMA3õê¼»ˉ
DMA_Cmd(DMA1_Channel1, ENABLE); //ê1ÄüDMA
//ÅäÖÃADC1í¨μà5òy½ÅÎaÄ£ÄaêäèëÄ£ê½******************************/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_4|GPIO_Pin_7;//2é¼ˉμçÑ1μÄòy½ÅÎaPA0
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //Ä£ÄaêäèëÄ£ê½
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶èÎa50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
//ADC1 3õê¼»ˉ ****************************************************************/
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE; //ADCá¬DøÄ£꽿a
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right; //12λêy¾Yóò¶ÔÆë
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None; //ía2¿¿aÆô1Ø óéèí¼t¿aÆô
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent; //ADC1¤×÷Ôú¶àá¢Ä£ê½
ADC_InitStructure.ADC_NbrOfChannel=3; //¿aÆôí¨μàêyÎa3
ADC_InitStructure.ADC_ScanConvMode=ENABLE; //é¨ÃèÄ£꽿a
ADC_Init(ADC1, &ADC_InitStructure); //¸ù¾YADC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèADCxμļÄ′æÆ÷
//ADC1 1æÔò channel5 ÅäÖÃ *************************************/
// ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_84Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_4, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_7, 3, ADC_SampleTime_55Cycles5);
/* ê1ÄüDMAÇëÇó(μ¥ADCÄ£ê½) */
//ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* ê1Äü ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* ê1Äü ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1); //ê1Äü¸′λD£×¼
while(ADC_GetResetCalibrationStatus(ADC1)); //μè′y¸′λD£×¼½áêø
ADC_StartCalibration(ADC1); //¿aÆôADD£×¼
while(ADC_GetCalibrationStatus(ADC1)); //μè′yD£×¼½áêø
/* èí¼tÆô¶ˉADC×a»» */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
下面是主程序
while(1)
{
/* Data=LUX_Read_Data(); //¶áè¡êy¾Y
LUX_temp=(float)(Data)/1.2;//¶áè¡16λADêy¾Y×a»»Îa1aÕÕÇ¿¶è
printf(" light=%f
",LUX_temp); //′®¿úêä3ö1aÇ¿
Read_DHT22(&DHT22_Data); //¶áè¡DHT22μÄÖμ
RH_Value= DHT22_Data.humi_int*256 + DHT22_Data.humi_deci; //êa¶èÖμ
RH_H = RH_Value/10; //êa¶èÕûêy£¬á½Î»
RH_L = RH_Value%10; //êa¶èD¡êy£¬ò»Î»
TEMP_Value = DHT22_Data.temp_int*256 + DHT22_Data.temp_deci; //ζèÖμ
TP_H = TEMP_Value/10; //ζèÕûêy£¬á½Î»
TP_L = TEMP_Value%10; //ζèD¡êy£¬ò»Î»
printf("
DHT22¿ÕÆøÎÂêa¶è
êa¶è£o%d.%d RH ,ζè£o%d.%d
",RH_H,RH_L,TP_H,TP_L);//′®¿úêä3öDHT22ÎÂêa¶è
delay_ms(2500);*/
ADC1ConvertedVoltage[0] = 0;
for(i=0;i<3;i++)
{
ADC1ConvertedVoltage
= 0;
for(j=0;j<10;j++)
{
ADC1ConvertedVoltage+= ADC1ConvertedValue[j];
}
ADC1ConvertedVoltage = ADC1ConvertedVoltage/10;
};
for(k=0;k<3;k++)
{
temp[k] = ADC1ConvertedVoltage[k]*3.3/4096;
printf("%d
",ADC1ConvertedVoltage[k]); //ÏÔê¾ADCC2éÑùoóμÄÔ-ê¼Öμ
i=temp[k]; //¸3ÖμÕûêy2¿·Ö¸øadcx±äá¿£¬òòÎaadcxÎau16ÕûDÎ
printf("μçÑ1%d=%d",k,i); //ÏÔê¾μçÑ1ÖμμÄÕûêy2¿·Ö£¬3.1111μÄ»°£¬Õaàï¾íêÇÏÔê¾3
temp[k]-=i; //°ÑòѾ-ÏÔê¾μÄÕûêy2¿·Öè¥μô£¬áôÏÂD¡êy2¿·Ö£¬±èèç3.1111-3=0.1111
temp[k]*=1000; //D¡êy2¿·Ö3ËòÔ1000£¬àyèç£o0.1111¾í×a»»Îa111.1£¬Ïàμ±óú±£áôèyλD¡êy¡£
printf(".%f
",temp[k]); //ÏÔê¾D¡êy2¿·Ö£¨Ç°Ãæ×a»»ÎaáËÕûDÎÏÔê¾£©£¬ÕaàïÏÔê¾μľíêÇ111.
}
delay_ms(1000);
}
}
我想用ADC测3路模拟值,然后用串口发送出去,程序是参考论坛里的帖子的,新手求助,急急急
稍微改动就可以改成端口发送,你贴的代码太长 看晕了,参考下这个
串口调试时,就出现这个,然后就不会动了,按一下复位出一个,但每次出来的都只有1个
恩恩好,谢谢你
一周热门 更多>