FRDM-KL46Z board sLCD demo code

Dependencies:   SLCD mbed

Fork of FRDM-KL46Z LCD rtc Demo by Paul Staron

Committer:
salemtang
Date:
Thu Oct 02 14:14:28 2014 +0000
Revision:
12:cae0afb130b1
Parent:
9:02f0249db903
20141002_2315 modified

Who changed what in which revision?

UserRevisionLine numberNew contents of line
salemtang 8:18e86eb228ca 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
salemtang 8:18e86eb228ca 2 *
salemtang 8:18e86eb228ca 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
salemtang 8:18e86eb228ca 4 * and associated documentation files (the "Software"), to deal in the Software without
salemtang 8:18e86eb228ca 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
salemtang 8:18e86eb228ca 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
salemtang 8:18e86eb228ca 7 * Software is furnished to do so, subject to the following conditions:
salemtang 8:18e86eb228ca 8 *
salemtang 8:18e86eb228ca 9 * The above copyright notice and this permission notice shall be included in all copies or
salemtang 8:18e86eb228ca 10 * substantial portions of the Software.
salemtang 8:18e86eb228ca 11 *
salemtang 8:18e86eb228ca 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
salemtang 8:18e86eb228ca 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
salemtang 8:18e86eb228ca 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
salemtang 8:18e86eb228ca 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
salemtang 8:18e86eb228ca 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
salemtang 8:18e86eb228ca 17 */
salemtang 8:18e86eb228ca 18
salemtang 8:18e86eb228ca 19 #include "MMA8451Q.h"
salemtang 8:18e86eb228ca 20
salemtang 8:18e86eb228ca 21 #define INT_SOURCE 0x0C
salemtang 8:18e86eb228ca 22 #define REG_WHO_AM_I 0x0D
salemtang 8:18e86eb228ca 23 #define HP_FILTER_CUTOFF 0x0F
salemtang 8:18e86eb228ca 24 #define PULSE_CFG 0x21
salemtang 8:18e86eb228ca 25 #define PULSE_SRC 0x22
salemtang 8:18e86eb228ca 26 #define PULSE_THSX 0x23
salemtang 8:18e86eb228ca 27 #define PULSE_THSY 0x24
salemtang 8:18e86eb228ca 28 #define PULSE_THSZ 0x25
salemtang 8:18e86eb228ca 29 #define PULSE_TMLT 0x26
salemtang 8:18e86eb228ca 30 #define PULSE_LTCY 0x27
salemtang 8:18e86eb228ca 31 #define PULSE_WIND 0x28
salemtang 8:18e86eb228ca 32 #define REG_CTRL_REG_1 0x2A
salemtang 8:18e86eb228ca 33 #define CTRL_REG2 0x2B
salemtang 8:18e86eb228ca 34 #define CTRL_REG4 0x2D
salemtang 8:18e86eb228ca 35 #define CTRL_REG5 0x2E
salemtang 8:18e86eb228ca 36 #define REG_OUT_X_MSB 0x01
salemtang 8:18e86eb228ca 37 #define REG_OUT_Y_MSB 0x03
salemtang 8:18e86eb228ca 38 #define REG_OUT_Z_MSB 0x05
salemtang 8:18e86eb228ca 39
salemtang 8:18e86eb228ca 40 #define UINT14_MAX 16383
salemtang 8:18e86eb228ca 41
salemtang 8:18e86eb228ca 42 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
salemtang 8:18e86eb228ca 43 // activate the peripheral
salemtang 8:18e86eb228ca 44 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
salemtang 8:18e86eb228ca 45 writeRegs(data, 2);
salemtang 8:18e86eb228ca 46 }
salemtang 8:18e86eb228ca 47
salemtang 8:18e86eb228ca 48 MMA8451Q::~MMA8451Q() { }
salemtang 8:18e86eb228ca 49
salemtang 8:18e86eb228ca 50 uint8_t MMA8451Q::getWhoAmI() {
salemtang 8:18e86eb228ca 51 uint8_t who_am_i = 0;
salemtang 8:18e86eb228ca 52 readRegs(REG_WHO_AM_I, &who_am_i, 1);
salemtang 8:18e86eb228ca 53 return who_am_i;
salemtang 8:18e86eb228ca 54 }
salemtang 8:18e86eb228ca 55
salemtang 8:18e86eb228ca 56 float MMA8451Q::getAccX() {
salemtang 8:18e86eb228ca 57 //divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity)
salemtang 8:18e86eb228ca 58 return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
salemtang 8:18e86eb228ca 59 }
salemtang 8:18e86eb228ca 60
salemtang 8:18e86eb228ca 61 float MMA8451Q::getAccY() {
salemtang 8:18e86eb228ca 62 return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0);
salemtang 8:18e86eb228ca 63 }
salemtang 8:18e86eb228ca 64
salemtang 8:18e86eb228ca 65 float MMA8451Q::getAccZ() {
salemtang 8:18e86eb228ca 66 return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0);
salemtang 8:18e86eb228ca 67 }
salemtang 8:18e86eb228ca 68
salemtang 8:18e86eb228ca 69 void MMA8451Q::getAccAllAxis(float * res) {
salemtang 8:18e86eb228ca 70 res[0] = getAccX();
salemtang 8:18e86eb228ca 71 res[1] = getAccY();
salemtang 8:18e86eb228ca 72 res[2] = getAccZ();
salemtang 8:18e86eb228ca 73 }
salemtang 8:18e86eb228ca 74
salemtang 8:18e86eb228ca 75 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
salemtang 8:18e86eb228ca 76 int16_t acc;
salemtang 8:18e86eb228ca 77 uint8_t res[2];
salemtang 8:18e86eb228ca 78 readRegs(addr, res, 2);
salemtang 8:18e86eb228ca 79
salemtang 8:18e86eb228ca 80 acc = (res[0] << 6) | (res[1] >> 2);
salemtang 8:18e86eb228ca 81 if (acc > UINT14_MAX/2)
salemtang 8:18e86eb228ca 82 acc -= UINT14_MAX;
salemtang 8:18e86eb228ca 83
salemtang 8:18e86eb228ca 84 return acc;
salemtang 8:18e86eb228ca 85 }
salemtang 8:18e86eb228ca 86
salemtang 8:18e86eb228ca 87 void MMA8451Q::setDoubleTap(void){
salemtang 8:18e86eb228ca 88 //Implemented directly from Freescale's AN4072
salemtang 8:18e86eb228ca 89 //Added to MMA8451Q lib
salemtang 8:18e86eb228ca 90
salemtang 8:18e86eb228ca 91 uint8_t CTRL_REG1_Data;
salemtang 8:18e86eb228ca 92 // int adds;
salemtang 8:18e86eb228ca 93 uint8_t data[2] = {REG_CTRL_REG_1, 0x08};
salemtang 8:18e86eb228ca 94
salemtang 8:18e86eb228ca 95 //400 Hz, Standby Mode
salemtang 8:18e86eb228ca 96 writeRegs(data,2);
salemtang 8:18e86eb228ca 97
salemtang 8:18e86eb228ca 98 //Enable X, Y and Z Double Pulse with DPA = 0 no double pulse abort
salemtang 8:18e86eb228ca 99 data[0]=PULSE_CFG;data[1]=0x2A;
salemtang 8:18e86eb228ca 100 writeRegs(data,2);
salemtang 8:18e86eb228ca 101
salemtang 8:18e86eb228ca 102 //SetThreshold 3g on X and Y and 5g on Z
salemtang 8:18e86eb228ca 103 //Note: Every step is 0.063g
salemtang 8:18e86eb228ca 104 //3 g/0.063g = 48 counts
salemtang 8:18e86eb228ca 105 //5g/0.063g = 79 counts
salemtang 8:18e86eb228ca 106 data[0]=PULSE_THSX;data[1]=0x30;
salemtang 8:18e86eb228ca 107 writeRegs(data,2);//Set X Threshold to 3g
salemtang 8:18e86eb228ca 108 data[0]=PULSE_THSY;data[1]=0x30;
salemtang 8:18e86eb228ca 109 writeRegs(data,2);//Set Y Threshold to 3g
salemtang 8:18e86eb228ca 110 data[0]=PULSE_THSZ;data[1]=0x4F;
salemtang 8:18e86eb228ca 111 writeRegs(data,2);//Set Z Threshold to 5g
salemtang 8:18e86eb228ca 112
salemtang 8:18e86eb228ca 113 //Set Time Limit for Tap Detection to 60 ms LP Mode
salemtang 8:18e86eb228ca 114 //Note: 400 Hz ODR, Time step is 1.25 ms per step
salemtang 8:18e86eb228ca 115 //60 ms/1.25 ms = 48 counts
salemtang 8:18e86eb228ca 116 data[0]=PULSE_TMLT;data[1]=0x30;
salemtang 8:18e86eb228ca 117 writeRegs(data,2);//60 ms
salemtang 8:18e86eb228ca 118
salemtang 8:18e86eb228ca 119 //Set Latency Time to 200 ms
salemtang 8:18e86eb228ca 120 //Note: 400 Hz ODR LPMode, Time step is 2.5 ms per step 00 ms/2.5 ms = 80 counts
salemtang 8:18e86eb228ca 121 data[0]=PULSE_LTCY;data[1]=0x50;
salemtang 8:18e86eb228ca 122 writeRegs(data,2);//200 ms
salemtang 8:18e86eb228ca 123
salemtang 8:18e86eb228ca 124 //Set Time Window for second tap to 300 ms
salemtang 8:18e86eb228ca 125 //Note: 400 Hz ODR LP Mode, Time step is 2.5 ms per step
salemtang 8:18e86eb228ca 126 //300 ms/2.5 ms = 120 counts
salemtang 8:18e86eb228ca 127 data[0]=PULSE_WIND;data[1]=0x78;
salemtang 8:18e86eb228ca 128 writeRegs(data,2);//300 ms
salemtang 8:18e86eb228ca 129
salemtang 8:18e86eb228ca 130 //Route INT1 to System Interrupt
salemtang 8:18e86eb228ca 131 data[0]=CTRL_REG4;data[1]=0x08;
salemtang 8:18e86eb228ca 132 writeRegs(data,2);//Enable Pulse Interrupt in System CTRL_REG4
salemtang 8:18e86eb228ca 133 data[0]=CTRL_REG5;data[1]=0x08;
salemtang 8:18e86eb228ca 134 writeRegs(data,2);//Route Pulse Interrupt to INT1 hardware Pin CTRL_REG5
salemtang 8:18e86eb228ca 135
salemtang 8:18e86eb228ca 136 //Set the device to Active Mode
salemtang 8:18e86eb228ca 137 readRegs(0x2A,&CTRL_REG1_Data,1);//Read out the contents of the register
salemtang 8:18e86eb228ca 138 CTRL_REG1_Data |= 0x01; //Change the value in the register to Active Mode.
salemtang 8:18e86eb228ca 139 data[0]=REG_CTRL_REG_1;
salemtang 8:18e86eb228ca 140 data[1]=CTRL_REG1_Data;
salemtang 8:18e86eb228ca 141 writeRegs(data,2);//Write in the updated value to put the device in Active Mode
salemtang 8:18e86eb228ca 142 }
salemtang 8:18e86eb228ca 143
salemtang 8:18e86eb228ca 144 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
salemtang 8:18e86eb228ca 145 char t[1] = {addr};
salemtang 8:18e86eb228ca 146 m_i2c.write(m_addr, t, 1, true);
salemtang 8:18e86eb228ca 147 m_i2c.read(m_addr, (char *)data, len);
salemtang 8:18e86eb228ca 148 }
salemtang 8:18e86eb228ca 149
salemtang 8:18e86eb228ca 150 void MMA8451Q::writeRegs(uint8_t * data, int len) {
salemtang 8:18e86eb228ca 151 m_i2c.write(m_addr, (char *)data, len);
salemtang 8:18e86eb228ca 152 }