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

 

 

 

 

         

Shown below is a simple VB.net code for 4-bit Digital-to-Analog 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 analog output voltage from a digital input code for a given full scale output voltage. For example, in the screenshot of the form below, the analog output voltage is computed as 20.8 V, since the LSB is equal to 24V/15 steps or 1.6 V, and since the input code 1101 is equivalent to 13 steps. Thus, the output voltage equals 1.6V per step multiplied by 13 steps, or 20.8 V.

 

  

  

  

Public Class frmMain Inherits System.Windows.Forms.Form
          

Function baseN2dec(ByVal value, ByVal inBase) As String
'Converts any base to base 10
     Dim strValue, i, x, y
     strValue = StrReverse(CStr(UCase(value)))
     For i = 0 To Len(strValue) - 1
          x = Mid(strValue, i + 1, 1)
          If Not IsNumeric(x) Then
               y = y + ((Asc(x) - 65) + 10) * (inBase ^ i)
          Else
               y = y + ((inBase ^ i) * CInt(x))
          End If
     Next
     baseN2dec = y
End Function

          

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Private Sub cmdCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCompute.Click
     txtOV.Text = (Val(txtFSO.Text) / 15) * baseN2dec(Val(txtIC.Text), 2)
End Sub

  
End Class