一、系统滴答定时器
由于之前的模板创建未添加gd32f10x.h头文件及相关的core_cm3.h等文件,模板仅包含了启动文件,需要了解systick的寄存器;
二、寄存器实现
static u8 fac_us;
static u32 fac_ms;
void delay_init(u8 sysclk)
{
/*systick时钟源为内核时钟*/
(*((volatile unsigned int*)(0xE000E010)))|=1<<2;
/*使能systick时钟*/
(*((volatile unsigned int*)(0xE000E010)))|=1<<0;
fac_us=sysclk;
fac_ms=1000*fac_us;
}
/*系统时钟为96M nus<=174762 us */
void delay_mus(u32 nus)
{
u32 temp;
/*设置load寄存器值*/
(*((volatile unsigned int*)(0xE000E014)))=nus*fac_us;
/*设置val寄存器值初值为0*/
(*((volatile unsigned int*)(0xE000E018)))=0;
/*使能systick时钟*/
(*((volatile unsigned int*)(0xE000E010)))|=0x01;
do
{
temp=(*((volatile unsigned int*)(0xE000E010)));
}while((temp&0x01)&&!(temp&(1<<16)));
/*关闭systick时钟*/
(*((volatile unsigned int*)(0xE000E010)))&=~0x01;
/*设置val寄存器值初值为0*/
(*((volatile unsigned int*)(0xE000E018)))=0;
}
/*系统时钟为96M nms<=174 ms */
static void delay_ms(u8 nms)
{
u32 temp;
(*((volatile unsigned int*)(0xE000E014)))=nms*fac_ms;
(*((volatile unsigned int*)(0xE000E018)))=0;
(*((volatile unsigned int*)(0xE000E010)))|=0x01;
do
{
temp=(*((volatile unsigned int*)(0xE000E010)));
}while((temp&0x01)&&!(temp&(1<<16)));
(*((volatile unsigned int*)(0xE000E010)))&=~0x01;
(*((volatile unsigned int*)(0xE000E018)))=0;
}
三、其他延时
void delay_nms(u32 nms)
{
u32 zheng=nms/80;
u8 yu=nms%80;
while(zheng)
{
delay_ms(80);
zheng --;
}
if(yu)
{
delay_ms(yu);
}
}
void delay_s(u8 ns)
{
while(ns)
{
delay_nms(1000);
ns--;
}
}
void delay_xmin(u8 xmin)
{
while(xmin)
{
delay_s(60);
xmin--;
}
}
void delay_yhour(u8 yhour)
{
while(yhour)
{
delay_xmin(60);
yhour--;
}
}
void delay_zday(u16 zday)
{
while(zday)
{
delay_yhour(24);
zday--;
}
}
四、附件
|