' Obsługa przetwornika cyfrowo analogowego AD5667R
' Wersja testowa
$regfile = "m32def.dat"
$crystal = 11059200
' Konfiguracja LCD 2X16
Config Lcdpin = Pin , E = PortA.3 , Rs = PortA.2 , Db7 = PortA.7 , Db6 = PortA.6 , Db5 = PortA.5 , Db4 = PortA.4
Config Lcd = 16x2
Cursor Off
Cls
' Konfiguracja I2C
$lib "i2c_twi.lbx"
Config Scl = Portc.0
Config Sda = Portc.1
I2cinit
Config Twi = 100000
'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------
' 2 Channel AD5667R 16 Bit I2c DAC
'-------------------------------------------------------------------------------
' 2-kanałowy przetwornik cyfrowo-analogowy AD5667 16-bitowy I2c z wbudowanym precyzyjnym napięciem odniesienia
' do sterowania zasilaczem z analogowymi portami zdalnego sterowania 0-5 V
' Opis interfejsu I2C
' AD5667R wymaga dwóch bajtów danych dla DAC i bajtu polecenia, który kontroluje różne funkcje DAC.
' Dlatego w przetworniku cyfrowo-analogowym muszą zostać zapisane trzy bajty danych,
' bajt polecenia, po którym następuje najbardziej znaczący bajt danych i najmniej znaczący bajt danych.
' Opis Bajtu
'
' 1 [Command Data Byte ]
' 2 [Most Significant Data Byte ]
' 3 [Least Significant Data Byte]
' [ Command Data Byte ] [ Most Significant Data Byte ] [ Least Significant Data Byte ]
' [R S C2 C1 C0 A2 A1 A0] [D7 D6 D5 D4 D3 D2 D1 D0] [D7 D6 D5 D4 D3 D2 D1 D0]
' Inicjalizacja DAC
' [0 0 1 0 1 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] SOFTWARE RESET
' [0 0 1 1 1 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 1] INTERNAL REFERENCE ON
' [0 0 1 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 1 1] POWER UP BOTH DACS
' SET DACS
' [0 0 0 1 1 0 0 0] [ X X X X X X X X] [X X X X X X X X] SET DAC A
' [0 0 0 1 1 0 0 1] [ X X X X X X X X] [X X X X X X X X] SET DAC B
'Implemet each I2c command
Declare Sub Dac_reset()
Declare Sub Dac_ref_on()
Declare Sub Dac_ldac_async()
Declare Sub Dac_power_on()
Declare Sub Dac_set_a(value As Word)
Declare Sub Dac_set_b(value As Word)
Declare Sub Init_dac()
'Define the Hardware SCL and SDA port pins
$lib "i2c_twi.lbx"
Config Scl = Portc.0
Config Sda = Portc.1
I2cinit
Config Twi = 100000
'Define the DAC address
Dim Dac_addr As Byte
Dac_addr = &B00011110 ' shift left 1 for hardware i2c (DONT KNOW WHY!)
'xxxxxxxxxxxx
Dim Napiecie As Single
Dim Voltages As String * 10
'xxxxxxxxxxxxxxxxxx
Dim Dachigh As Byte
Dim Daclow As Byte
Dim Daccommand(3) As Byte
Dim Length As Byte
Dim Ia As Word
Dim Ib As Word
Dim Amp as Single
Dim Ampi As Single
Enable Interrupts
Call Dac_reset()
Call Dac_ref_on()
Call Dac_ldac_async()
Call Dac_power_on()
Do
For Ia = 0 To 65535 ' Na wyjściu DAC VOUTA napięcie 0 - 5V
' Ib = 32768 ' Na wyjściu DAC VOUTB ustawiłem na sztywno wartość 32768 = 2,5V
Amp = 2.5
Ampi = Amp / 5
Ampi = Ampi * 65536
Ib = Ampi
Call Dac_set_a(ia)
Call Dac_set_b(ib)
Next Ia
Loop
End
'Reset DAC
Sub Dac_reset
' [ Slave Address ] [ Command Data Byte ] [ Most Significant Data Byte ] [ Least Significant Data Byte ]
' [A7 A6 A5 A4 A3 A2 A1 A0] [R S C2 C1 C0 A2 A1 A0] [D7 D6 D5 D4 D3 D2 D1 D0] [D7 D6 D5 D4 D3 D2 D1 D0]
' [ 0 0 1 1 1 1 0 0] [0 0 1 0 1 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] SOFTWARE RESET
Length = 3
Daccommand(1) = &B00101000 ' Command Byte
Daccommand(2) = &B00000000 ' MSB
Daccommand(3) = &B00000000 ' LSB
I2csend Dac_addr , Daccommand(1) , Length
Locate 1 , 1
Lcd "Reset Dac " ; Err
End Sub
'Internal voltage reference on
Sub Dac_ref_on()
' [ Slave Address ] [ Command Data Byte ] [ Most Significant Data Byte ] [ Least Significant Data Byte ]
' [A7 A6 A5 A4 A3 A2 A1 A0] [R S C2 C1 C0 A2 A1 A0] [D7 D6 D5 D4 D3 D2 D1 D0] [D7 D6 D5 D4 D3 D2 D1 D0]
' [ 0 0 1 1 1 1 0 0] [0 0 1 1 1 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 1] INTERNAL REFERENCE ON
Length = 3
Daccommand(1) = &B00111000 ' Command Byte
Daccommand(2) = &B00000000 ' MSB
Daccommand(3) = &B00000001 ' LSB
I2csend Dac_addr , Daccommand(1) , Length
Locate 1 , 1
Lcd "Dac Ref on " ; Err
End Sub
'LDAC register set for asynchronous mode
Sub Dac_ldac_async()
' [ Slave Address ] [ Command Data Byte ] [ Most Significant Data Byte ] [ Least Significant Data Byte ]
' [A7 A6 A5 A4 A3 A2 A1 A0] [R S C2 C1 C0 A2 A1 A0] [D7 D6 D5 D4 D3 D2 D1 D0] [D7 D6 D5 D4 D3 D2 D1 D0]
' [ 0 0 1 1 1 1 0 0] [0 0 1 1 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 1 1] INTERNAL REFERENCE ON
Length = 3
Daccommand(1) = &B00110000 ' Command Byte
Daccommand(2) = &B00000000 ' MSB
Daccommand(3) = &B00000011 ' LSB
I2csend Dac_addr , Daccommand(1) , Length
Locate 1 , 1
Lcd "Dac Async " ; Err
End Sub
'Power up both DAC channels
Sub Dac_power_on()
' [ Slave Address ] [ Command Data Byte ] [ Most Significant Data Byte ] [ Least Significant Data Byte ]
' [A7 A6 A5 A4 A3 A2 A1 A0] [R S C2 C1 C0 A2 A1 A0] [D7 D6 D5 D4 D3 D2 D1 D0] [D7 D6 D5 D4 D3 D2 D1 D0]
' [ 0 0 1 1 1 1 0 0] [0 0 1 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 1 1] POWER UP BOTH DACS
Length = 3
Daccommand(1) = &B00100000 ' Command Byte
Daccommand(2) = &B00000000 ' MSB
Daccommand(3) = &B00000011 ' LSB
I2csend Dac_addr , Daccommand(1) , Length
Locate 1 , 1
Lcd "Dac Power " ; Err
End Sub
'Set DAC channel A
Sub Dac_set_a(value As Word)
' [ Slave Address ] [ Command Data Byte ] [ Most Significant Data Byte ] [ Least Significant Data Byte ]
' [A7 A6 A5 A4 A3 A2 A1 A0] [R S C2 C1 C0 A2 A1 A0] [D7 D6 D5 D4 D3 D2 D1 D0] [D7 D6 D5 D4 D3 D2 D1 D0]
' [ 0 0 1 1 1 1 0 0] [0 0 0 1 1 0 0 0] [ X X X X X X X X] [X X X X X X X X] SET DAC A
Length = 3
Dachigh = High(value)
Daclow = Low(value)
Daccommand(1) = &B00011000 ' Command Byte
Daccommand(2) = Dachigh ' MSB
Daccommand(3) = Daclow ' LSB
I2csend Dac_addr , Daccommand(1) , Length
Locate 1 , 1
Lcd "Set Dac A " ; Value ; " " ; Err
End Sub
'Set DAC channel B
Sub Dac_set_b(value As Word)
' [ Slave Address ] [ Command Data Byte ] [ Most Significant Data Byte ] [ Least Significant Data Byte ]
' [A7 A6 A5 A4 A3 A2 A1 A0] [R S C2 C1 C0 A2 A1 A0] [D7 D6 D5 D4 D3 D2 D1 D0] [D7 D6 D5 D4 D3 D2 D1 D0]
' [ 0 0 1 1 1 1 0 0] [0 0 0 1 1 0 0 1] [ X X X X X X X X] [X X X X X X X X] SET DAC B
Length = 3
Dachigh = High(value)
Daclow = Low(value)
Daccommand(1) = &B00011001 ' Command Byte
Daccommand(2) = Dachigh ' MSB
Daccommand(3) = Daclow ' LSB
I2csend Dac_addr , Daccommand(1) , Length
' Przekształcenie wartości Ib = 0 - 65535 na napięcie wyświetlane na LCD
Napiecie = Ib / 65536
Napiecie = Napiecie * 5
'
Locate 2 , 1
Voltages = Fusing(napiecie , "&&.&&&")
Lcd "Set Dac B " ; Voltages
End Sub