VB host computer and single chip microcomputer

Publisher:美好回忆Latest update time:2014-12-26 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

Reference address:VB host computer and single chip microcomputer

Previous article:About the solution process of Fatal error: Could not find device
Next article:Understanding the RTX51 Tiny real-time kernel

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号