2015-04-27 03:33 PM
Hello,
I am new to mcu programming and am working on a drone project which uses the STM32F is how the pins are laid out on this drone. I am using pin PA7 which according the the datasheet for this mcu, it is correlated to ADC Channel 7. When reading the converted values, I do see that there is a voltage, but when I apply heat to the sensor I do not see any changes to the values.
static bool isInit = false;
static float temp;
static float vref;
void tmp36Task(void *param);
void tmp36Init()
{
if (isInit) return;
/* Define ADC init structures */
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* IMPORTANT: populates structures with reset values */
ADC_StructInit(&ADC_InitStructure);
ADC_CommonStructInit(&ADC_CommonInitStructure);
/* enable ADC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* init GPIO */
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Speed = GPIO_Low_Speed;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* init ADCs in independent mode, div clock by two */
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* init ADC1: 12bit, single-conversion */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = 0;
ADC_InitStructure.ADC_ExternalTrigConv = 0;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* Enable VREF_INT & Temperature channel */
ADC_TempSensorVrefintCmd(ENABLE);
/* Enable ADC1 **************************************************************/
ADC_Cmd(ADC1, ENABLE);
xTaskCreate(tmp36Task, (const signed char * const)''tmp36'',
configMINIMAL_STACK_SIZE, NULL, 3, NULL);
isInit = true;
}
bool tmp36Test(void)
{
return isInit;
}
float adc_read(ADC_TypeDef* ADCx, uint8_t channel, uint8_t ADC_SampleTime) {
/* Configure Channel */
ADC_RegularChannelConfig(ADCx, channel, 1, ADC_SampleTime);
/* check if conversion was started, if not start */
ADC_SoftwareStartConv(ADCx);
/* wait for end of conversion */
while((ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) == RESET));
return (float)ADC_GetConversionValue(ADCx);
}
void tmp36Task(void *param)
{
float val;
float mv;
float steps;
vTaskSetApplicationTaskTag(0, (void*)TASK_ADC_ID_NBR);
vTaskDelay(1000);
while(1)
{
val = adc_read(ADC1, ADC_Channel_7, ADC_SampleTime_480Cycles); //TMP36
vref = adc_read(ADC1, ADC_Channel_17, ADC_SampleTime_480Cycles);
//For onboard temp sensor (not TMP36)
steps = (vref * 10) / 12;
mv = (val * 1000) / steps;
temp = (mv - 500) / 10;
}
}
The sensor that I am testing with is the
https://learn.adafruit.com/downloads/pdf/tmp36-temperature-sensor.pdf
temperature sensor. #adc #gpio #adc_channel2015-04-27 07:00 PM
Your words say PA7 but your code says PA6.
Cheers, Hal