| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | /*! \file lis3l02.c \brief ST LIS3L02 3-axis I2C Accelerometer Library. */ |
| 2 | //***************************************************************************** |
||
| 3 | // |
||
| 4 | // File Name : 'lis3l02.c' |
||
| 5 | // Title : ST LIS3L02 3-axis I2C Accelerometer Library |
||
| 6 | // Author : Pascal Stang - Copyright (C) 2004 |
||
| 7 | // Created : 2004.10.23 |
||
| 8 | // Revised : 2004.12.14 |
||
| 9 | // Version : 0.1 |
||
| 10 | // Target MCU : Atmel AVR Series |
||
| 11 | // Editor Tabs : 4 |
||
| 12 | // |
||
| 13 | // NOTE: This code is currently below version 1.0, and therefore is considered |
||
| 14 | // to be lacking in some functionality or documentation, or may not be fully |
||
| 15 | // tested. Nonetheless, you can expect most functions to work. |
||
| 16 | // |
||
| 17 | // This code is distributed under the GNU Public License |
||
| 18 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
| 19 | // |
||
| 20 | //***************************************************************************** |
||
| 21 | |||
| 22 | #include <avr/io.h> |
||
| 23 | #include <avr/interrupt.h> |
||
| 24 | |||
| 25 | #include "global.h" |
||
| 26 | #include "i2c.h" |
||
| 27 | #include "lis3l02.h" |
||
| 28 | |||
| 29 | #include "rprintf.h" |
||
| 30 | #include "timer.h" |
||
| 31 | |||
| 32 | // global variables |
||
| 33 | |||
| 34 | // Functions |
||
| 35 | u08 lis3l02Init(void) |
||
| 36 | { |
||
| 37 | // reset LIS3L02 chip |
||
| 38 | return lis3l02Reset(); |
||
| 39 | } |
||
| 40 | |||
| 41 | u08 lis3l02Reset(void) |
||
| 42 | { |
||
| 43 | // turn on device and enable X,Y,Z |
||
| 44 | lis3l02WriteReg(LIS3L02_REG_CTRLREG1, |
||
| 45 | LIS3L02_CTRLREG1_XEN | |
||
| 46 | LIS3L02_CTRLREG1_YEN | |
||
| 47 | LIS3L02_CTRLREG1_ZEN | |
||
| 48 | LIS3L02_CTRLREG1_PD0); |
||
| 49 | |||
| 50 | // scale and justification options |
||
| 51 | lis3l02WriteReg(LIS3L02_REG_CTRLREG2, |
||
| 52 | LIS3L02_CTRLREG2_BOOT | |
||
| 53 | LIS3L02_CTRLREG2_DAS ); |
||
| 54 | |||
| 55 | return 0; |
||
| 56 | } |
||
| 57 | |||
| 58 | u08 lis3l02ReadReg(u08 reg) |
||
| 59 | { |
||
| 60 | u08 data; |
||
| 61 | u08 i2cStat; |
||
| 62 | |||
| 63 | // set register |
||
| 64 | i2cStat = i2cMasterSendNI(LIS3L02_I2C_ADDR, 1, ®); |
||
| 65 | if(i2cStat == I2C_ERROR_NODEV) |
||
| 66 | { |
||
| 67 | rprintf("No I2C Device\r\n"); |
||
| 68 | return i2cStat; |
||
| 69 | } |
||
| 70 | // read register |
||
| 71 | i2cStat = i2cMasterReceiveNI(LIS3L02_I2C_ADDR, 1, &data); |
||
| 72 | |||
| 73 | //rprintf("READ: Reg=0x%x Data=0x%x\r\n", reg, data); |
||
| 74 | |||
| 75 | return data; |
||
| 76 | } |
||
| 77 | |||
| 78 | u08 lis3l02WriteReg(u08 reg, u08 data) |
||
| 79 | { |
||
| 80 | u08 packet[2]; |
||
| 81 | u08 i2cStat; |
||
| 82 | |||
| 83 | // prepare packet |
||
| 84 | packet[0] = reg; |
||
| 85 | packet[1] = data; |
||
| 86 | // write register |
||
| 87 | i2cStat = i2cMasterSendNI(LIS3L02_I2C_ADDR, 2, packet); |
||
| 88 | if(i2cStat == I2C_ERROR_NODEV) |
||
| 89 | { |
||
| 90 | rprintf("No I2C Device\r\n"); |
||
| 91 | return i2cStat; |
||
| 92 | } |
||
| 93 | |||
| 94 | //rprintf("WRITE: Reg=0x%x Data=0x%x\r\n", reg, data); |
||
| 95 | |||
| 96 | return (i2cStat == I2C_OK); |
||
| 97 | } |
||
| 98 | |||
| 99 | s16 lis3l02GetAccel(u08 chxyz) |
||
| 100 | { |
||
| 101 | s16 value; |
||
| 102 | |||
| 103 | value = lis3l02ReadReg(LIS3L02_REG_OUTXL + (chxyz<<1)); |
||
| 104 | value |= lis3l02ReadReg(LIS3L02_REG_OUTXH + (chxyz<<1))<<8; |
||
| 105 | |||
| 106 | return value; |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 |
Powered by WebSVN v2.8.3