cancel
Showing results for 
Search instead for 
Did you mean: 

ADC1 STM32F0

sky_brake
Associate II
Posted on January 30, 2014 at 03:56

Hi all,

I just started working with STM32F0. I want to try ADC1 in this board but I find some trouble. I want to turn on the LED (PC8 and PC9) by using ADC asbenchmark, so when ADC1 reach 2000 bit or7D0 then LED PC8 and PC9 will tun on.

I've create ADC code and compile it using cocoox, no error. but when I download it to the flash this kode not working. I've try to give input 0-3,2volt to ADC1 (PA1) and the LED is not turn on. this is the code


#include ''stm32f0xx_gpio.h''

#include ''stm32f0xx_rcc.h''

#include ''stm32f0xx_conf.h''

#include ''stm32f0xx_adc.h''


#define ADC_CFGR1_CONT ((uint32_t)0x00002000) /*!< 
Continuous
Conversion */



void pinout_config(void) {

GPIO_InitTypeDef GPIO_InitStruct;


RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

GPIO_InitStruct.GPIO_Pin
= 
GPIO_Pin_1
; // that's ADC1 (PA1 on STM32)

GPIO_InitStruct.GPIO_Mode
= 
GPIO_Mode_AN
;

GPIO_InitStruct.GPIO_PuPd
= 
GPIO_PuPd_NOPULL
;

GPIO_Init(GPIOA, &GPIO_InitStruct);


ADC_Cmd (ADC1,ENABLE); //enable ADC1

}


void led_config(void){

GPIO_InitTypeDef GPIO_InitStruct;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);


/*Configure GPIO pin */

GPIO_InitStruct.GPIO_Pin
= 
GPIO_Pin_8
|GPIO_Pin_9;

GPIO_InitStruct.GPIO_Mode
= 
GPIO_Mode_OUT
;

GPIO_InitStruct.GPIO_OType
= 
GPIO_OType_PP
;

GPIO_InitStruct.GPIO_PuPd
= 
GPIO_PuPd_NOPULL
;

GPIO_InitStruct.GPIO_Speed
= 
GPIO_Speed_2MHz
;

GPIO_Init(GPIOC, &GPIO_InitStruct);

}


int main(void)

{

pinout_config();

led_config();



while(1)

{

void LED_set (void)

{

if(ADC1>2000)

{

(GPIOC->BSRR = GPIO_Pin_8);

(GPIOC->BSRR = GPIO_Pin_9);

}

else {

(GPIOC->BRR = GPIO_Pin_8);

(GPIOC->BRR = GPIO_Pin_9);

}

}

}

}
can someone tell me what is wrong with the code?
thank you for your help

#adc #led
5 REPLIES 5
Posted on January 30, 2014 at 05:17

Lack of compilation errors means no syntax issues, it doesn't mean the code is valid. Your example lacks configuration of the ADC, and association of the pin with the channels to be sampled.

/* ADC1 STM32F0-Discovery sourcer32@gmail.com */
#include ''stm32f0xx.h''
#include ''stm32f0_discovery.h''
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; // PA.1 ADC IN1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void ADC1_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
/* ADC1 DeInit */
ADC_DeInit(ADC1);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
/* Convert the ADC1 input with 5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, 1, ADC_SampleTime_55_5Cycles); // PA.1 - IN1
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
}
int main(void)
{
GPIO_Configuration();
ADC1_Configuration();
while(1)
{
uint16_t ADC1ConvertedValue;
/* Test EOC flag */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Get ADC1 converted data */
ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
if (ADC1ConvertedValue > 2000)
{
GPIOC->BSRR = GPIO_Pin_8;
GPIOC->BSRR = GPIO_Pin_9;
}
else
{
GPIOC->BRR = GPIO_Pin_8;
GPIOC->BRR = GPIO_Pin_9;
}
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sky_brake
Associate II
Posted on January 30, 2014 at 07:10

thanks for your respon clive.

I've try debug your code to my board but it doesn't work and in my compiler apear a message (No source available for '''') 

this link to my compile screen capture 

http://www.reduceimages.com/download.php?image=e8db48c6fa

sky_brake
Associate II
Posted on January 30, 2014 at 08:29

in console tab this message appear

Reading symbols from C:\CooCox\CoIDE\workspace\ADC Rev1.2\ADC Rev1.2\Debug\bin\ADC Rev1.2.elf...done.

file ''C:/CooCox/CoIDE/workspace/ADC Rev1.2/ADC Rev1.2/Debug/bin/ADC Rev1.2.elf''

file ''C:/CooCox/CoIDE/workspace/ADC Rev1.2/ADC Rev1.2/Debug/bin/ADC Rev1.2.elf''

set tdesc filename C:/CooCox/CoIDE/bin/target_desc/arm-with-m.xml

target remote 127.0.0.1:2009

Reset_Handler () at C:\CooCox\CoIDE\workspace\ADC Rev1.2\cmsis_boot\startup\startup_stm32f0xx.s:74

74  movs r1, #0

tbreak main

Temporary breakpoint 1 at 0x800049e: file C:\CooCox\CoIDE\workspace\ADC Rev1.2\main.c, line 66.

continue

Temporary breakpoint 1, main () at C:\CooCox\CoIDE\workspace\ADC Rev1.2\main.c:66

66  GPIO_Configuration();

kill

I have no idea about it. what should I do?

Posted on January 30, 2014 at 20:20

I have no idea about it. what should I do?

Use a different tool chain? I don't use CooCox, suggest you download an evaluation of Keil uv 4.7x
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sky_brake
Associate II
Posted on February 04, 2014 at 04:13

I found the problem.I have not entered this code

ADC_ChannelConfig(ADC1,ADC_Channel_1,ADC_SampleTime_1_5Cycles);