logikbomb
08.09.2005, 10:10
versuche gerade die Temperatur mit einem 18B20 auszulesen und zu speichern klappt aber nicht
ATmega16, DS18B20 und M24C16 laufen ohne probleme
liegt also an der software vieleicht kann mir ja jemand dabei helfen
hier schon mal mein code :
$regfile = "m16def.dat" ' ATmega8
$crystal = 16000000 ' für STK500
$baud = 19200
Config Lcdpin = Pin , Db4 = Portb.3 , Db5 = Portb.4 , Db6 = Portb.5 , Db7 = Portb.6 , E = Portd.5 , Rs = Portd.6
Config Lcd = 16 * 2
' HW Stack 40; Soft Stack 20; Frame Size 40 nicht optimiert
Const Device_id = &HA ' Device ID für EEPROM NM24Cxx
Const Page_addr = 1 ' verwendete Page
Const Word_addr = 5 ' verwendete Speicherzelle
Const Esc = 27
Dim Slave_wa As Byte ' Slave Write Adresse
' verwendetes Datenbyte
Dim Temp As Byte
Dim T1 As Single
Dim T2 As Single
Dim B1 As Byte , B2 As Byte , R As Byte
Dim Tmp As String * 6
Dim Vorzeichen As String * 1
R = &HFF
Config Scl = Portc.0 ' PB0 ist SCL
Config Sda = Portc.1 ' PB1 ist SDA
Config I2cdelay = 10 ' SCL = 100 kHz
Config 1wire = Portb.0 ' DS1820 on pin 12 (pull up)
Config Portd.7 = Output
Enable Interrupts
Declare Sub I2c_wb(byval Swa As Byte , Byval Wa As Byte , Dta As Byte)
Declare Function I2c_rb(byval Swa As Byte , Byval Wa As Byte) As Byte
Slave_wa = Device_id ' Berechnung der Slaveadresse
Shift Slave_wa , Left , 4
Temp = Page_addr
Shift Temp , Left
Cls
Slave_wa = Slave_wa Or Temp ' Berechnung Slave Write Adresse
Print "EEPROM Schreiben & Lesen - Abbruch mit ESC"
Lcd "EEPROM Schreiben"
Lowerline
Lcd "& Lesen ex. ESC "
T1 = &HA5
Wait 1
Cls
Do
1wreset
1wwrite &HCC
1wwrite &H44
Waitms 250
1wreset
1wwrite &HCC
1wwrite &HBE
B1 = 1wread()
B2 = 1wread()
1wreset
If B2 >= 248 Then
B1 = R - B1
B2 = R - B2
Vorzeichen = "-"
Else
Vorzeichen = "+"
End If
T1 = B1 / 16
T2 = B2 * 16
T1 = T1 + T2
Locate 2 , 1
Lcd "Temp"
Locate 2 , 6
Lcd Vorzeichen ; T1 ;
Locate 2 , 12
Lcd " C "
Incr T1 ' Inkrementieren Datenbyte
Print "Schreibe " ; Vorzeichen ; T1 ; " nach Adresse " ; Word_addr
Locate 1 , 1
Lcd "Schreibe " ; Vorzeichen ; T1 ;
Locate 2 , 1
Lcd "nach "
Locate 2 , 6
Lcd "Adresse " ; Word_addr
I2c_wb Slave_wa , 5 , T1 , ' Byte in EEPROM schreiben
Wait 2
Cls
Temp = I2c_rb(slave_wa , Word_addr) ' Byte aus EEPROM zurücklesen
Print "Lese " ; Temp ; " von Adresse " ; Word_addr ' Ausgabe gelesenes Byte
Locate 1 , 1
Lcd "Lese " ; Temp
Locate 2 , 1
Lcd "von "
Locate 2 , 6
Lcd "Adresse " ; Word_addr
Print ; Vorzeichen ; T1 ; " C "
Wait 1
Temp = Inkey()
Loop Until Temp = Esc
Cls
Print "Ende."
Lcd "ENDE. "
End
Sub I2c_wb(byval Swa As Byte , Byval Wa As Byte , Dta As Byte)
I2cstart ' I2C Write Sequence
I2cwbyte Swa
I2cwbyte Wa
I2cwbyte Dta
I2cstop
Waitms 10 ' Warten auf Ende des Schreibzyklus
End Sub
Function I2c_rb(byval Swa As Byte , Byval Wa As Byte) As Byte
Local T As Byte
Local Sra As Byte
Sra = Swa Or 1
I2cstart ' I2C Read Sequence
I2cwbyte Swa
I2cwbyte Wa
I2cstart
I2cwbyte Sra
I2crbyte T , Nack
I2cstop
I2c_rb = T
End Function
ich hoffe das mir jemand sagen kann wie es funtioniert
MFG Mario
ATmega16, DS18B20 und M24C16 laufen ohne probleme
liegt also an der software vieleicht kann mir ja jemand dabei helfen
hier schon mal mein code :
$regfile = "m16def.dat" ' ATmega8
$crystal = 16000000 ' für STK500
$baud = 19200
Config Lcdpin = Pin , Db4 = Portb.3 , Db5 = Portb.4 , Db6 = Portb.5 , Db7 = Portb.6 , E = Portd.5 , Rs = Portd.6
Config Lcd = 16 * 2
' HW Stack 40; Soft Stack 20; Frame Size 40 nicht optimiert
Const Device_id = &HA ' Device ID für EEPROM NM24Cxx
Const Page_addr = 1 ' verwendete Page
Const Word_addr = 5 ' verwendete Speicherzelle
Const Esc = 27
Dim Slave_wa As Byte ' Slave Write Adresse
' verwendetes Datenbyte
Dim Temp As Byte
Dim T1 As Single
Dim T2 As Single
Dim B1 As Byte , B2 As Byte , R As Byte
Dim Tmp As String * 6
Dim Vorzeichen As String * 1
R = &HFF
Config Scl = Portc.0 ' PB0 ist SCL
Config Sda = Portc.1 ' PB1 ist SDA
Config I2cdelay = 10 ' SCL = 100 kHz
Config 1wire = Portb.0 ' DS1820 on pin 12 (pull up)
Config Portd.7 = Output
Enable Interrupts
Declare Sub I2c_wb(byval Swa As Byte , Byval Wa As Byte , Dta As Byte)
Declare Function I2c_rb(byval Swa As Byte , Byval Wa As Byte) As Byte
Slave_wa = Device_id ' Berechnung der Slaveadresse
Shift Slave_wa , Left , 4
Temp = Page_addr
Shift Temp , Left
Cls
Slave_wa = Slave_wa Or Temp ' Berechnung Slave Write Adresse
Print "EEPROM Schreiben & Lesen - Abbruch mit ESC"
Lcd "EEPROM Schreiben"
Lowerline
Lcd "& Lesen ex. ESC "
T1 = &HA5
Wait 1
Cls
Do
1wreset
1wwrite &HCC
1wwrite &H44
Waitms 250
1wreset
1wwrite &HCC
1wwrite &HBE
B1 = 1wread()
B2 = 1wread()
1wreset
If B2 >= 248 Then
B1 = R - B1
B2 = R - B2
Vorzeichen = "-"
Else
Vorzeichen = "+"
End If
T1 = B1 / 16
T2 = B2 * 16
T1 = T1 + T2
Locate 2 , 1
Lcd "Temp"
Locate 2 , 6
Lcd Vorzeichen ; T1 ;
Locate 2 , 12
Lcd " C "
Incr T1 ' Inkrementieren Datenbyte
Print "Schreibe " ; Vorzeichen ; T1 ; " nach Adresse " ; Word_addr
Locate 1 , 1
Lcd "Schreibe " ; Vorzeichen ; T1 ;
Locate 2 , 1
Lcd "nach "
Locate 2 , 6
Lcd "Adresse " ; Word_addr
I2c_wb Slave_wa , 5 , T1 , ' Byte in EEPROM schreiben
Wait 2
Cls
Temp = I2c_rb(slave_wa , Word_addr) ' Byte aus EEPROM zurücklesen
Print "Lese " ; Temp ; " von Adresse " ; Word_addr ' Ausgabe gelesenes Byte
Locate 1 , 1
Lcd "Lese " ; Temp
Locate 2 , 1
Lcd "von "
Locate 2 , 6
Lcd "Adresse " ; Word_addr
Print ; Vorzeichen ; T1 ; " C "
Wait 1
Temp = Inkey()
Loop Until Temp = Esc
Cls
Print "Ende."
Lcd "ENDE. "
End
Sub I2c_wb(byval Swa As Byte , Byval Wa As Byte , Dta As Byte)
I2cstart ' I2C Write Sequence
I2cwbyte Swa
I2cwbyte Wa
I2cwbyte Dta
I2cstop
Waitms 10 ' Warten auf Ende des Schreibzyklus
End Sub
Function I2c_rb(byval Swa As Byte , Byval Wa As Byte) As Byte
Local T As Byte
Local Sra As Byte
Sra = Swa Or 1
I2cstart ' I2C Read Sequence
I2cwbyte Swa
I2cwbyte Wa
I2cstart
I2cwbyte Sra
I2crbyte T , Nack
I2cstop
I2c_rb = T
End Function
ich hoffe das mir jemand sagen kann wie es funtioniert
MFG Mario