From: AceVest Date: Mon, 27 May 2019 08:32:16 +0000 (+0800) Subject: SysTick Delay X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/doc/datamaps.china.min.js?a=commitdiff_plain;h=a46921e1eeb693cb1b2e4d1155d3b8ad30940266;p=acecode.git SysTick Delay --- diff --git a/learn/stm32/F103/.settings/language.settings.xml b/learn/stm32/F103/.settings/language.settings.xml index f90e2ce..5ccf38d 100644 --- a/learn/stm32/F103/.settings/language.settings.xml +++ b/learn/stm32/F103/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/learn/stm32/F103/F103 Run.cfg b/learn/stm32/F103/F103 Run.cfg index 0102e6a..023afef 100644 --- a/learn/stm32/F103/F103 Run.cfg +++ b/learn/stm32/F103/F103 Run.cfg @@ -3,7 +3,7 @@ # Generated by System Workbench for STM32 # Take care that such file, as generated, may be overridden without any early notice. Please have a look to debug launch configuration setup(s) -source [find interface/stlink-tcp.cfg] +source [find interface/stlink.cfg] set WORKAREASIZE 0x5000 diff --git a/learn/stm32/F103/src/main.c b/learn/stm32/F103/src/main.c index e87669d..20439a6 100644 --- a/learn/stm32/F103/src/main.c +++ b/learn/stm32/F103/src/main.c @@ -19,6 +19,8 @@ unsigned int pin = 9; void SystemInit() { InitSystemClock(); + InitSysTick(); + // 使能时钟 RCCEnableGPIOBClock(); @@ -26,10 +28,7 @@ void SystemInit() { } void delay() { - Delay(); - return; - int i=965536; - while(i--); + Delay(200); } int main() { diff --git a/learn/stm32/F103/src/systick.c b/learn/stm32/F103/src/systick.c index 646efc6..6fd6a4f 100644 --- a/learn/stm32/F103/src/systick.c +++ b/learn/stm32/F103/src/systick.c @@ -24,13 +24,41 @@ #define SYSTICK_CTRL_ENABLE_MASK (1 << SYSTICK_CTRL_ENABLE_POS) #define SYSTICK_CTRL_ENABLE (1 << SYSTICK_CTRL_ENABLE_POS) -void Delay() { +uint32_t gSysTickCnt = 0; +void SysTick_Handler() { + gSysTickCnt++; +} + +// 每1ms触发一次SysTick中断 +void InitSysTick() { + SysTick->CTRL &= ~SYSTICK_CTRL_ENABLE_MASK; + SysTick->CTRL &= ~SYSTICK_CTRL_TICKINT_MASK; + SysTick->CTRL &= ~SYSTICK_CTRL_CLKSOURCE_MASK; + + + SysTick->LOAD = 9000; + SysTick->VAL = 0; + + + SysTick->CTRL |= SYSTICK_CTRL_ENABLE | SYSTICK_CTRL_TICKINT_ENABLE; +} + +void Delay(uint32_t ms) { + gSysTickCnt = 0; + while(gSysTickCnt < ms) { + + } +} + +// 非中断版本的Delay +// ms 最大值为1864 +void NoneIntDelay(uint32_t ms) { SysTick->CTRL &= ~SYSTICK_CTRL_ENABLE_MASK; SysTick->CTRL &= ~SYSTICK_CTRL_TICKINT_MASK; SysTick->CTRL &= ~SYSTICK_CTRL_CLKSOURCE_MASK; - SysTick->LOAD = 9000*1000; // 1000ms + SysTick->LOAD = 9000*ms; SysTick->VAL = 0; diff --git a/learn/stm32/F103/src/systick.h b/learn/stm32/F103/src/systick.h index ac32069..51821b3 100644 --- a/learn/stm32/F103/src/systick.h +++ b/learn/stm32/F103/src/systick.h @@ -20,6 +20,7 @@ typedef struct { #define SysTick ((SysTick_t*) 0xE000E010) - -void Delay(); +void InitSysTick(); +void Delay(uint32_t ms); +void NoneIntDelay(uint32_t ms); #endif /* SYSTICK_H_ */