Home > other >  Microcontroller programming
Microcontroller programming

Time:12-03



(1) functional requirements: three buttons, three LED lights, according to one of the key, the corresponding leds light 1 s and 1 s and keys all interrupt method was adopted to realize,

CodePudding user response:

# include "reg52. H"
Typedef unsigned int under-16;
Typedef unsigned char u8;
Sbit k0 P1=^ 0;
P1 sbit k1=^ 1;
Sbit k2=p ^ 2;
P1 sbit k3=^ 3;
Sbit k4=P1 ^ 4;
Sbit k5=P1 ^ 5;
Sbit k6=P1 ^ 6;
Sbit k7=P1 ^ 7;

Sbit p20=P2 ^ 0;
Sbit p21=P2 ^ 1;
Sbit p22=P2 ^ 2;
Sbit p23=P2 ^ 3;
Sbit p24=P2 ^ 4;
Sbit p25=P2 ^ 5;
Sbit he=P2 ^ 6;
Sbit p27=P2 ^ 7;

U8 flag=0;//t=0;
u8 key;
U8 cisu;
Under-16 di;

Void Int0 () interrupt 0//interrupt function of the external interrupt 0
{
The switch (P1)
{
Case 0 xfe: key=0 x00;
break;
Case 0 XFD: key=0 x11;
break;
Case 0 XFB: key=0 x22;
break;
Case 0 xf7: key=0 x33;
break;
Case 0 xef: key=0 x44;
break;
Case 0 XDF: key=0 x55;
break;
Case 0 XBF: key=0 x66;
break;
Case 0 x7f: key=0 x77;
break;
Default: key=0 XFF;
}
}

Void Timer0 () interrupt 1//
{

XDC TH0=0;//10 ms - 0.01 s
TL0=0 x08;
Di++;
//led1=~ led1;
}

Void Timer0Init initialization ()//timer 0
{
TMOD |=0 x01;
XDC TH0=0;//11.0592 Mhz; 0 xdc08-10 ms;
TL0=0 x08;
ET0=1;
//EA=1;
//TR0=1;
}

Void Int0Init ()
{
//set INT0
IT0=1;//jump along the way (falling edge)
EX0=1;//open INT0 interruption allows,
EA=1;//open the total interruption
}

Void main ()
{
Timer0Init ();//
Int0Init ();//set the external interrupt 0
Key=0 XFF;
While (1)
{
If (cisu==0)
{
The switch (key)
{
Case 0 x00: P2=0 xfe; Di=0; Cisu=1; TR0=1;
break;
Case 0 x11: P2=0 XFD; Di=0; Cisu=1; TR0=1;
break;
Case 0 x22: P2=0 XFB; Di=0; Cisu=1; TR0=1;
break;
Case 0 x33: P2=0 xf7; Di=0; Cisu=1; TR0=1;
break;
Case 0 x44: P2=0 xef; Di=0; Cisu=1; TR0=1;
break;
Case 0 x55: P2=0 XDF; Di=0; Cisu=1; TR0=1;
break;
Case 0 x66: P2=0 XBF; Di=0; Cisu=1; TR0=1;
break;
Case 0 x77: P2=0 x7f; Di=0; Cisu=1; TR0=1;
break;
Case 0 XFF: P2=0 XFF;
break;
//default: P2=0 XFF;
}
}
If (di> 100 & amp; & Cisu==1)
{
Di=0; TR0=0; Key=0 XFF; The P2=0 XFF;
Cisu=0;
}
}
}

CodePudding user response:

Study hard to learn

CodePudding user response:

Wow, it's too much, thanks for the boss
  • Related