View previous topic :: View next topic |
Author |
Message |
Jody
Joined: 08 Sep 2006 Posts: 182
|
i2c problem with CAP1298 |
Posted: Tue Mar 31, 2015 7:01 am |
|
|
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
|
|
Posted: Tue Mar 31, 2015 7:48 am |
|
|
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
|
|
Posted: Tue Mar 31, 2015 7:55 am |
|
|
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. |
|
|
Jody
Joined: 08 Sep 2006 Posts: 182
|
|
Posted: Tue Mar 31, 2015 8:09 am |
|
|
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
|
|
Posted: Tue Mar 31, 2015 8:40 am |
|
|
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
|
|
Posted: Tue Mar 31, 2015 12:33 pm |
|
|
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
|
|
Posted: Tue Mar 31, 2015 3:20 pm |
|
|
hmm... i2C device...
What are the I2C bus pullup resistor values ?
Jay |
|
|
Jody
Joined: 08 Sep 2006 Posts: 182
|
|
Posted: Tue Mar 31, 2015 3:33 pm |
|
|
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
|
|
Posted: Tue Mar 31, 2015 11:02 pm |
|
|
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
|
|
Posted: Wed Apr 01, 2015 12:49 am |
|
|
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 |
|
|
|