单片机大致结构:
P1口是准双向口,只能作为通用I/O口使用。(P1P2P3亦同,P3可作第二功能)
P1口作为输出口使用时,无需再外接上拉电阻。 (P2同, P0P3需接上拉电阻)
P1口作为输入口使用时,应区分读引脚和读端口。读引脚时,必须先向电路中的锁存器写入“1”,使输出级的FET截止。(P0P2P3)
简单的花式流水灯
#include <reg51.h>
sbit P1_0 = P1^0;
void delay(unsigned int i)
{
while(i)
{
i--;
}
}
void main()
{
//P1 = 0xff; //点亮8个led灯
//P1 = 0x01;//点亮第一个
//P1 = 0x80;//点亮最后一个
/*while(1)
{
P1_0 = 1; //1个led灯闪烁
delay(50000);
P1_0 = 0;
delay(50000);
}*/
int i;
while(1)
{
for(i = 0; i < 8; i++)
{
P1 = (0x01) << i;
delay(60000);
}
for(i = 0; i < 8; i++)
{
P1 = (0x80) >> i;
delay(60000);
}
P1 = 0x81;
delay(60000);
P1 = 0x42;
delay(60000);
P1 = 0x24;
delay(60000);
P1 = 0x18;
delay(60000);//led灯两边向中间亮
P1 = 0x18;
delay(60000);
P1 = 0x24;
delay(60000);
P1 = 0x42;
delay(60000);
P1 = 0x81;
delay(60000);//led灯两边向两边亮
for(i = 0; i < 8; i += 2)
{
P1 = (0x01) << i;
delay(60000);
}//1357led亮
P1 = 0x01;
delay(60000);
P1 = 0x03;
delay(60000);
P1 = 0x07;
delay(60000);
P1 = 0x0f;
delay(60000);
P1 = 0x1f;
delay(60000);
P1 = 0x3f;
delay(60000);
P1 = 0x7f;
delay(60000);
P1 = 0xff;
delay(60000);//led灯从左向右点亮
P1 = 0x80;
delay(60000);
P1 = 0xc0;
delay(60000);
P1 = 0xe0;
delay(60000);
P1 = 0xf0;
delay(60000);
P1 = 0xf8;
delay(60000);
P1 = 0xfc;
delay(60000);
P1 = 0xfe;
delay(60000);
P1 = 0xff;
delay(60000);//led灯从右向左点亮
P1 = 0x18;
delay(60000);
P1 = 0x3c;
delay(60000);
P1 = 0x7e;
delay(60000);
P1 = 0xff;
delay(60000);//led灯从中间向两边点亮
P1 = 0x7e;
delay(60000);
P1 = 0x3c;
delay(60000);
P1 = 0x18;
delay(60000);
P1 = 0x00;
delay(60000);//led灯从两边向中间点灭
}
}