王忠遠(yuǎn) 張鳳桐
摘要:Proteus電路設(shè)計(jì)仿真軟件是世界上著名的EDA開發(fā)工具,在單片機(jī)中嵌入串行通信軟件,利用仿真串口COMPIM與PC機(jī)通信。本論文主要利用Proteus仿真軟件制作單片機(jī)串行通信仿真電路,經(jīng)過(guò)虛擬串口可以實(shí)現(xiàn)在同一臺(tái)計(jì)算機(jī)上實(shí)現(xiàn)下位單片機(jī)與上位計(jì)算機(jī)仿真串行通信,上位計(jì)算機(jī)采用VB語(yǔ)言編輯界面,在單片機(jī)教學(xué)和電子產(chǎn)品開發(fā)中具有一定的參考價(jià)值。
關(guān)鍵詞: Proteus;COMPIM;虛擬串口;MSComm
中圖分類號(hào):TP311 文獻(xiàn)標(biāo)識(shí)碼:A 文章編號(hào):1009-3044(2016)36-0261-02
在單片機(jī)串行教學(xué)及電子產(chǎn)品開發(fā)時(shí),通常涉及單片機(jī)(下位機(jī))與計(jì)算機(jī)(上位機(jī))的串行通信,利用串行通信上位機(jī)發(fā)送命令給下位機(jī),下位機(jī)將待顯示數(shù)據(jù)傳到上位機(jī)上顯示,本論文設(shè)計(jì)收發(fā)幀定長(zhǎng)21個(gè)字節(jié)的串行通信,一幀包括2字節(jié)同步頭、2字節(jié)結(jié)束字、1字節(jié)命令字、16字節(jié)數(shù)據(jù)。上下位機(jī)通過(guò)一對(duì)虛擬串口進(jìn)行連接實(shí)現(xiàn)雙機(jī)通信。
1 Proteus仿真軟件繪制串行通信電路(如圖1)
2 利用kile c51軟件編寫下位機(jī)程序代碼
#include
#define uchar unsigned char
sbit P1_1=P1^1;
uchar Fhead=0xA5,F(xiàn)end=0x5A,shead=0,send=0; //頭尾字節(jié),找到頭尾標(biāo)志
uchar x,uartbuf,rdata[20]; //接收變量,接收緩沖21字節(jié)
uchar temp,tdata[21]={0xa5,0xa5,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0x5a,0x5a};
uchar scount; //接收有效數(shù)據(jù)
main()
{ TMOD=0x20; PCON=0x00; SCON=0x50; //設(shè)置波特率為9600b/s,10位異步收發(fā),啟動(dòng)定時(shí)器1
TH1=0xfd; TL1=0xfd; TR1=1;
EA=1; ES=1; //開啟中斷
while(1)
{ if(send==1||P1_1==0) //接收到正確數(shù)據(jù)或按鍵按下時(shí)下位機(jī)發(fā)送數(shù)據(jù)到上位機(jī)
{ tdata[0]=0xa5;tdata[1]=0xa5;tdata[19]=0x5a;tdata[20]=0x5a;
for(temp=0;temp<21;temp++) //連續(xù)發(fā)送有效接收數(shù)
{ SBUF=tdata[temp]; while(TI==0);TI=0; }
send=0; scount=0;
while(P1_1==0);
} } }
void uart() interrupt 4 using 1
{ if(RI==1)
{x=SBUF;
if(x==Fhead && x==uartbuf) //找同步頭
{shead=1;scount=0;}
if(x==Fend && x==uartbuf) //找結(jié)束字
{shead=0;;send=1;}
if(shead==1) //是同步頭非結(jié)束字保存數(shù)據(jù)
{rdata[scount]=x;tdata[scount+1]=x;scount++;}
uartbuf=x; RI=0;
}}
3 上位機(jī)串行通信界面設(shè)計(jì)
利用VB設(shè)計(jì)數(shù)據(jù)收發(fā)控制界面如圖2,在設(shè)計(jì)時(shí)需要增加外部控件:在工程-部件中選擇microsoft comm control 6.0控件,添加后在工具箱中出現(xiàn)電話機(jī)圖標(biāo),拖入窗體即可使用。
數(shù)據(jù)收發(fā)控制界面程序代碼如下:
Option Explicit
Dim shuzu(20) As Integer '發(fā)送數(shù)組的長(zhǎng)度
Dim fnum As Integer '接收有效數(shù)據(jù)在本次接收收據(jù)的首位值
Dim lt() As Byte '每次事件接收數(shù)組
Dim counter As Integer '總計(jì)接收有效數(shù)組數(shù)
Dim TOU As Boolean '數(shù)據(jù)頭標(biāo)志
Private Sub Form_Load()
TOU = False
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1" '端口波特率
MSComm1.InputLen = 21 '接收緩沖器讀出字節(jié)數(shù)或字符數(shù)
MSComm1.InputMode = 1
MSComm1.RThreshold = 21
MSComm1.RTSEnable = True
MSComm1.SThreshold = 1
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
End Sub
Private Sub cmdfs_Click() '發(fā)送數(shù)據(jù)
Dim i As Integer: ReDim shuju(20) As Byte
shuju(0) = &HA5: shuju(1) = &HA5: shuju(2) = &H10
For i = 3 To 18
shuju(i) = "&H" & Mid(txtSend.Text, 2 * (i - 3) + 1, 2)
Next i
shuju(19) = &H5A:shuju(20) = &H5A
MSComm1.Output = shuju
End Sub
Private Sub MSComm1_OnComm() '接收數(shù)據(jù)
Dim RX_lins As Integer, i As Integer, temp As Byte
fnum = 0
If MSComm1.CommEvent = comEvReceive Then
RX_lins = MSComm1.InBufferCount
ReDim lt(RX_lins) As Byte
lt = MSComm1.Input
For i = 0 To RX_lins - 1 '查找同步頭A5
If i >= 1 Then
If lt(i) = &HA5 And lt(i - 1) = &HA5 Then
fnum = i + 1: TOU = True
End If
End If
Next i
If TOU = True Then
For i = fnum To RX_lins - 1
shuzu(i - fnum) = lt(i) '導(dǎo)入數(shù)據(jù)
Next i
counter = RX_lins - fnum '累加接收到的數(shù)據(jù)
End If
If TOU = True And counter >= 3 Then '找到頭尾時(shí)數(shù)據(jù)有效
If shuzu(counter - 2) = &H5A And shuzu(counter - 1) = &H5A Then
For i = 0 To counter - 1 '接收數(shù)據(jù)送至接收文本框顯示
txtRcv.Text = txtRcv.Text & Hex(shuzu(i)) & " "
Next i
counter = 0: TOU = False: fnum = 0
End If
End If
End If
End Sub
4 虛擬串口的連接
虛擬串口7.2如圖2-2,建立com1和com2為一對(duì)虛擬端口,其中com2設(shè)為proteus仿真軟件COMPIM串口,com1設(shè)為計(jì)算機(jī)vb界面中的MSComm1.CommPort = 1串口,雙機(jī)運(yùn)行實(shí)現(xiàn)單片機(jī)與PC的串行通信。
5 仿真調(diào)試
該實(shí)例已通過(guò)測(cè)試運(yùn)行正常,實(shí)現(xiàn)雙機(jī)通信21字節(jié)串行通信的傳輸,也可利用其他高級(jí)語(yǔ)言編寫上位機(jī)界面或計(jì)算機(jī)串口調(diào)試工具。
參考文獻(xiàn):
[1] 潘新明.微型計(jì)算機(jī)控制技術(shù)[M].北京:人民郵電出版社,2009.
[2] 王瑾.PC機(jī)與AT89C51單片機(jī)的串行通信[J].科技天地,2012(4):83-84.
[3] 郭宏亮.PC機(jī)與AT89C51單片機(jī)的串行通信接口設(shè)計(jì)[J].平原大學(xué)學(xué)報(bào),2007(6):118-119.
[4] 王忠遠(yuǎn).基于Proteus仿真軟件實(shí)現(xiàn)AT89C51單片機(jī)與計(jì)算機(jī)的串行通信[J].電子設(shè)計(jì)技術(shù),2013(11):43-45.