STC89C52单片机的一些程序包括串口通信定时中断.docx

上传人:b****6 文档编号:5283290 上传时间:2022-12-14 格式:DOCX 页数:16 大小:15.92KB
下载 相关 举报
STC89C52单片机的一些程序包括串口通信定时中断.docx_第1页
第1页 / 共16页
STC89C52单片机的一些程序包括串口通信定时中断.docx_第2页
第2页 / 共16页
STC89C52单片机的一些程序包括串口通信定时中断.docx_第3页
第3页 / 共16页
STC89C52单片机的一些程序包括串口通信定时中断.docx_第4页
第4页 / 共16页
STC89C52单片机的一些程序包括串口通信定时中断.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

STC89C52单片机的一些程序包括串口通信定时中断.docx

《STC89C52单片机的一些程序包括串口通信定时中断.docx》由会员分享,可在线阅读,更多相关《STC89C52单片机的一些程序包括串口通信定时中断.docx(16页珍藏版)》请在冰豆网上搜索。

STC89C52单片机的一些程序包括串口通信定时中断.docx

STC89C52单片机的一些程序包括串口通信定时中断

针对STC89C52RC单片机的一些程序

基本串口通信程序:

#include

#defineuintunsignedint

#defineucharunsignedchar

#defineSEND0

#defineRECEIVE1

ucharflag_Serial=SEND;

ucharRx_Buf;

sbitSMG=P1^7;

//0123456789abcdef

ucharcodeShow[16]={0x03,0x9f,0x25,0x0d,0x99,0x49,

0x41,0x1f,0x01,0x19,0x11,

0xc1,0x63,0x85,0x61,0x71};

voidSerial_Init();

voidSerial_send();

voidDisplay();

voidDelay_1ms(ucharz);

/****************************************************/

//mainfunction

//

/****************************************************/

voidmain()

{

Serial_Init();

while

(1)

{

switch(flag_Serial)

{

caseSEND:

flag_Serial=RECEIVE;

Serial_send();

break;

caseRECEIVE:

Display();

break;

}

}

}

/****************************************************/

//serialinitial

//

/****************************************************/

voidSerial_Init()

{

TMOD=0X20;//workinmode2

PCON=0X00;

SCON=0X50;

TH1=0XFD;//BRUD9600

TL1=0XFD;

TR1=1;//openinterrupt1

ES=1;//opentheserial

EA=1;//openthekeyinterrupt

flag_Serial=SEND;

SMG=0;

}

/****************************************************/

//serialinterruptfunction

//

/****************************************************/

voidSerial()interrupt4using2

{

EA=0;

if(RI!

=0&&flag_Serial==RECEIVE)

{

RI=0;

Rx_Buf=SBUF;

flag_Serial=SEND;

}

EA=1;

}

/****************************************************/

//mcusenddatatoserial

//

/****************************************************/

voidSerial_send()

{

SBUF=Rx_Buf;

while(TI==0);

TI=0;

}

/****************************************************/

//displaythedatabySMG

//

/****************************************************/

voidDisplay()

{

P2=Show[Rx_Buf];

Delay_1ms(3);

}

/****************************************************/

//delayforz*1ms

//

/****************************************************/

voidDelay_1ms(ucharz)

{

uchari,j;

for(i=z;i>0;i--)

for(j=110;j>0;j--);

}

串口通信,按键加减

#include

#defineuintunsignedint

#defineucharunsignedchar

#defineSEND0

#defineRECEIVE1

sbitKEY1=P1^4;

sbitKEY2=P1^5;

sbitLED1=P1^0;

sbitLED2=P1^1;

sbitSMG=P1^7;

ucharflag_Serial=SEND;

ucharRx_Buf;

ucharSmg_Buf;

//0123456789abcdef

ucharcodeShow[16]={0x03,0x9f,0x25,0x0d,0x99,0x49,

0x41,0x1f,0x01,0x19,0x11,

0xc1,0x63,0x85,0x61,0x71};

voidSerial_Init();

voidSerial_send();

voidDisplay();

voidDelay_1ms(ucharz);

voidScan();

/****************************************************/

//mainfunction

//

/****************************************************/

voidmain()

{

Serial_Init();

while

(1)

{

switch(flag_Serial)

{

caseSEND:

flag_Serial=RECEIVE;

Serial_send();

break;

caseRECEIVE:

Display();

break;

}

Scan();

}

}

/****************************************************/

//serialinitial

//

/****************************************************/

voidSerial_Init()

{

TMOD=0X20;//workinmode2

PCON=0X00;

SCON=0X50;

TH1=0XFD;//BRUD9600

TL1=0XFD;

TR1=1;//openinterrupt1

ES=1;//opentheserial

EA=1;//openthekeyinterrupt

flag_Serial=SEND;

SMG=0;

}

/****************************************************/

//serialinterruptfunction

//

/****************************************************/

voidSerial()interrupt4using2

{

EA=0;

if(RI!

=0&&flag_Serial==RECEIVE)

{

RI=0;

Rx_Buf=SBUF;

flag_Serial=SEND;

}

EA=1;

}

/****************************************************/

//mcusenddatatoserial

//

/****************************************************/

voidSerial_send()

{

SBUF=Rx_Buf;

while(TI==0);

TI=0;

}

/****************************************************/

//displaythedatabySMG

//

/****************************************************/

voidDisplay()

{

P2=Show[Rx_Buf];

Delay_1ms(3);

}

/****************************************************/

//delayforz*1ms

//

/****************************************************/

voidDelay_1ms(ucharz)

{

uchari,j;

for(i=z;i>0;i--)

for(j=110;j>0;j--);

}

/****************************************************/

//scanthekeys

//

/****************************************************/

voidScan()

{

if(KEY1==0)

{

Delay_1ms(10);

if(KEY1==0)

{

LED1=~LED1;

Rx_Buf++;

if(Rx_Buf==16)Rx_Buf=0;

Display();

while(KEY1==0);

}

}

if(KEY2==0)

{

Delay_1ms(10);

if(KEY2==0)

{

LED2=~LED2;

if(Rx_Buf==0)

Rx_Buf=15;

else

Rx_Buf--;

Display();

while(KEY2==0);

}

}

}

串口通信,按键加减提示音,(利用定时中断)快速加减提示音

#include

#defineuintunsignedint

#defineucharunsignedchar

#defineSEND0

#defineRECEIVE1

#defineNORMAL0

#defineINCREASE1

#defineDECREASE2

sbitKEY1=P1^4;

sbitKEY2=P1^5;

sbitLED1=P1^0;

sbitLED2=P1^1;

sbitSMG=P1^7;

sbitSPEAKER=P1^6;

ucharflag_Serial=SEND;

ucharRx_Buf;

ucharcount_2s;

ucharflag_KS;

//0123456789abcdef

ucharcodeShow[16]={0x03,0x9f,0x25,0x0d,0x99,0x49,

0x41,0x1f,0x01,0x19,0x11,

0xc1,0x63,0x85,0x61,0x71};

voidSerial_Init();

voidSerial_send();

voidDisplay();

voidDelay_1ms(ucharz);

voidScan();

/****************************************************/

//mainfunction

//

/****************************************************/

voidmain()

{

Serial_Init();

while

(1)

{

switch(flag_Serial)

{

caseSEND:

flag_Serial=RECEIVE;

Serial_send();

break;

caseRECEIVE:

Display();

break;

}

Scan();

}

}

/****************************************************/

//serialinitial

//

/****************************************************/

voidSerial_Init()

{

TMOD=0X21;//workinmode2

PCON=0X00;

SCON=0X50;

TH1=0XFD;//BRUD9600

TL1=0XFD;

TR1=1;//openinterrupt1

ES=1;//opentheserial

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

ET0=1;

TR0=0;

EA=1;//openthekeyinterrupt

flag_Serial=SEND;

SMG=0;

}

/****************************************************/

//serialinterruptfunction

//

/****************************************************/

voidSerial()interrupt4

{

EA=0;

if(RI!

=0&&flag_Serial==RECEIVE)

{

RI=0;

Rx_Buf=SBUF;

flag_Serial=SEND;

}

EA=1;

}

/****************************************************/

//interruptzerofunction

//

/****************************************************/

voidTimer0()interrupt1

{

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

count_2s++;

if(count_2s==40)//50ms*40=2s

{

count_2s=0;

if(KEY1==0)

flag_KS=INCREASE;

else

flag_KS=DECREASE;

}

}

/****************************************************/

//mcusenddatatoserial

//

/****************************************************/

voidSerial_send()

{

SBUF=Rx_Buf;

while(TI==0);

TI=0;

}

/****************************************************/

//displaythedatabySMG

//

/****************************************************/

voidDisplay()

{

P2=Show[Rx_Buf];

}

/****************************************************/

//delayforz*1ms

//

/****************************************************/

voidDelay_1ms(ucharz)

{

uchari,j;

for(i=z;i>0;i--)

for(j=110;j>0;j--);

}

/****************************************************/

//scanthekeys

//

/****************************************************/

voidScan()

{

if(KEY1==0)

{

Delay_1ms(10);

if(KEY1==0)

{

LED1=0;

SPEAKER=0;

Delay_1ms(250);

SPEAKER=1;

Delay_1ms(250);

LED1=1;

flag_Serial=SEND;

Rx_Buf++;

if(Rx_Buf==16)Rx_Buf=0;

Display();

while(KEY1==0)

{

TR0=1;

ES=0;

if(flag_KS==INCREASE&&count_2s==4)

{

count_2s=0;

Rx_Buf++;

if(Rx_Buf==16)Rx_Buf=0;

SPEAKER=0;

LED1=0;

Delay_1ms(200);

SPEAKER=1;

LED1=1;

Delay_1ms(200);

Display();

}

}

flag_KS=NORMAL;

ES=1;

TR0=0;

}

}

if(KEY2==0)

{

Delay_1ms(10);

if(KEY2==0)

{

LED2=0;

SPEAKER=0;

Delay_1ms(125);

SPEAKER=1;

Delay_1ms(250);

LED2=1;

flag_Serial=SEND;

if(Rx_Buf==0)

Rx_Buf=15;

else

Rx_Buf--;

Display();

while(KEY2==0)

{

TR0=1;

ES=0;

if(flag_KS==DECREASE&&count_2s==4)

{

count_2s=0;

if(Rx_Buf==0)

Rx_Buf=15;

else

Rx_Buf--;

SPEAKER=0;

LED2=0;

Delay_1ms(200);

SPEAKER=1;

LED2=1;

Delay_1ms(200);

Display();

}

}

flag_KS=NORMAL;

ES=1;

TR0=0;

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 院校资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1