LED灯连接原理图如图所示,控制对应IO口输出低电平则LED灯点亮。其中LED灯连接IO口为GPB5、6、7、8。
程序如下:
#define GPBCON (*(volatile unsigned int *)0x56000010) //GPB三个IO寄存器宏定义
#define GPBDAT (*(volatile unsigned int *)0x56000014)
#define GPBDUP (*(volatile unsigned int *)0x56000018)
#define CLBIT(n) ((1<<n)&0xf)
int main()
{
char n,i;
int j;
//由图知道GPB5、6、7、8需要输出
GPBCON &= (~(3<<10)|(3<<12)|(3<<14)|(3<<16));
GPBCON |= ((1<<10)|(1<<12)|(1<<14)|(1<<16));
//禁止上拉电阻
GPBDUP |= ((1<<5)|(1<<6)|(1<<7)|(1<<8));
while(1)
{
for(i=0;i<4;i++)
{
n = CLBIT(i);
//将数据口5、6、7、8位置1,其他位不变
GPBDAT |= ((1<<5)|(1<<6)|(1<<7)|(1<<8));
//循环使将数据口5、6、7、8位某一位置0另3位置1。
GPBDAT &= (~(n<<5));
//延时一段时间
for(j=0;j<300000;j++);
}
}
}
![](http://www.linewfish.com/data/attachment/forum/201312/10/230024xhurce8cbendn3i8.png)
程序如下:
#define GPBCON (*(volatile unsigned int *)0x56000010) //GPB三个IO寄存器宏定义
#define GPBDAT (*(volatile unsigned int *)0x56000014)
#define GPBDUP (*(volatile unsigned int *)0x56000018)
#define CLBIT(n) ((1<<n)&0xf)
int main()
{
char n,i;
int j;
//由图知道GPB5、6、7、8需要输出
GPBCON &= (~(3<<10)|(3<<12)|(3<<14)|(3<<16));
GPBCON |= ((1<<10)|(1<<12)|(1<<14)|(1<<16));
//禁止上拉电阻
GPBDUP |= ((1<<5)|(1<<6)|(1<<7)|(1<<8));
while(1)
{
for(i=0;i<4;i++)
{
n = CLBIT(i);
//将数据口5、6、7、8位置1,其他位不变
GPBDAT |= ((1<<5)|(1<<6)|(1<<7)|(1<<8));
//循环使将数据口5、6、7、8位某一位置0另3位置1。
GPBDAT &= (~(n<<5));
//延时一段时间
for(j=0;j<300000;j++);
}
}
}