|
楼主 |
发表于 2013-1-25 17:40:13
|
显示全部楼层
本版程序(陆续编写中,所以一些注释还没写,我会慢慢加上)
'///////////////////////////////////////////////////////////////////////////////////////////////////
'Voltage Generator
'By Lilith Washu
'///////////////////////////////////////////////////////////////////////////////////////////////////
$Device= m32 ' 使用的单片机名称
$Stack = 32
$Clock = 11.0592 ' 使用的单片机主频
$ShiftIn Data = PORTB.4, Clock = PORTB.3, Msb
$ShiftOut Data = PORTB.1, Clock = PORTB.2, Msb
$Baud = 19200,n,8,1
$1Wire = PORTD.6
$LeadChar="0", Format(2,6)
'************** Config the IO **************
' the Display Screen IO Connect
$Def IO_SCR_RST = PORTC.7
$Def IO_SCR_CSA = PORTC.6
$Def IO_SCR_CSB = PORTC.5
$Def IO_SCR_DoI = PORTC.4
$Def IO_SCR_RoW = PORTC.3
$Def IO_SCR_STB = PORTC.2
$Def IO_SCR_DAT = PORTA
' the Analog Board IO Connect
$Def IO_REG_STB = PORTB.0
$Def IO_REG_DAT = PORTB.1
$Def IO_REG_CLK = PORTB.2
$Def IO_ADC_CLK = PORTB.3
$Def IO_ADC_DAT = PINB.4
$Def IO_KEY_STB = PORTB.5
$Def IO_KEY_DAT = PINB.6
$Def IO_KEY_CLK = PORTB.7
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Constant
'///////////////////////////////////////////////////////////////////////////////////////////////////
Const fMasterReference = 7.042665e+00'7.080451E+00 'LM399 Voltage Value
Const fDACReference = 5.000000E+00 'DAC Voltage Reference Value
Const fDACenter = 2.500000E+00 'Slave DAC Default Output Voltage Value
Const fDACGain = 2.500000E+06 'DAC Output buff Gain
Const fMResistance = 2.49E+02 'DAC Voltgae Compound Resistance, the Master
Const fSResistance = 1E+06 'DAC Voltgae Compound Resistance, the Slave
Const Quadratic = -3.6031e+00 'No-line Error Adjuest Quadratic Coefficient 二次项系数
Const Linear = 4.5495e+01 'No-line Error Adjuest Linear Coefficient 一次项系数
Const Constant = 5.6295e+01 'No-line Error Adjuest Offset Coefficient 常数
Const fDacal = 1.005258e+00'1.012857e+00 'DAC Fullscale Error Adjuest Vale
Const Synthesis = &b00000001 ' Analog Switch = Synthesis Voltage Output | DG412 Pin
Const Reference = &b00000010 ' Analog Switch = Internal Reference(LM399) | DG412 Pin
Const ExternaIN = &b00000100 ' Analog Switch = Externa Voltage Input | DG412 Pin
Const GndOffset = &b00001000 ' Analog Switch = Auto Zero | DG412 Pin
'///////////////////////////////////////////////////////////////////////////////////////////////////
' String Variables
'///////////////////////////////////////////////////////////////////////////////////////////////////
Dim Ft1 As Flash Byte ' 软字体码表
Dim Ft2 As Flash Byte ' 软字体码表
Dim Charat As String *33' 要显示的字符缓冲区
Dim tTxt As String *15
Dim tm As String *7
Dim tCmd As String *4
Dim iCursors(8) As Byte
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Gen Variables
'///////////////////////////////////////////////////////////////////////////////////////////////////
Dim i As Byte
Dim bUartset As Byte
Dim bUartlk As Byte
Dim bAdjuest As Byte
Dim bDigitaLoop As Byte
Dim bRun As Byte
Dim bTrigcompleted As Byte
Dim bRefsource As Byte
Dim iErrlimit As Byte
Dim bSregister As Byte
Dim iSmlen As Byte
Dim iSmsetp As Byte
Dim iBusystp As Byte
Dim iTrd As Byte
Dim iDraw As Word
Dim CurrentChannel As Byte
Dim bNextchannel As Byte
Dim itm As Byte
Dim iDa As Byte
Dim iDb As Byte
Dim iDc As Byte
Dim iDd As Byte
Dim iTa As Byte
Dim iTb As Byte
Dim iTc As Byte
Dim iTd As Byte
Dim bADCResult(3) As Byte
Dim bREGa(64) As Byte
Dim bREGb(64) As Byte
Dim bREGc(64) As Byte
Dim bREGd(64) As Byte
Dim dTempdatum(1) As Byte
Dim dTempresult As Word
Dim iError As Integer
Dim wError As Word
Dim IsTempresultSense As Byte
Dim iTestcode As Byte
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Float Variables
'///////////////////////////////////////////////////////////////////////////////////////////////////
Dim fTrn As Float
Dim fOut As Float
Dim fGndOffset As Float
Dim fReference As Float
Dim fSynthesis As Float
Dim fCalsacles As Float
Dim fCalfSynth As Float
Dim fSetting As Float
Dim fVoltage As Float
Dim fError As Float
Dim fVset As Float
Dim fVtrn As Float
Dim fTemperature As Float
Dim Vseth As Word
Dim Vsetl As Word
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Screen Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
Declare Function WriteKS(Code As Byte, Channel As Byte) As Byte ' 向显示屏写数据的函数
Declare Function SetKS(Code As Byte, Channel As Byte) As Byte ' 向显示屏写指令的函数
Declare Function InitKS() As Byte ' 初始化显示屏
Declare Function ResetKS() As Byte ' 显示屏上电函数,可用硬件电路代替
Declare Function ClsKS(GRAM As Byte) As Byte ' 清屏函数
Declare Function DrawPix(x As Byte, y As Byte) As Byte ' 在显示屏上画一个点
Declare Function DrawPage(x As Byte, Page As Byte, Code As Byte) As Byte ' 在显示屏上画一个页
Declare Function DrawChr(x As Byte, Page As Byte) As Byte ' 显示一行标准 ASCII 字符
Declare Function DrawChrLarge(x As Byte, Page As Byte) As Byte ' 显示一行标准 ASCII 字符
Declare Function DrawCursors(x As Byte, Page As Byte, Length As Byte) As Byte
'///////////////////////////////////////////////////////////////////////////////////////////////////
' IO Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
Declare Function WriteSReg(bDat As Byte) As Byte
Declare Function SetChannel(Channel As Byte) As Byte
Declare Function ReadADC() As Byte
Declare Function ReadKEY() As Byte
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Mathematical Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
Declare Function SetVoltage(fVoltage As Float) As Byte
Declare Function Getinlerr(fVoltage As Float) As Float
Declare Function ReadADConvert(NextChannel As Byte) As Long
Declare Function FixADCResult() As Byte
Declare Function WriteResultREG(bSetp As Byte, bArea As Byte) As Byte
Declare Function MoveResultREG(bSetp As Byte, bArea As Byte) As Byte
Declare Function ReadADCResult(bChannel As Byte) As Float
Declare Sub StatusDisplay()
Declare Function UpdateTempresultInfo() As Byte
Declare Sub Trigcompleted()
Declare Function ADCheck(bNextChannel As Byte) As Integer
'///////////////////////////////////////////////////////////////////////////////////////////////////
' UART Interrupt
'///////////////////////////////////////////////////////////////////////////////////////////////////
Declare Interrupt Urxc()
Enable Interrupts
Enable Urxc
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Config the MCU IO Register
'///////////////////////////////////////////////////////////////////////////////////////////////////
DDRA = &b11111111 'Display Screen Data Bus
'Parallel 8bit Data bus
DDRC = &b11111111 'Display Screen Control Bus
'PC0: (Saved)
'PC1: (Saved)
'PC2: Screen Strobe
'PC3: Screen Write or Read
'PC4: Screen Data or Instructions
'PC5: Screen Area Enable LO Bit
'PC6: Screen Area Enable HI Bit
'PC7: Screen Reset
DDRB = &b10101111 'Analog Board Connect
'REG STB <--- PB0
'REG DAT <--- PB1
'REG CLK <--- PB2
'ADC CLK <--- PB3
'ADC DAT ---> PB4
'KEY STB <--- PB5
'KEY DAT ---> PB6
'KEY CLK <--- PB7
DDRD = &b11111111 'INT, UART and PWM Control
'PD0: UART RX
'PD1: UART TX
'PD2: INT0 TRIG(Key)
'PD3: INT1 TRIG(Saved)
'PD4: PWM OC1B(Saved)
'PD5: PWM OC1A(Saved)
'PD6: 1Wire BUS(DS18b20)
'PD7: PWM OC2(Saved)
PORTA = &b00000000
PORTB = &b00000000
PORTC = &b00000000
PORTD = &b00000000
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Program Initialization
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Analog Board Initialization, Open all Analog Switch
bSregister = &b00000000
WriteSReg(bSregister)
' DAC Initialization to 0V Output
SetVoltage(0)
InitKS() ' 初始化显示屏
ClsKS(0) ' 清屏
' LCD Test and Init Display Charat
Charat = "Voltage Generator" : DrawChrLarge(6,1)
Charat = "Version 1.0" : DrawChr(32,3)
Charat = "Build 173" : DrawChr(44,4)
Charat = "By Lilith Washu" : DrawChr(38,6)
Wait 2 'Wait the LM399 Initialization
ClsKS(0)
Charat = "Initialization" : DrawChr(1,0)
'Initialization Var?
Charat = "Initialization OK" : DrawChr(1,0)
'///////////////////////////////////////////////////////////////////////////////////////////////////
' EEPROM Initialization
'///////////////////////////////////////////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Self Test
'///////////////////////////////////////////////////////////////////////////////////////////////////
iTestcode = 0
Charat = "Self Test......" : DrawChr(1,1)
Format(5,0)
' Check the Tempresult Sense(DS18b20)
IsTempresultSense = 1Wreset
If Not(IsTempresultSense) Then
Charat = ">TepSense not install" : DrawChr(1,2)
IsTempresultSense = 0
Else
Charat = ">TemperatureSense Chk" : DrawChr(1,2)
1Wwrite &hcc; &h44
WaitMs 500
1Wreset
1Wwrite &hcc; &hbe
1Wread dTempdatum ,2
1Wreset
1Wwrite &hcc; &h44
dTempresult = dTempdatum(0)
'Charat = Str(dTempresult) : DrawChr(156,2)
If dTempresult = 0 Then
Charat = ">TemperatureSense Err" : DrawChr(1,2)
IsTempresultSense = 0
iTestcode = iTestcode Or &b00000001
Else
Charat = ">Temperature Sense OK" : DrawChr(1,2)
IsTempresultSense = 1
End If
End If
' Check the ADC(LTC2400)
'Charat = ">ADC Checked..." : DrawChr(1,3)
If IO_ADC_DAT = 1 Then
Charat = ">ADC Detect...Failed" : DrawChr(1,3)
iTestcode = iTestcode Or &b00000010
Else
SetChannel(GndOffset)
bADCResult(3) = ShiftIn : bADCResult(2) = ShiftIn : bADCResult(1) = ShiftIn : bADCResult(0) = ShiftIn
iBusystp = 0
For i = 1 To 200
iBusystp = iBusystp + 1
If IO_ADC_DAT = 0 Then Exit For
WaitMs 1
Next i
If iBusystp < 155 Or iBusystp > 166 Then
Charat = ">ADC ChK Failed" : DrawChr(1,3)
Charat = Str(iBusystp) : DrawChr(94,3)
iTestcode = iTestcode Or &b00000100
Else
Charat = ">ADC Checked...Passed" : DrawChr(1,3)
iError = ADCheck(Reference) 'Result = GND Offset < +10~-10 >
If iError <> 0 Then
If iError > -10 And iError < 10 Then
Charat = ">COM Checked...Passed" : DrawChr(1,4)
Else
Charat = ">COM ChK Failed " : DrawChr(1,4)
Charat = Str(iError) : DrawChr(94,4)
iTestcode = iTestcode Or &b00001000
End If
Else
Charat = ">COM Checked...Passed" : DrawChr(1,4)
End If
'Check the Reference
Charat = ">Reference Chk:" : DrawChr(1,5)
SetVoltage(0) : WaitMs 10
iError = ADCheck(Synthesis) 'Result = Reference, Next is Synthesis Out Voltage <6500*1.024~7500*1.024 / 6655~7680>
wError = iError * 25 / 8
If wError > 6800 And wError < 7475 Then
Charat = ">REF Checked...Passed" : DrawChr(1,5)
Else
Charat = Str(wError) : DrawChr(94,5)
iTestcode = iTestcode Or &b00010000
End If
'Check the DAC Offset
Charat = ">DAC Check 1st." : DrawChr(1,6)
WaitMs 200 'Wait the ADC to Converting...
SetVoltage(12000000) : WaitMs 10 'Set Next Check Item = DAC Fullscale Out
iError = ADCheck(Synthesis) 'Result = Synthesis Out Voltage(Set=0V) <+~-1>
If iError <> 0 Then
If iError > -10 And iError < 10 Then
Charat = ">DAC Check 1st Passed" : DrawChr(1,6)
Else
Charat = Str(iError) : DrawChr(94,6)
iTestcode = iTestcode Or &b00100000
End If
Else
Charat = ">DAC Check 1st Passed" : DrawChr(1,6)
End If
'Check the DAC Full Scale
Charat = "2nd" : DrawChr(67,7)
WaitMs 200
'SetVoltage(0)
iError = ADCheck(Synthesis) 'Result = Synthesis Out Voltage(Set=12V) <12282 +/- 10word, 12270~12295>
wError = (iError / 2) * 25 / 4
If wError > 12000 And wError < 12575 Then
Charat = "Passed" : DrawChr(91,7)
Else
Charat = Str(wError) : DrawChr(94,7)
iTestcode = iTestcode Or &b01000000
End If
End If
End If
If iTestcode > 1 Then
Charat = "Failed" : DrawChr(91,1)
Charat = "Code:" : DrawChr(1,7)
Format(3,0) : Charat = Str(iTestcode) : DrawChr(31,7)
End
Else
Charat = "OK" : DrawChr(115,1)
End If
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Self Test End
'///////////////////////////////////////////////////////////////////////////////////////////////////
Wait 1 'Wait the LM399 Warmed
ClsKS(0)
' Screen Fixed Display Initialization
Charat = "SET:"
DrawChrLarge(1,0)
Charat = "REB:"
DrawChrLarge(1,2)
Charat = "VDC"
DrawChrLarge(105,0)
DrawChrLarge(105,2)
' Right Sataus Lable Display
'Charat = "MH:"
'DrawChr(156,0)
'Charat = "ML:"
'DrawChr(156,1)
'Charat = "SH:"
'DrawChr(156,2)
'Charat = "SL:"
'DrawChr(156,3)
'Charat = "WAT:" : DrawChr(156,4)
'Charat = "ADJ:"
'DrawChr(156,5)
'Botton Sataus Label Display
Charat = "IGNO" : DrawChr(9,4)
Charat = "EXEC" : DrawChr(9,5)
'Charat = "TemP" : DrawChr(12,6)
'Charat = "Filt" : DrawChr(96,6)
Charat = "ErroR" : DrawChr(52,6)
Charat = "RuN" : DrawChr(100,5)
Charat = "StP" : DrawChr(100,4)
' Cursors Draw
'MemLoad(VarPtr(iCursors), &b00001000, &b00011100, &b00111110, &b01111111) : DrawCursors(1, 4, 4) ' Display a "<" Cursors
'MemLoad(VarPtr(iCursors), &b01111111, &b00111110, &b00011100, &b00001000) : DrawCursors(1, 4, 4) ' Display a ">" Cursors
'MemLoad(VarPtr(iCursors), &b00000100, &b00000110, &b00000111, &b00000110, &b00000100) : DrawCursors(1, 4, 5) ' Display a "^" Cursors
If IsTempresultSense = 1 Then
MemLoad(VarPtr(iCursors), &b00000110, &b00001001, &b00001001, &b000000110) : DrawCursors(31, 7, 4) ' Display a Degree Cursors
Charat = "C" : DrawChr(37,7)
UpdateTempresultInfo()
End If
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Program Initialization End
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Start the Temperature Sense(DS18b20) Convert
'1Wreset
'1Wwrite &hcc; &h44
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Running Register Initialization
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Init the DAC Output Default
fSetting = 10000000
fVoltage = fSetting
' Program Reset Point:
stt:
fVoltage = fVoltage / fDacal
SetVoltage(fVoltage)
' All Setting Register Value Initialization to Default
fReference = 0
fSynthesis = 0
fCalsacles = fMasterReference
iSmlen = 16 ' the Sommth Length, 1 Trig Cycle(Adjuest Cycle) = 3 x Sommth Length
bDigitaLoop = 1 ' Digital Loop = Close Loop(0=Open Loop)
itm = 0
iSmsetp = 0 ' Current Program Running Setp
bRun = 1 ' Flag the Program Run or Setp, 1 = Run
bRefsource = 0 ' Flag the Reference Source, 0 = INT, 1 = EXT
bNextchannel = 0 ' Flag the Analog Switch Channel
bAdjuest = 0 ' Flag is/isnot Adjuest Output Voltage
bUartlk = 1 ' Flag the UART Port Talk,bit 8 = All Talk; bit 1 = talk when Trigcompleted.
bUartset = 2
' Display and Update the Output Voltage Setting Value
Format(2,0)
Charat = Str(iSmlen) : DrawChr(104,7)
'Charat = Str(iSmlen * 3 + 1) : DrawChr(179,7)
Charat = "pt" : DrawChr(116,7)
Charat = "/" : DrawChr(98,7)
Format(2,6)
fTrn = fSetting / 1000000
Charat = Str(fTrn)
DrawChrLarge(28,0)
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'Program Main Loop
'///////////////////////////////////////////////////////////////////////////////////////////////////
Do
If bRun = 0 Then
Charat = " ---------" : DrawChrLarge(28,2)
MemLoad(VarPtr(iCursors), &h00, &h00, &h00, &h00) : DrawCursors(122, 5, 4)
MemLoad(VarPtr(iCursors), &h08, &h1C, &h3E, &h7F) : DrawCursors(122, 4, 4)
iSmsetp = 0
WaitMs 100
GoTo Runout
Else
MemLoad(VarPtr(iCursors), &h00, &h00, &h00, &h00) : DrawCursors(122, 4, 4)
MemLoad(VarPtr(iCursors), &h08, &h1C, &h3E, &h7F) : DrawCursors(122, 5, 4)
End If
bNextchannel = bNextchannel + 1
If bNextchannel > 3 Then bNextchannel = 1
iBusystp = 0
Do
iBusystp = iBusystp + 1
WaitMs 1
Loop While IO_ADC_DAT = 1
Select Case bNextChannel
Case 1 : CurrentChannel = SetChannel(Reference)
Case 2 : CurrentChannel = SetChannel(GndOffset)
Case 3 : CurrentChannel = SetChannel(Synthesis)
End Select
'Charat = Hex(iBusystp) : DrawChr(64,4)
bADCResult(3) = ShiftIn
bADCResult(2) = ShiftIn
bADCResult(1) = ShiftIn
bADCResult(0) = ShiftIn
FixADCResult()
For i = 1 To iSmlen
MoveResultREG(i, bNextchannel)
Next i
WriteResultREG(iSmlen, bNextchannel)
iSmsetp = iSmsetp + 1
Format(2,0)
Charat = Str(iSmsetp/3)
DrawChr(86,7)
fSynthesis = ReadADCResult(1) 'syn
fReference = ReadADCResult(2) 'ref
fGndOffset = ReadADCResult(3) 'gnd
fCalfSynth = (fSynthesis - fGndOffset) / ((fReference - fGndOffset) / fCalsacles)
'fCalfSynth = fCalfSynth - Getinlerr(fCalfSynth) * 1000000
fError = fSetting - fCalfSynth * 1000000
iError = fError
wError = iError * iError
Format(2,6)
Charat = Str(fCalfSynth)
DrawChrLarge(28,2)
Format(4,0)
Charat = " " : DrawChr(52,7)
Charat = Str(iError) : DrawChr(52,7)
'Charat = Str(wError) : DrawChr(1,5)
iErrlimit = 0
If wError > 99 Then iErrlimit = 1
bAdjuest = bDigitaLoop And iErrlimit
'Format(2,0)
'Charat = Str(bAdjuest) : DrawChr(180,5)
If iSmsetp = iSmlen * 3 + 1 Then
iSmsetp = 0
Trigcompleted()
End If
Runout:
If bUartset = 1 Then
bUartset = 0
iSmsetp = 0
fVoltage = fSetting
fVoltage = fVoltage / fDacal
SetVoltage(fVoltage)
fTrn = fSetting / 1000000
Format(2,6)
Charat = Str(fTrn)
DrawChrLarge(28,0)
End If
If bUartset = 2 Then
bUartset = 0
StatusDisplay()
End If
Loop
End
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'Operation Sub and Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
Sub StatusDisplay()
If bDigitaLoop = 1 Then
MemLoad(VarPtr(iCursors), &h00, &h00, &h00, &h00) : DrawCursors(1, 4, 4)
MemLoad(VarPtr(iCursors), &h7F, &h3E, &h1C, &h08) : DrawCursors(1, 5, 4)
Else
MemLoad(VarPtr(iCursors), &h00, &h00, &h00, &h00) : DrawCursors(1, 5, 4)
MemLoad(VarPtr(iCursors), &h7F, &h3E, &h1C, &h08) : DrawCursors(1, 4, 4)
End If
End Sub
Function UpdateTempresultInfo() As Byte
1Wreset
1Wwrite &hcc; &hbe
1Wread dTempdatum ,2
1Wreset
1Wwrite &hcc; &h44
dTempresult = dTempdatum(0)
fTemperature = dTempresult
fTemperature = fTemperature / 16
Format(2,1) : Charat = Str(fTemperature) : DrawChr(1,7)
End Function
Sub Trigcompleted()
If bAdjuest = 1 Then
fVoltage = fVoltage + fError
SetVoltage(fVoltage)
End If
If IsTempresultSense = 1 Then UpdateTempresultInfo()
If bUartlk = 1 Then
Format(5,2)
Print fError; fTemperature
End If
End Sub
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Return the 24bit Result from 32bit Data
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function FixADCResult() As Byte
iDa = bADCResult(3)
iDb = bADCResult(2)
iDc = bADCResult(1)
iDd = bADCResult(0)
' Casting out the lower 4bit Data and Return the low 8bit Result
iDd = iDd And &b11110000
iDd = Swap(iDd)
iTrd = iDc And &b00001111
iTrd = Swap(iTrd)
iTd = iDd + iTrd
' Move and Return the middle 8 bit Result
iTrd = iDc And &b11110000
iTc = Swap(iTrd)
iTrd = iDb And &b00001111
iTrd = Swap(iTrd)
iTc = iTrd + iTc
' Move and Return the hihg 8 bit Result
iTrd = iDb And &b11110000
iTb = Swap(iTrd)
iTrd = iDa And &b00001111
iTrd = Swap(iTrd)
iTb = iTrd + iTb
' Return the Overload and Sign Bit
iTrd = iDa And &b00110000
iTa = Swap(iTrd)
' Copy Result to Public Array
bADCResult(3) = iTa
bADCResult(2) = iTb
bADCResult(1) = iTc
bADCResult(0) = iTd
Return 0
End Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Write a ADC Result to FIFO Movewindow Array
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function WriteResultREG(bSetp As Byte, bArea As Byte) As Byte
iTrd = (bArea - 1) * iSmlen + bSetp
bREGa(iTrd) = bADCResult(3)
bREGb(iTrd) = bADCResult(2)
bREGc(iTrd) = bADCResult(1)
bREGd(iTrd) = bADCResult(0)
End Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Move the FIFO Array
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function MoveResultREG(bSetp As Byte, bArea As Byte) As Byte
iTrd = (bArea - 1) * iSmlen + bSetp
bREGa(iTrd) = bREGa(iTrd + 1)
bREGb(iTrd) = bREGb(iTrd + 1)
bREGc(iTrd) = bREGc(iTrd + 1)
bREGd(iTrd) = bREGd(iTrd + 1)
End Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Return a Float Format Smohth ADC Result
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function ReadADCResult(bChannel As Byte) As Float
fTrn = 0
fOut = 0
For i = 1 To iSmlen
iTrd = (bChannel - 1) * iSmlen + i
iTa = bREGa(iTrd)
iTb = bREGb(iTrd)
iTc = bREGc(iTrd)
iTd = bREGd(iTrd)
fTrn = 5 * 2.5 * (16777216 * iTa + 65536 * iTb + 256 * iTc + iTd - 33554432) / 16777216
fOut = fOut + fTrn
Next i
fOut = fOut / iSmlen
Return fOut
End Function
Function DrawCursors(x As Byte, Page As Byte, Length As Byte) As Byte
For i = 0 To Length - 1
DrawPage(x + i, Page, iCursors(i))
Next i
End Function
Function ADCheck(bNextChannel As Byte) As Integer
Local iChkresutl As Integer
Local iChk1 As Byte
Local iChk2 As Byte
Local iChk3 As Byte
Local iChk4 As Byte
Do
Loop While IO_ADC_DAT = 1
SetChannel(bNextChannel) : WaitMs 10
iChk4 = ShiftIn : iChk3 = ShiftIn : iChk2 = ShiftIn : iChk1 = ShiftIn
iChkresutl = iChk4 * 4
iChkresutl = iChkresutl * 256 + iChk3 * 4 - 32768
Return iChkresutl / 4
End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'Shiftreg IO
'///////////////////////////////////////////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Wrtie the Analog board Control Shift-Register
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function WriteSReg(bDat As Byte) As Byte
Local i As Byte
Local bWritedat As Byte
Local bWritebit As Byte
bWritedat = bDat
IO_REG_DAT = 0
IO_REG_CLK = 0
IO_REG_STB = 0
WaitUs 1
For i = 1 To 8
bWritebit = bWritedat And &b10000000
If bWritebit = 128 Then
IO_REG_DAT = 1
Else
IO_REG_DAT = 0
End If
WaitUs 1
IO_REG_CLK = 1 : WaitUs 1 : IO_REG_CLK = 0
bWritedat = Shift(Left, 1, bWritedat)
Next i
WaitUs 1
IO_REG_STB = 1 : WaitMs 1 : IO_REG_STB = 0
IO_REG_DAT = 0
IO_REG_CLK = 0
Return 0
End Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Setting the Analog Switch(ADC Input Channel)
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function SetChannel(Channel As Byte) As Byte
Local Channelset As Byte
Channelset = bSregister And &b11110000
WriteSReg(Channelset) ' Open all Analog Switch when Close Next Switch
WaitMs 10
bSregister = Channelset + Channel
WriteSReg(bSregister)
Return bSregister
End Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Read Analog Board ADC Result Shift-Register
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function ReadADC() As Byte
Local myByte As Byte
Local i As Byte
myByte = 0
' Read 8bit = 1 Byte Result form ADC Shift-Out Register(2Wire Serial Bus ADC)
For i = 0 To 7
IO_ADC_CLK = 1
Rotate(Left,1,myByte)
myByte = myByte + IO_ADC_DAT
WaitUs 1
IO_ADC_CLK = 0
WaitUs 1
Next i
' Return 1 Byte Result to Function
Return myByte
End Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Read MCU Block Keyboard Status Shift-Register
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function ReadKey() As Byte
Local myByte As Byte
Local i As Byte
myByte = 0
' Read 8bit = 1 Byte Result form Keyboard Shift-Out Register(74HC165)
For i = 0 To 7
Rotate(Left,1,myByte)
myByte = myByte + IO_KEY_DAT
IO_KEY_CLK = 1
WaitUs 1
IO_KEY_CLK = 0
WaitUs 1
Next i
' Return 1 Byte Result to Function
myByte = Not myByte
Return myByte
End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'Mathematical Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////////////////////
' Setting the Analogboard DAC Output Voltage
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function SetVoltage(fVoltage As Float) As Byte
Local Vsetm As Word ' The Master DAC Setting Return
Local Vsets As Word ' The Slave DAC Setting Return
Local mbMSB As Byte
Local mbLSB As Byte
Local sbMSB As Byte
Local sbLSB As Byte
' the VOut = Gain * (Vmaster * Rs + Vslave * Rm) / (Rm+Rs), Fullscale of the Vm = 5V and Vs = -5V, Gain = 2.5
fVtrn = fVoltage
fVtrn = fVtrn / fDACGain
fVset = fVtrn
'Get the MasteDAC Voltage Setting Value, Vm=[Vo*(Rm+Rs)+2.5*Rm]/Rs , at Default, the Slave DAC Output = -2.5V
fVtrn = (fVtrn * (fMResistance + fSResistance) + fDACenter * fMResistance) / fSResistance
fVtrn = fVtrn * 65535 / fDACReference 'The Master DAC(MAX541) is 16bit DAC, Fullscale = 2^16 = 65536
Vsetm = fVtrn
mbMSB = Msb(Vsetm) 'Split the Setting Word to two Byte, Get the MSB Byte
mbLSB = Vsetm - mbMSB * 256 'Split the Setting Word to two Byte, Get the LSB Byte
' Get the SlaveDAC Voltage Setting Value,Vs=[Vo*(Rm+Rs)-Vm*Rs]/Rm
fVtrn = Vsetm * fDACReference / 65535
fVtrn = 0 - ((fVset * (fMResistance + fSResistance) - fVtrn * fSResistance) / fMResistance)
fVtrn = fVtrn * 4096 / fDACReference 'The Slave DAC(MAX515) is 12bit DAC, Fullscale = 2^12 = 4096
Vsets = fVtrn
sbMSB = Msb(Vsets) 'Split the Setting Word to two Byte, Get the MSB Byte
sbLSB = Vsets - sbMSB * 256 'Split the Setting Word to two Byte, Get the LSB Byte
' Write the Setting to DAC
'Setp 1:
bSregister = bSregister And &b00001111 'Hold the Analog Switch Status and Setting the DAC to Write Mode
ShiftOut mbMSB;mbLSB;sbMSB;sbLSB;bSregister 'Write to Serial Bus
IO_REG_STB = 1 : WaitUs 10 : IO_REG_STB = 0 'Update the Control Shift-Register, DAC tobe Write
'Setp 1:
bSregister = bSregister + &b10000000 'Hold the Analog Switch Status and Setting the DAC to Update
ShiftOut mbMSB;mbLSB;sbMSB;sbLSB;bSregister
IO_REG_STB = 1 : WaitUs 10 : IO_REG_STB = 0 'Update the Control Shift-Register, DAC tobe Update
Return 0
End Function
'///////////////////////////////////////////////////////////////////////////////////////////////////
' ADC No-line Error Adjuest
'///////////////////////////////////////////////////////////////////////////////////////////////////
Function Getinlerr(fVoltage As Float) As Float
fVtrn = fVoltage
fVset = fVtrn * fVtrn * Quadratic - fVtrn * Linear + Constant
Return fVset
End Function
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'Self Test Code
'///////////////////////////////////////////////////////////////////////////////////////////////////
Interrupt Urxc(),Save All
Local uVseth As Word
Local uVsetl As Word
'Local uMSBh As Byte
'Local uLSBh As Byte
'Local uMSBl As Byte
'Local uLSBl As Byte
Local bMSB As Byte
Local bLSB As Byte
Local bRebtype As Byte
Local bSett As Byte
InputBin tTxt
tCmd = Mid(tTxt, 1, 3)
If tCmd = "STV" Then
tm = Mid(tTxt,4,4)
uVseth = Val(tm)
tm = Mid(tTxt,8,4)
uVsetl = Val(tm)
fVtrn = uVseth
fVtrn = fVtrn * 10000
fVtrn = fVtrn + uVsetl
tm = Mid(tTxt,12,1)
bSett = Val(tm)
If bSett = 0 Then
fSetting = fVtrn
bUartset = 1
End If
If bSett = 1 Then
fCalsacles = fVtrn
bUartset = 1
End If
End If
If tCmd = "STS" Then
tm = Mid(tTxt,4,1)
If tm <> "x" Then bDigitaLoop = Val(tm)
tm = Mid(tTxt,12,1)
If tm <> "x" Then bRun = Val(tm)
tm = Mid(tTxt,11,1)
If tm <> "x" Then bRefsource = Val(tm)
bUartset = 2
End If
If tCmd = "REB" Then
tm = Mid(tTxt,4,1)
bRebtype = Val(tm)
Select Case bRebtype
Case 0 ' Readback Measure Value(0-?)
Case 8 ' Readback Cal
Case 9 ' Readback Setting Status
End Select
End If
Enable Interrupts
End Interrupt
$Include "k12864drv.bas"
$Include "..\font1.bas"
$Include "..\font2.bas" |
|