Xin mọi người giúp em giải thích đoạn code VB này em cái.E mới tìm hiểu nên khônDe10_TocDoCOM.docg hiểu đoạn code phía cuối.e xin cảm ơn.
// Chuong trinh do toc do motoDC
// Giao tiep voi may tinh qua cong COM
// Su dung thach anh 11.0592Mhz, AT89S52
#include <at89x52.h>
unsigned int dem,tocdo;
unsigned int et0,gtocdo;
void Ngat_EX0 (void) interrupt 0
{
dem++;
}
void Ngat_Timer0 (void) interrupt 1
{
// (1000000 * 11.0592)/12/100 = 9216 => 1Giay
et0++;
if(et0 > 9216) // 1 giay
{
TR0 = 0;
et0 = 0;
tocdo = dem;
dem = 0;
TR0 = 1;
}
}
void Out(unsigned char temp)
{
SBUF=temp;
while(!TI);
TI = 0;
}
void Send_to_pc(unsigned int adata)
{
Out(0xDA);
Out(0xAD);
Out((unsigned char)(adata>>8));
Out((unsigned char)(adata));
Out(0XAB);
}
void main ()
{
SCON = 0x50; // cai dat cong COM 8bit,n,1
REN = TI = 1;
TMOD = 0x22; // Timer1 che do 2
TH1 = -3; // Toc do baud 9600
TR1 = 1; // Cho phep Timer1
TR0 = 1;
TH0 = -100;
ET0 = 1;
EA = 1; // Cho phep ngat toan bo
EX0 = 1;
IT0 = 1; // CHo phep ngat suon am
while(1)
{
gtocdo = tocdo*60;
Send_to_pc(gtocdo); // Gui du lieu len may tinh
}
}
Private DataFre As Integer ' Khai bao bien
Private Buff(6) As Integer
Private Index, TocDo As Integer
Dim X, Y
Private Sub Form_Load() ' Chuong trinh chinh
HienThi.Enabled = False
TimerDoThi.Enabled = False
dothi.Scale (0, 0)-(150, 5000)
dothi.Line -(0, 5000 - TocDo), vbBlue
End Sub
Private Sub MSComm1_OnComm() ' Chuong trinh nhan du lieu tren PC
Dim DataReceive As String
Dim DataCom As Long
DataReceive = MSComm1.Input
If (MSComm1.CommEvent = comEvReceive) And DataReceive <> "" Then
Buff(Index) = Asc(DataReceive)
Debug.Print "nhan duoc:" & Buff(Index)
If Index > 0 Then
If (Buff(Index - 1) = 218) And (Buff(Index) = 173) Then
Buff(0) = 218
Buff(1) = 173
Index = 1
End If
End If
Index = Index + 1
If (Index > 5) Then
If (Buff(4) = &HAB) Then
HienThi.Text = (Buff(2) * 256) + Buff(3)
End If
Index = 0
End If
End If
End Sub
Private Sub Start_Click()
If MSComm1.PortOpen = False Then
HienThi.Enabled = True
TimerDoThi.Enabled = True
With MSComm1
.RThreshold = 1
.InputLen = 1
.Settings = "9600,N,8,1"
.DTREnable = False
.CommPort = 1
.PortOpen = True
End With
Hthi.Caption = "Nhan Stop de dung hien thi"
End If
End Sub
Private Sub Stop_Click()
If MSComm1.PortOpen = True Then
HienThi.Enabled = False
TimerDoThi.Enabled = False
MSComm1.PortOpen = False
Hthi.Caption = "Nhan Start de tiep tuc"
End If
End Sub
Private Sub Exit_Click()
End
End Sub
Private Sub TimerDoThi_Timer()
TocDo = Val(HienThi)
dothi.Line -(X, 5000 - TocDo), vbRed
If X = 150 Then
dothi.Cls ' Xoa do thi
Y = TocDo – 5000 : nghĩa là gi
X = 0
dothi.Line -(X, 50 - DataAmpe), vbRed ; nghĩa là gi (không hiểu người viết)
Ls1.Caption = Ls1.Caption + 15 ; nghĩa là gi
Ls2.Caption = Ls2.Caption + 15; nghĩa là gi
Ls3.Caption = Ls3.Caption + 15
Ls4.Caption = Ls4.Caption + 15
Ls5.Caption = Ls5.Caption + 15
Ls6.Caption = Ls6.Caption + 15
Ls7.Caption = Ls7.Caption + 15
Ls8.Caption = Ls8.Caption + 15
Ls9.Caption = Ls9.Caption + 15
Ls10.Caption = Ls10.Caption + 15
Ls11.Caption = Ls11.Caption + 15
Ls12.Caption = Ls12.Caption + 15
Ls13.Caption = Ls13.Caption + 15
Ls14.Caption = Ls14.Caption + 15
Ls15.Caption = Ls15.Caption + 15
End If
X = X + 1
End Sub
// Chuong trinh do toc do motoDC
// Giao tiep voi may tinh qua cong COM
// Su dung thach anh 11.0592Mhz, AT89S52
#include <at89x52.h>
unsigned int dem,tocdo;
unsigned int et0,gtocdo;
void Ngat_EX0 (void) interrupt 0
{
dem++;
}
void Ngat_Timer0 (void) interrupt 1
{
// (1000000 * 11.0592)/12/100 = 9216 => 1Giay
et0++;
if(et0 > 9216) // 1 giay
{
TR0 = 0;
et0 = 0;
tocdo = dem;
dem = 0;
TR0 = 1;
}
}
void Out(unsigned char temp)
{
SBUF=temp;
while(!TI);
TI = 0;
}
void Send_to_pc(unsigned int adata)
{
Out(0xDA);
Out(0xAD);
Out((unsigned char)(adata>>8));
Out((unsigned char)(adata));
Out(0XAB);
}
void main ()
{
SCON = 0x50; // cai dat cong COM 8bit,n,1
REN = TI = 1;
TMOD = 0x22; // Timer1 che do 2
TH1 = -3; // Toc do baud 9600
TR1 = 1; // Cho phep Timer1
TR0 = 1;
TH0 = -100;
ET0 = 1;
EA = 1; // Cho phep ngat toan bo
EX0 = 1;
IT0 = 1; // CHo phep ngat suon am
while(1)
{
gtocdo = tocdo*60;
Send_to_pc(gtocdo); // Gui du lieu len may tinh
}
}
Private DataFre As Integer ' Khai bao bien
Private Buff(6) As Integer
Private Index, TocDo As Integer
Dim X, Y
Private Sub Form_Load() ' Chuong trinh chinh
HienThi.Enabled = False
TimerDoThi.Enabled = False
dothi.Scale (0, 0)-(150, 5000)
dothi.Line -(0, 5000 - TocDo), vbBlue
End Sub
Private Sub MSComm1_OnComm() ' Chuong trinh nhan du lieu tren PC
Dim DataReceive As String
Dim DataCom As Long
DataReceive = MSComm1.Input
If (MSComm1.CommEvent = comEvReceive) And DataReceive <> "" Then
Buff(Index) = Asc(DataReceive)
Debug.Print "nhan duoc:" & Buff(Index)
If Index > 0 Then
If (Buff(Index - 1) = 218) And (Buff(Index) = 173) Then
Buff(0) = 218
Buff(1) = 173
Index = 1
End If
End If
Index = Index + 1
If (Index > 5) Then
If (Buff(4) = &HAB) Then
HienThi.Text = (Buff(2) * 256) + Buff(3)
End If
Index = 0
End If
End If
End Sub
Private Sub Start_Click()
If MSComm1.PortOpen = False Then
HienThi.Enabled = True
TimerDoThi.Enabled = True
With MSComm1
.RThreshold = 1
.InputLen = 1
.Settings = "9600,N,8,1"
.DTREnable = False
.CommPort = 1
.PortOpen = True
End With
Hthi.Caption = "Nhan Stop de dung hien thi"
End If
End Sub
Private Sub Stop_Click()
If MSComm1.PortOpen = True Then
HienThi.Enabled = False
TimerDoThi.Enabled = False
MSComm1.PortOpen = False
Hthi.Caption = "Nhan Start de tiep tuc"
End If
End Sub
Private Sub Exit_Click()
End
End Sub
Private Sub TimerDoThi_Timer()
TocDo = Val(HienThi)
dothi.Line -(X, 5000 - TocDo), vbRed
If X = 150 Then
dothi.Cls ' Xoa do thi
Y = TocDo – 5000 : nghĩa là gi
X = 0
dothi.Line -(X, 50 - DataAmpe), vbRed ; nghĩa là gi (không hiểu người viết)
Ls1.Caption = Ls1.Caption + 15 ; nghĩa là gi
Ls2.Caption = Ls2.Caption + 15; nghĩa là gi
Ls3.Caption = Ls3.Caption + 15
Ls4.Caption = Ls4.Caption + 15
Ls5.Caption = Ls5.Caption + 15
Ls6.Caption = Ls6.Caption + 15
Ls7.Caption = Ls7.Caption + 15
Ls8.Caption = Ls8.Caption + 15
Ls9.Caption = Ls9.Caption + 15
Ls10.Caption = Ls10.Caption + 15
Ls11.Caption = Ls11.Caption + 15
Ls12.Caption = Ls12.Caption + 15
Ls13.Caption = Ls13.Caption + 15
Ls14.Caption = Ls14.Caption + 15
Ls15.Caption = Ls15.Caption + 15
End If
X = X + 1
End Sub