Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

lis3l02.c

Go to the documentation of this file.
00001 /*! \file lis3l02.c \brief ST LIS3L02 3-axis I2C Accelerometer Library. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'lis3l02.c'
00005 // Title        : ST LIS3L02 3-axis I2C Accelerometer Library
00006 // Author       : Pascal Stang - Copyright (C) 2004
00007 // Created      : 2004.10.23
00008 // Revised      : 2004.12.14
00009 // Version      : 0.1
00010 // Target MCU   : Atmel AVR Series
00011 // Editor Tabs  : 4
00012 //
00013 // NOTE: This code is currently below version 1.0, and therefore is considered
00014 // to be lacking in some functionality or documentation, or may not be fully
00015 // tested.  Nonetheless, you can expect most functions to work.
00016 //
00017 // This code is distributed under the GNU Public License
00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00019 //
00020 //*****************************************************************************
00021 
00022 #include <avr/io.h>
00023 #include <avr/signal.h>
00024 #include <avr/interrupt.h>
00025 
00026 #include "global.h"
00027 #include "i2c.h"
00028 #include "lis3l02.h"
00029 
00030 #include "rprintf.h"
00031 #include "timer.h"
00032 
00033 // global variables
00034 
00035 // Functions
00036 u08 lis3l02Init(void)
00037 {
00038     // reset LIS3L02 chip
00039     return lis3l02Reset();
00040 }
00041 
00042 u08 lis3l02Reset(void)
00043 {
00044     // turn on device and enable X,Y,Z
00045     lis3l02WriteReg(LIS3L02_REG_CTRLREG1,
00046         LIS3L02_CTRLREG1_XEN |
00047         LIS3L02_CTRLREG1_YEN |
00048         LIS3L02_CTRLREG1_ZEN |
00049         LIS3L02_CTRLREG1_PD0);
00050 
00051     // scale and justification options
00052     lis3l02WriteReg(LIS3L02_REG_CTRLREG2,
00053         LIS3L02_CTRLREG2_BOOT | 
00054         LIS3L02_CTRLREG2_DAS );
00055 
00056     return 0;
00057 }
00058 
00059 u08 lis3l02ReadReg(u08 reg)
00060 {
00061     u08 data;
00062     u08 i2cStat;
00063 
00064     // set register
00065     i2cStat = i2cMasterSendNI(LIS3L02_I2C_ADDR, 1, &reg);
00066     if(i2cStat == I2C_ERROR_NODEV)
00067     {
00068         rprintf("No I2C Device\r\n");
00069         return i2cStat;
00070     }
00071     // read register
00072     i2cStat = i2cMasterReceiveNI(LIS3L02_I2C_ADDR, 1, &data);
00073 
00074     //rprintf("READ: Reg=0x%x  Data=0x%x\r\n", reg, data);
00075 
00076     return data;
00077 }
00078 
00079 u08 lis3l02WriteReg(u08 reg, u08 data)
00080 {
00081     u08 packet[2];
00082     u08 i2cStat;
00083     
00084     // prepare packet
00085     packet[0] = reg;
00086     packet[1] = data;
00087     // write register
00088     i2cStat = i2cMasterSendNI(LIS3L02_I2C_ADDR, 2, packet);
00089     if(i2cStat == I2C_ERROR_NODEV)
00090     {
00091         rprintf("No I2C Device\r\n");
00092         return i2cStat;
00093     }
00094 
00095     //rprintf("WRITE: Reg=0x%x  Data=0x%x\r\n", reg, data);
00096 
00097     return (i2cStat == I2C_OK);
00098 }
00099 
00100 s16 lis3l02GetAccel(u08 chxyz)
00101 {
00102     s16 value;
00103     
00104     value  = lis3l02ReadReg(LIS3L02_REG_OUTXL + (chxyz<<1));
00105     value |= lis3l02ReadReg(LIS3L02_REG_OUTXH + (chxyz<<1))<<8;
00106 
00107     return value;
00108 }
00109 
00110 
00111 

Generated on Tue Sep 20 03:11:42 2005 for Procyon AVRlib by  doxygen 1.4.2