使用锁存主要是为了达到I/O口复用的目的,节省I/O口。如果I/O口够用当然可以不用锁存器。示例如下:
#include <AT89X51.H>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
uchar code table1[]={
0x01,0x02,0x04,0x08,
0x10,0x20,0x40,0x80};
void delay(uint k)
{
uint i,j;
for(i=k;i>0;i--)
for(j=114;j>0;j--);
}
void main()
{
uchar i=0;
while(1)
{
for(i=0;i<8;i++)
{
P0=0xff;
P2=table1;
P0=table;
delay(1);
}
}
}
|