PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : C-Control I 1.1 und mehrere DS1621 am I2C



cni
13.03.2007, 23:49
Hallo,

ich habe mehrere DS1621 am I2C-Bus, besser gesagt erst mal 2!
Der eine ist Adressiert auf &H90 und der andere auf &H91.

Nun habe ich mir aus den Sample Basic Programmen etwas zusammen gecodet, das aber anscheinend nicht funktioniert, sobald ich den 2. DS1621 auslesen will!

Hier mal der Code:


'**************** I/O PORTS ****************'
define sda port[7]
define scl port[8]
'*******************************************'
'**************** VARIABLES ****************'
define control_flags byte[1]
define i2c_nack bit[1]
define i2c_last bit[2]
define i2c_nack_count byte[2]
define i2c_byte byte[3]
define i2c_out_bit bit[24]
define i2c_in_bit bit[17]
define loop byte[4]

define temp_1 word
define temp_2 word
'*******************************************'
' *** Daten-Definition ***
define lcd_buf byte
define lcd_param byte

' *** Definition LCD-Ports ***
define lcd_port byteport[2]
define lcd_rs port[14]
define lcd_rw port[13]
define lcd_e port[15]

'*** ASCII-Codes ***
define A_ &H41
define B_ &H42
define C_ &H43
define D_ &H44
define E_ &H45
define F_ &H46
define G_ &H47
define H_ &H48
define I_ &H49
define J_ &H4A
define K_ &H4B
define L_ &H4C
define M_ &H4D
define N_ &H4E
define O_ &H4F
define P_ &H50
define Q_ &H51
define R_ &H52
define S_ &H53
define T_ &H54
define U_ &H55
define V_ &H56
define W_ &H57
define X_ &H58
define Y_ &H59
define Z_ &H5A

define LEERZ &H20
define MINUS &H2D
define NULL &H30
define GLEICH &H3D
define DOPPELPUNKT &H3A
define KOMMA &H2C
define GRAD &HDF
'**************** CONSTANTS ****************'
define ds1621_1_address &H90
define ds1621_2_address &H91
'*******************************************'
'****************** SETUP ******************'
gosub LCD_INIT
pause 10
'*******************************************'
'***************** PROGRAM *****************'
#start
gosub get_ds1621_temp1
'gosub get_ds1621_temp2
temp_2 = temp_1 + 10
if i2c_nack then goto start

gosub LCD_CLS
lcd_param = T_ : gosub LCD_WRITECHAR
lcd_param = E_ : gosub LCD_WRITECHAR
lcd_param = M_ : gosub LCD_WRITECHAR
lcd_param = P_ : gosub LCD_WRITECHAR
lcd_param = NULL + 1 : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = GLEICH : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
if temp_1 < 0 then lcd_param = MINUS : gosub LCD_WRITECHAR
lcd_param = NULL + abs(temp_1/10/10) : gosub LCD_WRITECHAR
lcd_param = NULL + abs((temp_1-temp_1/100*100)/10) : gosub LCD_WRITECHAR
lcd_param = KOMMA : gosub LCD_WRITECHAR
lcd_param = NULL + abs(temp_1-temp_1/10*10) : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = GRAD : gosub LCD_WRITECHAR
lcd_param = C_ : gosub LCD_WRITECHAR
lcd_param = 2 : gosub LCD_GOTOLINE
lcd_param = T_ : gosub LCD_WRITECHAR
lcd_param = E_ : gosub LCD_WRITECHAR
lcd_param = M_ : gosub LCD_WRITECHAR
lcd_param = P_ : gosub LCD_WRITECHAR
lcd_param = NULL + 2 : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = GLEICH : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
if temp_2 < 0 then lcd_param = MINUS : gosub LCD_WRITECHAR
lcd_param = NULL + abs(temp_2/10/10) : gosub LCD_WRITECHAR
lcd_param = NULL + abs((temp_2-temp_2/100*100)/10) : gosub LCD_WRITECHAR
lcd_param = KOMMA : gosub LCD_WRITECHAR
lcd_param = NULL + abs(temp_2-temp_2/10*10) : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = GRAD : gosub LCD_WRITECHAR
lcd_param = C_ : gosub LCD_WRITECHAR

print"****************"
print"Temp1 = "
if temp_1 < 0 then print"-";
print temp_1
print abs(temp_1/10);",";abs(temp_1-temp_1/10*10);"ºC"
print"****************"
print"Temp2 = "
if temp_2 < 0 then print"-";
print temp_2
print abs(temp_2/10);",";abs(temp_2-temp_2/10*10);"ºC"
print"****************"
pause 200
goto start
'*******************************************'
'*************** SUBROUTINES ***************'
#get_ds1621_temp1
#ds1621_1_start_convert
gosub start_i2c
i2c_byte = ds1621_1_address : gosub write_byte_i2c
i2c_byte = &hee : gosub write_byte_i2c
gosub stop_i2c
if i2c_nack_count > 6 then goto no_ack_from_ds1621_1
if i2c_nack then goto ds1621_1_start_convert
#ds1621_1_check_conversion_done
gosub start_i2c
i2c_byte = ds1621_1_address : gosub write_byte_i2c
i2c_byte = &hac : gosub write_byte_i2c
gosub stop_i2c
if i2c_nack then goto ds1621_1_pass_check_conversion_done
gosub start_i2c
i2c_byte = ds1621_1_address+1 : gosub write_byte_i2c
gosub read_last_byte_i2c
gosub stop_i2c
#ds1621_1_pass_check_conversion_done
if i2c_nack_count > 9 then goto no_ack_from_ds1621_1
if i2c_nack then goto ds1621_1_check_conversion_done
if (i2c_byte and 1) = 1 then if (i2c_byte and 128) = 0 then goto ds1621_1_check_conversion_done
#ds1621_1_read_temp
gosub start_i2c
i2c_byte = ds1621_1_address : gosub write_byte_i2c
i2c_byte = &haa : gosub write_byte_i2c
gosub stop_i2c
if i2c_nack then goto pass_ds1621_1_read_temp
gosub start_i2c
i2c_byte = ds1621_1_address+1 : gosub write_byte_i2c
gosub read_byte_i2c : if i2c_byte > 127 then temp_1 = (&hff00 + i2c_byte) * 10 else temp_1 = i2c_byte * 10
gosub read_last_byte_i2c : if (i2c_byte and 128) = 128 then temp_1 = temp_1 + 5
gosub stop_i2c
#pass_ds1621_1_read_temp
if i2c_nack_count > 9 then goto no_ack_from_ds1621_1
if i2c_nack then goto ds1621_1_read_temp
if temp_1 = - 600 then goto ds1621_1_start_convert
return

#get_ds1621_temp2
#ds1621_2_start_convert
gosub start_i2c
i2c_byte = ds1621_2_address : gosub write_byte_i2c
i2c_byte = &hee : gosub write_byte_i2c
gosub stop_i2c
if i2c_nack_count > 6 then goto no_ack_from_ds1621_2
if i2c_nack then goto ds1621_2_start_convert
#ds1621_2_check_conversion_done
gosub start_i2c
i2c_byte = ds1621_2_address : gosub write_byte_i2c
i2c_byte = &hac : gosub write_byte_i2c
gosub stop_i2c
if i2c_nack then goto ds1621_2_pass_check_conversion_done
gosub start_i2c
i2c_byte = ds1621_2_address+1 : gosub write_byte_i2c
gosub read_last_byte_i2c
gosub stop_i2c
#ds1621_2_pass_check_conversion_done
if i2c_nack_count > 9 then goto no_ack_from_ds1621_2
if i2c_nack then goto ds1621_2_check_conversion_done
if (i2c_byte and 1) = 1 then if (i2c_byte and 128) = 0 then goto ds1621_2_check_conversion_done
#ds1621_2_read_temp
gosub start_i2c
i2c_byte = ds1621_2_address : gosub write_byte_i2c
i2c_byte = &haa : gosub write_byte_i2c
gosub stop_i2c
if i2c_nack then goto pass_ds1621_1_read_temp
gosub start_i2c
i2c_byte = ds1621_2_address+1 : gosub write_byte_i2c
gosub read_byte_i2c : if i2c_byte > 127 then temp_2 = (&hff00 + i2c_byte) * 10 else temp_2 = i2c_byte * 10
gosub read_last_byte_i2c : if (i2c_byte and 128) = 128 then temp_2 = temp_2 + 5
gosub stop_i2c
#pass_ds1621_2_read_temp
if i2c_nack_count > 9 then goto no_ack_from_ds1621_2
if i2c_nack then goto ds1621_2_read_temp
if temp_2 = - 600 then goto ds1621_2_start_convert
return

#start_i2c
sda = 0 : scl = 0
return

#stop_i2c
sda = 0 : deact scl : deact sda
return

#write_byte_i2c
for loop = 1 to 8
sda = i2c_out_bit
deact scl
#write_byte_i2c_clock_stretch
if not scl then goto write_byte_i2c_clock_stretch
scl = 0
i2c_byte = i2c_byte shl 1
next
deact sda
deact scl
#write_byte_i2c_ack_clock_stretch
if not scl then goto write_byte_i2c_ack_clock_stretch
i2c_nack = sda
scl = 0
if i2c_nack then goto i2c_error
i2c_nack_count = 0
return
#i2c_error
i2c_nack_count = i2c_nack_count + 1
goto stop_i2c

#read_byte_i2c
i2c_last = 0
#get_i2c_byte
deact sda
for loop = 1 to 8
deact scl
#read_byte_i2c_clock_stretch
if not scl then goto read_byte_i2c_clock_stretch
i2c_in_bit = sda
scl = 0
if loop < 8 then i2c_byte = i2c_byte shl 1
next
if i2c_last then deact sda else sda = 0
deact scl
#read_byte_i2c_ack_clock_stretch
if not scl then goto read_byte_i2c_ack_clock_stretch
scl = 0
return

#read_last_byte_i2c
i2c_last = 1
goto get_i2c_byte
'*******************************************'
'******* INITIALIZATION SUBROUTINES ********'

'*******************************************'
'****************** DATA *******************'

'*******************************************'
'************* ERROR MESSAGES **************'
#no_ack_from_ds1621_1
gosub LCD_CLS
lcd_param = I_ : gosub LCD_WRITECHAR
lcd_param = NULL + 2 : gosub LCD_WRITECHAR
lcd_param = C_ : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = E_ : gosub LCD_WRITECHAR
lcd_param = R_ : gosub LCD_WRITECHAR
lcd_param = R_ : gosub LCD_WRITECHAR
lcd_param = O_ : gosub LCD_WRITECHAR
lcd_param = R_ : gosub LCD_WRITECHAR
lcd_param = 2 : gosub LCD_GOTOLINE
lcd_param = N_ : gosub LCD_WRITECHAR
lcd_param = O_ : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = C_ : gosub LCD_WRITECHAR
lcd_param = H_ : gosub LCD_WRITECHAR
lcd_param = I_ : gosub LCD_WRITECHAR
lcd_param = P_ : gosub LCD_WRITECHAR
lcd_param = NULL + 1 : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = F_ : gosub LCD_WRITECHAR
lcd_param = O_ : gosub LCD_WRITECHAR
lcd_param = U_ : gosub LCD_WRITECHAR
lcd_param = N_ : gosub LCD_WRITECHAR
lcd_param = D_ : gosub LCD_WRITECHAR

print"*******************"
print"* E-I2C ERROR: *"
print"* Chip1 not found *"
print"*******************"
pause 300
return

#no_ack_from_ds1621_2
gosub LCD_CLS
lcd_param = I_ : gosub LCD_WRITECHAR
lcd_param = NULL + 2 : gosub LCD_WRITECHAR
lcd_param = C_ : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = E_ : gosub LCD_WRITECHAR
lcd_param = R_ : gosub LCD_WRITECHAR
lcd_param = R_ : gosub LCD_WRITECHAR
lcd_param = O_ : gosub LCD_WRITECHAR
lcd_param = R_ : gosub LCD_WRITECHAR
lcd_param = 2 : gosub LCD_GOTOLINE
lcd_param = N_ : gosub LCD_WRITECHAR
lcd_param = O_ : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = C_ : gosub LCD_WRITECHAR
lcd_param = H_ : gosub LCD_WRITECHAR
lcd_param = I_ : gosub LCD_WRITECHAR
lcd_param = P_ : gosub LCD_WRITECHAR
lcd_param = NULL + 2 : gosub LCD_WRITECHAR
lcd_param = LEERZ : gosub LCD_WRITECHAR
lcd_param = F_ : gosub LCD_WRITECHAR
lcd_param = O_ : gosub LCD_WRITECHAR
lcd_param = U_ : gosub LCD_WRITECHAR
lcd_param = N_ : gosub LCD_WRITECHAR
lcd_param = D_ : gosub LCD_WRITECHAR

print"*******************"
print"* E-I2C ERROR: *"
print"* Chip2 not found *"
print"*******************"
pause 300
return
'*******************************************'

'LCD Subroutinen
'*** LCD_Interface ***
'( muss in jedes Programm mit LCD-Ausgabe eingefuegt werden)

#LCD_INIT

' alle ports 0
lcd_port = OFF

' 8-Bit-Modus aktivieren
lcd_param=&H38 : gosub LCD_WRITECMD

' mit 8-Bit-Command in 4-Bit-Modus umschalten
lcd_port=&B00000010
tog lcd_e
tog lcd_e

' ab jetzt 4-Bit-Modus
lcd_param = &H28 : gosub LCD_WRITECMD
lcd_param = &H0C : gosub LCD_WRITECMD

' Display loeschen
#LCD_CLS
lcd_param = &H02 : gosub LCD_WRITECMD
lcd_param = &H01 : gosub LCD_WRITECMD
return

' Zeilenwechsel
#LCD_GOTOLINE
if lcd_param = 1 then lcd_param = &H80
if lcd_param = 2 then lcd_param = &HC0
goto LCD_WRITECMD

' LCD-Kommando
#LCD_WRITECMD
lcd_buf = OFF
goto LCD_WRITE

' Zeichenausgabe
#LCD_WRITECHAR
lcd_buf = &B00100000

' Kommando oder Zeichen an Display senden
#LCD_WRITE
lcd_port = lcd_buf or (lcd_param shr 4) ' Hi-Nibble
tog lcd_e
tog lcd_e
lcd_port = lcd_buf or (lcd_param and &H0F) ' Lo-Nibble
tog lcd_e
tog lcd_e
return


Kann mir dabei jemand helfen, evtl. habe ich ja noch einen Fehler im Programm?

Und kann man das genze evtl. kürzer und vereinfachter schreiben?

Danke

Drifter2006
19.03.2007, 16:53
Hello,
It looks like you are trying to adapt one of my example programs to work with 2 ds1621 chips.

First thing to look at is if your hardware has been addressed correctly.

To address the first ds1621 connect pin 7 (A0), pin 6 (A1) and pin 5 (A2) to GND.
This will address the first ds1621 to hex 90.
Hex 90 is the WRITE address of the first ds1621 and the READ address is the WRITE address + 1 (hex 91)
WRITE address is hex 90.
READ address is hex 91.


To address the 2nd ds1621 connect pin 7 (A0) to +5V, pin 6 (A1) to GND and pin 5 (A2) to GND.
This will address the 2nd ds1621 to hex 92.
Hex 92 is now the WRITE address of the 2nd ds1621 and hex 93 is now the READ address.

Also, did you remember to put pull-ups on the SDA and SCL lines?
About 4k7 should been fine.

And now its up to you to look through your software.
I see a small error under **constants**.

Greetings,
Drifter2006

cni
19.03.2007, 23:46
Super, Danke

Hab den Fehler gefunden, Adresse war falsch:
define ds1621_2_address &H92

Und schon funktioniert es! =D>