' ##############################
' #### DC POWER SUPPLY ####
' #### M644P ####
' ##############################
$regfile = "m644pdef.dat"
$crystal = 11059200
$hwstack = 64
$swstack = 64
$framesize = 64
Const Timeout_time = 8 'x500ms
Config Submode = New
' config LCD 2X16
Config Lcdpin = Pin , Rs = Porta.0 , E = Porta.1 , Db4 = Porta.2 , Db5 = Porta.3 , Db6 = Porta.4 , Db7 = Porta.5
Config Lcd = 20x4
Cursor Off
Cls
Const Error = " ERROR"
Const Info = "Press switch On/Off"
Const Frame_long = "--------------------"
'Konfiguracja 1-WIRE dla DS18B20
Config 1wire = Portc.6
' Zmienne pomiar temperatury
Dim Ds1(8) As Byte
Dim Temperatura As Byte
Dim Temperatura_str As String * 5
Dim Ts As Integer
Dim Wew(9) As Byte
Dim T_heatsink As Integer
' ID Ds1 (HEX) 28E731CB010000FD czujnik temperatury głównego radiatora
Ds1(1) = &H28
Ds1(2) = &HE7
Ds1(3) = &H31
Ds1(4) = &HCB
Ds1(5) = &H01
Ds1(6) = &H00
Ds1(7) = &H00
Ds1(8) = &HFD
Declare Sub Temperature()
Declare Sub Show_temperature()
' Konfiguracja i/o
Config POrTC.3 = Input : PORTC.3 = 1 : output_sw Alias PINC.3 ' switch OUTPUT ON/OFF
Config Portc.5 = Output : Portc.5 = 0 : relay Alias Portc.5 ' Przekaźnik OUTPUT ON/OFF
Config PortC.4 = Output : PortC.4 = 0 : output_led Alias PortC.4 'Led OUTPUT ON/0FF
Config PortD.4 = Output : PortD.4 = 0 : buzzer Alias PortD.4 'Buzzer
Config POrTA.7 = Input : PORTA.7 = 1 : cv Alias PINA.7 ' constant voltage
Config POrTC.7 = Input : PORTC.7 = 1 : cc Alias PINC.7 ' constant current
Config POrTB.0 = Input : PORTB.0 = 1 : ovp_sw Alias PINB.0 ' switch OVP
Config POrTB.1 = Input : PORTB.1 = 1 : ocp_sw Alias PINB.1 ' switch OCP
Config POrTB.3 = Input : PORTB.3 = 1 : sw1 Alias PINB.3 ' switch 1
Config POrTB.2 = Input : PORTB.2 = 1 : sw2 Alias PINB.2 ' switch 2
Config POrTB.4 = Input : PORTB.4 = 1 : sw3 Alias PINB.4 ' switch 3
Dim ovp As Single
Dim ocp As Single
Dim ov As Bit ' zmienna pomocnicza dla OVP
Dim oc As Bit ' zmienna pomocnicza dla OCP
Dim ot As Bit ' zmienna pomocnicza dla OTP
Dim S As Bit
ov = 0
oc = 0
ot = 0
S = 0
Dim Pulses As Word 'Zmienna długości dźwięku
Dim Periods As Word ' Zmienna barwy dźwięku
' Symbole na LCD
Deflcdchar 0,17,17,17,10,4,32,32,32' (volt)
Deflcdchar 1,14,17,17,31,17,32,32,32' (amper)
Deflcdchar 2,17,17,21,21,10,32,32,32' (wat)
Deflcdchar 3,14,17,17,14,32,32,32,32' (stopień celcjusza)
' Zmienne do zapisu danych w eeprom 24C02
Dim N As Byte
Dim Napiecie_ee As Single
Dim Buf1(20) As Byte At Napiecie_ee Overlay
Dim Pos_ee As Single
Dim Buf2(20) As Byte At Pos_ee Overlay
Dim Prad_ee As Single
Dim Buf3(20) As Byte At Prad_ee Overlay
Dim Pos2_ee As Single
Dim Buf4(20) As Byte At Pos2_ee Overlay
' Define the 24C02 EEPROM address
Const Addressw_ee = 168
Const Addressr_ee = 169
' Konfiguracja I2C
$lib "i2c_twi.lbx"
Config Scl = Portc.0
Config Sda = Portc.1
I2cinit
Config Twi = 100000
'####################### Config for encoders #########################################
' ENCODER U_SET
Config PortD.5 = Input : PortD.5 = 1 : Encoder1_a Alias PinD.5 'ENKODER1A
Config Portd.3 = Input : Portd.3 = 1 : Encoder1_b Alias Pind.3 'ENKODER1B, INT1
Config PortD.7 = Input : PortD.7 = 1 : Enc_sw1 Alias PinD.7 ' Przycisk Enkoder1
' ENCODER I_SET
Config PortD.6 = Input : PortD.6 = 1 : Encoder2_a Alias PinD.6 'ENKODER2A
Config Portd.2 = Input : Portd.2 = 1 : Encoder2_b Alias Pind.2 'ENKODER2B, INT0
Config PortC.2 = Input : PortC.2 = 1 : Enc_sw2 Alias PinC.2 ' Przycisk Enkoder2
Config Int1 = Rising 'przerwanie generować będzie rosnące zbocze
Enable Int1 : On Int1 Encoder1_isr Nosave
Config Int0 = Rising 'przerwanie generować będzie rosnące zbocze
Enable Int0 : On Int0 Encoder2_isr Nosave
Dim Encoder1_turn_left As Byte , Encoder1_turn_right As Byte
Dim Encoder2_turn_left As Byte , Encoder2_turn_right As Byte
'#######################################################################################
'Config Timer2
Config Timer2 = Timer , Prescale = 1024 , Clear_timer = 1
Compare2a = 107 '10ms @11MHz/1024
' Settings
Dim 500ms As Byte , Mig As Byte , Timeout As Byte , Mig1 As Byte , Timeout1 As Byte
'Zmienne dla encodera prądu
Dim Prad As Single
Dim Pos2 As Byte
Dim Current_str As String * 10
Dim Temp_str As String * 10
'Zmienne dla encodera napięcia
Dim Napiecie As Single
Dim Pos As Byte
Dim Volt_str As String * 10
Dim Temp_str1 As String * 10
'================== Zmienne ADC ADS1110 ====================
' Konfiguracj zmiennych ADS (158) - Pomiar prądu pobieranego I_OUT
Dim Current As Single
Dim Addressw As Byte
Dim Addressr As Byte
Dim Pomiarh As Byte
Dim Pomiarl As Byte
Dim Current_out As String * 10
Dim ADC_current As Integer
Dim I_str As String * 10
' Konfiguracj zmiennych ADS1 (150) - Pomiar napięcia wyjściowego U_OUT
Dim Voltage As Single
Dim Addressw1 As Byte
Dim Addressr1 As Byte
Dim Pomiarh1 As Byte
Dim Pomiarl1 As Byte
Dim Voltage_out As String * 10
Dim ADC_voltage As Integer
Dim U_str As String * 10
Dim Moc AS Single
Dim Moc_out As String * 10
Dim Moc_str AS String * 10
'Define the ADC address
Addressw = &B10011110 '158; adres zapizu danych dla [ED7]
Addressr = &B10011111 '159; adres odczytu danych dla [ED7]
Addressw1 = &B10010110 '150; adres zapizu danych dla [ED3]
Addressr1 = &B10010111 '151; adres odczytu danych dla [ED3]
Declare Sub Measure()
Declare Sub Show_measure()
'============== Zmienne DAC AD5667 =======================
'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 DAC address
Dim Dac_addr As Byte
Dac_addr = &B00011110 ' shift left 1 for hardware i2c === ADRR podłączone do GND
'zmienne dla przekształcenia prądu I_SET
Dim Amp as Single
Dim Ampi As Single
Dim i_set As Single
'zmienna dla przekształcenia napięcia U_SET
Dim Vol as Single
Dim Voli As Single
Dim u_set As Single
Dim Dachigh As Byte
Dim Daclow As Byte
Dim Daccommand(3) As Byte
Dim Length As Byte
Dim Ia As Word 'przyjmuje wartość 0 - 65535 wysłana do DAC (channel A)
Dim Ib As Word 'przyjmuje wartość 0 - 65535 wysłana do DAC (channel B)
Enable Interrupts
Call Dac_reset()
Call Dac_ref_on()
Call Dac_ldac_async()
Call Dac_power_on()
' ======================= Koniec zmiennych DAC ===========================
output_led = 1
Waitms 900
output_led = 0
Locate 1 , 1 : Lcd " DC Power Supply " ' Headline
Locate 2 , 1 : Lcd " 0 - 20V / 3A "
Locate 3 , 1 : Lcd " ATmega 644PA "
Waitms 3000
Buzzer = 1
Waitms 50
Buzzer = 0
cls
' Procedura wyświetlania napięcia U_SET
Sub Show_voltage
Temp_str1 = Fusing(napiecie , "#.&&&")
Volt_str = Format(temp_str1 , " ") 'dwa znaki+kropka+trzy znaki
If Mig1 = 1 Then
Select Case Pos
Case 1
Mid(volt_str , 1) = " " 'wstaw spację w miejscu pos (migaj)
Case 2
Mid(volt_str , 4) = " "
Case 3
Mid(volt_str , 5) = " "
Case 4
Mid(volt_str , 6) = " "
End Select
End If
Locate 1 , 1 : Lcd "SET " ; Volt_str ; Chr(0) ' Wyświetlenie na LCD napięcia ustawionego U_SET
End Sub
' Procedura wyświetlania prądu I_SET
Sub Show_current
Temp_str = Fusing(prad , "#.&&&")
Current_str = Format(temp_str , " ") 'dwa znaki+kropka+trzy znaki
If Mig = 1 Then
Select Case Pos2
Case 1
Mid(current_str , 1) = " " 'wstaw spację w miejscu pos (migaj)
Case 2
Mid(current_str , 4) = " "
Case 3
Mid(current_str , 5) = " "
Case 4
Mid(current_str , 6) = " "
End Select
End If
Locate 1 , 14 : Lcd Current_str ; Chr(1) ' Wyświetlenie na LCD ograniczenia prądowego I_SET
End Sub
' Przekształcenie zmiennych "prad" oraz "napiecie" wyświetlanych na lcd na kod 0 - 65536 i wysłanie do DAC
Sub Set_dac
u_set = napiecie / 4
Vol = u_set 'zmienna Vol to napięcie wyrażone w voltach podany z ENCODER1, przyjmuje wartość 0 - 5V
Voli = Vol / 5
Voli = Voli * 65535
Ia = Voli
i_set = prad / 20
Amp = i_set 'zmienna Amp to prąd wyrażony w amperach podany z ENCODER2, przyjmuje wartość 0 - 150mV
Ampi = Amp / 5
Ampi = Ampi * 65535
Ib = Ampi
Call Dac_set_a(ia) 'U_SET
Call Dac_set_b(ib) 'I_SET
End Sub
'sub zapisuje Eeprom wartością pod podanym adresem
Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte)
I2cstart
I2cwbyte Addressw_ee
I2cwbyte Adres
I2cwbyte Value
I2cstop
Waitms 5
End Sub
'funkcja zwraca wartosć bajtu pod podanym adresem w eepromie
Function Read_eeprom(byval Adres As Byte )as Byte
I2cstart
I2cwbyte Addressw_ee
I2cwbyte Adres
I2cstart
I2cwbyte Addressr_ee
I2crbyte Read_eeprom , Nack
I2cstop
End Function
' eeprom 24C02 reading U_SET
'------------------------------
For N = 1 To 5
Buf2(n) = Read_eeprom(n + 6)
Next
Pos = Pos_ee
If Pos > 4 Then
Pos = 4 : Pos_ee = Pos
Napiecie_ee = 0
end if
' ODCZYT WAROTOŚCI NAPIĘCIA Z 24C02
For N = 1 To 5
Buf1(n) = Read_eeprom(n + 1)
Next
Napiecie = Napiecie_ee
Call Show_voltage()
Call Set_dac()
' eeprom 24C02 reading I_SET
'------------------------------
For N = 1 To 5
Buf4(n) = Read_eeprom(n + 18)
Next
Pos2 = Pos2_ee
If Pos2 > 4 Then
Pos2 = 4 : Pos2_ee = Pos2
Prad_ee = 0
End If
' ODCZYT WAROTOŚCI PRĄDU Z 24C02
For N = 1 To 5
Buf3(n) = Read_eeprom(n + 12)
Next
Prad = Prad_ee
Call Show_current()
Call Set_dac()
relay = 0 ' Przekaźnik zawsze wyłączony po uruchomieniu zasilacza
oc = 0
ov = 0
' #######################################################################
' ########################## GŁÓWNA PĘTLA ###############################
' #######################################################################
Do
ot = relay
If relay = 0 Then
Locate 3 , 18 : Lcd "Off"
end if
Debounce output_sw , 0 , Out_sw_sub , Sub 'switch output
Debounce ovp_sw , 0 , Ovp_sw_sub , Sub 'switch ovp
'Obsługa funkcji OVP
ovp = voltage
If ov = 1 Then
If ovp > napiecie Then gosub Overvoltage
End if
Debounce ocp_sw , 0 , Ocp_sw_sub , Sub 'switch ocp
'Obsługa funkcji OCP
ocp = current + 0.001
If oc = 1 Then
If ocp > prad Then gosub Overcurrent
End if
'*************************** Obsługa encodera dla prądu
Debounce Enc_sw2 , 0 , Enc_sw2_sub , Sub 'ENCODER I_SET
'##### encoder turns left ######
If 0 < Encoder2_turn_left Then
Decr Encoder2_turn_left
If Timeout > 0 Then
Select Case Pos2
Case 1
Prad = Prad - 1.0
Case 2
Prad = Prad - 0.1
Case 3
Prad = Prad - 0.01
Case 4
Prad = Prad - 0.001
End Select
If Prad < 0 Then Prad = 0
Mig = 0 : Call Show_current
End If
Timeout = Timeout_time 'przedłuzaj/uruchamiaj timeout przy kazdym kliku
Eifr.intf0 = 1
Enable INT0
End If
' #### encoder turns right ######
If 0 < Encoder2_turn_right Then
Decr Encoder2_turn_right
If Timeout > 0 Then
Select Case Pos2
Case 1
Prad = Prad + 1
Case 2
Prad = Prad + 0.1
Case 3
Prad = Prad + 0.01
Case 4
Prad = Prad + 0.001
End Select
If Prad > 3.0 Then Prad = 3.0
Mig = 0 : Call Show_current
End If
Timeout = Timeout_time 'przedłuzaj/uruchamiaj timeout przy kazdym kliku
Eifr.intf0 = 1
Enable INT0
End If
' *********************** Obsługa encodera dla napięcia
Debounce Enc_sw1 , 0 , Enc_sw1_sub , Sub 'ENCODER U_SET
'##### encoder turns left ######
If 0 < Encoder1_turn_left Then
Decr Encoder1_turn_left
If Timeout1 > 0 Then
Select Case Pos
Case 1
Napiecie = Napiecie - 1.0
Case 2
Napiecie = Napiecie - 0.1
Case 3
Napiecie = Napiecie - 0.01
Case 4
Napiecie = Napiecie - 0.001
End Select
If Napiecie < 0 Then Napiecie = 0
Mig1 = 0 : Call Show_voltage
End If
Timeout1 = Timeout_time 'przedłuzaj/uruchamiaj timeout przy kazdym kliku
Eifr.intf1 = 1 'dla Mega8, M32 itp
Enable Int1
End If
' #### encoder turns right ######
If 0 < Encoder1_turn_right Then
Decr Encoder1_turn_right
If Timeout1 > 0 Then
Select Case Pos
Case 1
Napiecie = Napiecie + 1
Case 2
Napiecie = Napiecie + 0.1
Case 3
Napiecie = Napiecie + 0.01
Case 4
Napiecie = Napiecie + 0.001
End Select
If Napiecie > 20.0 Then Napiecie = 20.0
Mig1 = 0 : Call Show_voltage
End If
Timeout1 = Timeout_time 'przedłuzaj/uruchamiaj timeout przy kazdym kliku
Eifr.intf1 = 1 'dla Mega8, M32 itp
Enable Int1
End If
If Tifr2.ocf2A = 1 Then '10ms
Tifr2.ocf2A = 1
If 500ms < 10 Then ' 49 było
Incr 500ms
Else
500ms = 0
If Mig = 0 Then Mig = 1 Else Mig = 0
If Timeout > 0 Then
Decr Timeout
If Timeout = 0 Then
'wyślij do DAC
Mig = 0
' Zapisz prąd i_set do eeprom dopiero po Timeout
Prad_ee = Prad
For N = 1 To 5
Call Write_eeprom(n + 12 , Buf3(n))
Next
Call Set_dac
End If
Call Show_current()
End If
If Mig1 = 0 Then Mig1 = 1 Else Mig1 = 0
If Timeout1 > 0 Then
Decr Timeout1
If Timeout1 = 0 Then
'wyślij do DAC
Mig1 = 0
' Zapisz napięcie u_set do eeprom dopiero po Timeout
Napiecie_ee = Napiecie
For N = 1 To 5
Call Write_eeprom(n + 1 , Buf1(n))
Next
Call Set_dac
End If
Call Show_voltage()
End If
End If
End If
Call Measure ' Pomiar napięcia i prądu na wujściu
Call Show_measure ' Wyświetlenie napięcia i prądu na wyjściu
Call Temperature ' Pomiar temperatury
Call Show_temperature ' Wyświetlenie zmierzonej temperatury
Loop
End
' #################################### KONIEC GŁÓWNEJ PĘTLI ####################################
' Obsługa przycisku Output on/off
Out_sw_sub:
gosub Beep
If output_sw = 0 Then
Toggle relay
end if
If relay = 1 Then
output_led = 1
Locate 3 , 18 : Lcd " On"
Else
If relay = 0 Then
Locate 3 , 18 : Lcd "Off"
output_led = 0
end if
end if
Return
' Obsługa przycisku Encoder1 U_SET
Enc_sw1_sub:
If Timeout1 > 0 Then
Incr Pos : If Pos > 4 Then Pos = 1
Pos_ee = Pos 'Zapisz pozycję do eeprom
For N = 1 To 5
Call Write_eeprom(n + 6 , Buf2(n))
Next
End If
Timeout1 = Timeout_time 'przedłuzaj/uruchamiaj timeout przy kazdym kliku
Return
' Obsługa przycisku Encoder2 I_SET
Enc_sw2_sub:
If Timeout > 0 Then
Incr Pos2 : If Pos2 > 4 Then Pos2 = 1
Pos2_ee = Pos2 ' Zapisz pozycję do eeprom
For N = 1 To 5
Call Write_eeprom(n + 18 , Buf4(n))
Next
End If
Timeout = Timeout_time 'przedłuzaj/uruchamiaj timeout przy kazdym kliku
Return
'obsługa przerwania zewnetrznego z encoder2
Encoder2_isr:
$asm
PUSH R23
PUSH R24
PUSH R26
PUSH R27
!in R24, sreg
PUSH R24
$end Asm
Disable INT0
If Encoder2_a = 1 Then
Incr Encoder2_turn_right
Else
Incr Encoder2_turn_left
End If
'Tuned with NoSave Tool
$asm
POP R24
!out sreg, r24
POP R27
POP R26
POP R24
POP R23
$end Asm
Return
'obsługa przerwania zewnetrznego z encoder1
Encoder1_isr:
$asm
PUSH R23
PUSH R24
PUSH R26
PUSH R27
!in R24, sreg
PUSH R24
$end Asm
Disable Int1
If Encoder1_a = 1 Then
Incr Encoder1_turn_right
Else
Incr Encoder1_turn_left
End If
'Tuned with NoSave Tool
$asm
POP R24
!out sreg, r24
POP R27
POP R26
POP R24
POP R23
$end Asm
Return
' ================== OBSŁUGA DAC =====================
'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
End Sub
'Internal voltage reference off
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) = &B00000000 ' Command Byte (00111000)internal ref off musi być
Daccommand(2) = &B00000000 ' MSB
Daccommand(3) = &B00000001 ' LSB
I2csend Dac_addr , Daccommand(1) , Length
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
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
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
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
End Sub
' ==================== KONIEC OBSŁUGI DAC ================================
' ==================== OBSŁUGA ADC =======================================
' Pomiar U_OUT / I_OUT
Sub Measure
' I_OUT
I2cstart
I2cwbyte Addressr 'Adres Do Odczytu
I2crbyte Pomiarh , Ack 'Odczyt Pomiaru
I2crbyte Pomiarl , Nack
I2cstop 'Bit Stopu
Const K = 2.048 / 32768
ADC_current = Makeint(Pomiarl,Pomiarh)
Current = ADC_current * K
Current = Current + 0.001 ' Kalibracja
' U_OUT
I2cstart
I2cwbyte Addressr1 'Adres Do Odczytu
I2crbyte Pomiarh1 , Ack 'Odczyt Pomiaru
I2crbyte Pomiarl1 , Nack
I2cstop 'Bit Stopu
Const K = 2.048 / 32768
ADC_voltage = Makeint(Pomiarl1,Pomiarh1)
Voltage = ADC_voltage * K
Voltage = Voltage * 10 ' Współczynnik K dla dzielnika rezystorowego
Voltage = Voltage - 0.0250 ' Kalibracja
' P_OUT
Moc = voltage * current
End Sub
' Wyświetlenie pomiarów U_OUT/I_OUT/P_OUT
Sub Show_measure
Voltage_out = Fusing(voltage , "#.&&&")
U_str = Format(voltage_out , " ")
Locate 2 , 1 : Lcd "OUT " ; U_str ; Chr(0)
Current_out = Fusing(current , "#.&&&")
I_str = Format(current_out , " ")
Locate 2 , 14 : Lcd I_str ; Chr(1)
Moc_out = Fusing(moc , "#.&&&")
Moc_str = Format(Moc_out , " ")
Locate 3 , 1 : Lcd Moc_str ; Chr(2)
If CC = 0 Then ' constant voltage
Locate 3 , 10 : LCD "CC"
end if
If CV = 0 Then ' constant current
Locate 3 , 10 : LCD "CV"
end if
End Sub
' ============================= KONIEC OBSŁUGI ADC ========================
' Pomiar temperatury DS18B20
Sub Temperature
1wreset
1wwrite &HCC
1wwrite &H44
1wverify Ds1(1)'1wreset i 1wwrite &H55 niepotrzebne bo 1wverify samo to przeprowadza
1wwrite &HBE
Wew(1) = 1wread(9)
Ts = Makeint(wew(1) , Wew(2)) ' Obliczenie wartości teperatury
Ts = Ts * 10
Ts = Ts / 16
T_heatsink = Ts / 10 ' obliczenie temperatury dla funkcji OTP (Over-temperature protection)
Temperatura_str = Str(ts)
Temperatura_str = Format(temperatura_str , "0.0")
If T_heatsink > 30 Then ' Jeżeli temperatura głównego radiatora przekroczy 55 stC, to skok do procedury OTP
If ot = 1 Then gosub Overtemperature
end if
If ot = 1 Then
If Err = 1 Then gosub Overtemperature ' Jeżeli błąd czujnika DS18B20 to skok do procedury OTP
end if
End Sub
' Wyświetlenie temperatury DS18B20
Sub Show_temperature
If Err = 0 Then
Locate 4 , 15 : Lcd Temperatura_str ; Chr(3) ; "C"
else
Locate 4 , 15 : Lcd Error
End if
End Sub
Beep:
buzzer = 1
waitms 50
buzzer = 0
Return
' Obsługa przycisku OVP
Ovp_sw_sub:
gosub Beep
If ovp_sw = 0 Then
Toggle ov
end if
If ov = 1 Then
Locate 4 , 1 : Lcd "OVP"
Else
If ov = 0 Then
Locate 4 , 1 : Lcd " "
end if
end if
Return
' Obsługa przycisku OCP
Ocp_sw_sub:
gosub Beep
If ocp_sw = 0 Then
Toggle oc
end if
If oc = 1 Then
Locate 4 , 5 : Lcd "OCP"
Else
If oc = 0 Then
Locate 4 , 5 : Lcd " "
end if
end if
Return
' ########## Procedura ochrony OVP ##########
Overvoltage:
relay = 0 'Wyłączenie przekaźnika
output_led = 0
' Sygnalizacja dźwiękowa przeciążenia
Pulses = 120 ' ustalenie długości tonu dźwiękowego
Periods = 2200 ' ustawienie barwy tonu
Sound buzzer , Pulses , Periods ' dźwięk ostrzegawczy
Do
relay = 0
output_led = 0
ov = 0
Locate 1 , 1 : Lcd Frame_long
Locate 2 , 1 : Lcd "- OVP Out: Off -"
Locate 3 , 1 : Lcd Frame_long
Locate 4 , 1 : Lcd Info
If output_sw = 0 Then
cls
waitms 20
If output_sw = 0 Then
relay = 0 : Mig = 0 : Mig1 = 0
Call Show_voltage
Call Show_current
return
end if : end if
Loop
' ########## Procedura ochrony OCP ##########
Overcurrent:
relay = 0 'Wyłączenie przekaźnika
output_led = 0
' Sygnalizacja dźwiękowa przeciążenia
Pulses = 120 ' ustalenie długości tonu dźwiękowego
Periods = 2200 ' ustawienie barwy tonu
Sound buzzer , Pulses , Periods ' dźwięk ostrzegawczy
Do
relay = 0
output_led = 0
oc = 0
Locate 1 , 1 : Lcd Frame_long
Locate 2 , 1 : Lcd "- OCP Out: Off -"
Locate 3 , 1 : Lcd Frame_long
Locate 4 , 1 : Lcd Info
If output_sw = 0 Then
cls
waitms 20
If output_sw = 0 Then
relay = 0 : Mig = 0 : Mig1 = 0
Call Show_voltage
Call Show_current
return
end if : end if
Loop
' ########## Procedura ochrony OTP ##########
Overtemperature:
relay = 0 'Wyłączenie przekaźnika
output_led = 0
' Sygnalizacja dźwiękowa przeciążenia
Pulses = 120 ' ustalenie długości tonu dźwiękowego
Periods = 2200 ' ustawienie barwy tonu
Sound buzzer , Pulses , Periods ' dźwięk ostrzegawczy
Do
relay = 0
output_led = 0
ot = 0
Locate 1 , 1 : Lcd Frame_long
Locate 2 , 1 : Lcd "- OTP Out: Off -"
Locate 3 , 1 : Lcd Frame_long
Locate 4 , 1 : Lcd Info
If output_sw = 0 Then
cls
waitms 20
If output_sw = 0 Then
relay = 0 : Mig = 0 : Mig1 = 0
Call Show_voltage
Call Show_current
return
end if : end if
Loop