CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

CCS Exercise on I2C

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest_X44
Guest







CCS Exercise on I2C
PostPosted: Sun Feb 01, 2004 10:28 pm     Reply with quote

I’m following the example set out in the CCS Exercise book. P. 17, on using the I2C Temperature Sensor (device is DS1631).

There are a few things I still don’t quite understand. Perhaps, someone here can shed some light.

According to the datasheet, initiating a 2-wire communication requires for the Master (in this case the PIC16F877) to generate a START followed by a CONTROL BYTE – in this case, i2c_write(0x90), then followed by a COMMAND BYTE, which indicates what type of operation is to be performed.

But, looking at the definition inside the init_temp(), in the call to i2c_write(0xee), the parameter 0xee is not among the command protocols in the COMMAND SET listed in the datasheet.

Question: What then is this for if it’s not a command protocol in the COMMAND SET?

Question: What is the temp_config(BYTE data) for, although it makes sense that the temp_config(BYTE data) is called because I recognise that the 12c_write(0xac) corresponds to one of the command protocols in the COMMAND SET. If the temp_config(BYTE data is to configure the device then why is this not called first?



#include <16f877.h>
#device ICD=TRUE
#fuses HS, NOLVP, NOWDT, PUT
#use delay(clock=20000000)
#use rs232(DEBUGGER)

#ifndef DAL_SCL
#define DAL_SCL PIN_B0
#define DAL_SDA PIN_B1
#endif

#define read_temp read_high_temp // for backwards compatability

#use i2c(master,sda=DAL_SDA, scl=DAL_SCL)


void temp_config(BYTE data) {

i2c_start();
i2c_write(0x90);
i2c_write(0xac);
i2c_write(data);
i2c_stop();
}


void init_temp() {
output_high(DAL_SDA);
output_high(DAL_SCL);
i2c_start();
i2c_write(0x90);
i2c_write(0xee);
i2c_stop();
temp_config(8);
}


BYTE read_high_temp() { // Returns degrees F (0-255)
BYTE datah,datal;
signed long data;

i2c_start();
i2c_write(0x90);
i2c_write(0xaa);
i2c_start();
i2c_write(0x91);
datah=i2c_read();
datal=i2c_read(0);
i2c_stop();

data=datah;
data=data*9;
if(bit_test(datal,7))
{
if(data < 0)
data -= 4;
else
data += 4;
}
data=(data/5)+32;

if(data < 0)
data = 0;
else if(data > 255)
data = 255;

return((int)data);
}


signed int read_low_temp() { // Returns degrees F (-67 to 127)
signed int datah, datal;
signed long data;

i2c_start();
i2c_write(0x90);
i2c_write(0xaa);
i2c_start();
i2c_write(0x91);
datah=i2c_read();
datal=i2c_read(0);
i2c_stop();

data=datah;
data=data*9;
if(bit_test(datal,7))
{
if(data < 0)
data -= 4;
else
data += 4;
}
data = (data / 5) + 32;

if(data > 127)
data = 127;

return((int)data);
}


signed long read_full_temp() { // Returns degrees F (-67 to 257)
signed int datah, datal;
signed long data;

i2c_start();
i2c_write(0x90);
i2c_write(0xaa);
i2c_start();
i2c_write(0x91);
datah=i2c_read();
datal=i2c_read(0);
i2c_stop();

data=datah;
data=data*9;
if(bit_test(datal,7))
{
if(data < 0)
data -= 4;
else
data += 4;
}
data = (data / 5) + 32;

return(data);
}




main()
{
byte value;
init_temp();

do{
value = read_temp();
printf("%u\r\n", value);
delay_ms(1000);
}while(TRUE);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 01, 2004 11:57 pm     Reply with quote

The code that you posted, with the 0xEE command, is from ds1621.c,
not ds1631.c. That file is available in the following folder:
c:\Program Files\Picc\Drivers
So there's either a problem in the exercise book, or you misread it.
Guest








PostPosted: Mon Feb 02, 2004 3:43 am     Reply with quote

The circuit in the exercise book shows a ds1621.

Thank you so much, PCM Programmer.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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