The speed measurement system
collects speed data through the microcontroller, sends it to the PC through the USB port, and generates a curve graph.
MCU program:
//---------------------Serial port initialization----------------------------
void initSerial()
{
TMOD=0x20; //Set timer 1 to mode 2
TH1=0xfd;
TL1=0xfd; //Set baud rate to 9600
SCON = 0x50; //Set serial port working mode
PCON &= 0x00; //Baud rate does not double
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1;
}
//---------------------------Sending program---------------------------------------------
void send_char(void)
//Send 16-bit speed data, low bit first
{
unsigned i=0;
while (i < 6)
{
SBUF =zhuansu_char[i];
while (!TI); // Waiting for special data to be transmitted
TI = 0; // Clear data transmission flag
i++;
}
}
//----------------------Main program--------------------------
void main()
{
uchar receive;
lcdrs=0;
init(); //Interrupt initialization
initSerial(); //Communication initialization
lcdinit(); //LCD display initialization
write_project_title();
while(1)
{
write_project_data(zhuansu);
if (RI) // Is there any data coming?
{
RI = 0;
receive = SBUF;
if (receive == 's') // Start collecting speed?
{
send_char(); // Send the collected speed
}
}
}
}
The VB interface is as follows:
The result after debugging is:
VB program is as follows:
'Define form-level variables
' Used in display, drawing and other processes
Dim datazhuansu(200) As Single ' Used to store speed sampling value
Dim num As Integer ' Used to store the number of sampling values
'-----------------------------------------------
' Speed acquisition
'-----------------------------------------------
Private Sub CmdStart_Click() ' Start acquisition
Timer1.Enabled = True
End Sub
'-----------------------------------------------
' Stop acquisition
'-----------------------------------------------
Private Sub CmdStop_Click() ' Stop acquisition
Timer1.Enabled = False
End Sub
'Serial port initialization
' Add the following code to the Load event of the form to initialize the serial port:
'-----------------------------------------------
' Load the form
'-----------------------------------------------
Private Sub Form_Load()
MSComm1.CommPort = 1 ' Set the serial port
MSComm1.InputMode = comInputModeBinary ' Binary input modeMSComm1.RThreshold
= 1 ' Receive 1 character and trigger the OnComm eventMSComm1.SThreshold
= 1 ' Send 1 character and trigger the OnComm eventMSComm1.Settings
= "9600,n,8,2" ' Set the baud rateMSComm1.PortOpen
= True ' Open the serial portCall
ScaleSys ' Draw the coordinate systemCmdStop.Enabled
= False
End Sub
'-----------------------------------------------
' Receive trigger events
'-----------------------------------------------
' Get the speed measurement value and display it
' Each time a command is sent, the following event is triggered and the data string is returnedPrivate
Sub MSComm1_OnComm()
Dim Inbyte() As Byte ' Temporarily store received dataDim
buffer As String ' Speed data bufferDim
datasu2a, datasu2b As String ' Two-byte speed dataDim
datasu2 As String ' Hexadecimal speed data
If num > 199 Then 'Judge the number of received data
Call renew 'Receiving completed
End If
'Read the data string returned by the instrument
Select Case MSComm1.CommEvent
Case comEvReceive
Inbyte = MSComm1.Input 'Receive speed data
For i = LBound(Inbyte) To UBound(Inbyte) 'Put the received data in hexadecimal format into the buffer
buffer = buffer + Hex(Inbyte(i)) + Chr(32)
Next i
End Select
'Get decimal measurement data
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
'Get hexadecimal measurement dataIf
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
'Display the measured speed valueIf
datazhuansu(num) <> 0 Then
zhuansuText = datasu2
Call draw ' Call the curve drawing process
End If
End Sub
'-----------------------------------------------
' Speed curve drawing
'-----------------------------------------------
' Draw the real-time speed change curve
Private Sub draw()
Picture1.DrawWidth = 2 ' Set the line width
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) ' Draw the speed curve
Next i
End Sub
'-----------------------------------------------
' Refresh the drawing area
'-----------------------------------------------
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
'-----------------------------------------------
' Send collection flag periodically
'-----------------------------------------------
' Send a read data command string to the instrument every x ms
' Each instrument has an instrument number, and the PC uses the instrument number to identify multiple instruments on the Internet
' The instrument number (i.e. address code) in the program must be consistent with the instrument setting value, otherwise no data can be returned.
Private Sub Timer1_Timer()
MSComm1.Output = "s" ' Send start flag
End Sub
'-----------------------------------------------
' Unload the form
'-----------------------------------------------
Private Sub Cmdquit_Click()
Unload Me ' Unload the form
End Sub
'-----------------------------------------------
' Establish image coordinate system
'-----------------------------------------------
Sub ScaleSys() ' Coordinate systemPicture1.AutoRedraw
= True ' Automatically redraw validPicture1.DrawWidth
= 1 ' Line width 1
pixelPicture1.ScaleMode = vbPixels ' Pixels as unitPicture1.Scale
(0, 125)-(200, -50) ' Coordinate systemPicture1.DrawStyle
= vbDot ' Dot line
' Horizontal
coordinatePicture1.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, 13 0, 130)
Picture1.Line (0, 100)-(200, 100), RGB(130, 130, 130)
Picture1.Line (0, -25)-(200, -25), RGB(130, 130, 130)
' Vertical coordinate
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, 13 0, 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(13 0,130, 130)
End Sub
Previous article:About the solution process of Fatal error: Could not find device
Next article:Understanding the RTX51 Tiny real-time kernel
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- [Repost] What are the grounding techniques for electronic design?
- TI microcontroller I8669/I44445 (MCU) MSP430 combines low power consumption with high performance
- The role of magnetic beads and their working principle
- I'm new to PCB, need help, thanks
- Is this true? Most people who buy Apple phones are "invisible poor people"
- As shown in the figure, which type of capacitor filtering wiring is better?
- Power supply tips you must know: A small negligence can ruin EMI performance!
- What is the inverted state of the transistor and what is its function!
- IAP issue when porting from Qinheng CH32F103 to CH32V103
- Playing with Bluetooth chips on a perforated board, where is the antenna? Come and see Qinheng's 10-pin CH9141K