We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I'm trying to use an ADC in my STM32F3 Discovery. I've prepared a code as follows
#include "stm32f30x.h" #include "stm32f30x_rcc.h" #include "stm32f30x_adc.h" #include <stm32f30x_gpio.h> #define LED (GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15) uint16_t convertedValue= 0; void delay_ms (int time) { int i; for (i = 0; i < time * 4000; i++) {} } void Configure_ADC(void) { RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12,ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); ADC_DeInit(ADC1); ADC_InitTypeDef ADC_InitStructure; ADC_InitStructure.ADC_Resolution = ADC_Resolution_10b; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_NbrOfRegChannel = 1; ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable; ADC_Init(ADC1,&ADC_InitStructure); ADC_RegularChannelConfig(ADC1,ADC_Channel_2, 1, ADC_SampleTime_7Cycles5); ADC_Cmd(ADC1, ENABLE); } uint16_t Read_ADC() { while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY)); ADC_StartConversion(ADC1); while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET); return ADC_GetConversionValue(ADC1); } int main() { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Pin = LED; GPIO_Init(GPIOE, &GPIO_InitStructure); uint32_t led = 0; while(1) { convertedValue = Read_ADC();//Read the ADC converted value GPIO_SetBits(GPIOE, 1 << led); //turn on a diode delay_ms(150); // wait GPIO_ResetBits(GPIOE, 1 << led); //turn off a diode if (++led >= 16) { led = 8; } } }
The problem is that the ADC doesn't work. A program stops on Read_ADC() function in the main loop. Have I missed something?