2018-06-11 09:37 PM
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-l4Solved! Go to Solution.
2018-06-14 03:11 AM
'
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.2018-06-13 06:53 PM
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 pullRCC->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);}2018-06-13 11:00 PM
>>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.
2018-06-13 11:45 PM
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
2018-06-14 02:54 AM
SQR3 tells that conversion is done on ADC123_IN0, but PA1 is ADC123_IN1
2018-06-14 03:11 AM
'
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.2018-06-14 08:37 AM
Thank u . I got output.
2018-06-14 08:38 AM
Thank u so much. I followed your link and tried same thing in controller. Now i got the output. !!!!
2018-06-16 04:52 AM
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 selectionvoid 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 pullGPIOD->OSPEEDR|=(1<<29)|(1<<28);//LOW speedGPIOD->PUPDR&=~(1<<29)|(1<<28);//No push pullGPIOD->MODER|=(1<<30); // pin 15 is output
GPIOD->OTYPER&=~(1<<15);//pin 15 as push pullGPIOD->OSPEEDR|=(1<<30)|(1<<31);//LOW speedGPIOD->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);}
2018-06-16 07:13 AM