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

i2c problem with CAP1298

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



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

i2c problem with CAP1298
PostPosted: Tue Mar 31, 2015 7:01 am     Reply with quote

Hello,
What I have:
- Compiler 4.119
- PIC18F87J50
- CAP1298
I have the pin 65 SDA connectect to the pin 2 SMDATA, with pull-up,
pin 64 SCL connected to the pin 3 SMCLK olso with pull-up.
What I want to do at this moment is to see that I have a connection to the CAP1298 chip.
I send the address and the register address what I want to read back and read the data. I get alway 255.. included mine code.. where o where am I missing the point??

main.c:
Code:

#include <main.h>
#include <CAP1298.h>

//    _ __ ___   __ _(_)_ __    / / \ \
//   | '_ ` _ \ / _` | | '_ \  | |   | |
//   | | | | | | (_| | | | | | | |   | |
//   |_| |_| |_|\__,_|_|_| |_| | |   | |
//                              \_\ /_/
void main()
{
   setup_adc_ports(sAN15);
   setup_timer_4(T4_DISABLED,0,1);
   while(1)
   {
      CAP_init();
   }
}


main.h:
Code:

#include <18F87J50.h>
#device ICD=TRUE
#device adc=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(clock=40000000)
#use i2c(Master,sda=PIN_D5,scl=PIN_D6,I2C2,SMBUS,FORCE_HW)
#define CAPxxxx_ADDRESS     0b01010000
#define CAPxxxx_ID          0x71


CAP1298.h:
Code:

void CAP_init(void)
{
    int16 value   = 0;

   i2c_start();
   i2c_write(CAPxxxx_ADDRESS);
   i2c_write(0xFD); // addres waar device id moet staan
delay_us(10);
   value  =i2c_read(); 
   i2c_stop();
delay_us(1);
}
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Tue Mar 31, 2015 7:48 am     Reply with quote

Table 3.3 shows you what to do.
You need to restart after you send the register address, then read.
_________________
David
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue Mar 31, 2015 7:55 am     Reply with quote

You are not handling the I2C right.

The device address is 0101 000R The 'R' is '1' for reading, and '0' for writing. When you talk to an I2C device the sequence is:

For write:

start
send write address
send register address
send data
send stop

For read:
start
send write address
send register address
send second start (restart)
send read address
read data (nack the last byte)
send stop

Table 3-6 in the data sheet.

I see Drh pointed the same thing out while I was typing. Smile
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 31, 2015 8:09 am     Reply with quote

Thanks for the info!!!
I am still getting nothing.... (I am still not doing it right)

I have tried:
Code:

void CAP_init(void)
{
    int16 value   = 0;

    //-----------------------------------------
    // Verify the CAPxxxx's Device ID
    //-----------------------------------------
   i2c_start();
   i2c_write(0b0101000);   
   i2c_write(0xFD);         // addres waar device id moet staan

   i2c_start();

   i2c_write(0b0101001);
   value = i2c_read();
   i2c_stop();

   delay_us(1);
}
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 31, 2015 8:40 am     Reply with quote

Hi I have made some progress!!!!
After programming it works!!!!
only once....
If I asked the value again it get stuck in the i2c_start() routine..
Maybe a reset or so??
Code:

#define CAP_ADDRESS_write   0b01010000
#define CAP_ADDRESS_read   0b01010001

Code:

void CAP_init(void)
{
    int16 value   = 0;
   i2c_start();
   i2c_write(CAP_ADDRESS_write);   
   i2c_write(0xFD);         // addres waar device id moet staan
   i2c_start();
   i2c_write(CAP_ADDRESS_read);
   value = i2c_read();
   i2c_stop();
   delay_us(1);
}



Regards,
Jody
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 31, 2015 12:33 pm     Reply with quote

Study the read code that Ttelmah posted. Compare it to your code.
Make sure you read about the function parameters for i2c_read().
Quote:
For read:
start
send write address
send register address
send second start (restart)
send read address
read data (nack the last byte)
send stop
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 31, 2015 3:20 pm     Reply with quote

hmm... i2C device...

What are the I2C bus pullup resistor values ?

Jay
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 31, 2015 3:33 pm     Reply with quote

Yes Ttelmah and Drh you where right.....
Code:

   i2c_start();
   i2c_write(CAP_ADDRESS_write);   
   i2c_write(0xFD);         // addres waar device id moet staan
   i2c_start();
   i2c_write(CAP_ADDRESS_read);
   value = i2c_read();
   i2c_write(CAP_ADDRESS_read);
   value1 = i2c_read();
   i2c_stop();

Did solved I has to read more accurate.. it was all there...
Thanks for the assistance!! It pointed me in the right direction!!

Pull-ups are 4k7.

Regards,
Jody
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 31, 2015 11:02 pm     Reply with quote

Jody wrote:

i2c_start();
i2c_write(CAP_ADDRESS_write);
i2c_write(0xFD); // addres waar device id moet staan
i2c_start();
i2c_write(CAP_ADDRESS_read);
value = i2c_read();
i2c_write(CAP_ADDRESS_read);
value1 = i2c_read();
i2c_stop();

The CAP1298 data sheet doesn't say to do it that way. Look at Table 3.6
(on page 17): http://ww1.microchip.com/downloads/en/DeviceDoc/00001571A.pdf
Quote:
Table 3.6 Block Read Protocol

The data sheet says you should do it this way, when reading a block
of bytes (ie., two or more bytes):
Code:

i2c_start();
i2c_write(CAP_ADDRESS_write);   
i2c_write(0xFD);        // Register address for Product ID
i2c_start();
i2c_write(CAP_ADDRESS_read);
value = i2c_read();
value1 = i2c_read(0);   // Must do a NACK on the last read
i2c_stop();
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Wed Apr 01, 2015 12:49 am     Reply with quote

Yes you are right!!
I tested it and it works!!
Again: I have to read the datasheet more accurate!

Thanks for the assistance!!!

Regards,
Jody
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