Question
ADC accuracy problem
I am sensing the voltage using stm32f103c8 module.
- using Keil micro--vision 5
- register level coding
code:
#include "stm32f10x.h"
float av,volt;
int main(void)
{
// SEND A CLOCK SOURCE FOR ADC
//LcdSend2StringWithDelay(50000,"Start Clock","");
RCC->APB2ENR |=RCC_APB2ENR_IOPAEN;
RCC->APB2ENR |=RCC_APB2ENR_ADC2EN; // Enable ADC1 clock
RCC->CR |=RCC_CR_HSEON; // on the HSI oscilator
while((RCC->CR & RCC_CR_HSERDY)==0)
{}
GPIOA->CRL&=~(1<<7)|(1<<6);
//LcdSend2StringWithDelay(50000,"Start Clock","Clock enabled");
RCC->CFGR &=~(RCC_CFGR_HPRE); // set AHB PRESCALAR TO 1
RCC->CFGR &=~(RCC_CFGR_PPRE2);
RCC->CFGR |=RCC_CFGR_ADCPRE_DIV8; // Analog adc clocked
// ADC ENABLE
ADC2->CR2 |=(ADC_CR2_ADON|ADC_CR2_CONT);
//LcdSend2StringWithDelay(50000,"Enable adc","ADC enabled");
ADC2->SMPR2 |=ADC_SMPR2_SMP1; // Setting sampling rate as 111 (slow sampling)
ADC2->SQR3 |=(1<<0);
// CALIBRATION FOR ADC
ADC2->CR2|=(ADC_CR2_ADON);
ADC2->CR2|=ADC_CR2_CAL;
while((ADC2->CR2 & ADC_CR2_CAL)!=0){}
while(1)
{
ADC2->CR2 |=(ADC_CR2_ADON);
//ADC1->CR2 |=(ADC_CR2_SWSTART);
while((ADC2->SR & ADC_SR_EOC)==0)
{}
av=ADC2->DR;
volt=(3.3/4095)*av;
}
}adc value varies from 1050 to 1080....
ADC value varies it not constant ...
