立創梁山派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)
{
}
}