2020-05-10 03:21 PM
#include <stdio.h>
#include "diag/Trace.h"
#include "stm32f0xx.h"
#include "stm32f0xx_conf.h"
#include "pin.h"
#include "adc.h"
int main(int argc, char* argv[])
{
//(#) ADC pins (pA1) configuration --------------------------------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//leds (pB0... pB3) configuration--------------------------------------------------------------
GPIO_InitTypeDef GPIO_LEDs;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_LEDs.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_LEDs.GPIO_Mode = GPIO_Mode_OUT;
GPIO_LEDs.GPIO_OType = GPIO_OType_PP;
GPIO_LEDs.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_LEDs.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_LEDs);
//ADC-------------------------------------------------------------------------------------------
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
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);
ADC_GetCalibrationFactor(ADC1);
ADC_Cmd(ADC1, ENABLE);
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN) == RESET);
ADC_StartOfConversion(ADC1);
uint16_t valueADC = ADC_GetConversionValue(ADC1);
ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_1_5Cycles);
//dma--------------------------------------------------------------------------------------
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr= (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&valueADC;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
ADC_DMACmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion
return ADC_GetConversionValue(ADC1); //Return the converted data
ADC_StartOfConversion(ADC1);
while (1)
if(valueADC<2000){
GPIO_SetBits(GPIOB, GPIO_Pin_0);
}
else if(valueADC>2000){
GPIO_SetBits(GPIOB, GPIO_Pin_1);
}
}
// Infinite loop, never return.
2020-05-10 06:34 PM
return ADC_GetConversionValue(ADC1); //Return the converted data
The main() function will return when it hits this statement. It never gets to the main loop.
2020-05-10 06:51 PM
It would be better if you create a function other than main and read the adc operation and perform the cycle inside that function and call it in the location in the main.
2020-05-10 10:35 PM
should I delete this part completely?
2020-05-10 10:38 PM
yes you are right. but it is easier for me to write everything in the main.
2020-05-11 12:35 AM
uint32_t valueADC (void){
ADC_RegularChannelConfig(ADC1,ADC_Channel_9,1,ADC_SampleTime_56Cycles);
ADC_SoftwareStartConv(ADC1);
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
return ADC_GetConversionValue(ADC1);
}
int main(int argc, char* argv[])
{
//(#) ADC pins (pA1) configuration --------------------------------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//leds (pB0... pB3) configuration--------------------------------------------------------------
GPIO_InitTypeDef GPIO_LEDs;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_LEDs.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_LEDs.GPIO_Mode = GPIO_Mode_OUT;
GPIO_LEDs.GPIO_OType = GPIO_OType_PP;
GPIO_LEDs.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_LEDs.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_LEDs);
//ADC-------------------------------------------------------------------------------------------
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
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);
ADC_GetCalibrationFactor(ADC1);
ADC_Cmd(ADC1, ENABLE);
uint16_t valueADC = ADC_GetConversionValue(ADC1);
//dma--------------------------------------------------------------------------------------
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr= (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&valueADC;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
ADC_DMACmd(ADC1, ENABLE);
while (1)
uint16_t valueADC = ADC_GetConversionValue(ADC1);
if(valueADC<2000){
GPIO_SetBits(GPIOB, GPIO_Pin_0);
}
else if(valueADC>2000){
GPIO_SetBits(GPIOB, GPIO_Pin_1);
}
}
I also recommend you to try it.
2020-05-11 02:02 AM
I write with STM32f0, so I have to change your code a bit.
but that doesn't work yet.
ADC_ChannelConfig(ADC1,ADC_Channel_9,ADC_SampleTime_55_5Cycles);
ADC_StartOfConversion(ADC1);
2020-05-11 02:19 AM
Set the cycle for yourself, but it will work like this.
2020-05-11 02:25 AM
uint32_t ReadValue;
uint32_t valueADC (void){
ADC_RegularChannelConfig(ADC1,ADC_Channel_9,1,ADC_SampleTime_56Cycles);
ADC_SoftwareStartConv(ADC1);
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
return ADC_GetConversionValue(ADC1);
}
int main(int argc, char* argv[])
{
//(#) ADC pins (pA1) configuration --------------------------------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//leds (pB0... pB3) configuration--------------------------------------------------------------
GPIO_InitTypeDef GPIO_LEDs;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_LEDs.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_LEDs.GPIO_Mode = GPIO_Mode_OUT;
GPIO_LEDs.GPIO_OType = GPIO_OType_PP;
GPIO_LEDs.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_LEDs.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_LEDs);
//ADC-------------------------------------------------------------------------------------------
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
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);
ADC_GetCalibrationFactor(ADC1);
ADC_Cmd(ADC1, ENABLE);
//dma--------------------------------------------------------------------------------------
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr= (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&valueADC;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
ADC_DMACmd(ADC1, ENABLE);
while (1)
ReadValue = valueADC();
if(ReadValue<2000){
GPIO_SetBits(GPIOB, GPIO_Pin_0);
}
else if(ReadValue>2000){
GPIO_SetBits(GPIOB, GPIO_Pin_1);
}
}
Would you try again this way? Please
2020-05-11 02:42 AM
in STM32f0xx we have not code "ADC_RegularChannelConfig..." we have ADC_ChannelConfig(ADC1,ADC_Channel_9,ADC_SampleTime_55_5Cycles);
and we have not "ADC_SoftwareStartConv(ADC1);.."