秋月販売のL3GD20使用8pinDIPジャイロセンサーモジュールのライブラリです。 FIFOを使って等間隔データの読み込みができます。 構造体のポインタをreadメソッドに渡すと値が入ります。 <今後の予定> ・レジスタへのアクセスの整理整頓 ・データレート/HighLowのフィルター/分解能の設定メソッド ・mbedピン変化割り込みによるデータ取得(おそらく関数ポインタを割り込み処理のメソッドに渡してもらう) ・読み取りデータにフィルターをかける。 ・データ取得の高速化
L3GD20.cpp@0:a07d3c1c7d2d, 2014-04-03 (annotated)
- Committer:
- lelect
- Date:
- Thu Apr 03 12:26:20 2014 +0000
- Revision:
- 0:a07d3c1c7d2d
<L3GD20 SPI class(unused interrupt)>; you can use normal mode and FIFO mode(only change fifo configration,cannot change other configration); I'm going to use the interrupt other class.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lelect | 0:a07d3c1c7d2d | 1 | #include "mbed.h" |
lelect | 0:a07d3c1c7d2d | 2 | #include "L3GD20.h" |
lelect | 0:a07d3c1c7d2d | 3 | L3GD20::L3GD20(PinName miso, PinName mosi, PinName scl, PinName cs/*, PinName interrupt1=NC, PinName interrupt2=NC*/) |
lelect | 0:a07d3c1c7d2d | 4 | :_spi(miso,mosi,scl) |
lelect | 0:a07d3c1c7d2d | 5 | ,_cs(cs) |
lelect | 0:a07d3c1c7d2d | 6 | { |
lelect | 0:a07d3c1c7d2d | 7 | _spi.format(8,3); |
lelect | 0:a07d3c1c7d2d | 8 | _spi.frequency(10000000); |
lelect | 0:a07d3c1c7d2d | 9 | int val; |
lelect | 0:a07d3c1c7d2d | 10 | read(null,&val); |
lelect | 0:a07d3c1c7d2d | 11 | read(WhoAmI,&val); |
lelect | 0:a07d3c1c7d2d | 12 | if(val==0x0F) |
lelect | 0:a07d3c1c7d2d | 13 | printf("L3GD20 is OK\r\n"); |
lelect | 0:a07d3c1c7d2d | 14 | else |
lelect | 0:a07d3c1c7d2d | 15 | printf("Where is L3GD20\r\n"); |
lelect | 0:a07d3c1c7d2d | 16 | configReadOut(); |
lelect | 0:a07d3c1c7d2d | 17 | statusReadOut(); |
lelect | 0:a07d3c1c7d2d | 18 | |
lelect | 0:a07d3c1c7d2d | 19 | } |
lelect | 0:a07d3c1c7d2d | 20 | void L3GD20::write(RESISTER reg,int val) |
lelect | 0:a07d3c1c7d2d | 21 | { |
lelect | 0:a07d3c1c7d2d | 22 | _cs=0; |
lelect | 0:a07d3c1c7d2d | 23 | _spi.write(reg); |
lelect | 0:a07d3c1c7d2d | 24 | _spi.write(val); |
lelect | 0:a07d3c1c7d2d | 25 | _cs=1; |
lelect | 0:a07d3c1c7d2d | 26 | } |
lelect | 0:a07d3c1c7d2d | 27 | void L3GD20::read(RESISTER reg,int* val) |
lelect | 0:a07d3c1c7d2d | 28 | { |
lelect | 0:a07d3c1c7d2d | 29 | _cs=0; |
lelect | 0:a07d3c1c7d2d | 30 | _spi.write(READ|reg); |
lelect | 0:a07d3c1c7d2d | 31 | (*val)=_spi.write(0x00); |
lelect | 0:a07d3c1c7d2d | 32 | _cs=1; |
lelect | 0:a07d3c1c7d2d | 33 | } |
lelect | 0:a07d3c1c7d2d | 34 | void L3GD20::start(DIRECTION enable) |
lelect | 0:a07d3c1c7d2d | 35 | { |
lelect | 0:a07d3c1c7d2d | 36 | _config.CTRL_REG1.B.Enable=enable; |
lelect | 0:a07d3c1c7d2d | 37 | _config.CTRL_REG1.b.PD=1; |
lelect | 0:a07d3c1c7d2d | 38 | write(CtrlReg1,_config.CTRL_REG1.word); |
lelect | 0:a07d3c1c7d2d | 39 | } |
lelect | 0:a07d3c1c7d2d | 40 | void L3GD20::stop() |
lelect | 0:a07d3c1c7d2d | 41 | { |
lelect | 0:a07d3c1c7d2d | 42 | _config.CTRL_REG1.word=_config.CTRL_REG1.word&0xF0; |
lelect | 0:a07d3c1c7d2d | 43 | write(CtrlReg1,_config.CTRL_REG1.word); |
lelect | 0:a07d3c1c7d2d | 44 | } |
lelect | 0:a07d3c1c7d2d | 45 | void L3GD20::sleep() |
lelect | 0:a07d3c1c7d2d | 46 | { |
lelect | 0:a07d3c1c7d2d | 47 | _config.CTRL_REG1.word=_config.CTRL_REG1.word&0xF8; |
lelect | 0:a07d3c1c7d2d | 48 | write(CtrlReg1,_config.CTRL_REG1.word); |
lelect | 0:a07d3c1c7d2d | 49 | } |
lelect | 0:a07d3c1c7d2d | 50 | void L3GD20::read(anglerrates* val,DIRECTION direction) |
lelect | 0:a07d3c1c7d2d | 51 | { |
lelect | 0:a07d3c1c7d2d | 52 | if(direction&X) { |
lelect | 0:a07d3c1c7d2d | 53 | int x_l,x_h; |
lelect | 0:a07d3c1c7d2d | 54 | read(OutXL,&x_l); |
lelect | 0:a07d3c1c7d2d | 55 | read(OutXH,&x_h); |
lelect | 0:a07d3c1c7d2d | 56 | val->X=(x_l)|(x_h<<8); |
lelect | 0:a07d3c1c7d2d | 57 | } |
lelect | 0:a07d3c1c7d2d | 58 | if(direction&Y) { |
lelect | 0:a07d3c1c7d2d | 59 | int y_l,y_h; |
lelect | 0:a07d3c1c7d2d | 60 | read(OutYL,&y_l); |
lelect | 0:a07d3c1c7d2d | 61 | read(OutYH,&y_h); |
lelect | 0:a07d3c1c7d2d | 62 | val->Y=(y_l)|(y_h<<8); |
lelect | 0:a07d3c1c7d2d | 63 | } |
lelect | 0:a07d3c1c7d2d | 64 | if(direction&Z) { |
lelect | 0:a07d3c1c7d2d | 65 | int z_l,z_h; |
lelect | 0:a07d3c1c7d2d | 66 | read(OutZL,&z_l); |
lelect | 0:a07d3c1c7d2d | 67 | read(OutZH,&z_h); |
lelect | 0:a07d3c1c7d2d | 68 | val->Z=(z_l)|(z_h<<8); |
lelect | 0:a07d3c1c7d2d | 69 | } |
lelect | 0:a07d3c1c7d2d | 70 | if(_config.CTRL_REG4.B.FS==0x00) { |
lelect | 0:a07d3c1c7d2d | 71 | val->x=8.75*(float)val->X; |
lelect | 0:a07d3c1c7d2d | 72 | val->y=8.75*(float)val->Y; |
lelect | 0:a07d3c1c7d2d | 73 | val->z=8.75*(float)val->Z; |
lelect | 0:a07d3c1c7d2d | 74 | } else if(_config.CTRL_REG4.B.FS==0x01) { |
lelect | 0:a07d3c1c7d2d | 75 | val->x=17.5*(float)val->X; |
lelect | 0:a07d3c1c7d2d | 76 | val->y=17.5*(float)val->Y; |
lelect | 0:a07d3c1c7d2d | 77 | val->z=17.5*(float)val->Z; |
lelect | 0:a07d3c1c7d2d | 78 | } else if((_config.CTRL_REG4.B.FS==0x10)|(_config.CTRL_REG4.B.FS==0x11)) { |
lelect | 0:a07d3c1c7d2d | 79 | val->x=70.0*(float)val->X; |
lelect | 0:a07d3c1c7d2d | 80 | val->y=70.0*(float)val->Y; |
lelect | 0:a07d3c1c7d2d | 81 | val->z=70.0*(float)val->Z; |
lelect | 0:a07d3c1c7d2d | 82 | } |
lelect | 0:a07d3c1c7d2d | 83 | } |
lelect | 0:a07d3c1c7d2d | 84 | int L3GD20::readTemperature() |
lelect | 0:a07d3c1c7d2d | 85 | { |
lelect | 0:a07d3c1c7d2d | 86 | read(OutTemp,&_status.OUT_TEMP); |
lelect | 0:a07d3c1c7d2d | 87 | return _status.OUT_TEMP; |
lelect | 0:a07d3c1c7d2d | 88 | } |
lelect | 0:a07d3c1c7d2d | 89 | void L3GD20::enableFIFO(FIFO_mode mode,FIFOstatus interrupt,const int threshold) |
lelect | 0:a07d3c1c7d2d | 90 | { |
lelect | 0:a07d3c1c7d2d | 91 | _config.CTRL_REG5.b.FIFO_EN=1; |
lelect | 0:a07d3c1c7d2d | 92 | if(interrupt&empty) { |
lelect | 0:a07d3c1c7d2d | 93 | _config.CTRL_REG3.b.I2_Empty=1; |
lelect | 0:a07d3c1c7d2d | 94 | } else if(interrupt&watermark) { |
lelect | 0:a07d3c1c7d2d | 95 | _config.CTRL_REG3.b.I2_WTM=1; |
lelect | 0:a07d3c1c7d2d | 96 | } else if(interrupt&overrun) { |
lelect | 0:a07d3c1c7d2d | 97 | _config.CTRL_REG3.b.I2_ORun=1; |
lelect | 0:a07d3c1c7d2d | 98 | } |
lelect | 0:a07d3c1c7d2d | 99 | _config.FIFO_CTRL_REG.B.FM=mode; |
lelect | 0:a07d3c1c7d2d | 100 | if((0<threshold)&&(threshold<31)) { |
lelect | 0:a07d3c1c7d2d | 101 | _config.FIFO_CTRL_REG.B.WTM=threshold; |
lelect | 0:a07d3c1c7d2d | 102 | } else { |
lelect | 0:a07d3c1c7d2d | 103 | _config.FIFO_CTRL_REG.B.WTM=30; |
lelect | 0:a07d3c1c7d2d | 104 | } |
lelect | 0:a07d3c1c7d2d | 105 | write(CtrlReg3,_config.CTRL_REG3.word); |
lelect | 0:a07d3c1c7d2d | 106 | write(CtrlReg5,_config.CTRL_REG5.word); |
lelect | 0:a07d3c1c7d2d | 107 | write(FIFOCtrlReg,_config.FIFO_CTRL_REG.word); |
lelect | 0:a07d3c1c7d2d | 108 | } |
lelect | 0:a07d3c1c7d2d | 109 | void L3GD20::updateFIFO(void) |
lelect | 0:a07d3c1c7d2d | 110 | { |
lelect | 0:a07d3c1c7d2d | 111 | read(FIFOSrcReg,&_status.FIFO_SRC_REG.word); |
lelect | 0:a07d3c1c7d2d | 112 | read(FIFOCtrlReg,&_config.FIFO_CTRL_REG.word); |
lelect | 0:a07d3c1c7d2d | 113 | |
lelect | 0:a07d3c1c7d2d | 114 | if(_status.FIFO_SRC_REG.b.WTM) { |
lelect | 0:a07d3c1c7d2d | 115 | FIFO.status=watermark; |
lelect | 0:a07d3c1c7d2d | 116 | } else if(_status.FIFO_SRC_REG.b.EMPTY) { |
lelect | 0:a07d3c1c7d2d | 117 | FIFO.status=empty; |
lelect | 0:a07d3c1c7d2d | 118 | } else if(_status.FIFO_SRC_REG.b.OVR) { |
lelect | 0:a07d3c1c7d2d | 119 | FIFO.status=overrun; |
lelect | 0:a07d3c1c7d2d | 120 | } else { |
lelect | 0:a07d3c1c7d2d | 121 | FIFO.status=none; |
lelect | 0:a07d3c1c7d2d | 122 | } |
lelect | 0:a07d3c1c7d2d | 123 | FIFO.level=_status.FIFO_SRC_REG.B.FSS; |
lelect | 0:a07d3c1c7d2d | 124 | } |
lelect | 0:a07d3c1c7d2d | 125 | void L3GD20::configReadOut(void) |
lelect | 0:a07d3c1c7d2d | 126 | { |
lelect | 0:a07d3c1c7d2d | 127 | read(CtrlReg1,&_config.CTRL_REG1.word); |
lelect | 0:a07d3c1c7d2d | 128 | read(CtrlReg2,&_config.CTRL_REG2.word); |
lelect | 0:a07d3c1c7d2d | 129 | read(CtrlReg3,&_config.CTRL_REG3.word); |
lelect | 0:a07d3c1c7d2d | 130 | read(CtrlReg4,&_config.CTRL_REG4.word); |
lelect | 0:a07d3c1c7d2d | 131 | read(CtrlReg5,&_config.CTRL_REG5.word); |
lelect | 0:a07d3c1c7d2d | 132 | read(Reference,&_config.REF_DATACAP.word); |
lelect | 0:a07d3c1c7d2d | 133 | read(FIFOCtrlReg,&_config.FIFO_CTRL_REG.word); |
lelect | 0:a07d3c1c7d2d | 134 | read(INT1Cfg,&_config.INT1_CFG.word); |
lelect | 0:a07d3c1c7d2d | 135 | read(INT1ThsXH,&_config.INT1_TSH_XH.word); |
lelect | 0:a07d3c1c7d2d | 136 | read(INT1ThsXL,&_config.INT1_TSH_XL.word); |
lelect | 0:a07d3c1c7d2d | 137 | read(INT1ThsYH,&_config.INT1_TSH_YH.word); |
lelect | 0:a07d3c1c7d2d | 138 | read(INT1ThsYL,&_config.INT1_TSH_YL.word); |
lelect | 0:a07d3c1c7d2d | 139 | read(INT1ThsZH,&_config.INT1_TSH_ZH.word); |
lelect | 0:a07d3c1c7d2d | 140 | read(INT1ThsZL,&_config.INT1_TSH_ZL.word); |
lelect | 0:a07d3c1c7d2d | 141 | read(INT1Duration,&_config.INT1_DURATION.word); |
lelect | 0:a07d3c1c7d2d | 142 | } |
lelect | 0:a07d3c1c7d2d | 143 | |
lelect | 0:a07d3c1c7d2d | 144 | void L3GD20::statusReadOut(void) |
lelect | 0:a07d3c1c7d2d | 145 | { |
lelect | 0:a07d3c1c7d2d | 146 | read(OutTemp,&_status.OUT_TEMP); |
lelect | 0:a07d3c1c7d2d | 147 | read(StatusReg,&_status.STATUS_REG); |
lelect | 0:a07d3c1c7d2d | 148 | read(FIFOSrcReg,&_status.FIFO_SRC_REG.word); |
lelect | 0:a07d3c1c7d2d | 149 | read(INT1Src,&_status.INT1_SRC); |
lelect | 0:a07d3c1c7d2d | 150 | } |