cancel
Showing results for 
Search instead for 
Did you mean: 

ADC of stm32f103c8 not working

ALE1
Associate II

i would like to switch the LED ON (PB0) when ADC is from above threshold value ''if (adcValue >2275)''

and in case of below threshold it should switch LED ON (PB1)

code showing no error in Keil , if you see any erroe plz highlight me thanks.

//************************************** ADC simple software trigger

#include <stdio.h>

#include "stm32f10x.h"         // Device header

#include "stm32f10x_adc.h"       // Keil::Device:StdPeriph Drivers:ADC

#include "stm32f10x_rcc.h"       // Keil::Device:StdPeriph Drivers:RCC

#include "stm32f10x_gpio.h"       // Keil::Device:StdPeriph Drivers:GPIO

GPIO_InitTypeDef    GPIO_InitStructure;

ADC_InitTypeDef     ADC_InitStructure;

int main ()  

{

uint16_t adcValue;

//----------------------------------------------------------------------------------------

// ******************************ADC ******************************                    

//----------------------------------------------------------------------------------------

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);

 //RCC_ADCCLKConfig(RCC_PCLK2_Div6); //clock for ADC (max 14MHz --> 72/6=12MHz) APB2 operates at full speed 72 MHz

 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;  //Analog signal Input

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

// TWO LEDS that will blink when voltage is raised to above threshold

GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0| GPIO_Pin_1;

 GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;

 GPIO_Init(GPIOB, &GPIO_InitStructure);

 ADC_InitStructure.ADC_Mode =ADC_Mode_Independent; //one ADC used, other is dual mode.

 ADC_InitStructure.ADC_ScanConvMode     = DISABLE; //ONLY single channel conversion is used.

 ADC_InitStructure.ADC_ContinuousConvMode  = ENABLE; //the ADC starts another conversion as soon as it finishes one.

ADC_InitStructure.ADC_DataAlign       = ADC_DataAlign_Right;

ADC_InitStructure.ADC_NbrOfChannel=1;

ADC_InitStructure.ADC_ExternalTrigConv= DISABLE;

ADC_InitStructure.ADC_ExternalTrigConv= ADC_ExternalTrigConv_None;

 ADC_Init(ADC1, &ADC_InitStructure);   //init ADC

// Select input channel for ADC1

// ADC1 channel 0 (PA0)

ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_13Cycles5);

// Start ADC conversion

ADC_SoftwareStartConvCmd(ADC1, ENABLE);

// Wait until ADC conversion finished

while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))

{

adcValue= ADC_GetConversionValue(ADC1);

}

  

if (adcValue >2275)

{

GPIOB->BSRR = GPIO_BSRR_BS0; // set GPIOB.0

}

if (adcValue < 2275)

{

GPIOB->BSRR = GPIO_BSRR_BS1; // set GPIOB.1

}

}

0 REPLIES 0