使用Freedom-k64开发板,尝试使用寄存器绝对地址进行IO控制,控制PROTB21输出。
寄存器地址的编写和定义
#define PORT_B_PDOR *(volatile uint32_t*)(0x400FF040u)
#define PORT_B_PSOR *(volatile uint32_t*)(0x400FF044u)
#define PORT_B_PDDR *(volatile uint32_t*)(0x400FF054u)
#define PORT_B_PCR21 *(volatile uint32_t*)(0x4004A054u)
- 配置GPIOB的时钟,这是与51使用寄存器控制的最大区别之一。
- 配置GPIOB21的引脚功能,也就是复用功能,有的引脚存在好多种功能,所以就需要选择功能,这里选择GPIO功能,每个引脚功能都是一个32位寄存器来控制。
- 配置GPIOB21的数据方向,输入输出方向.
#include "include.h"
#define PORT_B_PDOR *(volatile uint32_t*)(0x400FF040u)
#define PORT_B_PSOR *(volatile uint32_t*)(0x400FF044u)
#define PORT_B_PDDR *(volatile uint32_t*)(0x400FF054u)
#define PORT_B_PCR21 *(volatile uint32_t*)(0x4004A054u)
#define PORT_SIM 0x40047000u
#define SCGC5_OFFSET 0x1038u
#define SCGC5_B_MASK (0x400u)
int main(void)
{
*(volatile uint32_t*)(PORT_SIM+SCGC5_OFFSET) |=SCGC5_B_MASK;//开启PROTB时钟
PORT_B_PCR21 =(PORT_B_PCR21&(~0x700u))|(1<<8);//配置引脚功能
PORT_B_PDDR|= (1 << 21);//配置数据方向为输出
PORT_B_PDOR &= ~( 1 << 21);//写入数据,低电平
while(1){
}
}