Accelermoter c code SPI/I2C

Thread Starter

john_doe01

Joined Nov 27, 2011
24
Hi,
Im a third year student working on my final year project, a two wheeled balancing robot.

Im using an LIS3LV02DL 3-Axis accelerometer to gather the acceleration of the bot

but i have no experience in I2C or SPI programming and have no idea where to startim using a PIC18F452 and programming in C

Any help will be appreciated, Thanks
 

hgmjr

Joined Jan 28, 2005
9,029
Hi,
Im a third year student working on my final year project, a two wheeled balancing robot.

Im using an LIS3LV02DL 3-Axis accelerometer to gather the acceleration of the bot

but i have no experience in I2C or SPI programming and have no idea where to startim using a PIC18F452 and programming in C

Any help will be appreciated, Thanks
Are you also including a sensor that will provide you with the tilt information. If not I think you are going to need to consider including one. If all you have is acceleration information then you will need to do some intense math to get from acceleration to positional information. With a gyroscope you can get tilt information directly and instantly.

Here is an IMU that though it is a little pricey will supply your robot with the feedback it needs to know when it is in the proper upright position. It too uses an I2C or SPI interface.

http://www.parallax.com/portals/0/downloads/docs/prod/sens/27911-GyroscopeL3G4200D-v1.0.pdf



hgmjr
 
Last edited:
That device has the I2C/SPI controllers. It's really just a matter of doing the register reads/writes and filling the RX/TX buffers with data.

The datasheet will be the most helpful.
 

ErnieM

Joined Apr 24, 2011
8,040
To start, Microchip states the PIC18F452 is "Not Recommended for new designs" and has been superseded by the PIC18F4520.

Both are good choices as the Microchip C18 compiler is both excellent and free.

You'll need a programmer and debugger, something like the PICkit II or III will do nicely. They come bundled with some decent development boards but you probably will not find your specific PIC chip there. Still, if you go with a PIC18 type most of your code will still be good. (more info)

AFAIK an accelerometer should be enough to sense the position angle. You can get one from Sparkfun on a breakout board (easier to solder then the bare IC).
 

Thread Starter

john_doe01

Joined Nov 27, 2011
24
hgmjr
Im already using a gyroscope to get the tilt information (LISY300AL) and the PIC is already correctly getting the correct value from it.
so far i have sorted out the PWM to the two motors and a H-bridge driver that will drive the motors, chassis has also been built.
next step is to get the accelerometer information in to the PIC, combine the two values (Gyro and Acc) via a Kalman Filter and then begin programming the PID loop

ErnieM:
i would prefer to use a different PIC but the specification requires the use of an 18F452 so ill have to stick to that

would you recommend I2C over SPI?
im competent at programming i just lack experience with either

thanks again
 

ErnieM

Joined Apr 24, 2011
8,040
ErnieM:
would you recommend I2C over SPI?
im competent at programming i just lack experience with either
I2C needs 2 I/O pins to drive up to 127 devices. SPI needs 3 I/O's plus 1 for each device it drives, and it has the speed advantage. I personally prefer I2C but don't have a rational reason beyond pin count.

AFAIK the library support in C18 is for software versions of both interfaces, meaning the code does not use the MSSP module. To use that module you just have to set the control registers and go. Taking a peek at the library helpfile isn't a bad idea as it outlines the steps you need take to perform a read or write operation.

While I like I2C for the simple circuitry I always seen to stumble over organizing the code to complete a transfer between start & write & restart & read & stop signals.

I've not done low level SPI transfers (my SD cards use it but they work thru a library) but the protocol seems much simpler due to the address information being contained on the chip select CS line.

Of the two, SPI may be the simpler protocol for you to use.
 

Thread Starter

john_doe01

Joined Nov 27, 2011
24
Thank you all for your help

decided to use I2C to collect the accelerometer readings and take a bit banging approach to the coding
 
Top