2022-10-09 01:39 AM
Hello,
I want to read 8 bit ADC value from ADC2(PA1 pin) and use it to control a LED which is connected to DAC1(PA4 pin). I wrote a code(using registers) as below, however, I cannot read the ADC value(I tested it on STM Studio). Where is my problem? Thanks.
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
void ADC_Config();
void RCC_Config();
void GPIO_Config();
void DAC1_Config();
uint8_t c=0;
uint8_t adcread();
int main(void)
{
ADC_Config();
RCC_Config();
GPIO_Config();
DAC1_Config();
while (1)
{
c= adcread();
DAC->DHR8RD =c;
}
}
void DAC1_Config(){
RCC->APB1ENR|= 0x20000000;
DAC->CR|= 0x00000001;
DAC->SWTRIGR=0;
DAC->DHR8R1|=0;
}
void ADC_Config(){
RCC->APB2ENR |=0x00000200;
ADC2->CR1|=0x02000000;
ADC2->CR2|=0x00000001;
ADC2->SMPR2|=0x00000018;
ADC->CCR|=0x00010000;
}
void RCC_Config(){
RCC->CR |= 0x00030000; // HSEON and HSEONRDY enable
while(!(RCC->CR & 0x00020000)); // HSEON Ready Flag wait
RCC->CR |= 0x00080000; // CSS Enable
RCC->PLLCFGR |= 0x00400000; // PLL e HSE seçtik
RCC->PLLCFGR |= 0x00000004; // PLL M = 4
RCC->PLLCFGR |= 0x00005A00; // Pll N = 168
RCC->PLLCFGR |= 0x00000000; // PLL p = 2
RCC->CFGR |= 0x00000000; // AHB Prescaler = 1
RCC->CFGR |= 0x00080000; // APB2 Prescaler = 2
RCC->CFGR |= 0x00001400; // APB1 Prescaler = 4
RCC->CIR |= 0x00800000; // CSS Flag clear
}
void GPIO_Config(){
RCC->AHB1ENR |= 0x00000001;
GPIOA->MODER |= 0x0000000C;
GPIOA->OSPEEDR|= 0x000000C;
}
uint8_t adcread(){
uint8_t value=0;
ADC2->CR2 =0x40000000;
while(!(ADC2->SR&0x00000002));
value=ADC2->DR;
return value;
}
void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size){
/* TODO, implement your code here */
return;
}
uint16_t EVAL_AUDIO_GetSampleCallBack(void){
/* TODO, implement your code here */
return -1;
}