cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 reading internal temperature sensor and reference voltage at the same time.

felisleonline
Associate
Posted on August 02, 2016 at 21:25

Hi ,

I interested in measuring STM32F0 temperature and reference voltage using internal sensor of it's processor at the same time using UART. so I used channels like this;

ADC_ChannelConfig(ADC1, ADC_Channel_16 , ADC_SampleTime_55_5Cycles);

ADC_TempSensorCmd(ENABLE);

ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_55_5Cycles);

ADC_VrefintCmd(ENABLE);

Now I can measured only one values properly..If I applied to read two channel at the same time.I mixed volues eachother. I also looked for multichannel adc titles in the forum but it didn't work properly again. I think should use dma but I'm not good now.Thank you in advance for your help.

Regards.
2 REPLIES 2
Posted on August 02, 2016 at 22:44

You must use DMA to load both values into an array, use the DMA TC interrupt as an ADC EOC equivalent.

Review

STM32F0xx_StdPeriph_Lib_V1.5.0\Projects\STM32F0xx_StdPeriph_Examples\ADC\ADC_DMA\main.c

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on August 02, 2016 at 22:59

This should demonstrate

// STM32 ADC 2 Channel DMA STM32F0-Discovery sourcer32@gmail.com
#include ''stm32f0xx.h''
//**************************************************************************************
#define SAMPLES 4
__IO uint16_t RegularConvData[SAMPLES]; // 2 Samples to half
//**************************************************************************************
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* DMA1 clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
/* DMA1 Channel1 Config */
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&RegularConvData[0];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = SAMPLES;
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);
/* DMA1 Channel1 enable */
DMA_Cmd(DMA1_Channel1, ENABLE);
}
//**************************************************************************************
void ADC_Config(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 continuous mode withe a resolution 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 temperature sensor with 5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor, ADC_SampleTime_55_5Cycles);
ADC_TempSensorCmd(ENABLE);
/* Convert the ADC1 Vref with 5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint, ADC_SampleTime_55_5Cycles);
ADC_VrefintCmd(ENABLE);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
/* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
/* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable the ADC peripheral */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADRDY flag */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
}
//**************************************************************************************
void DMA1_Channel1_IRQHandler(void)
{
/* Test on DMA1 Channel1 Transfer Complete interrupt */
if (DMA_GetITStatus(DMA1_IT_HT1))
{
/* Clear DMA1 Channel1 Half Transfer interrupt pending bits */
DMA_ClearITPendingBit(DMA1_IT_HT1);
GPIOC->BSRR = 0x300; /* Set PC8 and PC9 */
}
/* Test on DMA1 Channel1 Transfer Complete interrupt */
if (DMA_GetITStatus(DMA1_IT_TC1))
{
/* Clear DMA1 Channel1 Transfer Complete interrupt pending bits */
DMA_ClearITPendingBit(DMA1_IT_TC1);
GPIOC->BRR = 0x300;/* Reset PC8 and PC9 */
}
}
//**************************************************************************************
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable and set DMA1_Channel1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//**************************************************************************************
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* Configure PC8 and PC9 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
//**************************************************************************************
int main(void)
{
NVIC_Config();
GPIO_Config();
DMA_Config();
ADC_Config();
while(1); /* Infinite loop */
}
//**************************************************************************************
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
//**************************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..