题目
第一题代码
#include <REGX51.H>
sbit K24=P0^0;
sbit LED1=P1^0;
sbit LED2=P1^1;
sbit LED3=P1^2;
sbit LED4=P1^3;
sbit LED5=P1^4;
sbit LED6=P1^5;
sbit LED7=P1^6;
sbit LED8=P1^7;
void delayms(unsigned int t)
{
unsigned int j,i;
for(i=0;i<t;i++)
{
for(j=0;j<120;j++);
}
}
void main(void)
{
unsigned char flag1=0;
unsigned char flag2=0;
while(1)
{
if(K24==0)
{
delayms(20);
if(K24==0)
{
if(flag1)
{
flag1=0;
flag2++;
if(flag2%2)
{
LED1=1;
LED2=0;
LED3=1;
LED4=0;
LED5=1;
LED6=0;
LED7=1;
LED8=0;
}
else
{
LED1=0;
LED2=1;
LED3=0;
LED4=1;
LED5=0;
LED6=1;
LED7=0;
LED8=1;
}
}
}
}
else
flag1=1;
}
}
第二题代码
#include<reg52.h>
sbit LED1=P2^4;
sbit LED2=P2^5;
sbit LED3=P2^6;
sbit LED4=P2^7;
unsigned char TimerCounter=0;
unsigned char flag=0xff;
unsigned char code Table[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void T0zd(void)interrupt 1 //外部中断
{
TH0=0xDC;
TL0=0x00;
TimerCounter++;
}
void sv0(void)interrupt 0
{
flag=~flag;
}
void main(void)
{
unsigned char index=0;
TMOD=0x01;
TH0=0xDC;
TL0=0x00;
TR0=1;
ET0=1;
IT0=1;
EX0=1;
EA=1;
LED1=0;
LED2=0;
LED3=0;
LED4=0;
while(1)
{
if(TimerCounter>=60)
{
TimerCounter=0;
P0=Table[index];
index++;
if(index>=6)
index=0;
if(flag)
TR0=1;
else
TR0=0;
}
}
}
第三题代码
#include"reg52.h"
typedef unsigned int u16;
typedef unsigned char u8;
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
static u16 countime;
static u8 count;
void Timer1Init()
{
TMOD=0X20;//选择为定时器1模式,工作方式2,
TH1=0X06; //给定时器赋初值,定时250us 晶振为12M/HZ
TL1=0X00;
ET1=1;//打开定时器1中断允许
EA=1;//打开总中断
TR1=1;//打开定时器
}
void main()
{
Timer1Init(); //定时器1初始化
while(1);
}
void Timer1() interrupt 3
{
TH1=0X1A; //给定时器赋初值,定时250us
TL1=0X1A;
countime++;
if(countime%4000==0)
{
if(count==10)
{
count=0;
}
P0=~smgduan[count];
count++;
}
else if(countime==40000)
{
countime=0;
}
}
第四题代码
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar thedata;
bit flag;
void main()
{
TMOD = 0x20;//设置T0的工作方式为方式2
TH1 = 0xf3;
TL1 = 0xf3;
TR1 = 1;
SCON = 0x50; //0101 0000 smo,sm1=01 ren =1;
PCON=0x80;
EA = 1;
ES = 1;
while (1)
{
if (flag == 1)
{
ES = 0;
flag = 0;
SBUF = thedata;
while (!TI);
TI = 0;
ES = 1;
}
}
}
void serial()interrupt 4
{
thedata = SBUF;
RI = 0;
flag = 1;
}
其余四道题目在我的下一篇博客中