1 新建模板文件夹
新建一个名叫03_GD32TemplateProject
的文件夹,用于建造工程模板
2 移植官方库文件
在模板文件夹里新建5个文件夹,分别存放官方库文件和系统驱动文件
01_main
存放main函数
02_Startup
存放系统启动文件
03_System
存放官方的系统文件
04_Firmware_PeripheralDriver
存放官方的外设驱动库文件
05_UserDriver
存放用户驱动文件
3 新建keil工程
3.1 安装芯片pack
去官方网站找到对应的资料包:官网资料包
安装pack
3.2 新建工程
选择对应的芯片型号
勾选CMSIS内核
工程创建完成
3.3 添加文件
3.4 添加路径
4 修改文件
4.1 修改中断文件
打开gd32e23x_it.c文件,拉到最后面,然后删掉Systick_Handler下面的代码
4.2 修改systick文件
systick.h
#ifndef SYS_TICK_H
#define SYS_TICK_H
#include <stdint.h>
/* function declarations */
/* configure systick */
void systick_config(void);
/* delay a time in milliseconds */
void delay_1ms(uint32_t count);
/* delay a time in milliseconds */
void delay_ms(uint32_t count);
/* delay a time in microseconds */
void delay_1us(uint32_t count);
/* delay a time in microseconds */
void delay_us(uint32_t count);
#endif /* SYS_TICK_H */
systick.c
#include "gd32e23x.h"
#include "systick.h"
volatile static float count_1us = 0;
volatile static float count_1ms = 0;
/*!
\brief configure systick
\param[in] none
\param[out] none
\retval none
*/
void systick_config(void)
{
/* systick clock source is from HCLK/8 */
systick_clksource_set(SYSTICK_CLKSOURCE_HCLK_DIV8);
count_1us = (float)SystemCoreClock/8000000;
count_1ms = (float)count_1us * 1000;
}
/*!
\brief delay a time in microseconds in polling mode
\param[in] count: count in microseconds
\param[out] none
\retval none
*/
void delay_1us(uint32_t count)
{
uint32_t ctl;
/* reload the count value */
SysTick->LOAD = (uint32_t)(count * count_1us);
/* clear the current count value */
SysTick->VAL = 0x0000U;
/* enable the systick timer */
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
/* wait for the COUNTFLAG flag set */
do{
ctl = SysTick->CTRL;
}while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));
/* disable the systick timer */
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
/* clear the current count value */
SysTick->VAL = 0x0000U;
}
void delay_us(uint32_t count)
{
uint32_t ctl;
/* reload the count value */
SysTick->LOAD = (uint32_t)(count * count_1us);
/* clear the current count value */
SysTick->VAL = 0x0000U;
/* enable the systick timer */
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
/* wait for the COUNTFLAG flag set */
do{
ctl = SysTick->CTRL;
}while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));
/* disable the systick timer */
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
/* clear the current count value */
SysTick->VAL = 0x0000U;
}
/*!
\brief delay a time in milliseconds in polling mode
\param[in] count: count in milliseconds
\param[out] none
\retval none
*/
void delay_1ms(uint32_t count)
{
uint32_t ctl;
/* reload the count value */
SysTick->LOAD = (uint32_t)(count * count_1ms);
/* clear the current count value */
SysTick->VAL = 0x0000U;
/* enable the systick timer */
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
/* wait for the COUNTFLAG flag set */
do{
ctl = SysTick->CTRL;
}while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));
/* disable the systick timer */
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
/* clear the current count value */
SysTick->VAL = 0x0000U;
}
void delay_ms(uint32_t count)
{
uint32_t ctl;
/* reload the count value */
SysTick->LOAD = (uint32_t)(count * count_1ms);
/* clear the current count value */
SysTick->VAL = 0x0000U;
/* enable the systick timer */
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
/* wait for the COUNTFLAG flag set */
do{
ctl = SysTick->CTRL;
}while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));
/* disable the systick timer */
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
/* clear the current count value */
SysTick->VAL = 0x0000U;
}
4.3 修改main文件
#include "gd32e23x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
int main(void)
{
while(1)
{
}
}
5 编译工程
无错误,模板创建完成
6 工程模板点灯测试
6.1 代码
#include "gd32e23x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
int main(void)
{
/* configure systick */
systick_config();
// 使能GPIOC时钟
rcu_periph_clock_enable(RCU_GPIOC);
// 设置为推挽输出模式
gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_13);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
// GPIOC_13 设定为高电平
gpio_bit_set(GPIOC, GPIO_PIN_13);
while(1)
{
gpio_bit_toggle(GPIOC, GPIO_PIN_13);
delay_ms(500);
}
}
6.2 烧录
选择DAP-link烧录模式
点击setting
,检查是否识别到芯片
无误后开始烧录即可
6.3 实验现象
LED开始正常闪烁
标签:count,CTRL,systick,----,Msk,GD32,SysTick,uint32,模板 From: https://www.cnblogs.com/zxr-blog/p/18175017