Hi i dont know how use ADC. I have very strange readings: On Vref: http://i.imgur.com/6eHe2k5.png (chanel17) On PA1: http://i.imgur.com/ZtxtBVF.png (chanel1) My code:
int main(void) { SysTick_Config(SystemCoreClock/1000); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1ENR_GPIOAEN ,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); GPIO_InitTypeDef gpio; gpio.GPIO_Pin = GPIO_Pin_1; gpio.GPIO_Mode=GPIO_Mode_AN; gpio.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA,&gpio); RCC_PCLK1Config(RCC_HCLK_Div8); ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1,&ADC_InitStructure); ADC_RegularChannelConfig(ADC1,ADC_Channel_17,1,ADC_SampleTime_15Cycles); ADC_TempSensorVrefintCmd(ENABLE); ADC_Cmd(ADC1,ENABLE); setup_USART2(); USART_puts(USART2,"Pomiar napiecia\r\n"); uint16_t adc; while (1) { ADC_RegularChannelConfig(ADC1,ADC_Channel_17,1,ADC_SampleTime_15Cycles); ADC_SoftwareStartConv(ADC1); while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)!= SET ); adc = ADC_GetConversionValue(ADC1); printf("%.6fV\r\n",adc*3.3f/4096.0f); Delay(50); } return 0; }
I know that the code is not the best. But I wanted something that works, and no matter what I do the results are not accurate.
I try find on google but nothing.
What does not accurate mean in this context? What voltages are you expecting to measure? Have you confirmed the external VDDA (VREF), how much noise is on that rail?
Analogue values tend to be noisy, you're unlikely to see the exact same sample over and over. You could try extending the sample time. You could try averaging.
I expected something like this (Vref): http://i.imgur.com/KBrV8U6.png Vref is internal. I do not expect such large fluctuations.
Your readings seems quite normal. Digital and analog circuitry aren't really friends with each other and the digital noise is a big problem. There is a reason why better equipment goes all the way to separate power supplies for the analog and digital sections, and have them on well separated regions of the PCB.
You'll normally get best results by using an external Vref, and to make sure that this voltage is properly filtered. So one or more suitable capacitors to ground and a series inductor. And a band-gap reference to decide the actual voltage. This allows you to properly isolate the reference voltage from the load variations on VCC. Note that you also needs to properly take care of GND, since ground fluctuations will also hurt your readings - better processors have dedicated AGND pins, allowing additional filtering of the ground reference too.
When using an internal voltage reference, it's normally more important to look into the actual power consumption of the processor while making the reading - or filter the data by averaging multiple readings. It can also make a significant difference to try different clock frequencies to the ADC, to affect how fast each conversion is.
Remember that each additional usable bit is twice as hard to get noise-free.