51单片机的串口通信程序(C语言)

  • 格式:doc
  • 大小:29.50 KB
  • 文档页数:6

下载文档原格式

  / 6
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#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;