立创梁山派GD32F450ZGT6--寄存器点灯
bsp_led.h 如下:(条件编译,避免重复编译)
//bsp_led.h 如下:
#ifndef _BSP_LED_H
#define _BSP_LED_H
#include "gd32f4xx.h"
#include "systick.h"
#define BSP_RCU_BASE (unsigned int)0x40023800 //RCU基地址
#define BSP_RCU_AHB1EN *(unsigned int*) (BSP_RCU_BASE + 0x30) //基地址 + 地址偏移 //立即数转化为地址(*),再转化为值(*)
#define BSP_GPIOD_BASE (unsigned int)0x40020C00
#define BSP_GPIOD_CTL *(unsigned int*)(BSP_GPIOD_BASE + 0x00) //控制端口寄存器地址
#define BSP_GPIOD_PUD *(unsigned int*)(BSP_GPIOD_BASE + 0x0C) //上下拉寄存器地址
#define BSP_GPIOD_OMODE *(unsigned int*)(BSP_GPIOD_BASE + 0x04) //端口输出模式寄存器地址
#define BSP_GPIOD_OSPD *(unsigned int*)(BSP_GPIOD_BASE + 0x08) //端口输出速度寄存器地址
#define BSP_GPIOD_OCTL *(unsigned int*)(BSP_GPIOD_BASE + 0x14) //OCTL寄存器地址
//函数声明如下:
void led_gpio_config(void);
#endif
bsp_led.c 如下:
//bsp_led.c 如下:
#include "bsp_led.h"
void led_gpio_config(void)
{
//开启GPIOD的端口时钟
BSP_RCU_AHB1EN |= 0x00000008; //BSP_RCU_AHB1EN |= (1<<3);
//配置GPIOD的模式(两步)
//1.配置为输出功能
BSP_GPIOD_CTL &= ~(0x03<<(2*7)); //14位15位都拉低 //BSP_GPIOD_CTL &= 0xFFFF3FFF;
BSP_GPIOD_CTL |= (0x01<<(2*7)); //14位拉高 //BSP_GPIOD_CTL |= 0x00004000;
//2.配置为浮空模式
BSP_GPIOD_PUD &= ~(0x03<<(2*7));
//配置GPIOD的输出(两步)
//1.端口输出模式寄存器:推挽输出
BSP_GPIOD_OMODE &= ~(0x01<<7);
//2.端口输出速度寄存器:50MHz(等级和速度的关系)10
BSP_GPIOD_OSPD &= ~(0x03<<(2*7)); //14位15位都拉低
BSP_GPIOD_OSPD |= (0x02<<(2*7)); //15位拉高
}
主函数如下:
//主函数
#include "gd32f4xx.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "bsp_led.h" //新增头文件
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
systick_config();
led_gpio_config(); //led初始化
BSP_GPIOD_OCTL |=(0x01<<7);//led2亮
while(1)
{
}
}