[英]How to read value from GPIO port of an ARM microcontroller?
How do get ARM microcontroller port value into a 32 bit variable. 如何将ARM微控制器端口值转换为32位变量。
I am using LPC2378 microcontroller. 我正在使用LPC2378微控制器。
You need to access the GPIO registers just like you would any other special function registers in the chip. 您需要像访问芯片中的任何其他特殊功能寄存器一样访问GPIO寄存器。 The LPC2378 docs show these details:
LPC2378文档显示了这些细节:
#define GPIO_BASE 0xE0028000
#define IOPIN0 (GPIO_BASE + 0x00) // Port 0 value
#define IOSET0 (GPIO_BASE + 0x04) // Port 0 set
#define IODIR0 (GPIO_BASE + 0x08) // Port 0 direction
#define IOCLR0 (GPIO_BASE + 0x0C) // Port 0 clear
#define IOPIN1 (GPIO_BASE + 0x10) // Port 1 value
#define IOSET1 (GPIO_BASE + 0x14) // Port 1 set
#define IODIR1 (GPIO_BASE + 0x18) // Port 1 direction
#define IOCLR1 (GPIO_BASE + 0x1C) // Port 1 clear
I like to use this macro to access memory-mapped registers: 我喜欢使用这个宏来访问内存映射寄存器:
#define mmioReg(a) (*(volatile unsigned long *)(a))
Then the code to read the port looks like this: 然后读取端口的代码如下所示:
unsigned long port0 = mmioReg(IOPIN0); // Read port 0
unsigned long port1 = mmioReg(IOPIN1); // Read port 1
The same macro works for accessing the set/clear/direction registers. 相同的宏用于访问置位/清除/方向寄存器。 Examples:
例子:
mmioReg(IOSET1) = (1UL << 3); // set bit 3 of port 1
mmioReg(IOCLR0) = (1UL << 2); // clear bit 2 of port 0
mmioReg(IODIR0) |= (1UL << 4); // make bit 4 of port 0 an output
mmioReg(IODIR1) &= ~(1UL << 7); // make bit 7 of port 1 an input
In a real system, I'd normally write some macros or functions for those operations, to cut down on the magic numbers. 在实际系统中,我通常会为这些操作编写一些宏或函数,以减少幻数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.