友善之臂的ARM9板子做实验发现:
- C/C++ code
/* key_led.c */#define GPBCON (*(volatile unsigned long *)0x56000010)#define GPBDAT (*(volatile unsigned long *)0x56000014)#define GPGCON (*(volatile unsigned long *)0x56000060)#define GPGDAT (*(volatile unsigned long *)0x56000064)/* * LED1~4,GPB5/GPB6/GPB7/GPB8 */#define GPB5_out (1 << (5 * 2))#define GPB6_out (1 << (6 * 2))#define GPB7_out (1 << (7 * 2))#define GPB8_out (1 << (8 * 2))/* * KEY1~4, GPG0/GPG3/GPG5/GPG6 */#define GPG0_in (0 << (0 * 2))#define GPG3_in (0 << (3 * 2))#define GPG5_in (0 << (5 * 2))#define GPG6_in (0 << (6 * 2))int main(){ unsigned long dwDat; GPBCON = GPB5_out | GPB6_out | GPB7_out | GPB8_out; GPGCON = GPG0_in & GPG3_in & GPG5_in & GPG6_in; while(1) { dwDat = GPGDAT; /* 对应位同1做与运算,低电平LED亮 */ if (dwDat & (1<<0)) GPBDAT |= (1 << 5); // LED1 turn off else GPBDAT &= ~(1 << 5); // LED1 turn on if (dwDat & (1<<3)) GPBDAT |= ( 1 << 6); // LED2 turn off else GPBDAT &= ~(1 << 6); // LED2 turn on if (dwDat & (1<<5)) GPBDAT |= (1 << 7); // LED3 turn off else GPBDAT &= ~(1 << 7); // LED3 turn on if (dwDat & (1 << 6)) GPBDAT |= (1 << 8); // LED4 turn off else GPBDAT &= ~(1 << 8); // LED4 turn on } return 0;}
这段代码可以很方便准确地实现,按键1、2、3、4对应LED灯1、2、3、4。上述代码详实,直白,我将其中main函数的while循环内容改为如
下:
GPBDAT = ( ((GPGDAT & 0x01 ) << 5)
| ((GPGDAT & 0x08 ) << 6)
| ((GPGDAT & 0x32 ) << 7)
| ((GPGDAT & 0x64 ) << 8));
这段代码显得简洁,但是达不到预期效果,只有按键1对应LED灯1有反应,其余皆为,而且上电后LED2、3是常亮。
不知为何?
------解决方案--------------------
1<<3= 8 , 1<<5 =32 ,1<<6 =64. 你加个16进制,不错能行么?