cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 ADC LOWER LEVEL PROGRAMMING ISSUE

Yadav
Associate II
Posted on June 12, 2018 at 06:37

Hi Everyone,

       I m interested in doing lower level programming. Now i currently working with stm32f407 board. I have successfully got the output for GPIO. Now i am started working on ADC interface. But i couldnt get the output for my code. I haD given 3V to PA1 pin but still i couldnt get the proportional value in DR register. Kindly help me out to solve this issue.

&sharpinclude 'stm32f4xx.h'

void GPIO_Init(void);

void adc_init(void);

uint16_t adc_read();

uint16_t adc_value1=0,adc_value2=0;

int main(void)

{

GPIO_Init();

adc_init();

while (1)

{

adc_value2=adc_read();

if(adc_value2>=0X000C)

{

GPIOD->ODR|=(1<<14);

}

else

{

GPIOD->ODR|=(1<<15);

}

//

// GPIOD->ODR&=~(1<<14);

// for(delay=0;delay<=4166666;delay++);

// GPIOD->ODR|=(1<<15);

// for(delay=0;delay<=4166666;delay++);

//

// GPIOD->ODR&=~(1<<15);

// for(delay=0;delay<=4166666;delay++);

}

}

void GPIO_Init(void)

{

RCC->AHB1ENR |=(1<<1); // clock for gpioA

GPIOA->MODER |= 0X03; // PA1 analog inputs

}

void adc_init(void)

{

RCC->APB2ENR|=(1<<8);

ADC1->CR1|=(1<<24)|(1<<0); // 1st CHANNEL // 10BIT RESOLUTION

ADC1->CR2|=(1<<10);// ADC1->CR2

ADC1->SMPR2 |= 1; //15 cycles

ADC1->SQR1 &= ~(1<<20); // 1 conversion

ADC1->SQR3 = 0x00;

}

uint16_t adc_read()

{

ADC1->CR2|=(1<<0);// ADC ON

ADC1->CR2|=(1<<30);// START CONVERSION

// if((ADC1->SR&(1<<1))) // wait untill EOC bit clear

//{

ADC1->SR = 0X00;

adc_value1=ADC1->DR;

//}

ADC1->CR2&=~(1<<0);// ADC OFF

return(adc_value1);

}

#stm32f407-discovery-startup #learning-stm32 #stm32-l4
1 ACCEPTED SOLUTION

Accepted Solutions
henry.dick
Senior II
Posted on June 14, 2018 at 12:11

'

Kindly help me out to solve this issue.'

1. go through the datasheet and see what needs to be done; 

2. go through the library code and see how they did it.

3. or take a look at sample code. one example is here: 

https://github.com/dannyf00/My-MCU-Libraries---2nd-try/tree/master/STM32F0

  different devices but similar peripherals.

View solution in original post

9 REPLIES 9
Yadav
Associate II
Posted on June 14, 2018 at 03:53

Now i am getting random values but not correct values.

Is there no one to help me out to solve this issue ??

#include 'stm32f4xx.h'

void GPIO_Init(void);

void adc_init(void);

uint16_t adc_read();

uint16_t adc_value1=0,adc_value2=0;

int main(void)

{

GPIO_Init();

adc_init();

while (1)

{

adc_value2=adc_read();

if(adc_value2>=500)

{

GPIOD->ODR|=(1<<14);

}

else

{

GPIOD->ODR|=(1<<15);

}

//

// GPIOD->ODR&=~(1<<14);

// for(delay=0;delay<=4166666;delay++);

// GPIOD->ODR|=(1<<15);

// for(delay=0;delay<=4166666;delay++);

//

// GPIOD->ODR&=~(1<<15);

// for(delay=0;delay<=4166666;delay++);

}

}

void GPIO_Init(void)

{

RCC->AHB1ENR|=RCC_AHB1ENR_GPIODEN;

GPIOD->MODER|=(1<<28); // pin 14 is output

GPIOD->OTYPER&=~(1<<14);//pin 14 as push pull

GPIOD->OSPEEDR|=(1<<29)|(1<<28);//LOW speed

GPIOD->PUPDR&=~(1<<29)|(1<<28);//No push pull

GPIOD->MODER|=(1<<30); // pin 14 is output

GPIOD->OTYPER&=~(1<<15);//pin 14 as push pull

GPIOD->OSPEEDR|=(1<<30)|(1<<31);//LOW speed

GPIOD->PUPDR&=~(1<<30)|(1<<31);//No push pull

RCC->AHB1ENR |=(1<<0); // clock for gpioA

GPIOA->MODER |= (1<<3)|(1<<2); // Analog mode

GPIOA->PUPDR&=~(0X00000000);//No push pull

// GPIOA->AFR[0]|=(1<<0)|(1<<1)|(1<<2); // ADC FUNCTION FOR PA1

GPIOA->OTYPER&=~(1<<1);//pin 1 as push pull

GPIOA->OSPEEDR|=(1<<3);//HIGH speed

}

void adc_init(void)

{

RCC->APB2ENR|=(1<<8);

ADC1->CR1|=(1<<0)|(1<<23); // 1st CHANNEL // WATCHDOG ENABLE

ADC1->CR1&=~(1<<24);//

ADC1->CR1|=(1<<24); //10BIT RESOLUTION

ADC1->CR2|=(1<<10);// EOCs is set

ADC1->SMPR2 |= (1<<3); //15 cycles

ADC1->SQR1 &= ~(1<<20); // 1 conversion

ADC1->SQR3 = 0x00;

}

uint16_t adc_read()

{

ADC1->CR2|=(1<<0);// ADC ON

ADC1->CR2|=(1<<30);// START CONVERSION

while(ADC1->SR&(1<<1)) // wait untill EOC bit set

{

ADC1->SR = 0X00;

adc_value1=ADC1->DR;

ADC1->CR2&=~(1<<0);// ADC OFF

ADC1->CR2&=~(1<<30);// STOP CONVERSION

}

return(adc_value1);

}
Posted on June 14, 2018 at 06:00

>>Is there no one to help me out to solve this issue ??

If you want someone to sit with you and work through your issues, hire a tutor.

Want to learn by inspection, implement with library code, replicate the low level functionality. If your code doesn't work, look at how the registers differ, cross-reference with the documentation, refine the model in your head.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 14, 2018 at 06:45

I have tried with library file and got output already. Since i am new to the STM32 controllers, so only i couldn't make it out. I didn't post this question here without trying anything. I have been struggling with this issue for almost 1 week. Then only i posted this question here. I never asked someone to sit with me and solve it. It is a forum and i am trying get positive solution from senior developers.

I am expertise in AVR controller. If somebody posted question like this, i would have given the good positive response instead of ur answer.

Thank you

stm322399
Senior
Posted on June 14, 2018 at 11:54

SQR3 tells that conversion is done on ADC123_IN0, but PA1 is ADC123_IN1

henry.dick
Senior II
Posted on June 14, 2018 at 12:11

'

Kindly help me out to solve this issue.'

1. go through the datasheet and see what needs to be done; 

2. go through the library code and see how they did it.

3. or take a look at sample code. one example is here: 

https://github.com/dannyf00/My-MCU-Libraries---2nd-try/tree/master/STM32F0

  different devices but similar peripherals.
Posted on June 14, 2018 at 15:37

Thank u . I got output. 

Posted on June 14, 2018 at 15:38

Thank u so much. I followed your link and tried same thing in controller. Now i got the output. !!!!

Yadav
Associate II
Posted on June 16, 2018 at 13:52

Hi.. Now I am working on STM32F4Doscovery board timers. I want to make a timer interrupt for an application. It can be 1sec or 500ms interrupt. I have read the datasheet and written the program accordingly. But i dont know how to use interrupt in STM32. I have tried something, but nothing is happening. Can someone suggest me a solution??

#include 'stm32f4xx.h'

// Component selection

void GPIO_Init(void);

void tim10_init(void);

int main(void)

{

GPIO_Init();

tim10_init();

while (1)

{

GPIOD->ODR|=(1<<15);

}

}

void GPIO_Init(void)

{

RCC->AHB1ENR|=RCC_AHB1ENR_GPIODEN;

GPIOD->MODER|=(1<<28); // pin 14 is output

GPIOD->OTYPER&=~(1<<14);//pin 14 as push pull

GPIOD->OSPEEDR|=(1<<29)|(1<<28);//LOW speed

GPIOD->PUPDR&=~(1<<29)|(1<<28);//No push pull

GPIOD->MODER|=(1<<30); // pin 15 is output

GPIOD->OTYPER&=~(1<<15);//pin 15 as push pull

GPIOD->OSPEEDR|=(1<<30)|(1<<31);//LOW speed

GPIOD->PUPDR&=~(1<<30)|(1<<31);//No push pull

}

void tim10_init(void)

{

RCC->APB2ENR|=(1<<17);

TIM10->CR1|=(1<<7);

TIM10->DIER|=(1<<1);

TIM10->SR|=(1<<1);

TIM10->EGR|=(1<<1);

TIM10->CCMR1|=(1<<3)|(0<<1)|(0<<0);

TIM10->CCER|=(1<<0);

//TIM10->CCR1=5;

TIM10->PSC|=4200;

TIM10->ARR|=1000;

NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);

}

void TIM10_IRQHandler(void)

{

GPIOD->ODR|=(1<<14);

}

Posted on June 16, 2018 at 14:13

https://community.st.com/0D50X00009XkWGnSAN