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

PIC18F: i2c only working with Gyro, not with Accelerometer

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



Joined: 22 Nov 2011
Posts: 10

View user's profile Send private message

PIC18F: i2c only working with Gyro, not with Accelerometer
PostPosted: Tue Nov 22, 2011 10:15 am     Reply with quote

Hello!
I'm trying to get my code to work on PIC18F27J53 since weeks :/

I'm using a couple of features like USB, UART1 for bluetooth, UART2 for ANT+, USB-Charger for LiPo Battery. Everything is powered from 3.3V regulator (max 100mA, approx. 20mA current). I have a gyroscope (L3G4200D) and an accelerometer (LIS3DH), both connected through I2C.

L3G4200D manual: http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00265057.pdf
LIS3DH manual: http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00274221.pdf

To verify the functionality of these two sensors, I'm currently just asking the WHOAMI registers of both sensors (both are 0x0F). the gyro is returning 0b11010011. the accelerometer is just returning 0xFF all the time, doest matter which register and if connected only alone without gyro.

I use 10k pullups for each line of the i2c. I also replaced the parts with new ones and tried it on two different pcbs. when i think back, i can remember that the accelerometer worked about a month ago, but i did a lot of changes to my hardware and software. i have a backup of my software when the acc. was working, but now it isn't anymore.

Here are the most important parts of my code (and it is working only for the gyro):

main.c header
Code:

/* ###### HEADER ##### */
#include <18F27J53.h>
#include <pins.h>
#include <string.h>

#fuses WDT, PLL2, PLLEN, STVREN, NOXINST, DEBUG, NOCPUDIV, NOPROTECT, INTRC_PLL, NOCLOCKOUT, SOSC_LOW, NOFCMEN, NOIESO, WDT2048, NODSWDT, NOIOL1WAY, LS48MHZ
#use delay(clock=48MHz)

//UART 1 - Bluetooth
#use rs232(baud=115200, stream=COM_BT, xmit=P_BT_RX, rcv=P_BT_TX, ERRORS, DISABLE_INTS)

//UART 2 - ANT
#use rs232(baud=57600, stream=COM_ANT, xmit=P_ANT_RX, rcv=P_ANT_TX, ERRORS, DISABLE_INTS)

//I2C Accelerometer / Gyro / Flash
#use i2c(master, sda=P_SDA, scl=P_SCL, FORCE_HW) //Tried out with slow, fast, fast=100000, force_sw, result is exactly the same

//PORT-Trisregister automatisch setzen
#use standard_io(ALL)

//I2C Adressen
#define I2C_Acc      0x30
#define I2C_Gyro    0xD0

#define ACC_outX    0x29
#define ACC_outY    0x2B
#define ACC_outZ    0x2D

#define GYRO_outX    0x29
#define GYRO_outY    0x2B
#define GYRO_outZ    0x2D


pins.c part
Code:

#define P_LED_RED         PIN_A0
#define P_LED_GREEN      PIN_A1
#define P_LED_BLUE         PIN_B6 //B6 = PGC

#define P_BUTTON         PIN_C2
#define P_POWER_ON      PIN_C1
#define P_USB_ATTACHED      PIN_A5

#define P_BT_RX         PIN_C6
#define P_BT_TX         PIN_C7
#define P_BT_RESET         PIN_A3

#define P_ANT_SLEEP      PIN_A2
#define P_ANT_RX         PIN_B0
#define P_ANT_TX         PIN_B1

#define P_POWER_ACC      PIN_B3
#define P_POWER_GYRO      PIN_B2
#define P_SCL            PIN_B4
#define P_SDA            PIN_B5


i2c.c driver part
Code:

BYTE read_sensor(BYTE device, BYTE reg)
{   
   BYTE data;
   
   i2c_start();
   i2c_write(device);
   i2c_write(reg);

   i2c_start();
   i2c_write(device+1);
    data=i2c_read(0);
   i2c_stop();

   return(data);
}


most important part of my void main():

Code:

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   setup_oscillator(OSC_PLL_ON, OSC_8MHZ);
   .
   .
   .
   output_high(P_POWER_ACC);
   output_high(P_POWER_GYRO);
   delay_ms(100);
   .
   .
   .
   //Now reading the WHOAMI registers in a loop...

... I also use some interrupts (T0, T1 and T2) for LED PWM, USB-Attach etc.

-------------------------
RESULTS
-------------------------

GYRO
Code:

read_sensor(I2C_Gyro, 0x0F)

...returns 0b11010011 in my printf through USB-RS232


ACCEL.
Code:

read_sensor(I2C_Acc, 0x0F)

...returns 0b11111111 everytime, the same way and doesn't matter which one i read first.


--------------------------
CIRCUIT for the Accel.
--------------------------
...also powered from the PIC, as like as the Gyro.
...pullups both connected to SCL/SDA and to P_POWER_ACC
...circuit in the LIS3DH is bottom view --> mirrored

Code:

PIN   Desc.      Connected to
1   Vdd_IO   P_POWER_ACC
2   NC      -
3   NC      -
4   SCL      P_SCL
5   GND      Ground
6   SDA      P_SDA
7   SA0/SDO   Ground
8   CS      P_POWER_ACC
9   INT2      -
10   RES      Ground
11   INT1      -
12   GND      Ground
13   ADC3      -
14   VDD      P_POWER_ACC
15   ADC2      -
16   ADC1      -

... a 100nF and a 10uF from Vdd to Ground is present, as like as in the datasheet.
... also tried it without P_POWER_ACC and P_POWER_GYRO and direct connection to 3.3V.

I hope you can help me!
Thank you very much!
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 11:13 am     Reply with quote

First comment, you need much lower pull-up resistors.
The resistance needed, depends on bus capacitance, speed, and the voltage being used for the bus. 10K, is a 'high but useable' value for a 5v I2C bus, with a single device connected. For 3.3v operation, is would only allow a bus with about 10pF total capacitance....
The minimum value is only about 800R at 3.3v, for a standard mode driver (and lower if the device supports fast mode plus). Something like 1K2R is a much more likely to work value at 3.3v. With your current resistance value you will be out of spec on the rise times.
I'd start by trying dropping these. It may just be that the accelerometer is fussier on this part of the timings.

Best Wishes
steav



Joined: 22 Nov 2011
Posts: 10

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 4:24 pm     Reply with quote

Thanks for your reply!

I changed the pullups to 1k, but still exactly the same :/
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 22, 2011 5:12 pm     Reply with quote

Run this i2c bus scanner program and see if it finds the LIS3DH chip:
http://www.ccsinfo.com/forum/viewtopic.php?t=42368&start=4
(Modify the program slightly so it works with the 18F27J53).
steav



Joined: 22 Nov 2011
Posts: 10

View user's profile Send private message

PostPosted: Wed Nov 23, 2011 1:17 am     Reply with quote

Thanks for your code...

I got it easily to work and it returns only this ack:
Code:

ACK addr: D0

...which is the Gyro's address
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Wed Nov 23, 2011 3:24 am     Reply with quote

So, realistically, it says there is something wrong with the hardware. When you did your changes 'a month ago', something has gone wrong. I'd be checking that every pin does connect as you think, looking for microscopic breaks in the tracks, whiskers etc. _everything_that you think does connect, is doubtful. If the chip was there, connected, and working OK, it would respond. Check with a meter as well as by eye, and also check for bridges to the adjacent pins. Check ten+ times. With a microscope, by eye, with a meter, and a scope look at the signals at each ends of the wire verifying they are the same, and repeat the process.

Best Wishes
steav



Joined: 22 Nov 2011
Posts: 10

View user's profile Send private message

PostPosted: Wed Nov 23, 2011 7:04 am     Reply with quote

Hm yes it sounds like the hardware is the only possible source of error, but
I ordered some more PCBs and also have a perfboard with these SMT to DIP adapters, for each IC, and the behaviour is everywhere the same.

Should I try another accelerometer, like the LIS331DH or make a new MPLab-project only with necessary code?

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 24, 2011 4:53 pm     Reply with quote

Are you using a factory breakout board for the LIS3DH like this:
http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATA_BRIEF/DM00028742.pdf

Or did you make your own board ?
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