开发者

How to read value from GPIO port of an ARM microcontroller?

开发者 https://www.devze.com 2022-12-12 22:06 出处:网络
How d开发者_JAVA百科o get ARM microcontroller port value into a 32 bit variable. I am using LPC2378 microcontroller. You need to access the GPIO registers just like you would any other special functi

How d开发者_JAVA百科o get ARM microcontroller port value into a 32 bit variable.

I am using LPC2378 microcontroller.


You need to access the GPIO registers just like you would any other special function registers in the chip. The LPC2378 docs show these details:

#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.

0

精彩评论

暂无评论...
验证码 换一张
取 消