![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
steav
Joined: 22 Nov 2011 Posts: 10
|
PIC18F: i2c only working with Gyro, not with Accelerometer |
Posted: Tue Nov 22, 2011 10:15 am |
|
|
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! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19386
|
|
Posted: Tue Nov 22, 2011 11:13 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
steav
Joined: 22 Nov 2011 Posts: 10
|
|
Posted: Tue Nov 22, 2011 4:24 pm |
|
|
Thanks for your reply!
I changed the pullups to 1k, but still exactly the same :/ |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
steav
Joined: 22 Nov 2011 Posts: 10
|
|
Posted: Wed Nov 23, 2011 1:17 am |
|
|
Thanks for your code...
I got it easily to work and it returns only this ack:
...which is the Gyro's address |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19386
|
|
Posted: Wed Nov 23, 2011 3:24 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
steav
Joined: 22 Nov 2011 Posts: 10
|
|
Posted: Wed Nov 23, 2011 7:04 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|