GD32F103VE点灯主要用来学习端口引脚的输出配置。它由LED.c,LED.h,SoftDelay.c和main.c组成。
#include "gd32f10x.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t
#include "SoftDelay.h"
#include "LED.h"
int main(void)
{
LED_Init();//初始化MCU_Led指示灯端口
while(1)
{
MCU_Led_On();
delay_X_ms(200);//软件延时200ms
MCU_Led_Off(); delay_X_ms(200);//软件延时200ms
}
}
#ifndef __LED_H
#define __LED_H
#include "gd32f10x.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t
#define MCU_Led_Off() GPIO_BOP(GPIOD)=GPIO_PIN_15 //GD15输出高电平
#define MCU_Led_On() GPIO_BC(GPIOD)=GPIO_PIN_15 //GD15输出低电平
#define MCU_Led_Toggle() gpio_bit_write( GPIOD,GPIO_PIN_15,(bit_status)((1-gpio_input_bit_get(GPIOD,GPIO_PIN_15))) )
//GD15取反输出电平
extern void LED_Init(void);
#endif
#include "LED.h"
void LED_Init(void);
//函数功能:初始化MCU_Led指示灯
void LED_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOD);//使能GPIOD时钟,enable GPIO clock
gpio_init(GPIOD, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
//将GPIOD15设置为输出上拉
MCU_Led_Off();
// GPIO_BC(GPIOD) = GPIO_PIN_15;//清除GPIOD15输出,GPIO bit clear register
}
#include "SoftDelay.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t
void delay_X_us(uint16_t i)
{
uint16_t cnt;
while(i--)
{
cnt=7;
while(cnt--);
}
}
void delay_X_ms(uint16_t i)
{
uint16_t cnt;
while(i--)
{
cnt=8000;
while(cnt--);
}
}
main.c如下:
#include "gd32f10x.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t
#include "SoftDelay.h"
#include "LED.h"
int main(void)
{
LED_Init();//初始化MCU_Led指示灯端口
while(1)
{
MCU_Led_On();
delay_X_ms(200);//软件延时200ms
MCU_Led_Off(); delay_X_ms(200);//软件延时200ms
}
}