标题:
单片机串口中断和定时器中断的利用
[打印本页]
作者:
卓zz
时间:
2019-3-9 12:56
标题:
单片机串口中断和定时器中断的利用
使用单片机,每秒,通过串口发个10个字节的数据到电脑,然后点灯(每秒取反),让蜂鸣器响或不响。
需要使用到定时器和定时器中断和串口中断。
单片机源程序如下:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
char code num[]={0x44,0x5f,0x34,0x15,0x0f,0x85,0x84,0x57,0x04,0x05};
char code fnum[]={0x40,0x5b,0x30,0x11,0x0b,0x81,0x80,0x53,0x00,0x01};
uchar f_shi,f_ge,shi,ge,num1,a=0,b=0,j,i=4;
void matrixkeyscan();
void delayms(uint xms);
void display(uchar, uchar, uchar, uchar );
void main()
{
TMOD=0x11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TH1=(65536-458)/256;
TL1=(65536-458)%256;
EA=1;
ET0=1;
TR0=1;
ET1=1;
TR1=1;
while(1)
{
matrixkeyscan();
}
}
void display(uchar f_shi,uchar f_ge,uchar shi,uchar ge)
{
P3=0xfb;P1=num[f_shi];P1=0Xff;
P3=0xf7;P1=fnum[f_ge];P1=0Xff;
P3=0xef;P1=num[shi]; P1=0Xff;
P3=0xdf;P1=num[ge]; P1=0Xff;
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void matrixkeyscan()
{
uchar temp,key;
P2=0Xfe;
temp=P2;
temp=temp&0xf0;
if(temp != 0xf0)
{
delayms(10);
temp=P2;
temp=temp & 0xf0;
if(temp != 0xf0)
{
temp=P2;
switch(temp)
{
case 0x7e: j=0;
break;
case 0xbe: j=1;
break;
case 0xde: j=2;
break;
case 0xee: j=3;
break;
}
while(temp != 0Xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
P2=0Xfd;
temp=P2;
temp=temp&0xf0;
if(temp != 0xf0)
{
delayms(10);
temp=P2;
temp=temp & 0xf0;
if(temp != 0xf0)
{
temp=P2;
switch(temp)
{
case 0x7d: j=4;
break;
case 0xbd: j=5;
break;
case 0xdd: j=6;
break;
case 0xed: j=7;
break;
}
while(temp != 0Xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
P2=0Xfb;
temp=P2;
temp=temp&0xf0;
if(temp != 0xf0)
{
delayms(10);
temp=P2;
temp=temp & 0xf0;
if(temp != 0xf0)
{
temp=P2;
switch(temp)
{
case 0x7b: j=8;
break;
case 0xbb: j=9;
break;
case 0xdb: j=0;
break;
case 0xeb: j=0;
break;
}
while(temp != 0Xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
P2=0Xf7;
temp=P2;
temp=temp&0xf0;
if(temp != 0xf0)
{
delayms(10);
temp=P2;
temp=temp & 0xf0;
if(temp != 0xf0)
{
temp=P2;
switch(temp)
{
case 0x77: i=0;
break;
case 0xb7: i=1;
break;
case 0xd7: i=2;
break;
case 0xe7: i=3;
break;
}
while(temp != 0Xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
}
void chronograph() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
if(i==0)
{
switch(j)
{
case 0: f_shi=0; i=4;break;
case 1: f_shi=1; i=4;break;
case 2: f_shi=2; i=4;break;
case 3: f_shi=3; i=4;break;
case 4: f_shi=4; i=4;break;
case 5: f_shi=5; i=4;break;
}
}
if(i==1)
{
switch(j)
{
case 0: f_ge=0; i=4;break;
case 1: f_ge=1; i=4;break;
case 2: f_ge=2; i=4;break;
case 3: f_ge=3; i=4;break;
case 4: f_ge=4; i=4;break;
case 5: f_ge=5; i=4;break;
case 6: f_ge=6; i=4;break;
case 7: f_ge=7; i=4;break;
case 8: f_ge=8; i=4;break;
case 9: f_ge=9; i=4;break;
}
}
if(i==2)
{
switch(j)
{
case 0: shi=0; i=4;break;
case 1: shi=1; i=4;break;
case 2: shi=2; i=4;break;
case 3: shi=3; i=4;break;
case 4: shi=4; i=4;break;
case 5: shi=5; i=4;break;
}
}
if(i==3)
{
switch(j)
{
case 0: ge=0; i=4;break;
case 1: ge=1; i=4;break;
case 2: ge=2; i=4;break;
case 3: ge=3; i=4;break;
case 4: ge=4; i=4;break;
case 5: ge=5; i=4;break;
case 6: ge=6; i=4;break;
case 7: ge=7; i=4;break;
case 8: ge=8; i=4;break;
case 9: ge=9; i=4;break;
}
}
num1++;
if(num1==20)
{
num1=0;
ge++;
if(ge==10)
{
ge=0;
shi++;
if(shi==6)
{
shi=0;
f_ge++;
if(f_ge==10)
{
f_ge=0;
f_shi++;
if(f_shi==6)
{
ge=0;
shi=0;
f_ge=0;
f_shi=0;
}
}
}
}
}
}
void xianshi() interrupt 3
{
TH1=(65536-458)/256;
TL1=(65536-458)%256;
display(f_shi,f_ge,shi,ge);
}
复制代码
所有资料51hei提供下载:
单片机编程4.zip
(27.77 KB, 下载次数: 5)
2019-3-9 12:56 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
admin
时间:
2019-3-11 00:16
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1