AnsweredAssumed Answered

STM32F405 ADC Project

Question asked by rivera.amador on Apr 28, 2015
Latest reply on Apr 28, 2015 by baird.hal.001
Hello,

I am new to mcu programming and am working on a drone project which uses the STM32F405. Here 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.

01.static bool isInit = false;
02.static float temp;
03.static float vref;
04. 
05.void tmp36Task(void *param);
06. 
07.void tmp36Init()
08.{
09.    if (isInit) return;
10. 
11.    /* Define ADC init structures */
12.    ADC_InitTypeDef       ADC_InitStructure;
13.    ADC_CommonInitTypeDef ADC_CommonInitStructure;
14.    GPIO_InitTypeDef GPIO_InitStructure;
15. 
16.    /* IMPORTANT: populates structures with reset values */
17.    ADC_StructInit(&ADC_InitStructure);
18.    ADC_CommonStructInit(&ADC_CommonInitStructure);
19. 
20.    /* enable ADC clock */
21.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
22.    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
23. 
24.    /* init GPIO */
25.    GPIO_StructInit(&GPIO_InitStructure);
26.    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6;
27.    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
28.    GPIO_InitStructure.GPIO_Speed = GPIO_Low_Speed;
29.    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
30.    GPIO_Init(GPIOA, &GPIO_InitStructure);
31. 
32.    /* init ADCs in independent mode, div clock by two */
33.    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
34.    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
35.    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
36.    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
37.    ADC_CommonInit(&ADC_CommonInitStructure);
38. 
39.    /* init ADC1: 12bit, single-conversion */
40.    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
41.    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
42.    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
43.    ADC_InitStructure.ADC_ExternalTrigConvEdge = 0;
44.    ADC_InitStructure.ADC_ExternalTrigConv = 0;
45.    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
46.    ADC_InitStructure.ADC_NbrOfConversion = 1;
47.    ADC_Init(ADC1, &ADC_InitStructure);
48. 
49.    /* Enable VREF_INT & Temperature channel */
50.    ADC_TempSensorVrefintCmd(ENABLE);
51. 
52.    /* Enable ADC1 **************************************************************/
53.    ADC_Cmd(ADC1, ENABLE);
54. 
55.    xTaskCreate(tmp36Task, (const signed char * const)"tmp36",
56.            configMINIMAL_STACK_SIZE, NULL, 3, NULL);
57. 
58.    isInit = true;
59.}
60. 
61.bool tmp36Test(void)
62.{
63.    return isInit;
64.}
65. 
66.float adc_read(ADC_TypeDef* ADCx, uint8_t channel, uint8_t ADC_SampleTime) {
67.    /* Configure Channel */
68.    ADC_RegularChannelConfig(ADCx, channel, 1, ADC_SampleTime);
69. 
70.    /* check if conversion was started, if not start */
71.    ADC_SoftwareStartConv(ADCx);
72. 
73.    /* wait for end of conversion */
74.    while((ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) == RESET));
75. 
76.    return (float)ADC_GetConversionValue(ADCx);
77.}
78. 
79.void tmp36Task(void *param)
80.{
81.    float val;
82.    float mv;
83.    float steps;
84. 
85.    vTaskSetApplicationTaskTag(0, (void*)TASK_ADC_ID_NBR);
86.    vTaskDelay(1000);
87. 
88.    while(1)
89.    {
90.        val = adc_read(ADC1, ADC_Channel_7, ADC_SampleTime_480Cycles); //TMP36
91.        vref = adc_read(ADC1, ADC_Channel_17, ADC_SampleTime_480Cycles);
92. 
93.        //For onboard temp sensor (not TMP36)
94.        steps = (vref * 10) / 12;
95.        mv = (val * 1000) / steps;
96.        temp = (mv - 500) / 10;
97.    }
98.}

The sensor that I am testing with is the TMP36 temperature sensor.

Outcomes