- #include <REG52.h>
- void delay(int num)
- {
- int i,j;
- for(i=0;i<=num;i++)
- for(j=0;j<10000;j++)
- ;
- }
- void set_p0(int num)
- {
- switch(num)
- {
- case 0:
- P0=0X3F;
- break;
-
- case 1:
- P0=0X06;
- break;
- case 2:
- P0=0X5B;
- break;
- case 3:
- P0=0X4F;
- break;
- case 4:
- P0=0X66;
- break;
- case 5:
- P0=0X6D;
- break;
- case 6:
- P0=0X7D;
- break;
- case 7:
- P0=0X07;
- break;
- case 8:
- P0=0X7F;
- break;
- case 9:
- P0=0X6F;
- break;
- }
- }
- void set_p2(int num)
- {
- P2=num;
- }
- int main()
- {
- int i;
- for(i=1;i<9;i++)
- {
- set_p0(i);
- set_p2(i-1);
- delay(10);
- }
- }
由上述代码既可以在LED的8个灯中,轮训显示1,2,3,4,5,6,7,8.
下面是控制P3^7进行简单的锁存
#include
//加上code表示数据将存储在rom中
unsigned char code table[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(int num)
{
int i,j;
for(i=0;i for(j=0;j<10000;j++)
;
}
sbit key=P3^4;
sbit lock=P3^7;
sbit lock_key=P3^3;
int i;
int main()
{
P2=2;
i=0;
while(1)
{
P0=table[i%16];
if(lock_key==0)
{
delay(2);
if(lock_key==0)
{
lock=!lock;
}
}
if(key==0)
{
delay(2);
if(key==0)
i++;
}
}
}