|
如果打不开的可以把控件mscomm放到c//windows //system 目录下
下面是上位机和单片机源程序,用了两个外中断和三个定时器,电路图很简单
////////////////////////////////////vb源程序/////////////////////////////////////
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form For1
Caption = "For Gz Mtro"
ClientHeight = 5415
ClientLeft = 60
ClientTop = 450
ClientWidth = 9195
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 361
ScaleMode = 3 'Pixel
ScaleWidth = 613
StartUpPosition = 3 '窗口缺省
Begin VB.Timer Timer1
Left = 2040
Top = 0
End
Begin VB.PictureBox Picture1
AutoRedraw = -1 'True
BackColor = &H00004000&
BorderStyle = 0 'None
ForeColor = &H0000FF00&
Height = 5400
Left = 0
ScaleHeight = 360
ScaleMode = 3 'Pixel
ScaleWidth = 616
TabIndex = 0
Top = 0
Width = 9240
Begin VB.TextBox Text10
Appearance = 0 'Flat
BackColor = &H00FF8080&
BorderStyle = 0 'None
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FFFFFF&
Height = 270
Left = 4440
TabIndex = 26
Text = "Text10"
Top = 4920
Width = 2055
End
Begin VB.Timer Timer2
Interval = 300
Left = 2520
Top = 0
End
Begin VB.Frame Frame2
BackColor = &H00FFFFC0&
Caption = "Sensor Date"
Height = 1935
Left = 3720
TabIndex = 6
Top = 2880
Width = 5295
Begin VB.TextBox Text9
Height = 270
Left = 4080
TabIndex = 25
Text = "Text9"
Top = 1440
Width = 1095
End
Begin VB.TextBox Text8
Height = 375
Left = 4080
TabIndex = 24
Text = "Text8"
Top = 1200
Width = 1095
End
Begin VB.TextBox Text7
BackColor = &H00C0FFFF&
Height = 495
Left = 4080
TabIndex = 13
Text = "Text7"
Top = 600
Width = 1095
End
Begin VB.TextBox Text6
BackColor = &H00C0FFFF&
Height = 495
Left = 2880
TabIndex = 12
Text = "Text6"
Top = 600
Width = 975
End
Begin VB.TextBox Text5
BackColor = &H00C0FFFF&
Height = 495
Left = 1680
TabIndex = 11
Text = "Text5"
Top = 600
Width = 975
End
Begin VB.TextBox Text4
BackColor = &H00C0FFFF&
Height = 495
Left = 480
TabIndex = 10
Text = "Text4"
Top = 600
Width = 975
End
Begin VB.TextBox Text3
BackColor = &H00C0FFFF&
Height = 495
Left = 2880
TabIndex = 9
Text = "Text3"
Top = 1200
Width = 975
End
Begin VB.TextBox Text2
BackColor = &H00C0FFFF&
Height = 495
Left = 1680
TabIndex = 8
Text = "Text2"
Top = 1200
Width = 975
End
Begin VB.TextBox Text1
BackColor = &H00C0FFFF&
Height = 495
Left = 480
TabIndex = 7
Text = "Text1"
Top = 1200
Width = 975
End
Begin VB.Label Label10
BackColor = &H00FFFFC0&
Caption = "AB相位角"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 4080
TabIndex = 23
Top = 240
Width = 1095
End
Begin VB.Label Label9
BackColor = &H00FFFFC0&
Caption = "B相"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 0
TabIndex = 22
Top = 1320
Width = 375
End
Begin VB.Label Label8
BackColor = &H00FFFFC0&
Caption = "A相"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 0
TabIndex = 21
Top = 720
Width = 375
End
Begin VB.Label Label7
BackColor = &H00FFFFC0&
Caption = "脉冲计数"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 2880
TabIndex = 20
Top = 240
Width = 1215
End
Begin VB.Label Label6
BackColor = &H00FFFFC0&
Caption = "占空比%"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 1800
TabIndex = 19
Top = 240
Width = 1095
End
Begin VB.Label Label5
BackColor = &H00FFFFC0&
Caption = "周 期"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 600
TabIndex = 18
Top = 240
Width = 975
End
End
Begin VB.Frame Frame1
BackColor = &H00FFFFC0&
Caption = "Control Butten"
ForeColor = &H000000FF&
Height = 1935
Left = 240
TabIndex = 1
Top = 2880
Width = 3255
Begin VB.CommandButton Command4
Caption = "关闭"
Height = 735
Left = 1680
TabIndex = 5
Top = 1080
Width = 1455
End
Begin VB.CommandButton Command3
Caption = "清0"
Enabled = 0 'False
Height = 735
Left = 120
TabIndex = 4
Top = 1080
Width = 1455
End
Begin VB.CommandButton Command2
Caption = "暂停"
Height = 735
Left = 1680
MaskColor = &H000000C0&
TabIndex = 3
Top = 240
Width = 1455
End
Begin VB.CommandButton Command1
BackColor = &H000000FF&
Caption = "开始"
Height = 735
Left = 120
MaskColor = &H000000C0&
TabIndex = 2
Top = 240
Width = 1455
End
End
Begin MSCommLib.MSComm MSComm1
Left = 1440
Top = 0
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
InBufferSize = 16
InputLen = 14
RThreshold = 14
InputMode = 1
End
Begin VB.Label Label12
BackColor = &H00004000&
Caption = "0.25ms"
ForeColor = &H00FFFFFF&
Height = 150
Left = 8280
TabIndex = 28
Top = 1275
Width = 615
End
Begin VB.Line Line17
BorderColor = &H80000005&
X1 = 599
X2 = 609
Y1 = 3
Y2 = 19
End
Begin VB.Line Line16
BorderColor = &H80000005&
X1 = 599
X2 = 589
Y1 = 3
Y2 = 19
End
Begin VB.Label Label11
BackColor = &H00004000&
Caption = "0"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FFFFFF&
Height = 255
Left = 9000
TabIndex = 27
Top = 1440
Width = 135
End
Begin VB.Line Line15
BorderColor = &H80000005&
X1 = 49
X2 = 49
Y1 = 88
Y2 = 104
End
Begin VB.Line Line14
BorderColor = &H80000005&
X1 = 99
X2 = 99
Y1 = 88
Y2 = 104
End
Begin VB.Line Line13
BorderColor = &H80000005&
X1 = 149
X2 = 149
Y1 = 88
Y2 = 104
End
Begin VB.Line Line12
BorderColor = &H80000005&
X1 = 199
X2 = 199
Y1 = 88
Y2 = 104
End
Begin VB.Line Line11
BorderColor = &H80000005&
X1 = 249
X2 = 249
Y1 = 88
Y2 = 104
End
Begin VB.Line Line10
BorderColor = &H80000005&
X1 = 299
X2 = 299
Y1 = 88
Y2 = 104
End
Begin VB.Line Line9
BorderColor = &H80000005&
X1 = 349
X2 = 349
Y1 = 88
Y2 = 104
End
Begin VB.Line Line8
BorderColor = &H80000005&
X1 = 399
X2 = 399
Y1 = 88
Y2 = 104
End
Begin VB.Line Line7
BorderColor = &H80000005&
X1 = 449
X2 = 449
Y1 = 88
Y2 = 104
End
Begin VB.Line Line6
BorderColor = &H80000005&
X1 = 499
X2 = 499
Y1 = 88
Y2 = 104
End
Begin VB.Line Line5
BorderColor = &H80000005&
X1 = 549
X2 = 549
Y1 = 88
Y2 = 104
End
Begin VB.Line Line4
BorderColor = &H80000005&
X1 = 0
X2 = 16
Y1 = 96
Y2 = 106
End
Begin VB.Line Line3
BorderColor = &H80000005&
X1 = 0
X2 = 16
Y1 = 96
Y2 = 86
End
Begin VB.Line Line2
BorderColor = &H80000005&
X1 = 599
X2 = 599
Y1 = 3
Y2 = 184
End
Begin VB.Label Label4
BackColor = &H00004000&
Caption = "VB.Version 1.01.01_LinHG"
ForeColor = &H8000000E&
Height = 255
Left = 6600
TabIndex = 17
Top = 4980
Width = 2535
End
Begin VB.Label Label3
BackColor = &H00004000&
Caption = "Sys Freq 11.0592MHz"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FFFFFF&
Height = 255
Left = 2040
TabIndex = 16
Top = 4920
Width = 3135
End
Begin VB.Label Label2
BackColor = &H000000FF&
Caption = "Red---B"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 1080
TabIndex = 15
Top = 4920
Width = 855
End
Begin VB.Label Label1
BackColor = &H0080FF80&
Caption = "Green--A"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 120
TabIndex = 14
Top = 4920
Width = 855
End
Begin VB.Line Line1
BorderColor = &H80000014&
X1 = 0
X2 = 605
Y1 = 96
Y2 = 96
End
End
End
Attribute VB_Name = "For1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Dim CycleTimeAL0, CycleTimeAL1, CycleTimeAH0, CycleTimeAH1, CycleTimeA, HighTimeA, LowTimeA As Single
Dim FreCountAH, FreCountAL, FreCountA As Single
Dim CycleTimeBL0, CycleTimeBL1, CycleTimeBH0, CycleTimeBH1, CycleTimeB, HighTimeB, LowTimeB As Single
Dim FreCountBH, FreCountBL, FreCountB As Single
Dim HigheRatioA, HigheRatioB As Single
Dim AngerError, AngerErrorDisplay, ErrorTime, ErrorTimeL, ErrorTimeH As Single
Dim ClockError As Single
Dim FlagB As Boolean
Dim FlagA As Boolean
Dim FlagDisplay As Boolean
Dim FlagStart As Boolean
Dim FlagVerticalA As Boolean
Dim FlagVerticalB As Boolean
Dim indate() As Byte
Dim a, ah, at, b, bh, bt As Single
Dim fanxa, fanxb As Boolean
Dim ReadDateCount As Integer
Dim CleanAll As Single
Private Sub Command1_Click()
Timer1.Interval = 3
Timer2.Interval = 300
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
Command3.Enabled = False
End Sub
Private Sub Command2_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
Timer1.Interval = 0
Timer2.Interval = 0
Command3.Enabled = True
End Sub
Private Sub Command3_Click()
Timer1.Interval = 0
Timer2.Interval = 0
Picture1.Cls
Text1.Text = "0"
Text2.Text = "0"
Text3.Text = "0"
Text4.Text = "0"
Text5.Text = "0"
Text6.Text = "0"
Text7.Text = "0"
Text8.Text = "0"
Text9.Text = "0"
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
Picture1.Cls
End Sub
Private Sub Command4_Click()
Unload Me
End Sub
Private Sub Form_Load()
Text1.Text = "0"
Text2.Text = "0"
Text3.Text = "0"
Text4.Text = "0"
Text5.Text = "0"
Text6.Text = "0"
Text7.Text = "0"
Text8.Text = "0"
Text9.Text = "0"
Text10.Text = "0"
ClockError = 12 / 11.0592
End Sub
Private Sub MSComm1_OnComm()
FlagDisplay = False
FlagA = False
FlagB = False
Select Case MSComm1.CommEvent
Case comEvReceive
indate = MSComm1.Input
CycleTimeAL0 = indate(0)
CycleTimeAL1 = indate(1)
CycleTimeAH0 = indate(2)
CycleTimeAH1 = indate(3)
FreCountAL = indate(4)
FreCountAH = indate(5)
CycleTimeBL0 = indate(6)
CycleTimeBL1 = indate(7)
CycleTimeBH0 = indate(8)
CycleTimeBH1 = indate(9)
FreCountBL = indate(10)
FreCountBH = indate(11)
ErrorTimeL = indate(12)
ErrorTimeH = indate(13)
HighTimeA = (CycleTimeAH1 * 256 + CycleTimeAH0) * ClockError
LowTimeA = (CycleTimeAL1 * 256 + HighTimeA) * ClockError
CycleTimeA = CycleTimeAL0 + LowTimeA
HigheRatioA = HighTimeA / CycleTimeA
FreCountA = FreCountAH * 256 + FreCountAL
HighTimeB = (CycleTimeAH1 * 256 + CycleTimeAH0) * ClockError
LowTimeB = (CycleTimeBL1 * 256 + HighTimeB) * ClockError
CycleTimeB = CycleTimeBL0 + LowTimeB
HigheRatioB = HighTimeB / CycleTimeB
FreCountB = FreCountBH * 256 + FreCountBL
ErrorTime = (ErrorTimeL + ErrorTimeH * 256) * ClockError
AngerError = ErrorTime * 360 / ((CycleTimeA + CycleTimeB) / 2)
AngerErrorDisplay = Format(AngerError, "0.00")
If AngerErrorDisplay > 360 Then
AngerErrorDisplay = 360.001
AngerErrorDisplay = Format(AngerErrorDisplay, "0.00")
End If
FlagDisplay = True
FlagA = True
FlagB = True
End Select
End Sub
Private Sub Timer1_Timer()
a = a + 1
b = b + 1
FlagStart = False
BitBlt Picture1.hDC, 0, 0, 599, 102, Picture1.hDC, 2, 0, &HCC0020
If a = ah Then
fanxa = True
If FlagVerticalA = True Then
Picture1.Line (595, 20)-(595, 80), vbGreen
End If
End If
If a > at Then
a = 0
fanxa = False
If FlagVerticalA = True Then
Picture1.Line (595, 20)-(595, 80), vbGreen
End If
If FlagA = True Then
at = CInt(CycleTimeA / 10)
ah = CInt(HighTimeA / 10)
If at > 200 Then
at = 200
End If
If ah > 199 Then
ah = 0
End If
If ah < 2 Then
FlagVerticalA = False
Else:
FlagVerticalA = True
End If
Text8.Text = Format(at / 100, "00.00ms")
Text9.Text = Format(ah / 100, "00.00ms")
End If
End If
If fanxa = True Then
Picture1.Line (595, 80)-(597, 80), vbGreen
Else:
Picture1.Line (595, 20)-(597, 20), vbGreen
End If
BitBlt Picture1.hDC, 0, 110, 598, 66, Picture1.hDC, 2, 110, &HCC0020
If b = bh Then
fanxb = True
If FlagVerticalB = True Then
Picture1.Line (595, 115)-(595, 175), vbRed
End If
End If
If b > bt Then
b = 0
fanxb = False
If FlagVerticalB = True Then
Picture1.Line (595, 115)-(595, 175), vbRed
End If
If FlagB = True Then
bt = CInt(CycleTimeB / 10)
bh = CInt(HighTimeB / 10)
If bt > 200 Then
bt = 200
End If
If bh > 199 Then
bh = 0
End If
If bh < 2 Then
FlagVerticalB = False
Else
FlagVerticalB = True
End If
End If
End If
If fanxb = True Then
Picture1.Line (595, 175)-(597, 175), vbRed
ElseIf fanxb = False Then
Picture1.Line (595, 115)-(597, 115), vbRed
End If
FlagStart = True
End Sub
Private Sub Timer2_Timer()
If FlagDisplay = True Then
Text1.Text = Format(CycleTimeA, "00000") & "us"
Text2.Text = Format(HigheRatioA * 100, "0.00") & "%"
Text3.Text = "+" & Format(FreCountA, "00000")
Text4.Text = Format(CycleTimeB, "00000") & "us"
Text5.Text = Format(HigheRatioB * 100, "0.00") & "%"
Text6.Text = "+" & Format(FreCountB, "00000")
Text7.Text = AngerErrorDisplay & "°"
End If
Text10.Text = Format(Now, "general date")
End Sub
/////////////////////////////////////////////c51源程序/////////////////////////////////////
#include <reg52.h>
#include <intrins.h>
sbit PinA=P3^7;
sbit PinB=P3^6;
char DateAB[16]={0xaa,0xbb,0xcc,0xdd,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
//char DateB[10]={0xaa,0xbb,0xcc,0xdd,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char temp;
void Send_DateAB();
//void Send_DateB();
void delay(int i);
static char KAB;
//static char KB;
static unsigned char CountAL=0x00;
static unsigned char CountAH=0x00;
static unsigned char CountBL=0x00;
static unsigned char CountBH=0x00;
void send_char(unsigned char txd);
sbit P20=P2^0;
void send_char(unsigned char txd)
// 传送一个字符
{
SBUF = txd;
while(!TI); // 等特数据传送
TI = 0; // 清除数据传送标志
}
void Send_DateAB() //A相数据发送
{
for(KAB=0;KAB<14;KAB++)
{
SBUF = DateAB[KAB];
while(!TI); // 等特数据传送
TI = 0;
}
}
/*
void Send_DateB() //B相数据发送
{
for(KB=0;KB<7;KB++)
{
SBUF = DateB[KB];
while(!TI); // 等特数据传送
TI = 0;
}
}
*/
void intioA() interrupt 0 //A相中断计数
{
TR2=1;
TL2=0;
TH2=0;
CountAL++;
if(CountAL==0xff)
{CountAL=0;CountAH++;}
DateAB[4]=CountAL;
DateAB[5]=CountAH;
if(CountAH==0xff)
{CountAH=0;}
}
void intioB() interrupt 2 //B相中断计数
{
TR2=0;
DateAB[12]=TL2;
DateAB[13]=TH2;
CountBL++;
if(CountBL==0xff)
{CountBL=0;CountBH++;}
DateAB[10]=CountBL;
DateAB[11]=CountBH;
if(CountBH==0xff)
{CountBH=0;}
}
main()
{
TMOD = 0x21; // 定时器1工作于8位自动重载模式, 用于产生波特率
// 定时器0工作于16位模式,用于测量脉冲电平值
TH1 = 0xFD; // 波特率9600
TL1 = 0xFD;
SCON = 0x50; // 设定串行口工作方式
PCON = 0x00; // 波特率不倍增
TR1 = 1; // 启动定时器1
EX0=1; //打开外中断0,1
IT0=1; //负跳变产生中断
EX1=1;
IT1=1;
EA=1; //打开总中断
while(1)
{
if(RI) // 是否有数据到来
{
RI = 0;
temp = SBUF; // 暂存接收到的数据
if(temp=='a')
{P2=0;}
if(temp=='b')
{P2=1;}
send_char(temp); // 回传接收到的数据
}
//*************************读取A相的脉冲宽度 ***************///
while(!PinA); // 过滤脉冲
while(PinA);
TR0=1;
TH0=0;
TL0=0;
while(!PinA&&((TH0>0xfe)==0)) ;
TR0=0;
DateAB[0]=TL0;
DateAB[1]=TH0;
TR0=1;
TH0=0;
TL0=0;
while(PinA&&((TH0>0xfe)==0)) ;
TR0=0;
DateAB[2]=TL0;
DateAB[3]=TH0;
//*****************************************//
//*************************读取B相的脉冲宽度******************//
while(!PinB); // 过滤脉冲
while(PinB);
TR0=1;
TH0=0;
TL0=0;
while(!PinB&&((TH0>0xfe)==0)) ;
TR0=0;
DateAB[6]=TL0;
DateAB[7]=TH0;
TR0=1;
TH0=0;
TL0=0;
while(PinB&&((TH0>0xfe)==0)) ;
TR0=0;
DateAB[8]=TL0;
DateAB[9]=TH0;
//********************************************//
Send_DateAB();
// Send_DateB();
// Send_Test();
}
}
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|