c51单片机串口程序c语言,51单片机串口程序C语言版

#include

#include

#include

#include

#define uchar unsigned char

#define uint unsigned int

sbit Key1 = P2^3;

sbit Key2 = P2^2;

sbit Key3 = P2^1;

sbit Key4 = P2^0;

sbit BELL = P3^6;

sbit CONNECT = P3^7;

unsigned int  Key1_flag = 0;

unsigned int  Key2_flag = 0;

unsigned int  Key3_flag = 0;

unsigned int  Key4_flag = 0;

unsigned char b;

unsigned char code Num[21]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,

0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,

0x10,0x89};

unsigned char code Disdigit[4] = {0x7F,0xBF,0xDF,0xEF};

unsigned char Disbuf[4];

void delayms(uint t)

{

uint i;

while(t--)

{

/* 对于11.0592M时钟,约延时1ms */

for (i=0;i<125;i++)

{}

}

}

//-----------------------------------------------------

void SendData(uchar Dat)

{

uchar i=0;

SBUF = Dat;

while (1)

{

if(TI)

{

TI=0;

break;

}

}

}

void ScanKey()

{

if(Key1 == 0)

{

delayms(100);

if(Key1 == 0)

{

Key1_flag = 1;

Key2_flag = 0;

Key3_flag = 0;

Key4_flag = 0;

Key1 = 1;

}

else;

}

if(Key2 == 0)

{

delayms(100);

if(Key2 == 0)

{

Key2_flag = 1;

Key1_flag = 0;

Key3_flag = 0;

Key4_flag = 0;

Key2 = 1;

}

else;

}

if(Key3 == 0)

{

delayms(50);

if(Key3 == 0)

{

Key3_flag = 1;

Key1_flag = 0;

Key2_flag = 0;

Key4_flag = 0;

Key3 = 1;

}

else;

}

if(Key4 == 0)

{

delayms(50);

if(Key4 == 0)

{

Key4_flag = 1;

Key1_flag = 0;

Key2_flag = 0;

Key3_flag = 0;

Key4 = 1;

}

else;

}

else;

}

void KeyProc()

{

if(Key1_flag)

{

TR1 = 1;

SendData(0x55);

Key1_flag = 0;

}

else if(Key2_flag)

{

TR1 = 1;

SendData(0x11);

Key2_flag = 0;

}

else if(Key3_flag)

{

P1=0xff;

BELL = 0;

CONNECT = 1;

Key3_flag = 0;

}

else if(Key4_flag)

{

CONNECT = 0;

BELL = 1;

Key4_flag = 0;

}

else;

}

void Initdisplay(void)

{

Disbuf[0] = 1;

Disbuf[1] = 2;

Disbuf[2] = 3;

Disbuf[3] = 4;

}

void Display()     //显示

{

unsigned int     i = 0;

unsigned int temp,count;

temp = Disdigit[count];

P2 =temp;

temp = Disbuf[count];

temp = Num[temp];

P0 =temp;

count++;

if  (count==4)

count=0;

}

void time0() interrupt 1  using 2

{

Display();

TH0 = (65535 - 2000)/256;

TL0 = (65535 - 2000)%256;

}

void main()

{

Initdisplay();

TMOD = 0x21;

TH0 = (65535 - 2000)/256;

TL0 = (65535 - 2000)%256;

TR0 = 1;

ET0 = 1;

TH1 = 0xFD; //11.0592M

TL1 = 0xFD;

PCON&=0x80;

TR1 = 1;

ET1 = 1;

SCON = 0x40; //串口方式

REN = 1;

PT1 = 0;

PT0 = 1;

EA = 1;

while(1)

{

ScanKey();

KeyProc();

if(RI)

{

Disbuf[0] = 0;

Disbuf[1] = 20;

Disbuf[2] = SBUF>>4;

Disbuf[3] = SBUF&0x0f;

RI = 0;

}

else;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值