C文件
#include "SystemTick.h"
#include "gd32f30x.h"
#include "gd32f30x_rcu.h"
static volatile SYSTEM_TICK_TYPE gSystickOverCnt = 0;
static int gSysTickPerUs = 0;
void SysTick_Handler(){ ++gSystickOverCnt;}
#define SYSTICK_REAL (SYSTEM_TICK_TYPE)((~SysTick->VAL)&0xFFFFFF | (gSystickOverCnt)<<24)
SYSTEM_TICK_TYPE GetSystemRealTick(){return SYSTICK_REAL;}
void delay_us(unsigned int us){
SYSTEM_TICK_TYPE dest = SYSTICK_REAL + us*gSysTickPerUs;
while( SYSTICK_REAL < dest );
}
SYSTEM_TICK_TYPE us2SystemTick(unsigned int us){
return us*gSysTickPerUs;
}
int IsSystemTimeOut(SYSTEM_TICK_TYPE startTick,unsigned int dUs){
return SYSTICK_REAL>=( startTick + us2SystemTick(dUs) );
}
void SystemTickInit(){
SysTick_Config( 0x1000000 );
systick_clksource_set( SYSTICK_CLKSOURCE_HCLK_DIV8 );
gSysTickPerUs = rcu_clock_freq_get(CK_AHB)/8e6;
}
头文件
#ifndef SYSTEM_TICK_H
#define SYSTEM_TICK_H
typedef unsigned long long SYSTEM_TICK_TYPE;
extern void delay_us(unsigned int us);
extern void SystemTickInit();
extern inline SYSTEM_TICK_TYPE GetSystemRealTick();
extern SYSTEM_TICK_TYPE us2SystemTick(unsigned int us);
extern int IsSystemTimeOut(SYSTEM_TICK_TYPE startTick,unsigned int dUs);
#endif
主程序
#include "gd32f30x.h"
#include "SystemTick.h"
int main(){
SystemTickInit();
rcu_periph_clock_enable(RCU_GPIOA);
gpio_init(GPIOA,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
while(1){
gpio_bit_set(GPIOA,GPIO_PIN_1);
delay_us(5e5);
gpio_bit_reset(GPIOA,GPIO_PIN_1);
delay_us(5e5);
}
}