PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
accelerometer.c
1 #include "badgetools.h"
2 #include "simpletools.h"
3 
4 void ee_init();
5 void init_MMA7660FC(void);
6 int raw2g100(char gRaw);
7 
9 int st_eeInitFlag;
10 volatile int bt_accelInitFlag = 0;
11 volatile int eei2cLock;
12 volatile int eei2cLockFlag;
13 
14 void init_MMA7660FC(void)
15 {
16  int x, y, z;
17  unsigned char val = 0;
18  bt_accelInitFlag = 1;
19  if(!eei2cLockFlag)
20  {
21  leds(0b111111);
22  eei2cLock = locknew();
23  lockclr(eei2cLock);
24  eei2cLockFlag = 1;
25  }
26  if(!st_eeInitFlag) ee_init();
27  while(lockset(eei2cLock));
28  i2c_out(st_eeprom, MMA7660_I2C,
29  MODE, 1, &val, 1);
30  i2c_out(st_eeprom, MMA7660_I2C,
31  INTSU, 1, &val, 1);
32  i2c_out(st_eeprom, MMA7660_I2C,
33  SR, 1, &val, 1);
34  val = 0xC1;
35  i2c_out(st_eeprom, MMA7660_I2C,
36  MODE, 1, &val, 1);
38  lockclr(eei2cLock);
39  accels(&x, &y, &z);
40 }
41 
42 int accel(int axis)
43 {
44  if(!st_eeInitFlag) ee_init();
45  unsigned char val = 0;
46  while(lockset(eei2cLock));
47  i2c_in (st_eeprom, MMA7660_I2C,
48  axis, 1, &val, 1);
50  lockclr(eei2cLock);
51  int g100 = raw2g100(val);
52  if(axis == AY) g100 = -g100;
53  return g100;
54 }
55 
56 void accels(int *x, int *y, int *z)
57 {
58  //char axis[4] = {0, 0, 0, 0};
59  //int g100[3] = {0, 0, 0};
60  *x = accel(AX);
61  *y = accel(AY);
62  *z = accel(AZ);
63 }
64 
65 int raw2g100(char gRaw)
66 {
67  int g100 = (int) gRaw;
68  int sign = 1 & (g100 >> 5);
69  if(sign)
70  g100 = 0xFFFFFFC0 | g100;
71  else
72  g100 = 0x3F & g100;
73  g100 = g100 * 469 / 100;
74  return g100;
75 }
76 
77 
78 /*
79  TERMS OF USE: MIT License
80 
81  Permission is hereby granted, free of charge, to any person obtaining a
82  copy of this software and associated documentation files (the "Software"),
83  to deal in the Software without restriction, including without limitation
84  the rights to use, copy, modify, merge, publish, distribute, sublicense,
85  and/or sell copies of the Software, and to permit persons to whom the
86  Software is furnished to do so, subject to the following conditions:
87 
88  The above copyright notice and this permission notice shall be included in
89  all copies or substantial portions of the Software.
90 
91  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
92  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
93  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
94  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
95  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
96  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
97  DEALINGS IN THE SOFTWARE.
98 */
99 
AX
#define AX
For choosing the accelerometer's x-axis. Example accel(AX) returns the component of the earth's gravi...
Definition: badgetools.h:223
i2c_in
HUBTEXT int i2c_in(i2c *busID, int i2cAddr, int memAddr, int memAddrCount, unsigned char *data, int dataCount)
Receive data from device using I2C protocol.
Definition: i2c_in.c:20
AY
#define AY
For choosing the accelerometer's y-axis. Example accel(AY) returns the component of the earth's gravi...
Definition: badgetools.h:215
st_eeprom
i2c * st_eeprom
The busID for the Propeller Activity Board's EEPROM bus.
Definition: accel_shaken.c:9
i2c_stop
HUBTEXT void i2c_stop(i2c *bus)
Send Signal i2c stop condition on bus.
Definition: simplei2c.c:78
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
accel
int accel(int axis)
Measures acceleration and tilt on one of 3 axes (AX, AY, or AZ) in terms of centigravity (cg) units,...
Definition: accelerometer.c:42
lockset
#define lockset(lockid)
Set a lock.
Definition: propeller.h:163
AZ
#define AZ
For choosing the accelerometer's z-axis. Example accel(AZ) returns the component of the earth's gravi...
Definition: badgetools.h:231
i2c_st
Definition: simplei2c.h:25
x
int x
Definition: 07 Box and Lines.c:13
leds
void leds(int bits)
Sets the on/off states of the 6 blue LEDs with a number that contains six binary 1/0 digits....
lockclr
#define lockclr(lockid)
Clear lock.
Definition: propeller.h:170
badgetools.h
This library provides convenient functions for a variety of Parallax eBadge operations.
st_eeInitFlag
int st_eeInitFlag
Initialization flag used by ee_ functions.
Definition: accel_shaken.c:10
MODE
static const SPI::Mode MODE
Definition: SPI_Demo.cpp:48
locknew
#define locknew()
Get a new lock from the pool of Propeller hardware locks.
Definition: propeller.h:147
i2c_out
HUBTEXT int i2c_out(i2c *busID, int i2cAddr, int memAddr, int memAddrCount, const unsigned char *data, int dataCount)
Send data to device using I2C protocol.
Definition: i2c_out.c:20
accels
void accels(int *x, int *y, int *z)
Measures acceleration and tilt on all 3 axes (x, y, and z) in terms of centigravity (cg) units,...
Definition: accelerometer.c:56