|
|
View previous topic :: View next topic |
Author |
Message |
padron70
Joined: 08 Dec 2005 Posts: 7
|
Temperature with two ds1624 slaves |
Posted: Mon Dec 26, 2005 2:49 pm |
|
|
In another post i've been able to solve the problem of the reading of a single ds1624 slave with 16f877 master using I2C, but now i want to read two temperatures with two connected ds1624 as slaves. I have built the circuit with the address A2_A1_A0=000, for the first, and A2_A1_A0=001 for the second. I have programmed the pic with the code show below, but the results are disturbing: the first temperature (T1) is O.K. but the in the second they appear -0.5 and similar (T� real +-20�C)...I have tried to modify the code but the results are seemed.
Code: |
/*Version Boa do programa leer temperatura*/
#include <16f877.h>
#use delay (clock=4000000)
#use I2C(MASTER, SDA=PIN_C4, SCL=PIN_C3, fast)
#use fast_io(A)
#fuses XT, NOPROTECT, NOPUT, NOWDT, NOBROWNOUT, NOLVP, NOCPD
#include <lcd_lolo.c>
#define simbolo_grados 0b11011111
#define simbolo_coma 0b00101100
#define I2CWRITE 0b10010000
#define I2CREAD 0b10010001
#define I2CWRITE2 0b10010010
#define I2CREAD2 0b10010011
#define DS1624_CMD_ACCESSCONFIG 0xAC
#define DS1624_CMD_INITIATECONVERT 0xEE
#define DS1624_CMD_ACCESSTEMPERATURE 0xAA
#define DS1624_CMD_ACCESSMEMORY 0x17
# byte port_a=6
void i2c_conversion()
{
i2c_start();
i2c_write(I2CWRITE);
i2c_write(DS1624_CMD_INITIATECONVERT);
i2c_start();
i2c_write(I2CWRITE2);
i2c_write(DS1624_CMD_INITIATECONVERT);
i2c_stop();
}
void i2c_ds1624_inicia()
{
i2c_start();
i2c_write(I2CWRITE);
i2c_write(DS1624_CMD_ACCESSCONFIG);
i2c_write(0b01001010);
delay_ms(20);
i2c_start();
i2c_write(I2CWRITE2);
i2c_write(DS1624_CMD_ACCESSCONFIG);
i2c_write(0b01001010);
i2c_stop();
delay_ms(20);
i2c_conversion();
}
float i2c_ds1624_leer_temp_c()
{
float dec,valor;
int datah, datal;
i2c_start();
i2c_write(I2CWRITE);
i2c_write(DS1624_CMD_ACCESSTEMPERATURE);
i2c_start();
i2c_write(I2CREAD);
datah=i2c_read();
datal=i2c_read(0);
switch(datal)
{
case 248: dec=0.98; break;
case 240: dec=0.94; break;
case 232: dec=0.90; break;
case 224: dec=0.87; break;
case 216: dec=0.83; break;
case 208: dec=0.80; break;
case 200: dec=0.77; break;
case 192: dec=0.75; break;
case 184: dec=0.72; break;
case 176: dec=0.70; break;
case 168: dec=0.68; break;
case 160: dec=0.66; break;
case 152: dec=0.62; break;
case 144: dec=0.60; break;
case 136: dec=0.58; break;
case 128: dec=0.55; break;
case 120: dec=0.53; break;
case 112: dec=0.50; break;
case 104: dec=0.48; break;
case 96: dec=0.44; break;
case 88: dec=0.40; break;
case 80: dec=0.37; break;
case 72: dec=0.34; break;
case 64: dec=0.30; break;
case 56: dec=0.28; break;
case 48: dec=0.23; break;
case 40: dec=0.20; break;
case 32: dec=0.17; break;
case 24: dec=0.13; break;
case 16: dec=0.10; break;
case 8: dec=0.05; break;
case 0: dec=0.00; break;
}
i2c_stop();
valor=datah+dec;
if (datah>127)
{
valor=valor-256;
}
// i2c_ds1624_inicia();
return (valor);
}
float i2c_ds1624_leer_temp_c2()
{
float dec,valor;
int datah, datal;
i2c_start();
i2c_write(I2CWRITE2);
i2c_write(DS1624_CMD_ACCESSTEMPERATURE);
i2c_start();
i2c_write(I2CREAD2);
datah=i2c_read();
datal=i2c_read(0);
switch(datal)
{
case 248: dec=0.98; break;
case 240: dec=0.94; break;
case 232: dec=0.90; break;
case 224: dec=0.87; break;
case 216: dec=0.83; break;
case 208: dec=0.80; break;
case 200: dec=0.77; break;
case 192: dec=0.75; break;
case 184: dec=0.72; break;
case 176: dec=0.70; break;
case 168: dec=0.68; break;
case 160: dec=0.66; break;
case 152: dec=0.62; break;
case 144: dec=0.60; break;
case 136: dec=0.58; break;
case 128: dec=0.55; break;
case 120: dec=0.53; break;
case 112: dec=0.50; break;
case 104: dec=0.48; break;
case 96: dec=0.44; break;
case 88: dec=0.40; break;
case 80: dec=0.37; break;
case 72: dec=0.34; break;
case 64: dec=0.30; break;
case 56: dec=0.28; break;
case 48: dec=0.23; break;
case 40: dec=0.20; break;
case 32: dec=0.17; break;
case 24: dec=0.13; break;
case 16: dec=0.10; break;
case 8: dec=0.05; break;
case 0: dec=0.00; break;
}
i2c_stop();
valor=datah+dec;
if (datah>127)
{
valor=valor-256;
}
// i2c_ds1624_inicia();
return (valor);
}
int read_ext_eeprom(int address)
{
int data;
i2c_start();
i2c_write(0x90);
i2c_write(0x17);
i2c_write(address);
i2c_start();
i2c_write(0x91);
data=i2c_read();
i2c_stop();
return data;
}
void write_ext_eeprom(int address, int data)
{
i2c_start();
i2c_write(0x90);
i2c_write(0x17);
i2c_write(address);
i2c_write(data);
i2c_stop();
delay_ms(50);
}
void main (void)
{
float tempe,tempe2;
int contador;
lcd_inicia();
i2c_ds1624_inicia();
set_tris_a(0b111110);
lcd_ponc("\f");
port_a=0;
while(1)
{
tempe=i2c_ds1624_leer_temp_c();
tempe2=i2c_ds1624_leer_temp_c2();
i2c_ds1624_inicia();
delay_ms(1000);
lcd_ponc("\f");
lcd_vaixy(1,1);
printf(lcd_ponc,"T1= %4.2f%cC",tempe,simbolo_grados);
lcd_vaixy(1,2);
printf(lcd_ponc,"T2= %4.2f%cC",tempe2,simbolo_grados);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 26, 2005 3:24 pm |
|
|
In each of these two functions, you are missing the i2c_stop() statement
after the first i2c transaction:
void i2c_conversion()
void i2c_ds1624_inicia()
Each transaction must be complete, with a start and a stop statement. |
|
|
padron70
Joined: 08 Dec 2005 Posts: 7
|
|
Posted: Mon Dec 26, 2005 4:04 pm |
|
|
I think that it's not necessary when having a single master.... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
Posted: Wed Jan 04, 2006 7:10 am |
|
|
why do you have two nearly identical float i2c_ds1624_leer_temp_c() functions? and the initialization function can be refactored as well.
why not pass the address of the device to just one function, so that you can debug your code only once?
from
http://www.ccsinfo.com/forum/viewtopic.php?t=19526
Code: | int i2c_ds1624_read_temp_c(int device_addr) { ... } |
and
Code: | i2c_ds1624_init(int device_addr) { ... } |
ps
i agree with PCM above; make your i2c bus transactions atomic. start/stop for each device.
jds-pic |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|