|
功能實現(xiàn):計數(shù)0~9,通過共陰數(shù)碼管顯示數(shù)值,計數(shù)到9時,實現(xiàn)8個LED燈依次熄滅或依次點亮
#include <reg52.h>
#include <intrins.h>
#define ledport P2 //LED燈
#define SMG_PORT P0 //數(shù)碼管
sbit LSA= P1^0;
sbit LSB= P1^1;
sbit LSC= P1^2;
unsigned char i;
unsigned char code LedChar[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char Ledwei[8]={
0x00,0x00 ,0x00 , 0x00, 0x00, 0x00,0x00,0x00
};
unsigned int cnt = 0;
unsigned char sec = 0;
/*unsigned char code ledliang[9]={0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff}; */
void led();
void main()
{
unsigned char cnt = 0;
LSC= 0;
LSB= 0;
LSA= 0;
TMOD=0x01;//0000 0001 方式一
TH0=0xFC;
TL0=0x18;
EA=1;
ET0=1;
TR0=1; //開啟T0
while(1)
{
led();
}
}
void tmier0() interrupt 1
{
TH0=0xFC;
TL0=0x18;
cnt++;
P0 = 0x00;
switch(i)
{
case 0: LSC=1; LSB=1; LSA=1; i++; P0=Ledwei[0];break;
case 1: LSC=1; LSB=1; LSA=0; i++; P0=Ledwei[1];break;
case 2: LSC=1; LSB=0; LSA=1; i++; P0=Ledwei[2];break;
case 3: LSC=1; LSB=0; LSA=0; i++; P0=Ledwei[3];break;
case 4: LSC=0; LSB=1; LSA=1; i++; P0=Ledwei[4];break;
case 5: LSC=0; LSB=1; LSA=0; i=0; P0=Ledwei[5];break;
default:break;
}
}
//延時
void delay_100ms(unsigned int ys)
{
while(ys--);
}
void led()
{
if(cnt >= 1000)
{
cnt = 0;
Ledwei[0] = LedChar[sec%10];
sec++;
if(sec==10)
{
sec=0;
P0 = 0x00;
while(1)
{
unsigned int i = 0;
for(i=0;i<9;i++)
{
/* P2=ledliang[i];*/ //方法1
P2=~(0xff<<i);//方法二
delay_100ms(50000);
P2=0Xff;
}
if(i == 9)
i=0;
}
}
}
}
|
評分
-
查看全部評分
|