📄 misccalc.bas
字号:
Attribute VB_Name = "MiscellaneousCalculations"
Option Explicit
'Various calculations related to serial links.
Public Function fncFindBiasResistor _
(Termination As Single, _
NumberOfUnitLoads As Single, _
SupplyVoltage As Single, _
BiasVoltage As Single) _
As Integer
'Calculates the value of fail-safe bias resistors for an RS-485 link.
Dim Count As Integer
Dim TotalTermination As Single
Dim Current As Single
'First find the parallel resistance of the termination resistors
'and the receivers.
'Assume two Terminations in parallel.
TotalTermination = Termination ^ 2 / (Termination * 2)
'One unit load is 12,000 ohms.
For Count = 1 To NumberOfUnitLoads
TotalTermination = _
TotalTermination * 12000 / (TotalTermination + 12000)
Next Count
'Find the current in the bias network.
Current = BiasVoltage / TotalTermination
'Each bias resistor equals
'input B's open-circuit voltage divided by the current.
fncFindBiasResistor = _
CInt(((SupplyVoltage / 2) - (BiasVoltage / 2)) / Current)
End Function
Public Function fncCalculateBaudMode _
(BitRate As Long, _
Parity As Boolean, _
Inverted As Boolean, _
OpenBaudMode As Boolean) _
As Long
'Returns the Basic Stamp抯 baudmode parameter
'for the requested settings.
Dim BitRateValue As Long
Dim ParityValue As Long
Dim InvertedValue As Long
Dim OpenBaudModeValue As Long
BitRateValue = 1000000 \ BitRate - 20
If Parity = True Then
ParityValue = 8192
Else
ParityValue = 0
End If
If Inverted = True Then
InvertedValue = 16384
Else
InvertedValue = 0
End If
If OpenBaudMode = True Then
OpenBaudModeValue = 32768
Else
OpenBaudModeValue = 0
End If
fncCalculateBaudMode = _
(BitRateValue + ParityValue + InvertedValue + OpenBaudModeValue)
End Function
Public Function fncIsTransmissionLine _
(PropagationRate_PicosecsPerIn As Single, _
CableLength_Feet As Single, _
DriversRiseTime_Nanosecs As Single) _
As Boolean
'Calculates whether a particular line will behave
'as a transmission line.
If (PropagationRate_PicosecsPerIn * CableLength_Feet * 12) > _
(DriversRiseTime_Nanosecs * 1000 / 4) Then
fncIsTransmissionLine = True
Else
fncIsTransmissionLine = False
End If
End Function
Public Function fncMaximumLengthOfShortLineInFeet _
(PropagationRate_PicosecsPerIn As Single, _
DriversRiseTime_Nanosecs As Single) _
As Single
'Returns the maximum cable length
'that will behave as a short line.
fncMaximumLengthOfShortLineInFeet = _
DriversRiseTime_Nanosecs * 1000 / _
(PropagationRate_PicosecsPerIn * 48)
End Function
Public Function fncTotalInputResistance(UnitLoads As Single) As Single
'Calculates the parallel input resistance
'of a number of RS-485 unit loads.
Dim InputResistance As Single
'Begin with one unit load
InputResistance = 12000
For UnitLoads = 2 To UnitLoads
InputResistance = _
(12000 * InputResistance) / _
(12000 + InputResistance)
Next UnitLoads
fncTotalInputResistance = InputResistance
End Function
Public Sub FindDivisorLatchValues(BitRate As Long)
'Calculates UART divisor latch values for a bit rate.
Dim LowByte As Long
Dim HighByte As Long
Dim Crystal As Long
Crystal = 1843200
HighByte = Crystal \ (&H1000 * BitRate)
LowByte = (Crystal \ (BitRate * &H10)) - (HighByte * &H100)
Debug.Print "Bitrate: ", BitRate
Debug.Print "high byte: ", Hex$(HighByte)
Debug.Print "low byte: ", Hex$(LowByte)
Debug.Print
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -