【超级详细教程】给 GD32F10x 系列建立keil工程

网友:郑爷爷,什么是朋友?
郑渊洁:孩子将玩具当朋友,成人将朋友当玩具。

如想获取本文相关资料和最后的工程文件,请关注公众号《嵌入式小散修》,回复 GD32F10x系列建立keil模板工程资料
在这里插入图片描述

准备

  • 首先,下载 GD32F10x 系列软件包和 keil支持包:http://www.gd32mcu.com/cn/download/7?kw=GD32F1
    目前 GD32F10x 固件库的最新版本是 2.2.2,keil支持包的版本为 2.02,如下所示:(写于2022-3-2
    在这里插入图片描述
  • 安装 GD32F10x 系列的 keil 支持包
    在这里插入图片描述
    看到如下界面,说明 keil 支持包安装成功。
    在这里插入图片描述

建立 GD32F10X 对应的Keil 工程(以GD32F103C8T6为例)

  • 新建文件夹如下:
    在这里插入图片描述

  • 新建 keil 的空工程,存放在上图中的 KeilProject 文件夹
    在这里插入图片描述

  • 选择对应芯片
    在这里插入图片描述

  • 新建工程下的目录如下
    在这里插入图片描述

  • 添加 CMSIS Cortex-M3 Device Peripheral Access Layer Source File,对于所有 GD32F10x 系列都有效。
    在这里插入图片描述

  • 添加芯片启动文件(因为GD32F103C8T6是中等容量,所以选择 startup_gd32f10x_md.s,别的芯片按照其容量选择)
    在这里插入图片描述

  • 添加 GB32F10x 标准库文件
    在这里插入图片描述

  • 从标准库中的模板工程拷贝如下器个文件到 User 文件夹。(???有标准模板,我在这折腾啥?算了就当熟悉结构吧)
    在这里插入图片描述

  • 将上述文件中的 .c 文件加入工程。
    在这里插入图片描述

  • 标准库需要增加两个宏定义才能使用:USE_STDPERIPH_DRIVER,GD32F10X_MD
    在这里插入图片描述

  • 添加头文件路径:
    在这里插入图片描述

  • 如果你的板子没有外部晶振,得配置成内部晶振,如下操作:
    在这里插入图片描述

  • 将 main 函数修改成如下代码:

/*!
    \file    main.c
    \brief   led spark with systick, USART print and key example

    \version 2014-12-26, V1.0.0, firmware for GD32F10x
    \version 2017-06-20, V2.0.0, firmware for GD32F10x
    \version 2018-07-31, V2.1.0, firmware for GD32F10x
    \version 2020-09-30, V2.2.0, firmware for GD32F10x
*/

/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32f10x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "gd32f10x_eval.h"

/*!
    \brief      toggle the led every 500ms
    \param[in]  none
    \param[out] none
    \retval     none
*/
void led_spark(void)
{
    static __IO uint32_t timingdelaylocal = 0U;

    if(timingdelaylocal){

        if(timingdelaylocal < 500U){
            gd_eval_led_on(LED2);
        }else{
            gd_eval_led_off(LED2);
        }

        timingdelaylocal--;
    }else{
        timingdelaylocal = 1000U;
    }
}

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/

int main(void)
{
    /* configure systick */
    systick_config();
    /* initilize the LEDs, USART and key */
    gd_eval_led_init(LED2); 
    gd_eval_led_init(LED3); 
    gd_eval_led_init(LED4);
    gd_eval_com_init(EVAL_COM0);
    gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO);
    
    /* print out the clock frequency of system, AHB, APB1 and APB2 */
    printf("\r\nCK_SYS is %d", rcu_clock_freq_get(CK_SYS));
    printf("\r\nCK_AHB is %d", rcu_clock_freq_get(CK_AHB));
    printf("\r\nCK_APB1 is %d", rcu_clock_freq_get(CK_APB1));
    printf("\r\nCK_APB2 is %d", rcu_clock_freq_get(CK_APB2));

    while(1){
        if(RESET == gd_eval_key_state_get(KEY_WAKEUP)){
            gd_eval_led_on(LED3);
            delay_1ms(500);
            gd_eval_led_off(LED3);
            gd_eval_led_toggle(LED4);
        }
    }
}

/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
    usart_data_transmit(EVAL_COM0, (uint8_t)ch);
    while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));

    return ch;
}

  • 连接烧录器和开发板,配置烧录器(这里以jlink为例)
    在这里插入图片描述
    如果是 SWD 模式,选择SW:
    在这里插入图片描述
    在这里插入图片描述

  • 编译OK,然后下载。
    在这里插入图片描述
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值