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/interrupt.h>
    00024
    00025 #include "global.h"
    00026 #include "i2c.h"
    00027 #include "lis3l02.h"
    00028
    00029 #include "rprintf.h"
    00030 #include "timer.h"
    00031
    00032 // global variables
    00033
    00034 // Functions
    00035 u08 lis3l02Init(void)
    00036 {
    00037     // reset LIS3L02 chip
    00038     return lis3l02Reset();
    00039 }
    00040
    00041 u08 lis3l02Reset(void)
    00042 {
    00043     // turn on device and enable X,Y,Z
    00044     lis3l02WriteReg(LIS3L02_REG_CTRLREG1,
    00045         LIS3L02_CTRLREG1_XEN |
    00046         LIS3L02_CTRLREG1_YEN |
    00047         LIS3L02_CTRLREG1_ZEN |
    00048         LIS3L02_CTRLREG1_PD0);
    00049
    00050     // scale and justification options
    00051     lis3l02WriteReg(LIS3L02_REG_CTRLREG2,
    00052         LIS3L02_CTRLREG2_BOOT |
    00053         LIS3L02_CTRLREG2_DAS );
    00054
    00055     return 0;
    00056 }
    00057
    00058 u08 lis3l02ReadReg(u08 reg)
    00059 {
    00060     u08 data;
    00061     u08 i2cStat;
    00062
    00063     // set register
    00064     i2cStat = i2cMasterSendNI(LIS3L02_I2C_ADDR, 1, &reg);
    00065     if(i2cStat == I2C_ERROR_NODEV)
    00066     {
    00067         rprintf("No I2C Device\r\n");
    00068         return i2cStat;
    00069     }
    00070     // read register
    00071     i2cStat = i2cMasterReceiveNI(LIS3L02_I2C_ADDR, 1, &data);
    00072
    00073     //rprintf("READ: Reg=0x%x  Data=0x%x\r\n", reg, data);
    00074
    00075     return data;
    00076 }
    00077
    00078 u08 lis3l02WriteReg(u08 reg, u08 data)
    00079 {
    00080     u08 packet[2];
    00081     u08 i2cStat;
    00082
    00083     // prepare packet
    00084     packet[0] = reg;
    00085     packet[1] = data;
    00086     // write register
    00087     i2cStat = i2cMasterSendNI(LIS3L02_I2C_ADDR, 2, packet);
    00088     if(i2cStat == I2C_ERROR_NODEV)
    00089     {
    00090         rprintf("No I2C Device\r\n");
    00091         return i2cStat;
    00092     }
    00093
    00094     //rprintf("WRITE: Reg=0x%x  Data=0x%x\r\n", reg, data);
    00095
    00096     return (i2cStat == I2C_OK);
    00097 }
    00098
    00099 s16 lis3l02GetAccel(u08 chxyz)
    00100 {
    00101     s16 value;
    00102
    00103     value  = lis3l02ReadReg(LIS3L02_REG_OUTXL + (chxyz<<1));
    00104     value |= lis3l02ReadReg(LIS3L02_REG_OUTXH + (chxyz<<1))<<8;
    00105
    00106     return value;
    00107 }
    00108
    00109
    00110