轉(zhuǎn)速測(cè)量系統(tǒng)
通過單片機(jī)采集轉(zhuǎn)速數(shù)據(jù),通過USB口發(fā)送到PC,并產(chǎn)生曲線圖。
單片機(jī)程序:
//---------------------串口初始化----------------------------
void initSerial()
{
TMOD=0x20; //定時(shí)器1置為方式2
TH1=0xfd;
TL1=0xfd; //設(shè)置波特率位9600
SCON = 0x50; // 設(shè)定串行口工作方式
PCON &= 0x00; // 波特率不倍增
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1;
}
//---------------------------發(fā)送程序---------------------------------------------
void send_char(void)
// 傳送十六位的蝯轉(zhuǎn)速數(shù)據(jù),低位在前
{
unsigned i=0;
while (i < 6)
{
SBUF =zhuansu_char[i];
while (!TI); // 等特?cái)?shù)據(jù)傳送
TI = 0; // 清除數(shù)據(jù)傳送標(biāo)志
i++;
}
}
//----------------------主程序------------------------
void main()
{
uchar receive;
lcdrs=0;
init(); //中斷初始化
initSerial(); //通信初始化
lcdinit(); //液晶顯示初始化
write_project_title();
while(1)
{
write_project_data(zhuansu);
if (RI) // 是否有數(shù)據(jù)到來
{
RI = 0;
receive = SBUF;
if (receive == 's') // 是否開始采集轉(zhuǎn)速
{
send_char(); // 傳送采集的轉(zhuǎn)速
}
}
}
}
VB界面如下:
調(diào)試后的結(jié)果是:
VB程序如下:
'定義窗體級(jí)變量
'在顯示、繪圖等過程中使用
Dim datazhuansu(200) As Single ' 用于存儲(chǔ)轉(zhuǎn)速采樣值
Dim num As Integer ' 用于存儲(chǔ)采樣值個(gè)數(shù)
'-----------------------------------------------
' 轉(zhuǎn)速采集
'-----------------------------------------------
Private Sub CmdStart_Click() ' 開始采集
Timer1.Enabled = True
End Sub
'-----------------------------------------------
' 停止采集
'-----------------------------------------------
Private Sub CmdStop_Click() ' 停止采集
Timer1.Enabled = False
End Sub
'串口初始化
'在窗體的Load事件中加入下列代碼對(duì)串口進(jìn)行初始化:
'-----------------------------------------------
' 載入窗體
'-----------------------------------------------
Private Sub Form_Load()
MSComm1.CommPort = 1 ' 設(shè)置串口
MSComm1.InputMode = comInputModeBinary ' 二進(jìn)制輸入模式
MSComm1.RThreshold = 1 ' 接收1個(gè)字符觸法OnComm 事件
MSComm1.SThreshold = 1 ' 發(fā)送1個(gè)字符觸法OnComm 事件
MSComm1.Settings = "9600,n,8,2" ' 設(shè)置波特率
MSComm1.PortOpen = True ' 打開串口
Call ScaleSys ' 繪制坐標(biāo)系
CmdStop.Enabled = False
End Sub
'-----------------------------------------------
' 接收觸法事件
'-----------------------------------------------
'獲取轉(zhuǎn)速測(cè)量值并顯示
'每發(fā)送一次指令,觸發(fā)下面事件,返回?cái)?shù)據(jù)串
Private Sub MSComm1_OnComm()
Dim Inbyte() As Byte ' 接收數(shù)據(jù)暫存
Dim buffer As String ' 轉(zhuǎn)速數(shù)據(jù)緩沖
Dim datasu2a, datasu2b As String ' 兩字節(jié)進(jìn)制轉(zhuǎn)速數(shù)據(jù)
Dim datasu2 As String ' 十六進(jìn)制轉(zhuǎn)速數(shù)據(jù)
If num > 199 Then ' 接收個(gè)數(shù)判斷
Call renew ' 接收完畢
End If
'讀取儀表返回?cái)?shù)據(jù)串
Select Case MSComm1.CommEvent
Case comEvReceive
Inbyte = MSComm1.Input ' 接收轉(zhuǎn)速數(shù)據(jù)
For i = LBound(Inbyte) To UBound(Inbyte) ' 把接收的數(shù)據(jù)安十六進(jìn)制格式放入緩沖中
buffer = buffer + Hex(Inbyte(i)) + Chr(32)
Next i
End Select
'獲取十進(jìn)制測(cè)量數(shù)據(jù)
If Len(Trim(Mid(buffer, 1, 2))) = 1 Then
datazhuansu(num) = Val("&H" & Mid(buffer, 3, 3) & Str("0") & Mid(buffer, 1, 2)) * 0.0625
Else
datazhuansu(num) = Val("&H" & Mid(buffer, 3, 3) & Mid(buffer, 1, 2)) * 0.0625
End If
'獲取十六進(jìn)制測(cè)量數(shù)據(jù)
If Len(Trim(Mid(buffer, 1, 2))) = 1 Then
datasu2a = Str("0") & Trim(Mid(buffer, 1, 2))
Else
datasu2a = Mid(buffer, 1, 2)
End If
If Len(Trim(Mid(buffer, 4, 2))) = 1 Then
datasu2b = Str("0") & Trim(Mid(buffer, 3, 2))
Else
datasu2b = Mid(buffer, 4, 2)
End If
datasu2 = datasu2a & " " & datasu2b
'顯示測(cè)量轉(zhuǎn)速值
If datazhuansu(num) <> 0 Then
zhuansuText = datasu2
Call draw ' 調(diào)用繪曲線過程
End If
End Sub
'-----------------------------------------------
' 轉(zhuǎn)速曲線繪制
'-----------------------------------------------
'繪制轉(zhuǎn)速實(shí)時(shí)變化曲線
Private Sub draw()
Picture1.DrawWidth = 2 ' 設(shè)置線寬
Picture1.DrawStyle = vbSolid
For i = 1 To num - 1
X1 = (i - 1): Y1 = datazhuansu(i - 1)
X2 = i: Y2 = datazhuansu(i)
Picture1.Line (X1, Y1)-(X2, Y2), QBColor(0) ' 繪制轉(zhuǎn)速曲線
Next i
End Sub
'-----------------------------------------------
' 刷新繪圖區(qū)
'-----------------------------------------------
Private Sub renew()
If num = 0 Then Exit Sub
zhuansuText.Text = "":
Picture1.Cls
Call ScaleSys
For i = 0 To num - 1
datazhuansu(i) = 0
Next i
num = 0
Counter = 0
End Sub
'-----------------------------------------------
' 定時(shí)發(fā)送采集標(biāo)志
'-----------------------------------------------
'每隔 x ms向儀表發(fā)送讀數(shù)據(jù)命令串
'每臺(tái)儀表有一個(gè)儀表號(hào),PC機(jī)通過儀表號(hào)來識(shí)別網(wǎng)上的多臺(tái)儀表
'程序中儀表號(hào)(即地址代號(hào))要與儀表設(shè)定值一致,否則不能返回?cái)?shù)據(jù)。
Private Sub Timer1_Timer()
MSComm1.Output = "s" ' 發(fā)送開始標(biāo)志
End Sub
'-----------------------------------------------
' 卸載窗體
'-----------------------------------------------
Private Sub Cmdquit_Click()
Unload Me ' 卸載窗體
End Sub
'-----------------------------------------------
' 建立圖像坐標(biāo)系
'-----------------------------------------------
Sub ScaleSys() ' 坐標(biāo)系
Picture1.AutoRedraw = True ' 自動(dòng)重繪有效
Picture1.DrawWidth = 1 ' 線寬1個(gè)像素
Picture1.ScaleMode = vbPixels ' 像素為單位
Picture1.Scale (0, 125)-(200, -50) ' 坐標(biāo)系
Picture1.DrawStyle = vbDot ' 點(diǎn)線
' 橫坐標(biāo)
Picture1.Line (0, 0)-(200, 0), RGB(130, 130, 130)
Picture1.Line (0, 25)-(200, 25), RGB(130, 130, 130)
Picture1.Line (0, 50)-(200, 50), RGB(130, 130, 130)
Picture1.Line (0, 75)-(200, 75), RGB(130, 130, 130)
Picture1.Line (0, 100)-(200, 100), RGB(130, 130, 130)
Picture1.Line (0, -25)-(200, -25), RGB(130, 130, 130)
' 縱坐標(biāo)
Picture1.Line (25, 125)-(25, -50), RGB(130, 130, 130)
Picture1.Line (50, 125)-(50, -50), RGB(130, 130, 130)
Picture1.Line (75, 125)-(75, -50), RGB(130, 130, 130)
Picture1.Line (100, 125)-(100, -50), RGB(130, 130, 130)
Picture1.Line (125, 125)-(125, -50), RGB(130, 130, 130)
Picture1.Line (150, 125)-(150, -50), RGB(130, 130, 130)
Picture1.Line (175, 125)-(175, -50), RGB(130, 130, 130)
End Sub