求51单片机编程

2024-12-30 16:22:47
推荐回答(1个)
回答1:

#include
#define uint unsigned int
#define uchar unsigned char
sbit key_1 = P3^7;
sbit key_2 = P3^6;
sbit key_3 = P3^5;
sbit led = P2^0;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay(uint);
void display(uint,uint,uint);
void init ();
void button();
uint i,z,minute,shi,ge;
uint j = 60;
uint k = 4;
void main()
{
while(1)
{
button();
display(minute,shi,ge);
}
}
void display(uint minute,uint shi,uint ge)
{

P1 = 0xfd;
P0 = table[k%10];
delay(2);
P1 = 0xff;
delay(2);

P1 = 0xfb;
P0 = table[shi];
delay(2);
P0 = 0xff;
delay(2);

P1 = 0xf7;
P0 = table[ge];
delay(2);
P1 = 0xff;
delay(2);
}
void time_0 ()interrupt 1
{
TH0 = (65536-16080)/256;
TL0 = (65536-16080)%256; //50ms
i++;
if(i == 20)//1s
{
i = 0;
j--; //1s
shi = j/10;
ge = j%10;
if(j == 0)
{
j = 60;
k--; //1minute add1

if(k == 0)
k = 5;
}
}
}
void init()
{
EA = 0;
ET0 = 0;
TMOD =0x01;
TH0 = (65536-16080)/256;
TL0 = (65536-16080)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void delay(uint z)
{
uint x,y;
for(x = z;x>0;x--)
for(y = 110;y>0;y--);
}
void button()
{
if(key_1&&key_2 == 1)
{
delay(10);
if(key_1&&key_2 == 1)
{
led = 0;
init();
}
}
if(key_3 == 1)
{
led = 1;
}
}