STM8 ADC problem on STM8S003K3

Status
Not open for further replies.

microcon555

Advanced Member level 4
Joined
Feb 13, 2009
Messages
108
Helped
9
Reputation
18
Reaction score
6
Trophy points
1,298
Activity points
1,877
Hi

I am using STM8S003K3 and trying to read adc value from channel 0.

Here is my try.

Code:
void SetupADC()
  {
   // ADC1_TRDL=1; 
//ADC_CR1_ADON = 1;       //  Turn ADC on, note a second set is required to start the conversion.
ADC_CSR_CH = 0x00;      //  ADC on AIN4 only.
ADC_CR3_DBUF = 0;
ADC_CR2_ALIGN = 1;      //  Data is right aligned.
//ADC_CSR_EOCIE = 1;      //  Enable the interrupt after conversion completed.
ADC_CR1_CONT= 0;
ADC_CSR_AWD = 0;

}

void get_adc()
{
  unsigned char low, high;
  ADC_CR1_ADON = 1;       //  Turn ADC on, note a second set is required to start the conversion.
  ADC_CR1_ADON = 1;       //  Turn ADC on, note a second set is required to start the conversion.
  value1=0;
 
  while(ADC_CSR_EOC== 0); // Wait till EOC
  low = ADC_DRL;            //    Extract the ADC reading.
high = ADC_DRH;
     value1 = (high*256)+low;
     
      
}


I am getting random values. Some one point out the error? or give me a good example.

I am using IAR compiler

MicroCon
 

Hi,

...what is connected at the ADC input?
Is there a stable voltage/signal source?

Klaus
 

Just a potentiometer and i can see the voltage and adc pin is stable.
 

void ADC_Init (unsigned char Channel)
{
CLK->PCKENR2 |= CLK_PCKENR2_ADC;
ADC1->TDRL = Channel;
ADC1->CR1 = ADC1_PRESSEL_FCPU_D18;
ADC1->CR2 = ADC1_ALIGN_RIGHT;
ADC1->CR1 |= ADC1_CR1_ADON;
}

unsigned int ADC_Read (unsigned char Channel)
{
unsigned int Result;
ADC1->CSR = Channel;
ADC1->CR1 |= ADC1_CR1_ADON;
ADC1->CR1 |= ADC1_CR1_ADON;
while (!(ADC1->CSR & ADC1_CSR_EOC));
Result = ADC1->DRL;
Result |= ADC1->DRH << 8;

return Result;
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…