Forum: Mikrocontroller und Digitale Elektronik STM32F4 - Temperatursensor


von Sebastian (Gast)


Lesenswert?

Hallo liebe Leute,

ich möchte gerne über mein STM32F4 Nucleo Board die aktuelle Temperatur 
über einen TMP03 Sensor auslesen und auf einem Display anzuzeigen. Das 
Display läuft gut, jedoch habe ich Probleme das PWM-Signal auszuwerten.

Es gibt bereits hier im forum für einen anderen MC ein Codebeispiel, mit 
dem ich aber leider nichts anfangen kann: 
Beitrag "Analog Devices TMP04 Temperatursensor Testprogramm"

Die genaue Spezifikation des TMP03 findet mann hier:
http://www.analog.com/static/imported-files/data_sheets/TMP03_04.pdf

Unser bisheriger Code orientiert sich daran, liefert aber leider falsche 
Ergebnisse:
1
void TIM4_IRQHandler(void)
2
{
3
  int temperatur;
4
  int t1, t2;
5
  
6
  RCC_ClocksTypeDef RCC_Clocks;
7
  RCC_GetClocksFreq(&RCC_Clocks);
8
9
  /* Clear TIM4 Capture compare interrupt pending bit */
10
  TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
11
12
  /* Get the Input Capture value */
13
  IC2Value = TIM_GetCapture2(TIM4);
14
15
  if (IC2Value != 0)
16
  {
17
    DutyCycle = (TIM_GetCapture1(TIM4) * 100) / IC2Value;         //Input Capture 2
18
    Frequency = (RCC_Clocks.HCLK_Frequency)/2 / IC2Value;
19
    
20
  t1=DutyCycle;
21
  t2=Frequency-DutyCycle;
22
23
  temperatur=235-400*t1/t2;
24
  }
25
  else
26
  {
27
    DutyCycle = 0;
28
    Frequency = 0;
29
  }
30
}


Hier noch die Timer Config:
1
int main(void)
2
{
3
  /* TIM Configuration */
4
  TIM_Config();
5
6
  /* TIM4 configuration: PWM Input mode ------------------------
7
     The external signal is connected to TIM4 CH2 pin (PB.07), 
8
     The Rising edge is used as active edge,
9
     The TIM4 CCR2 is used to compute the frequency value 
10
     The TIM4 CCR1 is used to compute the duty cycle value
11
  ------------------------------------------------------------ */
12
13
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
14
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
15
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
16
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
17
  TIM_ICInitStructure.TIM_ICFilter = 0x0;
18
19
  TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);
20
21
  /* Select the TIM4 Input Trigger: TI2FP2 */
22
  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
23
24
  /* Select the slave Mode: Reset Mode */
25
  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
26
  TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
27
28
  /* TIM enable counter */
29
  TIM_Cmd(TIM4, ENABLE);
30
31
  /* Enable the CC2 Interrupt Request */
32
  TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
33
}
34
35
36
void TIM_Config(void)
37
{
38
  GPIO_InitTypeDef GPIO_InitStructure;
39
  NVIC_InitTypeDef NVIC_InitStructure;
40
41
  /* TIM4 clock enable */
42
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
43
44
  /* GPIOB clock enable */
45
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
46
  
47
  /* TIM4 chennel2 configuration : PB.07 */
48
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
49
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
50
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
51
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
52
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
53
  GPIO_Init(GPIOB, &GPIO_InitStructure);
54
  
55
  /* Connect TIM pin to AF2 */
56
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
57
58
  /* Enable the TIM4 global Interrupt */
59
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
60
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
61
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
62
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
63
  NVIC_Init(&NVIC_InitStructure);
64
65
}

Ich hoffe mir kann hier jemand helfen ;) Bin schon am verzweifeln.

lg Sebastian

von RP6conrad (Gast)


Lesenswert?

Soll den Pin PB07 nicht als Eingang konfiguriert werden ? Sowohl AF und 
PP scheinen mir falsch.
 /* TIM4 chennel2 configuration : PB.07 */
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF; = Alternate function
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; = Push pull
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

Fur meinen STM32F100 discovery functioniert das mit :
 /* TIM3 channel 2 pin (PA.07) configuration */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

von Sebastian (Gast)


Lesenswert?

Ich hab mal GPIO_OType_PP durch GPIO_PuPd_UP ersetzt, die Werte stimmen 
aber immer noch nicht.
GPIO_Mode_AF sollte aber schon richtig sein oder?


Ich glaube der Fehler liegt bei mir in der Berechnung. Kenn mich mit den 
Frequenzen und PWM einfach nicht aus.

Vielleicht kennt ja jemand diesen oder ähnlichen Sensor ;)

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.