跪求解决方案,ADC1读数正常,ADC2无法工作,代码如下“
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ê1ÄüADC1ê±Öó
//PA1 ×÷ÎaÄ£Äaí¨μàêäèëòy½Å
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£Äaêäèëòy½Å
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//ὸö2éÑù½×¶ÎÖ®¼äμÄÑó3ù5¸öê±Öó
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled ; //DMA
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;//Ô¤·ÖÆμ8·ÖÆμ¡£ADCCLK=PCLK2/8=84/8=10.5Mhz
ADC_CommonInit(&ADC_CommonInitStructure);//3õê¼»ˉ
/* ----------------ADCx_1 Ä£ê½ÅäÖÃ--------------------- */
ADC_Init(ADC1, &ADC_InitStructure);//ADC3õê¼»ˉ
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE); //ADC1¸′λ
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE); //¸′λ½áêø
ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult ;//Ë«ADCí¬2½1æÔò
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Ä£êy×a»»1¤×÷Ôúμ¥í¨μàÄ£ê½
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //Ä£êy×a»»1¤×÷Ôúμ¥′Î×a»»Ä£ê½
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//½ûÖ1′¥·¢¼ì2a£¬ê1óÃèí¼t′¥·¢
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//óò¶ÔÆë
ADC_InitStructure.ADC_NbrOfConversion = 1;//1¸ö×a»»Ôú1æÔòDòáDÖD ò2¾íêÇÖ»×a»»1æÔòDòáD1
ADC_Init(ADC1, &ADC_InitStructure); //¸ù¾YADC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèADCxμļÄ′æÆ÷
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_480Cycles ); //ADC2,ADCí¨μà,480¸öÖüÆú,ìá¸ß2éÑùê±¼ä¿éòÔìá¸ß¾«è·¶è
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1, ENABLE);//¿aÆôAD×a»»Æ÷
}
void Adc_Init1(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); //ê1ÄüADC1ê±Öó
//PA1 ×÷ÎaÄ£Äaí¨μàêäèëòy½Å
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£Äaêäèëòy½Å
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//ὸö2éÑù½×¶ÎÖ®¼äμÄÑó3ù5¸öê±Öó
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled ; //DMA
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;//Ô¤·ÖÆμ8·ÖÆμ¡£ADCCLK=PCLK2/8=84/8=10.5Mhz
ADC_CommonInit(&ADC_CommonInitStructure);//3õê¼»ˉ
/* ----------------ADCx_1 Ä£ê½ÅäÖÃ--------------------- */
ADC_Init(ADC2, &ADC_InitStructure);//ADC3õê¼»ˉ
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2,ENABLE); //ADC2¸′λ
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2,DISABLE); //¸′λ½áêø
ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult ;//Ë«ADCí¬2½1æÔò
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Ä£êy×a»»1¤×÷Ôúμ¥í¨μàÄ£ê½
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //Ä£êy×a»»1¤×÷Ôúμ¥′Î×a»»Ä£ê½
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//½ûÖ1′¥·¢¼ì2a£¬ê1óÃèí¼t′¥·¢
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//óò¶ÔÆë
ADC_InitStructure.ADC_NbrOfConversion = 1;//1¸ö×a»»Ôú1æÔòDòáDÖD ò2¾íêÇÖ»×a»»1æÔòDòáD1
ADC_Init(ADC2, &ADC_InitStructure); //¸ù¾YADC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèADCxμļÄ′æÆ÷
ADC_RegularChannelConfig(ADC2, ADC_Channel_6, 1, ADC_SampleTime_480Cycles ); //ADC2,ADCí¨μà,480¸öÖüÆú,ìá¸ß2éÑùê±¼ä¿éòÔìá¸ß¾«è·¶è
ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);
ADC_DMACmd(ADC2,ENABLE);
ADC_Cmd(ADC2, ENABLE);//¿aÆôAD×a»»Æ÷
}
u32 ADCData[1];
int main(void)
{
u32 adcx1=0;
u16 adcx2=0,temp0=0 ,temp1=0;
float temp;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168); //3õê¼»ˉÑóê±oˉêy
uart_init(115200); //3õê¼»ˉ′®¿ú2¨ìØÂêÎa115200
LED_Init(); //3õê¼»ˉLED
LCD_Init(); //3õê¼»ˉLCD½ó¿ú
Adc_Init(); //3õê¼»ˉADC
Adc_Init1();
delay_ms(50);
LCD_Clear(WHITE);
delay_ms(5);
MYDMA_Config(DMA2_Stream0 ,DMA_Channel_0,(u32)&ADC1->DR,(u32)ADCData,1);
ADC_SoftwareStartConv(ADC2); //ê1ÄüÖ¸¶¨μÄADC1μÄèí¼t×a»»Æô¶ˉ1|Äü
ADC_SoftwareStartConv(ADC1); //ê1ÄüÖ¸¶¨μÄADC1μÄèí¼t×a»»Æô¶ˉ1|Äü
POINT_COLOR=BLUE;
LCD_ShowString(550,130,200,16,16,"ADC1_CH5_VAL:");
LCD_ShowString(550,150,200,16,16,"ADC1_CH5_VOL:0.000V"); //ÏèÔú1춨λÖÃÏÔê¾D¡êyμã
LCD_ShowString(550,170,200,16,16,"ADC2_CH6_VAL:");
LCD_ShowString(550,190,200,16,16,"ADC2_CH6_VOL:0.000V"); //ÏèÔú1춨λÖÃÏÔê¾D¡êyμã
while(1)
{
adcx1=ADCData[0];// è¡3öADC1êy¾Y¼Ä′æÆ÷μĸß16룬Õa¸öêÇADC2μÄ×a»»êy¾Y
temp0 = (adcx1&0XFFFF0000) >> 16;// è¡3öADC1êy¾Y¼Ä′æÆ÷μÄμí16룬Õa¸öêÇADC1μÄ×a»»êy¾Y
temp1 = (adcx1&0XFFFF);
adcx2=temp1;
LCD_ShowxNum(654,130,adcx2,4,16,0); //ÏÔê¾ADC2éÑùoóμÄÔ-ê¼Öμ
temp=(float)adcx2*(3.3/4096); //»ñ衼ÆËãoóμÄ′øD¡êyμÄêμ¼êμçÑ1Ö죬±èèç3.1111
adcx2=temp; //¸3ÖμÕûêy2¿·Ö¸øadcx±äá¿£¬òòÎaadcxÎau16ÕûDÎ
LCD_ShowxNum(654,150,adcx2,1,16,0); //ÏÔê¾μçÑ1ÖμμÄÕûêy2¿·Ö£¬3.1111μÄ»°£¬Õaàï¾íêÇÏÔê¾3
temp-=adcx2; //°ÑòѾ-ÏÔê¾μÄÕûêy2¿·Öè¥μô£¬áôÏÂD¡êy2¿·Ö£¬±èèç3.1111-3=0.1111
temp*=1000; //D¡êy2¿·Ö3ËòÔ1000£¬àyèç£o0.1111¾í×a»»Îa111.1£¬Ïàμ±óú±£áôèyλD¡êy¡
LCD_ShowxNum(669,150,temp,3,16,0X80); //ÏÔê¾D¡êy2¿·Ö£¨Ç°Ãæ×a»»ÎaáËÕûDÎÏÔê¾£©£¬ÕaàïÏÔê¾μľíêÇ111.1
delay_ms(5);
adcx2=temp0;
LCD_ShowxNum(654,170,adcx2,4,16,0); //ÏÔê¾ADCC2éÑùoóμÄÔ-ê¼Öμ
temp=(float)adcx2*(3.3/4096); //»ñ衼ÆËãoóμÄ′øD¡êyμÄêμ¼êμçÑ1Ö죬±èèç3.1111
adcx2=temp; //¸3ÖμÕûêy2¿·Ö¸øadcx±äá¿£¬òòÎaadcxÎau16ÕûDÎ
LCD_ShowxNum(654,190,adcx2,1,16,0); //ÏÔê¾μçÑ1ÖμμÄÕûêy2¿·Ö£¬3.1111μÄ»°£¬Õaàï¾íêÇÏÔê¾3
temp-=adcx2; //°ÑòѾ-ÏÔê¾μÄÕûêy2¿·Öè¥μô£¬áôÏÂD¡êy2¿·Ö£¬±èèç3.1111-3=0.1111
temp*=1000; //D¡êy2¿·Ö3ËòÔ1000£¬àyèç£o0.1111¾í×a»»Îa111.1£¬Ïàμ±óú±£áôèyλD¡êy¡
LCD_ShowxNum(669,190,temp,3,16,0X80); //ÏÔê¾D¡êy2¿·Ö£¨Ç°Ãæ×a»»ÎaáËÕûDÎÏÔê¾£©£¬ÕaàïÏÔê¾μľíêÇ111.1
delay_ms(5);
}
}
一周热门 更多>