Measuring analogue signal on PC without the need to program a microcontroller. The MCP3201 is a 12 bit ADC, the USB to Serial module drives the ADC by bit banging the Hardware Controls (Flow Controls).
The USB to Serial converter has to have DTR, CTS, and RTS, I used this one from Ebay: https://www.ebay.co.uk/itm/CP2102-USB-2-0-to-TTL-UART-Module-6Pin-Serial-Converter-STC-Replace-FT232-Module/381374541932?ssPageName=STRK:MEBIDX:IT&_trksid=p2060353.m2749.l2649
The USB to Serial converter has to have DTR, CTS, and RTS, I used this one from Ebay: https://www.ebay.co.uk/itm/CP2102-USB-2-0-to-TTL-UART-Module-6Pin-Serial-Converter-STC-Replace-FT232-Module/381374541932?ssPageName=STRK:MEBIDX:IT&_trksid=p2060353.m2749.l2649
B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private sp As Serial
Private astream As AsyncStreams
Private tmr1 As Timer
Private cmb1 As ComboBox
Private Label1 As Label
Private angle As Double = 40
Private Canvas1 As Canvas
Private Pane1 As Pane
Private bitim=11 As Int
Private dec As Int
Private lblVolt As Label
Private Button1 As Button
Private cmbSmpl As ComboBox
Private cmbBit As ComboBox
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
sp.Initialize("")
tmr1.Initialize("tmr1",1000)
tmr1.Enabled=False
cmbSmpl.Items.AddAll(Array As String("2000", "600", "200"))
cmbBit.Items.AddAll(Array As String("12", "10", "8"))
cmb1.Items.AddAll(sp.ListPorts)
Canvas1.DrawLine(100,100,100 - 80 * Cos(angle * 3.14 / 180),100 - 80 * Sin(angle * 3.14 / 180),fx.Colors.Black, 1dip)
drawVM
End Sub
Sub cmb1_SelectedIndexChanged(Index As Int, Value As Object)
Try
sp.Open(cmb1.Value)
sp.SetParams(1200,8,1,0)
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
Label1.Text = "Port is Open"
tmr1.Enabled=True
Catch
Label1.Text = "Port is busy"
Log("Port error")
End Try
End Sub
Sub cmbBit_SelectedIndexChanged(Index As Int, Value As Object)
bitim=cmbBit.value-1
End Sub
Sub cmbSmpl_SelectedIndexChanged(Index As Int, Value As Object)
tmr1.Interval=cmbSmpl.Value
End Sub
Sub tmr1_Tick
Dim Bit1, word=0, bitcount As Int
Dim t As Double
bitcount=bitim
dec=Power(2,(bitcount+1))
'reading the bits
sp.RTS=True 'CS low Enables ADC
Sleep(1)
For i = 0 To 2 'First 3 ADC Clocks are a flag for start readings.
sp.DTR=False
clock
sp.DTR=True
clock
Next
For i=0 To 20
sp.DTR=False 'ADC Clock low
clock
Bit1 = 0
If sp.CTS=True Then
Bit1 = Power(2, bitcount)
End If
word = word + Bit1
sp.DTR=True 'ADC Clock high.
clock
bitcount = bitcount - 1
If bitcount = 0 Then
Exit
End If
Next
sp.RTS=False 'CS high
'setting the meters
Canvas1.DrawRect(10,10,180,180,fx.Colors.White,True,0)
drawVM
t = word / dec * 5 't is the digital meter reading.
lblVolt.text=NumberFormat(t,1,3) & " V"
angle = (40 + t * 20) * 3.1415 / 180 'calculate the angle of the analogue meter needle.
Canvas1.DrawLine(100,100,100 - 80 * Cos(angle),100 - 80 * Sin(angle),fx.Colors.Black, 1dip)
End Sub
Sub clock
Dim i As Int
For i=0 To 10000
Next
End Sub
Sub AStream_NewData (Buffer() As Byte)
End Sub
Sub drawVM
For i=0 To 5
Canvas1.DrawLine(100-80*Cos((i*20+40)*3.14/180),100-80*Sin((i*20+40)*3.14/180),100-70*Cos((i*20+40)*3.14/180) _
,100-70*Sin((i*20+40)*3.14/180),fx.Colors.Black, 1dip)
Next
End Sub
Sub Button1_Click
cmb1.Items.Clear
cmb1.Items.AddAll(sp.ListPorts)
End Sub
Attachments
Last edited: