题目
第五题代码
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar Number;
bit flag;
void Timer0Init()//初始化函数
{
TMOD=0X20;
TH1=0Xf3;//选择4800比特率的初值
TL1=0xf3;
TR1=1;
SCON=0x50;
PCON=0x80;//SMOD=1,加倍
EA=1;
ES=1;
}
void main()
{
Timer0Init();
while(1)
{
if(flag==1)
{
ES=0;
flag=0;
if(Number>=48&&Number<=57)
Number=Number;
if(Number>=97&&Number<=122)
Number-=32;
SBUF=Number;
while(!TI);
TI=0;
ES=1;
}
}
}
void serial()interrupt 4
{
Number=SBUF;
RI=0;
flag=1;
}
第六题代码
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar count=0;
uchar temp;
uchar*Number;
bit flag;
void Timer_Init()
{
TMOD=0x21;
TH0=0x3C;
TL0=0xB0;
TH1=0xf3;//4800
TL1=0xf3;
PCON=0x80;//加倍
TR1=1;
SCON=0X50;
EA=1;
ET0=1;
ES=1;
}
void main()
{
uchar i;
Timer_Init();
while(1)
{
if(flag==1)
{
ES=0;
flag=0;
if(temp=='1')
{
TR0=1;
Number="Start";
}
if(temp=='2')
{
TR0=0;
Number="Stop";
}
for(i=0;i<5;i++)
while(*Number)
{
SBUF=*Number;
while(!TI);
TI=0;
Number++;
}
ES=1;
}
if(count==20)
{
count=0;
P1=~P1;
}
}
}
void t0()interrupt 1
{
TH0=0x3C;
TL0=0xB0;
count++;
}
void serial()interrupt 4
{
temp=SBUF;
RI=0;
flag=1;
}
第七题代码
#include<reg52.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
uchar i;
uchar count=0;
uchar data_count=0;
uchar receive[8]="\0\0";
uchar send_Ok1[]="Ok1\n";
uchar send_Ok2[]="Ok2\n";
uchar Start[]="Start";
uchar Stop[]="Stop";
uchar flag;
void Timer_Init()
{
TMOD=0x21;
TH0=0x3C;
TL0=0xB0;
TH1=0xf3;
TL1=0xf3;
PCON=0x80;
TR1=1;
SCON=0x50;
EA=1;
ET0=1;
ES=1;
}
void main()
{
Timer_Init();
while(1)
{
if(flag==1)
{
ES=0;
flag=0;
TR0=1;
data_count=0;
for(i=0;i<4;i++)
{
SBUF=send_Ok1[i];
while(!TI);
TI=0;
}
ES=1;
}
if(flag==2)
{
ES=0;
flag=0;
TR0=0;
data_count=0;
for(i=0;i<4;i++)
{
SBUF=send_Ok2[i];
while(!TI);
TI=0;
}
ES=1;
}
if(count==20)
{
count=0;
P1=~P1;
}
}
}
void t0()interrupt 1
{
TH0=0x3C;
TL0=0xB0;
count++;
}
void serial()interrupt 4
{
if(RI)
{
RI=0;
receive[data_count]=SBUF;
if(strcmp(receive,Start)==0)
{
flag=1;
for(i=0;i<8;i++)
receive[i]='\0';
}
if(strcmp(receive,Stop)==0)
{
flag=2;
for(i=0;i<8;i++)
{
receive[i]='\0';
}
}
data_count++;
}
}
第8题代码
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar count=0;
NUM=10;
sbit P33=P0^0;//外部中断1的输入线
sbit P35=P3^5;//定时器T1外部输入线
void delay(int x)
{
int m,n;
for(m=x;m>0;m--)
for(n=110;n>0;n--);
}
void Timer_Init()
{
TMOD=0x21;
TH0=0x3C;
TL0=0xB0;
TH1=0xf3; //4800
TL1=0xf3;
PCON=0X80;//加倍
TR1=1; //启动定时器1
SCON=0x50; //串行口工作方式1 sm0=0,sm1=1 ren=1
EA=1;
ET0=1;
ES=1;
P35=0;
P1=0x00;
}
void main()
{
uchar i=1;
Timer_Init();
while(1)
{
if(P33==0)
{
delay(20);
if(P33==0)
{
P1=0xff;
if(i==1)
{
count=0;
NUM=10;
SBUF='1';
while(!TI);
TI=0;
i++;
}
else if(i==2)
{
NUM=20;
SBUF='2';
while(!TI);
TI=0;
i++;
}
else //i=3
{
NUM=40;
ES=0;
SBUF='3';
while(!TI);
TI=0;
ES=1;
i=1;
}
TR0=1;
}
while(!P33);
delay(20);
}
}
}
void t0()interrupt 1
{
TH0=0x3C;
TL0=0xB0;
count++;
if(count==NUM)
{
count=0;
if(NUM==10)
P1=~P1+0xf0;
else if(NUM==20)
{
P1=~P1+0x0f;
}
else//num==40
{
P1=~P1;
}
}
}