Simple VB.net Code for 4-Bit Analog-to-Digital Conversion

 

 

 

 

         

Shown below is a simple VB.net code for 4-bit Analog-to-Digital conversion, along with a screenshot of the form used with it, for reference by students learning to program in VB.net in the context of electronics engineering. This VB.net program computes the expected digital output code from an analog input value and a given full scale input voltage. For example, in the screenshot of the form below, the digital output code is 1100 (which corresponds to 12 steps), since the LSB is equal to 24V/15 steps or 1.6 V, and the input voltage of 19 V is closest to 12 x 1.6V = 19.2 V.

 

      

  

Imports System.Math

Public Class frmMain Inherits System.Windows.Forms.Form

  

Function dec2baseN(ByVal value, ByVal outBase) As String
'Converts base 10 to any base
     Dim q 'quotient
     Dim r 'remainder
     Dim m 'denominator
     Dim y 'converted value
     m = outBase
     q = value
     Do
          r = q Mod m
          q = Int(q / m)
          If r >= 10 Then
               r = Chr(65 + (r - 10))
          End If
          y = y & CStr(r)
     Loop Until q = 0
     dec2baseN = StrReverse(y)
End Function

Private Sub cmdCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCompute.Click
     txtOC.Text = dec2baseN(Round(Val(txtIV.Text) / (Val(txtFSI.Text) / 15), 0), 2)
     Do Until txtOC.Text.Length = 4
          txtOC.Text = "0" & txtOC.Text
     Loop
End Sub

            

End Class