cancel
Showing results for 
Search instead for 
Did you mean: 

ADC example

madhu13
Associate II
Posted on September 06, 2010 at 08:13

ADC example

6 REPLIES 6
Andrew Neil
Evangelist
Posted on May 17, 2011 at 14:05

Have you actually looked at the examples included in the ST Peripheral Library?

Have you looked at the examples included in the Keil installation, and also available on their website?

gdiez
Associate II
Posted on May 17, 2011 at 14:05

Pls write the code of it. Then we can help you. I develop in IAR

madhu13
Associate II
Posted on May 17, 2011 at 14:05

thank you for the response, i got the example program.

madhu13
Associate II
Posted on May 17, 2011 at 14:05

i configured ADC1 in independent mode, it is working fine. I want to configure in dual mode ADC1 with channel0 and ADC2 with channel1 and after conversion transmitting the counts through USART to Hyperterminal, i able read the ADC1 channel0 counts but ADC2 channel1 counts, i have written my code as below, please tell me what is the fault in the code...

#include<stm32f10x_lib.h>

#include<math.h>

#define CFGR_REG_VAL  0x001D8402; //value to be written into RCC CFGR register

#define PSC_VALUE   0x0000       //value to be written into Prescalar register  of Timer3

#define ARR_VALUE  0x1C20 //0x2D0//10us  //value to be written into autoreload register

void RCC_Configuration(void);

void GPIOA_Config(void);

void USART_Configuration(void);

void ADC_DualMode_Config(void);

unsigned int i_ADC_val,ADC_val[2048]={0},temp_count=0,Y=0;

unsigned int x,i,temp1,temp2;

unsigned char temp[9]={0};

float RMS_val=0.0,DC_val=0.0;

main()

{

   RCC_Configuration();

    GPIOA_Config();

 USART_Configuration();

  ADC_DualMode_Config();

 

 /*ADC enable*/

 ADC1->CR2|=0x00500000;//0x00001;

 //ADC2->CR2|=0x00500000;//0x00001;

   while(1)

   {

  if((ADC1->SR & 0x2))

  {

   temp1=(ADC1->DR & 0X00000FFF);

   temp2=((ADC1->DR & 0X0FFF0000)>>16);   

   temp[0] =(temp1/1000)+0X30;//(temp1 & 0x000000F)+0x30;

   temp1=temp1%1000; 

   temp[1]=(temp1/100)+0X30;//((temp1 & 0x00000F0)>>4)+0x30;

   temp1=temp1%100;

   temp[2]=(temp1/10)+0X30;

   temp1=temp1%10;   

   temp[3]=temp1+0X30;  

  temp[4]=' ';

   temp[5] =(temp2/1000)+0X30;//(temp2 & 0x000000F)+0x30;

   temp2=temp2%1000; 

   temp[6]=(temp2/100)+0X30;//((temp2 & 0x00000F0)>>4)+0x30;

   temp2=temp2%100;

   temp[7]=(temp2/10)+0X30;

   temp2=temp2%10;   

   temp[8]=temp2+0X30;   

   USART1->CR1|=0x00002000; //USART ENABLE   

   for(i=0;i<9;i++)

   {   

    while((USART1->SR & 0x40)==0);

    x= USART1->SR;

    USART1->DR=temp[i];          

   }

   while((USART1->SR & 0x40)==0);

   x= USART1->SR;

   USART1->DR=0X0A;

   while((USART1->SR & 0x40)==0);

   x= USART1->SR;

   USART1->DR=0X0D; 

   USART1->CR1&=0xFFFFDFFF;

 

  }

   }

}

/*RCC Configuration*/

void RCC_Configuration(void)

{

 RCC->CSR|=0x01000000;

 RCC->CR=0x00010080; //external highe speed clock enabled

  if ((RCC->CR & RCC_HSE_ON))

  {                 // if HSE enabled

  while ((RCC->CR & RCC_FLAG_HSERDY)==0 ); // Wait for HSERDY = 1 (HSE is ready)

     //Flash access control reg

  FLASH->ACR=0X12;  //prefetch buffer enabled and flash latacy(2 wait state)

  RCC->CFGR= CFGR_REG_VAL

   

    RCC->CR|=0x01000000;   //PLL enable

    if (RCC->CR & RCC_PLL_ON)

    {                 // if PLL enabled

      while ((RCC->CR & RCC_FLAG_PLLRDY) == 0); // Wait for PLLRDY = 1 (PLL is ready)

    } 

    while ((RCC->CFGR & 0x0000008) ==0); 

  }         

}

/*End of RCC Configuration*/

/*GPIO Configuration Function*/

void GPIOA_Config(void)

{

   RCC->APB2ENR|=0x00000004;  //GPIOA clock enable

   AFIO->MAPR=0x00000000;

   GPIOA->CRH = (GPIOA->CRH & 0xFFFFF00F)|0x4B0;

   //GPIOA->CRL &= 0xFF00;  //PA0 and PA1 configured as Analog inputs

}

/*End of GPIO Configuration Function*/

/*USART Configuration Function*/

void USART_Configuration(void)

{

 RCC->APB2ENR|=0x00004000; //USART1 clock enable

 USART1->BRR=0x271; //0x271-115200 //0x1D4C;9600 baud rate

 USART1->CR1=0x0000004D;  //TCIE(transmit complete interrupt enable)

 USART1->CR2=0x000;    //eable clock

}

/*End of USART Configuration Function*/

/*ADC Configuration Function*/

void ADC_DualMode_Config(void)

{

 /*ADC1 Configuration*/

 RCC->APB2ENR|=0x601;  //Enable ADC1, ADC2 and AFIO peripheral clock

 ADC1->CR1=0x60100;

 ADC1->CR2=0x000E0003;

 ADC1->SQR3=0x0;

 /*end of ADC1 Configuration*/

 /*ADC2 Configuration*/

 ADC2->CR1=0x60100;

 ADC2->CR2=0x000E0003;

 ADC2->SQR3=0x1;

 /*end of ADC2 Configuration*/

}

/*End of ADC Configuration Function*/

Posted on May 17, 2011 at 14:05

You enable an interrupt for which you have no service code.

You do not enable the NVIC.

You disable the USART before you have checked to confirm the last bit has been transmitted.

Why do you need to enable/disable the USART in this manner?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
madhu13
Associate II
Posted on May 17, 2011 at 14:05

before i used Transmit complete interrupt, if i enable this interrupt it keeps transmitting the data in array but i wanted to transmit only at the end of conversion of ADC data , so i am enable/disable the USART in this manner and i left the interrupt enabled as it is in the CR1, is it affects working of my code now dont want to use the interrupt.