Two way AD acquisition of embedded learning of Blue Bridge Cup

hardware circuit

Needle cap

program

ADC mode sets the ADC to work in independent or dual ADC mode.
ADC mode description
 1. ADC mode independent ADC1 and ADC2 work in independent mode
 2. ADC mode reginjecsimult ADC1 and ADC2 work in synchronization rule and synchronization injection mode
 3. ADC mode regsimult altertrig ADC1 and ADC2 work in synchronous rule mode and alternate trigger mode
 4. ADC ﹣ mode ﹣ injecsimult ﹣ fastinterl ADC1 and ADC2 work in synchronous regular mode and fast alternating mode
 5. ADC ﹣ mode ﹣ injecsimult ﹣ slowinterl ADC1 and ADC2 work in synchronous injection mode and slow alternation mode
 6. ADC mode injecsimult ADC1 and ADC2 work in synchronous injection mode
 7. ADC mode regsimult ADC1 and ADC2 work in synchronous rule mode
 8. ADC mode fastinterl ADC1 and ADC2 work in fast alternate mode
 9. ADC? Mode? Slowinterl ADC1 and ADC2 work in slow alternate mode
 10. ADC mode altertrig ADC1 and ADC2 work in alternate trigger mode

For specific ADC configuration, please refer to the following guys
https://blog.csdn.net/qwq1503/article/details/96108617

ADC initialization configuration function

void ADC_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	ADC_InitTypeDef ADC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //Because the Blue Bridge Cup expansion board is all ADC1 acquisition, you can configure an independent channel
	ADC_InitStructure.ADC_ScanConvMode = DISABLE;
	ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStructure.ADC_NbrOfChannel = 2; //Dual AD acquisition channel
	ADC_Init(ADC1, &ADC_InitStructure);
	
	/* ADC1 regular channels configuration */ 
	ADC_Cmd(ADC1, ENABLE);
	
	ADC_ResetCalibration(ADC1);
	while(ADC_GetResetCalibrationStatus(ADC1));
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1));
}

Acquisition of data collected by ADC

u16 Get_ADCs(u8 channel)
{
	
	u16 ADC_Val = 0;	
	ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_239Cycles5);	
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);	
	while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
	ADC_Val = ADC_GetConversionValue(ADC1);//Returns the conversion result of the latest ADCx rule group
	ADC_ClearFlag(ADC1, ADC_FLAG_EOC);	
	ADC_SoftwareStartConvCmd(ADC1, DISABLE);	
	return ADC_Val;
}

If you want to collect ADC values more stable and more accurate, you can write an algorithm to improve the accuracy of the collection, but if it doesn't, it doesn't matter. It mainly takes some time to tap the following code during the competition. You can help the group understand the purpose of these algorithms through normal practice.
Bubble sorting is as follows

//Using bubble sorting method to improve the accuracy of acquisition
u16 Get_Filter(u8 channel)
{
	u16 tmp;
	u8 i = 0,j = 0;
	
	for(i=0; i<ADC_BUFF_LEN; i++)
	{
		adc_buff[i] = Get_ADCs(channel);
	}

	for(i=0; i<=ADC_BUFF_LEN-1; i++)  /* The external loop is the number of sorting trips, len number is len-1 */
	{
		for(j=0; j<	ADC_BUFF_LEN-i-1; j++)/* The internal cycle is the number of times for each comparison, and len-i times for the i-th comparison */
		{
			if(adc_buff[j+1] < adc_buff[j])/* If the adjacent elements are compared in reverse order, they will be exchanged (left in ascending order is greater than right, and vice versa in descending order) */
			{
				tmp = adc_buff[j+1];
				adc_buff[j+1] = adc_buff[j];
				adc_buff[j] = tmp;
			}
		}
	}
//Determine the parity of the number of cycles
	if(ADC_BUFF_LEN % 2 == 0) 
	{
		return(adc_buff[ADC_BUFF_LEN/2-1] + adc_buff[ADC_BUFF_LEN/2])/2;//Keep the middle two numbers in even number of cycles
	}
	else
	{
		return(adc_buff[ADC_BUFF_LEN/2]);//Odd number returns the middle number
	}
}

Main function

    ADC_Config();//Initialization of ADC configuration
	while(1)
	{		
		x = Get_ADCs(ADC_Channel_4);
		y = Get_ADCs(ADC_Channel_5);
				
		snprintf((char *)str, sizeof(str), " VRp5:%3.2fV", x/4095.*3.3);
		LCD_DisplayStringLine(Line6, str);
		snprintf((char *)str, sizeof(str), " VRp6:%3.2fV", y/4095.*3.3);
		LCD_DisplayStringLine(Line7, str);	
		Delay_Ms(200);
	}

Conclusion:
1. There is not much difference between the two ADC configurations on the expansion board and the initialization configuration of the individual ADC. One thing to note is that ADC ﹣ initstructure. ADC ﹣ nbrofchannel = two channels
2. If the acquisition is not too accurate and stable, the bubble sorting algorithm can be avoided

*wj
Published 10 original articles, won praise 6, visited 1333
Private letter follow

Posted on Sun, 15 Mar 2020 06:59:15 -0400 by tijger