欢迎来到天天文库
浏览记录
ID:16705375
大小:22.00 KB
页数:4页
时间:2018-08-24
《stc系列单片机串口通信的总结》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、串口通信:软件调试,在调试过程中需要使用虚拟串口助手。在编程中注意设计时钟和波特率。注意程序的串口设置和串口调试助手中串口设置相同。单串口:modecom29600,0,8,1assigncom2soutstime=0多串口:modecom29600,0,8,1assigncom2s0out0表示单片机的串口0编程:STC12介绍的方法:接收一个字节的函数和发送字符串的函数,发送字符串需要知道字符串的长度。对于接收字符串的函数,可以仿照通过调用接收一个字节的函数,写出接收字符串函数。voidWriteUart(uch
2、ar*pucData,ucharucLength){if(ucLength==0){return;}if(UartBuzy==1)return;ucSendLength=ucLength;pucSendData=pucData;SBUF=*pucSendData;UartBuzy=1;ucSendedCount=0;}ucharReadUart(void){ucharData;Data=ucRecData;ucRecData=0;returnData;}UnsignedcharRString(unsignedchar*s,unsigned
3、intlen){Unsignedinti;For(i=0;i4、teeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,MinistersandvoidUartIRQ(void)interrupt4{if(RI){RI=0;ucRe5、cData=SBUF;}if(TI){TI=0;ucSendedCount++;if(ucSendedCount>=ucSendLength){UartBuzy=0;return;}else{SBUF=*(pucSendData+ucSendedCount);}}}STC15:STC15只给出了发送函数,且发送函数有瑕疵,具体见最后分析。voidUART1_ISR(void)interrupt4using1{if(RI)//接收数据{RI=0;//清除RI位}if(TI)//发送数据{TI=0;//清除TI位busy=0;//清忙标志}}6、voidSendData(unsignedchardat){while(busy);//等待前面的数据发送完成busy=1;SBUF=dat;//写数据到UART数据寄存器}voidSendString(char*s){otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderof7、theWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Mini8、stersandwhile(*s)//检测字符串结束标志{SendData(*s++);//发送当前字符}}SendString()字符串函数,当遇到发送的字符串里面有0x00就会终止,实用性
4、teeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,MinistersandvoidUartIRQ(void)interrupt4{if(RI){RI=0;ucRe
5、cData=SBUF;}if(TI){TI=0;ucSendedCount++;if(ucSendedCount>=ucSendLength){UartBuzy=0;return;}else{SBUF=*(pucSendData+ucSendedCount);}}}STC15:STC15只给出了发送函数,且发送函数有瑕疵,具体见最后分析。voidUART1_ISR(void)interrupt4using1{if(RI)//接收数据{RI=0;//清除RI位}if(TI)//发送数据{TI=0;//清除TI位busy=0;//清忙标志}}
6、voidSendData(unsignedchardat){while(busy);//等待前面的数据发送完成busy=1;SBUF=dat;//写数据到UART数据寄存器}voidSendString(char*s){otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderof
7、theWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Mini
8、stersandwhile(*s)//检测字符串结束标志{SendData(*s++);//发送当前字符}}SendString()字符串函数,当遇到发送的字符串里面有0x00就会终止,实用性
此文档下载收益归作者所有