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 support@ccsinfo.com

i2c problem

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



Joined: 16 Feb 2011
Posts: 42

View user's profile Send private message

i2c problem
PostPosted: Sun Sep 11, 2011 1:30 am     Reply with quote

Dear Sir.
I am a newbie.
I posted this thread last month.
http://www.ccsinfo.com/forum/viewtopic.php?t=45691&start=0
With helping of Ttelmah I comunicated with TSL2561T light digital sensor, read true values of ID, Time registers. But can not read true values of C,D,E,F registers.

Following TSL2561 datasheet.
"
The ADC channel data are expressed as 16-bit values spread across two registers. The ADC channel 0 data
registers, DATA0LOW and DATA0HIGH provide the lower and upper bytes, respectively, of the ADC value of
channel 0. Registers DATA1LOW and DATA1HIGH provide the lower and upper bytes, respectively, of the ADC
value of channel 1. All channel data registers are read-only and default to 00h on power up

REGISTER ADDRESS BITS DESCRIPTION
DATA0LOW Ch 7:0 ADC channel 0 lower byte
DATA0HIGH Dh 7:0 ADC channel 0 upper byte
DATA1LOW Eh 7:0 ADC channel 1 lower byte
DATA1HIGH Fh 7:0 ADC channel 1 upper byte
"

CCS compiler V4.018

I can not read the value of Ch,Dh,Eh,Fh registers at the same time. For example in the following code the values of Dh, Fh registers changed with lux. The values of Ch, Eh register is alway FF.


Code:

#include <16F877A.h>
#use delay(clock=8000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#include "tsl2561t.c"
#include "LCD_TM.c"


void main(void)
{   
BYTE C0;
BYTE C1;
BYTE C2;
BYTE C3;
lcd_init();
Init_TSL2561();
Write_TSL2561(0x80,0x03); //Power up
//P=Read_TSL2561(0x80); 
delay_ms(40);

while(true)
{
 
C0=Read_TSL2561(0x8C);  //ADC channel 0 lower byte
lcd_gotoxy(1,1);
printf(lcd_putc,"%02X",C0);
C1=Read_TSL2561(0x8D);  //ADC channel 0 higher byte
lcd_gotoxy(4,1);
printf(lcd_putc,"%02X",C1);

C2=Read_TSL2561(0x8E);  //ADC channel 1 lower byte
lcd_gotoxy(1,2);
printf(lcd_putc,"%02X",C2);

C3=Read_TSL2561(0x8F);  //ADC channel 1 higher byte
lcd_gotoxy(4,2);
printf(lcd_putc,"%02X",C3);

delay_ms(500);
 
}
 }


tsl2561.c file

Code:
#define TSL2561_SDA  PIN_C4
#define TSL2561_SCL  PIN_C3
#use i2c(master,low, sda=TSL2561_SDA, scl=TSL2561_SCL)
#include <math.h>

void Init_TSL2561()
{
   output_float(TSL2561_SCL);
   output_float(TSL2561_SDA);
}

void Write_TSL2561(byte address, BYTE data)
{
   short int status;
   i2c_start();
   i2c_write(0x52);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   i2c_start();
   status=i2c_write(0x52);
   while(status==1)
   {
      i2c_start();
      status=i2c_write(0x52);
   }
}
//==========================
// read data one byte from DS1307
//==========================
BYTE Read_TSL2561(byte address)
{
   BYTE data;
   i2c_start();
   i2c_write(0x52);
   i2c_write(address);
   i2c_start();
   i2c_write(0x53); //can not write data to TSL2561
   //delay_ms(40);
   data=i2c_read();
   i2c_stop();
   return(data);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 11, 2011 1:14 pm     Reply with quote

Quote:

I can not read the value of Ch,Dh,Eh,Fh registers at the same time. For
example in the following code the values of Dh, Fh registers changed with
lux. The values of Ch, Eh register is alway FF.

//==========================
// read data one byte from DS1307
//==========================
BYTE Read_TSL2561(byte address)
{
BYTE data;
i2c_start();
i2c_write(0x52);
i2c_write(address);
i2c_start();
i2c_write(0x53); //can not write data to TSL2561
//delay_ms(40);
data=i2c_read();
i2c_stop();
return(data);
}

1. The comment is incorrect. You're not reading a DS1307.

2. The last i2c_read() must do a NACK. In CCS this is done by using
a parameter of 0x00. Example:
Code:

data=i2c_read(0);


They show the NACK in the TSL2561 data sheet in Figures 13 and 15 on
page 12, but they show it in a very poor way. In the little box that says
"A" at the end of the last data byte read, they put a "1" below the box.
I would do it a better way. I would put a "N" in the box to show that a
NACK is required.
ntrungtruc2003



Joined: 16 Feb 2011
Posts: 42

View user's profile Send private message

PostPosted: Tue Sep 13, 2011 9:49 pm     Reply with quote

Thanks you so much. I am very happy, it works smoothly.
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