單片機(jī)驅(qū)動(dòng)8*8LED點(diǎn)陣c程序
typedef unsigned char uint8;
typedef unsigned int uint16;
sbit acon=P1^0; //38譯碼器
sbit bcon=P1^1;
sbit ccon=P1^2;
sbit en=P1^7; //譯碼器使能
uint8 j=0,k=0;
uint16 i=0;
code uint8 ledcode[]={ 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, //
0xFF,0xC3,0xE7,0xE7,0xE7,0xE7,0xC3,0xFF, // I
0xFF,0x99,0x66,0x7E,0xBD,0xDB,0xE7,0xFF, // 心
0xFF,0x99,0x99,0x99,0x99,0x99,0xC3,0xFF, // U
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF }; // 哄老婆用
void timer1_init()
{
TMOD|=0x10;
TMOD&=0xef;
TH1=0xfc; //1ms
TL1=0x66;
TR1=1; //定時(shí)器1開啟
}
void inter_init()
{
ET1=1; //定時(shí)器1中斷開啟
EA=1; //總中斷開啟
}
void frush()
{
switch(j)
{
case 0:acon=0;bcon=0;ccon=0;break; //掃描LED點(diǎn)陣
case 1:acon=1;bcon=0;ccon=0;break;
case 2:acon=0;bcon=1;ccon=0;break;
case 3:acon=1;bcon=1;ccon=0;break;
case 4:acon=0;bcon=0;ccon=1;break;
case 5:acon=1;bcon=0;ccon=1;break;
case 6:acon=0;bcon=1;ccon=1;break;
case 7:acon=1;bcon=1;ccon=1;break;
}
P2=ledcode[k+j]; //逐行輸出
if(j==7)j=0;
j++;
}
main()
{
en=1;
P2=0xff;
timer1_init();
inter_init();
while(1);
}
void timer1_interrupt() interrupt 3
{
TH1=0xfc;
TL1=0x66;
i++;
frush();
if(i==500)
{
i=0;
if(k==32)k=0;
k++;
}
}
仿真圖:
評(píng)論