STM32F1, Delay stimmt nicht

Gast #4878918
Lesenswert?

Hallo,

auf einem STM32F1 Board (kein Discovery) ohne externen Quartz versuche 
ich einen LED mit 500ms Verzögerung zu toggeln. Dabei stimmt der Delay 
nicht.
Wenn ich die SysTick_Config(SystemCoreClock / 1400) so setze, stimmt 
Delay so ungefähr.

Weiss jemand, woran das liegt?
Danke!
1
#include "stm32f10x.h"
2

3
static __IO uint32_t TimingDelay;
4
void Delay(uint32_t nTime);
5
void TimingDelay_Decrement(void);
6

7

8
int main(void)
9
{
10
  GPIO_InitTypeDef GPIO_InitStructure;
11

12
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
13
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
14

15
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
16
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
17
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
18
  GPIO_Init(GPIOB, &GPIO_InitStructure);
19

20
  GPIO_SetBits(GPIOB, GPIO_Pin_2);
21

22
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
23
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
24
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
25
  GPIO_Init(GPIOC, &GPIO_InitStructure);
26

27
  SysTick_Config(SystemCoreClock / 1000);
28
  
29
  while (1)
30
  {
31
    GPIO_ResetBits(GPIOB, GPIO_Pin_2);
32
    GPIO_ResetBits(GPIOC, GPIO_Pin_13); 
33

34
    Delay(500);
35

36
    GPIO_SetBits(GPIOB, GPIO_Pin_2);
37
    GPIO_SetBits(GPIOC, GPIO_Pin_13);
38

39
    Delay(500);
40
  }
41
}
42

43
void Delay(uint32_t nTime)
44
{ 
45
  TimingDelay = nTime;
46

47
  while(TimingDelay != 0);
48
}
49

50

51
void TimingDelay_Decrement(void)
52
{
53
  if (TimingDelay != 0x00)
54
  { 
55
    TimingDelay--;
56
  }
57
}
58

59

60
#ifdef  USE_FULL_ASSERT
61

62
void assert_failed(uint8_t* file, uint32_t line)
63
{ 
64
  /* User can add his own implementation to report the file name and line number,
65
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
66

67
  /* Infinite loop */
68
  while (1)
69
  {
70
  }
71
}
72

73
#endif
Gast #4878934
Lesenswert?

TimingDelay_Decrement() dürfte im Systick Handler, also im Interrupt 
laufen.

Deshalb muss TimingDelay volatile sein. Könnte aber auch sein, dass das 
__IO Macro genau das bewirkt.

Das muss aber nicht den von Dir beobachteten Effekt bewirken.
Persönliche Seite #4878942
Lesenswert?

Probiere mal in der main die Funktion SystemInit() und 
SystemCoreClockUpdate() aufzurufen.

einfügen schrieb:
> Deshalb muss TimingDelay volatile sein. Könnte aber auch sein, dass das
> __IO Macro genau das bewirkt.

volatile ist äquivalent zu __IO in der SPL.

Sonst mal checken ob der Systemtakt stimmt (am Port messen, siehe RM: 
RCC-Register)

Gruß
Daniel

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren