Versión sin post-procesado del software del VmRideR

Dependencies:   mbed

Committer:
JuanManuelAmador
Date:
Mon Jun 15 15:34:27 2015 +0000
Revision:
0:3d456b8ce449
prueba

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JuanManuelAmador 0:3d456b8ce449 1 /**
JuanManuelAmador 0:3d456b8ce449 2 * @author Juan Manuel Amador Olivares (virtualmech)
JuanManuelAmador 0:3d456b8ce449 3 */
JuanManuelAmador 0:3d456b8ce449 4
JuanManuelAmador 0:3d456b8ce449 5 #include "KXR94.h"
JuanManuelAmador 0:3d456b8ce449 6
JuanManuelAmador 0:3d456b8ce449 7 KXR94::KXR94(PinName mosi, PinName miso, PinName sck, PinName cs) : spi_(mosi, miso, sck), nCS_(cs)
JuanManuelAmador 0:3d456b8ce449 8 {
JuanManuelAmador 0:3d456b8ce449 9 // Se configura el puerto SPI
JuanManuelAmador 0:3d456b8ce449 10 spi_.frequency(4000000); // El máximo según el datasheet es 5MHz
JuanManuelAmador 0:3d456b8ce449 11 spi_.format(8,0);
JuanManuelAmador 0:3d456b8ce449 12
JuanManuelAmador 0:3d456b8ce449 13 // Se prepara el acelerómetro para comenzar las lecturas
JuanManuelAmador 0:3d456b8ce449 14 AcelerometroWakeUp();
JuanManuelAmador 0:3d456b8ce449 15 }
JuanManuelAmador 0:3d456b8ce449 16
JuanManuelAmador 0:3d456b8ce449 17 void KXR94::ReadAccels_KXR94(int* Acc) {
JuanManuelAmador 0:3d456b8ce449 18 char x_lsb, x_msb;
JuanManuelAmador 0:3d456b8ce449 19 char y_lsb, y_msb;
JuanManuelAmador 0:3d456b8ce449 20 char z_lsb, z_msb;
JuanManuelAmador 0:3d456b8ce449 21 signed short ax, ay, az;
JuanManuelAmador 0:3d456b8ce449 22
JuanManuelAmador 0:3d456b8ce449 23 //------------X----------------
JuanManuelAmador 0:3d456b8ce449 24 nCS_ = 0;
JuanManuelAmador 0:3d456b8ce449 25 spi_.write(CONVERTX);
JuanManuelAmador 0:3d456b8ce449 26 wait_us(40);
JuanManuelAmador 0:3d456b8ce449 27 x_msb = spi_.write(0x00); // Dummy byte
JuanManuelAmador 0:3d456b8ce449 28 x_lsb = spi_.write(0x00); // Dummy byte
JuanManuelAmador 0:3d456b8ce449 29 nCS_ = 1;
JuanManuelAmador 0:3d456b8ce449 30 wait_us(1);
JuanManuelAmador 0:3d456b8ce449 31 ax = (x_msb << 8) | x_lsb ; // combineer msb en lsb
JuanManuelAmador 0:3d456b8ce449 32 ax = ax >> 4; // Get rid of four non-value bits in LSB
JuanManuelAmador 0:3d456b8ce449 33 ax &=~0xF000;
JuanManuelAmador 0:3d456b8ce449 34 //------------Y----------------
JuanManuelAmador 0:3d456b8ce449 35 nCS_ = 0;
JuanManuelAmador 0:3d456b8ce449 36 spi_.write(CONVERTY);
JuanManuelAmador 0:3d456b8ce449 37 wait_us(40);
JuanManuelAmador 0:3d456b8ce449 38 y_msb = spi_.write(0x00); // Dummy byte
JuanManuelAmador 0:3d456b8ce449 39 y_lsb = spi_.write(0x00); // Dummy byte
JuanManuelAmador 0:3d456b8ce449 40 nCS_ = 1;
JuanManuelAmador 0:3d456b8ce449 41 wait_us(1);
JuanManuelAmador 0:3d456b8ce449 42 ay = (y_msb << 8) | y_lsb; // combineer msb en lsb
JuanManuelAmador 0:3d456b8ce449 43 ay = ay >> 4; // Get rid of four non-value bits in LSB
JuanManuelAmador 0:3d456b8ce449 44 ay &=~0xF000;
JuanManuelAmador 0:3d456b8ce449 45 //------------Z----------------
JuanManuelAmador 0:3d456b8ce449 46 nCS_ = 0;
JuanManuelAmador 0:3d456b8ce449 47 spi_.write(CONVERTZ);
JuanManuelAmador 0:3d456b8ce449 48 wait_us(40);
JuanManuelAmador 0:3d456b8ce449 49 z_msb = spi_.write(0x00); // Dummy byte
JuanManuelAmador 0:3d456b8ce449 50 z_lsb = spi_.write(0x00); // Dummy byte
JuanManuelAmador 0:3d456b8ce449 51 nCS_ = 1;
JuanManuelAmador 0:3d456b8ce449 52 wait_us(1);
JuanManuelAmador 0:3d456b8ce449 53 az = (z_msb << 8) | z_lsb; // combineer msb en lsb
JuanManuelAmador 0:3d456b8ce449 54 az = az >> 4; // Get rid of four non-value bits in LSB
JuanManuelAmador 0:3d456b8ce449 55 az &=~0xF000;
JuanManuelAmador 0:3d456b8ce449 56
JuanManuelAmador 0:3d456b8ce449 57 Acc[0] = ax;
JuanManuelAmador 0:3d456b8ce449 58 Acc[1] = ay;
JuanManuelAmador 0:3d456b8ce449 59 Acc[2] = az;
JuanManuelAmador 0:3d456b8ce449 60 }
JuanManuelAmador 0:3d456b8ce449 61
JuanManuelAmador 0:3d456b8ce449 62 //-----------------READ OUT the X-Y-Z values---------------
JuanManuelAmador 0:3d456b8ce449 63 char KXR94::read_reg() {
JuanManuelAmador 0:3d456b8ce449 64 char byte;
JuanManuelAmador 0:3d456b8ce449 65 nCS_ = 0;
JuanManuelAmador 0:3d456b8ce449 66 spi_.write(READREG);
JuanManuelAmador 0:3d456b8ce449 67 byte = spi_.write(0x00); // Dummy byte
JuanManuelAmador 0:3d456b8ce449 68 nCS_ = 1;
JuanManuelAmador 0:3d456b8ce449 69 wait_us(1);
JuanManuelAmador 0:3d456b8ce449 70 return byte;
JuanManuelAmador 0:3d456b8ce449 71 }
JuanManuelAmador 0:3d456b8ce449 72
JuanManuelAmador 0:3d456b8ce449 73 void KXR94::AcelerometroSleep(void)
JuanManuelAmador 0:3d456b8ce449 74 {
JuanManuelAmador 0:3d456b8ce449 75 nCS_ = 0;
JuanManuelAmador 0:3d456b8ce449 76 spi_.write(WRITEREG);
JuanManuelAmador 0:3d456b8ce449 77 spi_.write(0x00); // Enable = 0
JuanManuelAmador 0:3d456b8ce449 78 nCS_ = 1;
JuanManuelAmador 0:3d456b8ce449 79 wait_us(1);
JuanManuelAmador 0:3d456b8ce449 80 }
JuanManuelAmador 0:3d456b8ce449 81 void KXR94::AcelerometroWakeUp(void)
JuanManuelAmador 0:3d456b8ce449 82 {
JuanManuelAmador 0:3d456b8ce449 83 nCS_ = 0;
JuanManuelAmador 0:3d456b8ce449 84 spi_.write(WRITEREG);
JuanManuelAmador 0:3d456b8ce449 85 spi_.write(0x04); // Enable = 1
JuanManuelAmador 0:3d456b8ce449 86 nCS_ = 1;
JuanManuelAmador 0:3d456b8ce449 87 wait_us(1);
JuanManuelAmador 0:3d456b8ce449 88 }