$regfile = "m16def.dat" 'definieren des verwendeten Chips '$crystal = 1600000 ' generated. Take care that the chip supports all fuse bytes.$PROG &HFF,&HE1,&H99,&H00' generated. Take care that the chip supports all fuse bytes.$crystal = 16000000 ' generated. Take care that the chip supports all fuse bytes.$PROG &HFF,&HFF,&HFF,&H00' generated. Take care that the chip supports all fuse bytes.$crystal = 16000000 '$baud = 9600 Waitms 300 '--------------------------Für I2C LCD ----------------------------------------- Const Pcf8574_lcd = 112 'PCF Adresse für LCD (original Pcf8574_lcd =70) $lib "Lcd_i2c.lib" 'I2C Treiber für das LCD Dim _lcd_e As Byte 'Um auch LCDs mit 2 Controllern 'nutzen zu können. _lcd_e = 128 'Für LCDs mit 1 Controler Waitms 300 'WICHTIG! ggf. erhöhen Config Lcd = 16 * 2 Config Scl = Portc.0 Config Sda = Portc.1 '------------------------------------------------------------------------------- Config Adc = Single , Prescaler = Auto , Reference = Avcc 'Prescaler = 64 'Now Give Power To The Chip Start Adc Config Porta = Input Config Pind.5 = Output Config Pind.4 = Output Config Timer1 = Pwm , Prescale = 8 , Pwm = 10 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down 'Config Timer1 = Pwm , Prescale = 64 , Pwm = 10 , Compare A Pwm = Clear Down , Compare B Pwm = Clear down Declare Function Pida(vlia As Word , Vlaa As Byte , Vlsa As Byte)as Byte Declare Function Pidz(vliz As Word , Vlaz As Byte , Vlsz As Byte)as Byte Dim S0 As Integer Dim S1 As Integer Dim S2 As Integer Dim S3 As Integer Dim S4 As Integer Dim S5 As Integer Dim S6 As Integer Dim S7 As Integer Dim Ax1 As Word Dim Ax2 As Word Dim A1 As Byte Dim A2 As Byte Dim Dout1 As Byte Dim Din14 As Byte Dim Din58 As Byte Dim Dinhk As Byte Dim Dra1 As Word Dim Dra2 As Word Dim Menu As Byte Dim Xs As Byte Dim T As Byte Dim D As Byte Dim Q As Byte Dim L As Byte Dim Qq As Byte Dim Sr As Byte Dim Dfl As Byte Dim Reg As Byte Dim Idh As Byte Dim Idl As Byte Dim Id As Word Dim Hilf As Word Dim Hilfs As Long Dim Temp As Word Dim Idd14 As Word Dim Idd58 As Word Dim Ida12 As Word Dim Idi As Word Dim Kp As Byte Dim Ki As Byte Dim Kd As Byte Dim Mauf As Byte Dim Mzu As Byte Dim Vlsoll As Byte Dim Vlalt As Byte Dim Vlsoll2 As Byte Dim Vlalt2 As Byte Dim Zeit As Byte Dim Diff As Byte Dim Hzkr As Byte Dim P1 As Bit Dim P2 As Bit Dim P3 As Bit Dim P4 As Bit Dim P5 As Bit Dim P6 As Bit Dim P7 As Bit Dim P8 As Bit Dim P7a As Bit Dim P8a As Bit Dim V1 As Byte Dim V2 As Byte Dim V3 As Byte Dim V4 As Byte Dim Dout2 As Byte Dim Hkraus1 As Word Dim Hkraus2 As Word Dim Hk1 As Bit Dim Hk2 As Bit Config Portd.2 = Output Config Portd.3 = Output Config Portc.6 = Output Config Portc.7 = Output Config Pind.6 = Input Config Pind.7 = Input Ale Alias Portd.2 Cs Alias Portd.3 Rd Alias Portc.6 Wr Alias Portc.7 Xt Alias Pind.6 Men Alias Pind.7 I2csend &H72 , 0 Waitms 50 Cursor Off Cls Locate 1 , 1 Lcd "CAN-Mod. MegaCAN" Locate 2 , 1 Lcd "Version 5.2 2511" Ale = 0 : Cs = 1 : Rd = 1 : Wr = 1 'Steuerleitungen von SJA1000 setzen Menu = 0 'Idi = 10 'Idd14 = 1 'Idd58 = 1 'Ida12 = 1 'Writeeeprom Idi , 1 'idi 'Writeeeprom Idd14 , 5 'idd14 'Writeeeprom Idd58 , 10 'idd58 'Writeeeprom Ida12 , 15 'ida12 Kp = 8 : Ki = 5 : Kd = 2 Waitms 800 Gosub Initiale Readeeprom Idi , 1 Readeeprom Idd14 , 5 Readeeprom Idd58 , 10 Readeeprom Ida12 , 15 '------------------- Hauptprogramm -------------------------------------- Prog: Xs = 0 '--------------------------------------- T = T + 1 If T > 150 Then T = 1 If T = 5 Then Gosub Senddata '----------------- Fühler Auswertung ----------------------------- Dout2 = 0 S0 = Getadc(0) : Hilfs = S0 * 100 Hilfs = Hilfs / 161 : S0 = Hilfs -154 S1 = Getadc(1) : Hilfs = S1 * 100 Hilfs = Hilfs / 161 : S1 = Hilfs -154 S2 = Getadc(2) : Hilfs = S2 * 100 Hilfs = Hilfs / 161 : S2 = Hilfs -154 S3 = Getadc(3) : Hilfs = S3 * 100 Hilfs = Hilfs / 161 : S3 = Hilfs -154 S4 = Getadc(4) : Hilfs = S4 * 100 Hilfs = Hilfs / 161 : S4 = Hilfs -154 S5 = Getadc(5) : Hilfs = S5 * 100 Hilfs = Hilfs / 161 : S5 = Hilfs -154 S6 = Getadc(6) : Hilfs = S6 * 100 Hilfs = Hilfs / 161 : S6 = Hilfs -154 S7 = Getadc(7) : Hilfs = S7 * 100 Hilfs = Hilfs / 161 : S7 = Hilfs -154 If S0 < -50 Then Dout2 = Dout2 + 1 If S1 < -50 Then Dout2 = Dout2 + 2 If S2 < -50 Then Dout2 = Dout2 + 4 If S3 < -50 Then Dout2 = Dout2 + 8 If S4 < -50 Then Dout2 = Dout2 + 16 If S5 < -50 Then Dout2 = Dout2 + 32 If S6 < -50 Then Dout2 = Dout2 + 64 If S7 < -50 Then Dout2 = Dout2 + 128 '----------------- berechnung ob Heizkreis ein ist -------------------------- P1 = 0 : P2 = 0 : P3 = 0 : P4 = 0 : P5 = 0 : P6 = 0 : P7 = 0 : P8 = 0 P7a = 0 : P8a = 0 Dout1 = 0 'Digital Ausgang 5-8 Hilf = Din58 If Hilf > 127 Then P8 = 1 : Hilf = Hilf - 128 : Dout1 = Dout1 + 128 : End If If Hilf > 63 Then P7 = 1 : Hilf = Hilf - 64 : Dout1 = Dout1 + 64 : End If If Hilf > 31 Then P6 = 1 : Hilf = Hilf - 32 : Dout1 = Dout1 + 32 : End If If Hilf > 15 Then P5 = 1 : Hilf = Hilf - 16 : Dout1 = Dout1 + 16 : End If 'Digital Ausgang 1-4 Hilf = Din14 If Hilf > 127 Then Hilf = Hilf - 128 If Hilf > 63 Then Hilf = Hilf - 64 If Hilf > 31 Then Hilf = Hilf - 32 If Hilf > 15 Then Hilf = Hilf - 16 If Hilf > 7 Then P4 = 1 : Hilf = Hilf -8 : Dout1 = Dout1 + 8 : End If If Hilf > 3 Then P3 = 1 : Hilf = Hilf -4 : Dout1 = Dout1 + 4 : End If If Hilf > 1 Then P2 = 1 : Hilf = Hilf -2 : Dout1 = Dout1 + 2 : End If If Hilf = 1 Then P1 = 1 : Dout1 = Dout1 + 1 : End If 'digital Eingang für Heizkreis Hilf = Dinhk If Hilf > 127 Then P8a = 1 : Hilf = Hilf -128 : Dout1 = Dout1 + 128 : End If If Hilf > 63 Then P7a = 1 : Hilf = Hilf -64 : Dout1 = Dout1 + 64 : End If If P7a = 1 Then Hk1 = 1 If P8a = 1 Then Hk2 = 1 '--------------- Ausgabe auf I2c Bus ------------------------------ If T = 10 And Hk1 = 1 Then Gosub Mischera If T = 15 And Hk2 = 1 Then Gosub Mischerb If P7a = 0 And Hk1 = 1 Then Gosub Mischerzua If P8a = 0 And Hk2 = 1 Then Gosub Mischerzub I2csend &H72 , Dout1 Hilf = Dra1 * 103 : Hilf = Hilf / 10 : If Hilf > 1023 Then Hilf = 1023 Pwm1a = Hilf Hilf = Dra2 * 103 : Hilf = Hilf / 10 : If Hilf > 1023 Then Hilf = 1023 Pwm1b = Hilf '------------------- Display Anzeige ---------------------------------- If Men = 0 Then Menu = Menu + 1 If Menu > 8 Then Menu = 0 If Menu = 1 Then Locate 1 , 1 : Lcd "S1: " ; S0 ; " " : Locate 1 , 9 : Lcd "S2: " ; S1 ; " " Locate 2 , 1 : Lcd "S3: " ; S2 ; " " : Locate 2 , 9 : Lcd "S4: " ; S3 ; " " If Men = 0 Then Menu = Menu + 1 Waitms 500 End If End If If Menu = 2 Then Locate 1 , 1 : Lcd "S5: " ; S4 ; " " : Locate 1 , 9 : Lcd "S6: " ; S5 ; " " Locate 2 , 1 : Lcd "S7: " ; S6 ; " " : Locate 2 , 9 : Lcd "S8: " ; S7 ; " " If Men = 0 Then Menu = Menu + 1 Waitms 500 End If End If If Menu = 3 Then Locate 1 , 1 : Lcd "A1:" ; P1 ; " " : Locate 1 , 6 : Lcd "A2:" ; P2 ; " " : Locate 1 , 11 : Lcd "A3:" ; P3 ; " " Locate 2 , 1 : Lcd "A4:" ; P4 ; " " : Locate 2 , 6 : Lcd "A5:" ; P5 ; " " : Locate 2 , 11 : Lcd "A6:" ; P6 ; " " If Men = 0 Then Menu = Menu + 1 Waitms 500 End If End If If Menu = 4 Then Locate 1 , 1 : Lcd "A7:" ; P7 ; " " : Locate 1 , 6 : Lcd "A8:" ; P8 ; " " Locate 2 , 1 : Lcd "TR1:" ; Dra1 ; " " : Locate 2 , 9 : Lcd "TR2:" ; Dra2 ; " " If Men = 0 Then Menu = Menu + 1 Waitms 500 End If End If If Menu = 5 Then If Xt = 0 Then Idi = Idi + 1 If Idi > 63 Then Idi = 2 Locate 1 , 1 : Lcd "Knoten Nr: " ; Idi ; " " Locate 2 , 1 : Lcd " " Waitms 350 If Men = 0 Then Writeeeprom Idi , 1 Menu = Menu + 1 Waitms 350 End If End If If Menu = 6 Then If Xt = 0 Then Idd14 = Idd14 + 1 If Idd14 > 63 Then Idd14 = 1 Locate 1 , 1 : Lcd "Digital 1-4 von " Locate 2 , 1 : Lcd "Knoten Nr. " ; Idd14 ; " " Waitms 350 If Men = 0 Then Writeeeprom Idd14 , 5 Menu = Menu + 1 Waitms 350 End If End If If Menu = 7 Then If Xt = 0 Then Idd58 = Idd58 + 1 If Idd58 > 63 Then Idd58 = 1 Locate 1 , 1 : Lcd "Digital 5-8 von " Locate 2 , 1 : Lcd "Knoten Nr. " ; Idd58 ; " " Waitms 350 If Men = 0 Then Writeeeprom Idd58 , 10 Menu = Menu + 1 Waitms 350 End If End If If Menu = 8 Then If Xt = 0 Then Ida12 = Ida12 + 1 If Ida12 > 63 Then Ida12 = 1 Locate 1 , 1 : Lcd "Analog 1+2 von " Locate 2 , 1 : Lcd "Knoten Nr. " ; Ida12 ; " " Waitms 350 If Men = 0 Then Writeeeprom Ida12 , 15 Menu = Menu + 1 Waitms 350 End If End If If Menu = 0 Then Locate 1 , 1 Lcd "" ; Dout2 ; ":" ; P7a ; ":" ; P8a ; ":" ; Vlsoll ; ":" ; Vlsoll2 ; ":" ; S0 ; " " Locate 2 , 1 Lcd "" ; Dra1 ; ":" ; Dra2 ; ":" ; S1 ; ":" ; S3 ; ":T" ; T ; " " End If '----- Teste auf Empfang und auslesen ---------------------------- Reg = &H02 : Gosub C_rd : Sr = D Q = Sr And 2 : If Q = 2 Then Goto Clr_overrun Q = Sr And 1 : If Q = 0 Then Goto Prog Reg = &H14 : Gosub C_rd : Idh = D Reg = &H15 : Gosub C_rd : Idl = D Id = Idh * 8 : Hilf = Idl / 32 : Id = Id + Hilf Dfl = D And 15 If Menu = 0 Then Locate 2 , 1 Lcd "ID=" ; Id ; " DFL=" ; Dfl End If For L = 0 To 7 Reg = &H16 + L : Gosub C_rd 'digital Eingang 1-4 Hilf = Idd14 + 384 If Id = Hilf And L = 0 Then Din14 = D ' digital Eingang 5-8 Hilf = Idd58 + 384 If Id = Hilf And L = 0 Then Din58 = D If Id = Hilf And L = 1 Then Dinhk = D 'Analog Eingang für Triac Hilf = Ida12 + 512 If Id = Hilf And L = 0 Then Dra1 = D If Id = Hilf And L = 2 Then Dra2 = D 'Analog Eingang für VL-soll Hilf = Idd58 + 640 If Id = Hilf And L = 0 Then V1 = D If Id = Hilf And L = 1 Then V2 = D If Id = Hilf And L = 2 Then V3 = D If Id = Hilf And L = 3 Then V4 = D Next L Hilf = V2 * 255 Hilf = Hilf + V1 Hilf = Hilf / 10 Vlsoll = Hilf Hilf = V4 * 255 Hilf = Hilf + V3 Hilf = Hilf / 10 Vlsoll2 = Hilf Enderec: Reg = &H01 : D = &H0C : Gosub C_wr Goto Prog Clr_overrun: Reg = &H01 : D = 8 : Gosub C_wr Goto Prog End '----------------- Initialisieren des SJA1000 ----------------------------- Initiale: Locate 2 , 1 Lcd "Initiale: " Reg = &H00 : D = &H01 : Gosub C_wr Reg = &H00 : D = &H01 : Gosub C_wr W_r: Reg = &H00 : Gosub C_rd : Q = D And 1 : If Q <> 1 Then Goto W_r Locate 2 , 1 Lcd "Reset OK " 'Print "RESET OK" Reg = &H1F : D = &H43 : Gosub C_wr Reg = &H04 : D = &H00 : Gosub C_wr Reg = &H05 : D = &HFF : Gosub C_wr Reg = &H06 : D = &H49 : Gosub C_wr 'BTR0 500 KBIT (&H00 für 500 KBIT) Reg = &H07 : D = &H1C : Gosub C_wr 'BTR1 Reg = &H08 : D = &H1A : Gosub C_wr Reg = &H01 : D = &H0E : Gosub C_wr Reg = &H00 : D = &H00 : Gosub C_wr W_i: Reg = &H00 : Gosub C_rd : Q = D And 1 : If Q > 0 Then Goto W_i Locate 2 , 1 Lcd "INITIALE OK " Return ' ----------------------- Botschaft Senden -------------------------- Senddata: Id = 512 + Idi : Dfl = 8 Temp = S0 * 10 : Ax2 = Temp / 256 : Hilf = Ax2 * 256 : Ax1 = Temp - Hilf A1 = Ax1 : A2 = Ax2 Reg = 12 + 0 : D = A1 : Gosub C_wr Reg = 12 + 1 : D = A2 : Gosub C_wr Temp = S1 * 10 : Ax2 = Temp / 256 : Hilf = Ax2 * 256 : Ax1 = Temp - Hilf A1 = Ax1 : A2 = Ax2 Reg = 12 + 2 : D = A1 : Gosub C_wr Reg = 12 + 3 : D = A2 : Gosub C_wr Temp = S2 * 10 : Ax2 = Temp / 256 : Hilf = Ax2 * 256 : Ax1 = Temp - Hilf A1 = Ax1 : A2 = Ax2 Reg = 12 + 4 : D = A1 : Gosub C_wr Reg = 12 + 5 : D = A2 : Gosub C_wr Temp = S3 * 10 : Ax2 = Temp / 256 : Hilf = Ax2 * 256 : Ax1 = Temp - Hilf A1 = Ax1 : A2 = Ax2 Reg = 12 + 6 : D = A1 : Gosub C_wr Reg = 12 + 7 : D = A2 : Gosub C_wr Gosub Senden '------------------------------------------------------------------------------- Waitms 10 Id = 640 + Idi : Dfl = 8 Temp = S4 * 10 : Ax2 = Temp / 256 : Hilf = Ax2 * 256 : Ax1 = Temp - Hilf A1 = Ax1 : A2 = Ax2 Reg = 12 + 0 : D = A1 : Gosub C_wr Reg = 12 + 1 : D = A2 : Gosub C_wr Temp = S5 * 10 : Ax2 = Temp / 256 : Hilf = Ax2 * 256 : Ax1 = Temp - Hilf A1 = Ax1 : A2 = Ax2 Reg = 12 + 2 : D = A1 : Gosub C_wr Reg = 12 + 3 : D = A2 : Gosub C_wr Temp = S6 * 10 : Ax2 = Temp / 256 : Hilf = Ax2 * 256 : Ax1 = Temp - Hilf A1 = Ax1 : A2 = Ax2 Reg = 12 + 4 : D = A1 : Gosub C_wr Reg = 12 + 5 : D = A2 : Gosub C_wr Temp = S7 * 10 : Ax2 = Temp / 256 : Hilf = Ax2 * 256 : Ax1 = Temp - Hilf A1 = Ax1 : A2 = Ax2 Reg = 12 + 6 : D = A1 : Gosub C_wr Reg = 12 + 7 : D = A2 : Gosub C_wr Gosub Senden '------------------------------------------------------------------------------- Waitms 10 Id = 384 + Idi : Dfl = 2 Reg = 12 + 0 : D = Dout2 : Gosub C_wr Reg = 12 + 1 : D = 0 'Dout1 : Gosub C_wr 'Reg = 12 + 2 : D = 0 : Gosub C_wr 'Reg = 12 + 3 : D = 0 : Gosub C_wr 'Reg = 12 + 4 : D = 0 : Gosub C_wr 'Reg = 12 + 5 : D = 0 : Gosub C_wr 'Reg = 12 + 6 : D = 0 : Gosub C_wr 'Reg = 12 + 7 : D = 0 : Gosub C_wr Gosub Senden Return '------------------------------------------------------------------------------ Senden: Reg = &H02 : Gosub C_rd : Q = D And 4 Xs = Xs + 1 If Xs > 5 Then Return If Q = 0 Then Goto Senden Reg = &H0A : Hilf = Id / 8 : D = Hilf : Gosub C_wr Reg = &H0B Hilf = Id / 8 : D = Hilf Hilf = D * 8 Hilf = Id - Hilf Hilf = Hilf * 32 Hilf = Hilf + Dfl D = Hilf : Gosub C_wr Reg = &H01 : D = &H0D : Gosub C_wr 'D=&H0D Traransmission request 1100 Reg = &H02 : Gosub C_rd : Sr = D Return '------------------------------------------------------------------------------ C_wr: 'Daten an den SJA senden Config Portb = Output Ale = 1 : Cs = 1 : Rd = 1 : Wr = 1 Portb = Reg Ale = 0 Waitms 1 Cs = 0 Portb = D Wr = 0 Wr = 1 Cs = 1 Ale = 1 Return C_rd: 'vom SJA1000 lesen Config Portb = Output Ale = 1 : Cs = 1 : Rd = 1 : Wr = 1 Portb = Reg Ale = 0 Cs = 0 Rd = 0 Portb = 0 Waitms 1 Ddrb = &B00000000 'Port B als Input Portb = 0 'Waitms 1 D = Pinb Rd = 1 Cs = 1 Ale = 1 Return '---------------------------------------------------------------------------------------------- Mischera: If S0 > Vlsoll Then Mauf = 0 Hilf = S0 Mzu = Pidz(hilf , Vlalt , Vlsoll) Hilf = Mzu * 72 : If Hilf > 2046 Then Hilf = 2046 If Hilf < 400 Then Hilf = 400 Dout1 = Dout1 + 8 : I2csend &H72 , Dout1 : Waitms Hilf Dout1 = Dout1 - 8 End If If S0 < Vlsoll Then Mzu = 0 Hilf = S0 Mauf = Pida(hilf , Vlalt , Vlsoll) Hilf = Mauf * 72 : If Hilf > 2046 Then Hilf = 2046 If Hilf < 400 Then Hilf = 400 Dout1 = Dout1 + 4 : I2csend &H72 , Dout1 : Waitms Hilf Dout1 = Dout1 - 4 End If Vlalt = S0 Return '----------------------------------------------------------------------------- Mischerb: If S2 > Vlsoll2 Then Mauf = 0 Hilf = S2 Mzu = Pidz(hilf , Vlalt2 , Vlsoll2) Hilf = Mzu * 72 : If Hilf > 2046 Then Hilf = 2046 If Hilf < 400 Then Hilf = 400 Dout1 = Dout1 + 32 : I2csend &H72 , Dout1 : Waitms Hilf Dout1 = Dout1 - 32 End If If S2 < Vlsoll2 Then Mzu = 0 Hilf = S2 Mauf = Pida(hilf , Vlalt2 , Vlsoll2) Hilf = Mauf * 72 : If Hilf > 2046 Then Hilf = 2046 If Hilf < 400 Then Hilf = 400 Dout1 = Dout1 + 16 : I2csend &H72 , Dout1 : Waitms Hilf Dout1 = Dout1 - 16 End If Vlalt2 = S2 Return '------------------------------------------------------------------------------- Mischerzua: Dout1 = Dout1 + 8 ': I2csend &H72 , Dout1 Hkraus1 = Hkraus1 + 1 If Hkraus1 = 975 Then Hk1 = 0 Hkraus1 = 0 End If Return '------------------------------------------------------------------------------- Mischerzub: Dout1 = Dout1 + 32 ': I2csend &H72 , Dout1 Hkraus2 = Hkraus2 + 1 If Hkraus2 = 975 Then Hk2 = 0 Hkraus2 = 0 End If Return '---------------------------------------------------------------------------------------------- End Function Pida(vlia As Word , Vlaa As Byte , Vlsa As Byte)as Byte Dim Pa As Byte Dim Ia As Byte Dim Da As Byte 'Print "VL-soll=" ; Vlsa 'Print "VL-ist= " ; Vlia 'Print "VL-alt=" ; Vlaa 'Print "time=" ; Zeit Diff = Vlsa - Vlia If Diff > 15 Then Diff = 15 Pa = Kp * Diff Ia = Ki * Diff : Ia = Ia * Zeit Da = Vlaa - Vlia : Da = Da / Zeit : Da = Da * Kd Pida = Pa + Ia : Pida = Pida + Da End Function '-------------------------------------------------------------------------- Function Pidz(vliz As Word , Vlaz As Byte , Vlsz As Byte)as Byte Dim Pz As Byte Dim Iz As Byte Dim Dz As Byte 'Print "VL-soll=" ; Vlsz 'Print "VL-ist= " ; Vliz 'Print "VL-alt=" ; Vlaz 'Print "time=" ; Zeit Diff = Vliz - Vlsz If Diff > 15 Then Diff = 15 Pz = Kp * Diff Iz = Ki * Diff : Iz = Iz * Zeit Dz = Vlaz - Vliz : Dz = Dz / Zeit : Dz = Dz * Kd Pidz = Pz + Iz : Pidz = Pidz + Dz End Function