简体   繁体   English

如何从ARM微控制器的GPIO端口读取值?

[英]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.

相关问题 从ARM微控制器端口读取数据的步骤 - Steps to read data from ARM microcontroller port 如何在ARM Cortex M4 TM4C123G微控制器中使用GPIO端口控制(GPIOPCTL)? - How to use GPIO Port Control (GPIOPCTL) in ARM Cortex M4 TM4C123G microcontroller? ARM linux用户空间gpio操作使用mmap / dev / mem方法(能够写入GPIO寄存器,但无法读取它们) - ARM linux userspace gpio operations using mmap /dev/mem approach (able to write to GPIO registers, but fail to read from them) 如何从ARM的32位端口的第15引脚获取值? - How to get value from 15th pin of 32bit port in ARM? 与手臂微控制器的串行通讯 - serial communication with arm microcontroller 使用指向此函数的指针从 ARM 微控制器的 ROM 调用函数? - Calling function from ARM microcontroller's ROM by using pointer to this function? 如何将库放置在 ARM 微控制器上的不同 memory 位置? - How place a library in a different memory place on an ARM microcontroller? 如何通过微处理器/微控制器同时检测两个或多个按键(GPIO)? - How to detect two or more button press (GPIO) at the same time by a microprocessor/microcontroller? 使用串行端口将数据块从微控制器传输到PC - Transfering a block of data from a microcontroller to a PC using the serial port 从CC2530的GPIO引脚读取 - Read from GPIO Pin on CC2530
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM