//main routine for ADC (linear) calibration testing void TestADCCalibration(void) { unsigned int * ADCBuffer; double * ADCRealValues; double P, Q; unsigned int i; unsigned int NumOfData; volatile double diffOrig; volatile double diffCalib; unsigned char DEBUG_STRING[30]; //set external clock (16MHz XTALL required) SetCPUClock(0); //allocate buffer space if ((ADCBuffer = malloc(ADC_BUFFER_SIZE * sizeof(ADCBuffer[0]))) == NULL) _asm("trap\n"); if ((ADCRealValues = malloc(ADC_BUFFER_SIZE * sizeof(ADCRealValues[0]))) == NULL) _asm("trap\n"); //main test { //collect data NumOfData = ADCCollectDataCalib(ADCBuffer, ADCRealValues); P= GetMultiplierCalib(ADCBuffer, ADCRealValues, NumOfData); diffOrig = GetErrorCalib(ADCBuffer, ADCRealValues, NumOfData, P, 0); //calib data ADCCalibratePQ(ADCBuffer, ADCRealValues, NumOfData, &P, &Q); diffCalib = GetErrorCalib(ADCBuffer, ADCRealValues, NumOfData, P, Q); //print errors sprintf(DEBUG_STRING, "Orig = %f", diffOrig); LCD_PrintString(LCD_LINE1, ENABLE, DISABLE, DEBUG_STRING); sprintf(DEBUG_STRING, "Calib= %f", diffCalib); LCD_PrintString(LCD_LINE2, ENABLE, DISABLE, DEBUG_STRING); delay(500000l);//to show results } //see real values after ADC calibration - for checking calibration LCD_Clear(); LCD_PrintString(LCD_LINE1, DISABLE, ENABLE, "Real calibrated U"); while(!JOY_SEL) { //start single conversion ADC2_StartConversion(); while (!ADC2_GetFlagStatus()); //clear end of conversion bit ADC2_ClearFlag(); //collect ADC value and display LCD_SetCursorPos(LCD_LINE2, 4);//set cursor position LCD_PrintDec4((P*ADC2_GetConversionValue() + Q)*1000); } //unallocate buffer free(ADCBuffer); }//TestADCCalibration
/**************************实现函数******************************************** *函数原型: unsigned int Get_ADCCH_Value(ADC2_Channel_TypeDef ADC_Channel) *功 能: 单次采样某个ADC通道 *******************************************************************************/ unsigned int Get_ADCCH_Value(ADC2_Channel_TypeDef ADC_Channel) { unsigned int SUM = 0;//临时和 unsigned char j; //配置ADC转换通道,单次采样模式,ADC结果右对齐 ADC2_Cmd(ENABLE); ADC2_ConversionConfig(ADC2_CONVERSIONMODE_SINGLE, ADC_Channel, ADC2_ALIGN_RIGHT); for (j = 0; j < 16 ; j++) { ADC2_StartConversion(); // 开始转换 while (ADC2_GetFlagStatus() == RESET); // 等待转换完成 SUM += ADC2_GetConversionValue(); // 求和 ADC2_ClearFlag(); } SUM = SUM>>4; ADC2_Cmd(DISABLE); return(SUM); }
//collection of ADC data and also real precise values from external voltmeter //external data is entered with joystick movement as 0.000 number static unsigned int ADCCollectDataCalib(unsigned int * ADCBuffer, double * ADCRealValues) { unsigned int i; unsigned int multiplier; signed int Voltage; unsigned char cursorPos; unsigned char ArrowChar = '^'; unsigned char JoyUp,JoyDown,JoyLeft,JoyRight,JoySel; //disable ADC interrupts ADC2_ITConfig(DISABLE); //enable ADC ADC2_Init(ADC2_CONVERSIONMODE_SINGLE, ADC2_CHANNEL_12, ADC2_PRESSEL_FCPU_D6, ADC2_EXTTRIG_TIM, DISABLE, ADC2_ALIGN_RIGHT, ADC2_SCHMITTTRIG_CHANNEL12, DISABLE); //clear end of conversion bit ADC2_ClearFlag(); //for joystick control (init) //pull-ups on on all used buttons GPIOB->CR1 |= 0xF0; GPIOD->CR1 |= 0x80; Voltage = 0; multiplier = 1000; cursorPos = 2; //print values on LCD display LCD_Clear(); LCD_SetCursorPos(LCD_LINE1, 1); LCD_PrintDec4(Voltage); LCD_PrintString(LCD_LINE1, DISABLE, ENABLE, " =Real U"); LCD_SetCursorPos(LCD_LINE2, cursorPos/2); LCD_PrintChar(ArrowChar); //set external voltage - measure it with external voltmeter - enter voltmeter //value into LCD - measure ADC data - store real and ADC data to buffers //finish data collecting by KEY button on PCB board for(i=0; i<ADC_BUFFER_SIZE; i++) { LCD_SetCursorPos(LCD_LINE2, 0); LCD_PrintDec2(i); while(JOY_SEL); while(!JOY_SEL) { if(BUTTON_LOW) break; JoyUp = JOY_UP; JoyDown = JOY_DOWN; JoyLeft = JOY_LEFT; JoyRight= JOY_RIGHT; JoySel = JOY_SEL; if(JoyLeft)//increase multiplier { if (cursorPos!=2) { multiplier *= 10; cursorPos -= 1; } } if(JoyRight)//decrease multiplier { if (cursorPos!=5) { multiplier /= 10; cursorPos += 1; } } if(JoyRight || JoyLeft)//repaint cursor position { LCD_PrintString(LCD_LINE2, DISABLE, DISABLE, " ");//clear cursor LCD_SetCursorPos(LCD_LINE2, cursorPos/2);//set cursor position if(cursorPos & 1) LCD_PrintChar(' ');//print space LCD_PrintChar(ArrowChar);//print cursor while (JOY_RIGHT || JOY_LEFT); delay(1000l); } if(JoyUp)//increase real value { Voltage += multiplier; } if(JoyDown)//decrease real value { Voltage -= multiplier; } if(JoyUp || JoyDown)//repaint real value { LCD_PrintString(LCD_LINE1, DISABLE, DISABLE, " ");//clear voltage if (Voltage<0) { LCD_SetCursorPos(LCD_LINE1, 1); LCD_PrintDec4(-Voltage); LCD_SetCursorPos(LCD_LINE1, 0); LCD_PrintChar('-');//print sign } else { LCD_SetCursorPos(LCD_LINE1, 1); LCD_PrintDec4(Voltage); LCD_SetCursorPos(LCD_LINE1, 0); LCD_PrintChar('+');//print sign } while (JOY_UP || JOY_DOWN); delay(1000l); } //start single conversion ADC2_StartConversion(); while (!ADC2_GetFlagStatus()); //clear end of conversion bit ADC2_ClearFlag(); //collect ADC value and display it LCD_SetCursorPos(LCD_LINE2, 5);//set cursor position LCD_PrintDec4(ADC2_GetConversionValue()); } //stop collecting if KEY buttom pressed if(BUTTON_LOW) { break; } //otherwise collect ADC data (+ real data) and store them else { //start single conversion ADC2_StartConversion(); while (!ADC2_GetFlagStatus()); //clear end of conversion bit ADC2_ClearFlag(); //collect ADC value ADCBuffer[i] = ADC2_GetConversionValue(); //collect real value ADCRealValues[i] = (double)Voltage/1000; } } return i; }//ADCCollectDataCalib