このライブラリーはテストに作ったものです。近いうちに他の関数を加えた形でアップデートするつもりです。 read_8(int reg)、read_16(int reg) で レジスターregの値を8bit、16bitで読み込み、そのまま返します。受け取り側の変数をint8_t、int16_t で定義すれば正負に分かれた値を得られます。 write_8(int reg,int data)、write_16(int reg,int data)で レジスターregに8bit、16bit 書き込み、成功したら0、失敗したら1を返します。確認はread_8、read_16を用いています。 conect() でデバイスが使用可能かを調べ、見つかれば0、なければ1を返します。 reg_reset() で全てのレジスタの値を0にします。成功したら0、失敗したらその時点で処理を停止し、失敗したレジスタを返します。 レジスタは公式のデータシートに書いてあるものと文字は同じです。 もしかしたらうまく行かない関数があるかもしれません。次のアプデのときに修正予定です。
Dependents: Nucleo_L3GD20_MMA7361_Kalman
l3gd20.h@12:438175c58b44, 2016-09-30 (annotated)
- Committer:
- hirokimineshita
- Date:
- Fri Sep 30 04:14:43 2016 +0000
- Revision:
- 12:438175c58b44
- Parent:
- 11:11177c8a7915
+a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hirokimineshita | 0:f2770227e24c | 1 | #ifndef _L3GD20_H_20150124_1526_ |
hirokimineshita | 0:f2770227e24c | 2 | #define _L3GD20_H_20150124_1526_ |
hirokimineshita | 0:f2770227e24c | 3 | |
hirokimineshita | 1:354deb9168c0 | 4 | #include "binary.h" |
hirokimineshita | 1:354deb9168c0 | 5 | |
hirokimineshita | 0:f2770227e24c | 6 | #define WHO_AM_I 0x0F |
hirokimineshita | 0:f2770227e24c | 7 | #define CTRL_REG1 0x20 |
hirokimineshita | 0:f2770227e24c | 8 | #define CTRL_REG2 0x21 |
hirokimineshita | 0:f2770227e24c | 9 | #define CTRL_REG3 0x22 |
hirokimineshita | 0:f2770227e24c | 10 | #define CTRL_REG4 0x23 |
hirokimineshita | 0:f2770227e24c | 11 | #define CTRL_REG5 0x24 |
hirokimineshita | 0:f2770227e24c | 12 | #define REFERENCE 0x25 |
hirokimineshita | 0:f2770227e24c | 13 | #define OUT_TEMP 0x26 |
hirokimineshita | 0:f2770227e24c | 14 | #define STATUS_REG 0x27 |
hirokimineshita | 0:f2770227e24c | 15 | #define OUT_X_L 0x28 |
hirokimineshita | 0:f2770227e24c | 16 | #define OUT_X_H 0x29 |
hirokimineshita | 0:f2770227e24c | 17 | #define OUT_Y_L 0x2A |
hirokimineshita | 0:f2770227e24c | 18 | #define OUT_Y_H 0x2B |
hirokimineshita | 0:f2770227e24c | 19 | #define OUT_Z_L 0x2C |
hirokimineshita | 0:f2770227e24c | 20 | #define OUT_Z_H 0x2D |
hirokimineshita | 0:f2770227e24c | 21 | #define FIFO_CTRL_REG 0x2E |
hirokimineshita | 0:f2770227e24c | 22 | #define FIFO_SRC_REG 0x2F |
hirokimineshita | 0:f2770227e24c | 23 | #define INT1_CFG 0x30 |
hirokimineshita | 0:f2770227e24c | 24 | #define INT1_SRC 0x31 |
hirokimineshita | 0:f2770227e24c | 25 | #define INT1_TSH_XH 0x32 |
hirokimineshita | 0:f2770227e24c | 26 | #define INT1_TSH_XL 0x33 |
hirokimineshita | 0:f2770227e24c | 27 | #define INT1_TSH_YH 0x34 |
hirokimineshita | 0:f2770227e24c | 28 | #define INT1_TSH_YL 0x35 |
hirokimineshita | 0:f2770227e24c | 29 | #define INT1_TSH_ZH 0x36 |
hirokimineshita | 0:f2770227e24c | 30 | #define INT1_TSH_ZL 0x37 |
hirokimineshita | 0:f2770227e24c | 31 | #define INT1_DURATION 0x38 |
hirokimineshita | 0:f2770227e24c | 32 | #define READ 0x80 |
hirokimineshita | 0:f2770227e24c | 33 | #define WRITE 0x00 |
hirokimineshita | 1:354deb9168c0 | 34 | #define X 0 |
hirokimineshita | 1:354deb9168c0 | 35 | #define Y 2 |
hirokimineshita | 1:354deb9168c0 | 36 | #define Z 4 |
hirokimineshita | 0:f2770227e24c | 37 | |
hirokimineshita | 8:ed3bb603fe71 | 38 | /** This progrum is to be easy to control L3GD20 sensor\n |
hirokimineshita | 6:26002aa6f35e | 39 | * site http://akizukidenshi.com/catalog/g/gK-06779/ |
hirokimineshita | 2:de6dc4af9b75 | 40 | */ |
hirokimineshita | 0:f2770227e24c | 41 | class l3gd20{ |
hirokimineshita | 0:f2770227e24c | 42 | private: |
hirokimineshita | 0:f2770227e24c | 43 | SPI l3gdev; |
hirokimineshita | 0:f2770227e24c | 44 | DigitalOut l3gcs; |
hirokimineshita | 1:354deb9168c0 | 45 | Serial pcdev; |
hirokimineshita | 1:354deb9168c0 | 46 | int scale; |
hirokimineshita | 0:f2770227e24c | 47 | public: |
hirokimineshita | 2:de6dc4af9b75 | 48 | /** constracta |
hirokimineshita | 3:76a16de2531d | 49 | * |
hirokimineshita | 8:ed3bb603fe71 | 50 | * use default parm, mosi,miso,sck,cs is set to PB_15-PB_12\n |
hirokimineshita | 10:423afed6d24a | 51 | * SPI frequency:1Mhz\n |
hirokimineshita | 2:de6dc4af9b75 | 52 | * SPI mode:3 |
hirokimineshita | 2:de6dc4af9b75 | 53 | */ |
hirokimineshita | 0:f2770227e24c | 54 | l3gd20(); |
hirokimineshita | 2:de6dc4af9b75 | 55 | |
hirokimineshita | 2:de6dc4af9b75 | 56 | /** constracta |
hirokimineshita | 10:423afed6d24a | 57 | * |
hirokimineshita | 8:ed3bb603fe71 | 58 | * only set frequency parm\n |
hirokimineshita | 8:ed3bb603fe71 | 59 | * mosi,miso,sck,cs is set to PB_15-PB_12\n |
hirokimineshita | 2:de6dc4af9b75 | 60 | * SPI mode:3 |
hirokimineshita | 2:de6dc4af9b75 | 61 | * @param fhz SPI frequency |
hirokimineshita | 2:de6dc4af9b75 | 62 | */ |
hirokimineshita | 0:f2770227e24c | 63 | l3gd20(int fhz); |
hirokimineshita | 8:ed3bb603fe71 | 64 | |
hirokimineshita | 8:ed3bb603fe71 | 65 | /** constracta |
hirokimineshita | 10:423afed6d24a | 66 | * |
hirokimineshita | 8:ed3bb603fe71 | 67 | * mosi,miso,sck is set to PB_15-PB_13\n |
hirokimineshita | 8:ed3bb603fe71 | 68 | * SPI mode:3 |
hirokimineshita | 9:2989e90f78fe | 69 | * @param cs SPI's cs pin |
hirokimineshita | 8:ed3bb603fe71 | 70 | * @param fhz SPI frequency |
hirokimineshita | 8:ed3bb603fe71 | 71 | */ |
hirokimineshita | 0:f2770227e24c | 72 | l3gd20(PinName cs,int fhz); |
hirokimineshita | 10:423afed6d24a | 73 | |
hirokimineshita | 10:423afed6d24a | 74 | /** constracta |
hirokimineshita | 10:423afed6d24a | 75 | * |
hirokimineshita | 10:423afed6d24a | 76 | * set all param, you can ellipsis SPI's mode |
hirokimineshita | 10:423afed6d24a | 77 | * @param miso SPI's miso pin |
hirokimineshita | 10:423afed6d24a | 78 | * @param mosi SPI's mosi pin |
hirokimineshita | 10:423afed6d24a | 79 | * @param sck SPI's sck pin |
hirokimineshita | 10:423afed6d24a | 80 | * @param cs SPI's cs pin |
hirokimineshita | 10:423afed6d24a | 81 | * @param fhz SPI frequency |
hirokimineshita | 10:423afed6d24a | 82 | * @param mode SPI's mode (default = 3) |
hirokimineshita | 10:423afed6d24a | 83 | */ |
hirokimineshita | 2:de6dc4af9b75 | 84 | l3gd20(PinName miso,PinName mosi,PinName sck,PinName cs,int fhz,int mode = 3); |
hirokimineshita | 10:423afed6d24a | 85 | |
hirokimineshita | 10:423afed6d24a | 86 | /** function |
hirokimineshita | 10:423afed6d24a | 87 | * |
hirokimineshita | 10:423afed6d24a | 88 | * reset all register |
hirokimineshita | 12:438175c58b44 | 89 | * @return register number which return error (int) |
hirokimineshita | 10:423afed6d24a | 90 | */ |
hirokimineshita | 0:f2770227e24c | 91 | int reg_reset(); |
hirokimineshita | 11:11177c8a7915 | 92 | |
hirokimineshita | 11:11177c8a7915 | 93 | /** function |
hirokimineshita | 11:11177c8a7915 | 94 | * |
hirokimineshita | 11:11177c8a7915 | 95 | * send WHO_AM_I and return result |
hirokimineshita | 12:438175c58b44 | 96 | * @return 1:error 0:no error (int) |
hirokimineshita | 11:11177c8a7915 | 97 | */ |
hirokimineshita | 0:f2770227e24c | 98 | int conect(); |
hirokimineshita | 12:438175c58b44 | 99 | |
hirokimineshita | 12:438175c58b44 | 100 | /** function |
hirokimineshita | 12:438175c58b44 | 101 | * |
hirokimineshita | 12:438175c58b44 | 102 | * read 8bit register which set |
hirokimineshita | 12:438175c58b44 | 103 | * @param reg register number of what you want |
hirokimineshita | 12:438175c58b44 | 104 | * @return result (uint8_t) |
hirokimineshita | 12:438175c58b44 | 105 | */ |
hirokimineshita | 0:f2770227e24c | 106 | uint8_t read_8(uint8_t reg); |
hirokimineshita | 12:438175c58b44 | 107 | |
hirokimineshita | 12:438175c58b44 | 108 | /** function |
hirokimineshita | 12:438175c58b44 | 109 | * |
hirokimineshita | 12:438175c58b44 | 110 | * read 16bit register which set |
hirokimineshita | 12:438175c58b44 | 111 | * @param reg register number of what you want |
hirokimineshita | 12:438175c58b44 | 112 | * @return result (uint8_t) |
hirokimineshita | 12:438175c58b44 | 113 | */ |
hirokimineshita | 0:f2770227e24c | 114 | uint16_t read_16(uint8_t reg); |
hirokimineshita | 12:438175c58b44 | 115 | |
hirokimineshita | 12:438175c58b44 | 116 | /** function |
hirokimineshita | 12:438175c58b44 | 117 | * |
hirokimineshita | 12:438175c58b44 | 118 | * write 8bit to the register and check the register is realy set |
hirokimineshita | 12:438175c58b44 | 119 | * @param reg register number of what you want to write |
hirokimineshita | 12:438175c58b44 | 120 | * @param data register data what you want to write |
hirokimineshita | 12:438175c58b44 | 121 | * @return result 1:error 0:no error |
hirokimineshita | 12:438175c58b44 | 122 | */ |
hirokimineshita | 0:f2770227e24c | 123 | int write_8(uint8_t reg,uint8_t data); |
hirokimineshita | 12:438175c58b44 | 124 | |
hirokimineshita | 12:438175c58b44 | 125 | /** function |
hirokimineshita | 12:438175c58b44 | 126 | * |
hirokimineshita | 12:438175c58b44 | 127 | * only write 8bit to the register |
hirokimineshita | 12:438175c58b44 | 128 | * @param reg register number of what you want to write |
hirokimineshita | 12:438175c58b44 | 129 | * @param data register data what you want to write |
hirokimineshita | 12:438175c58b44 | 130 | */ |
hirokimineshita | 1:354deb9168c0 | 131 | void s_write_8(uint8_t reg,uint8_t data); |
hirokimineshita | 12:438175c58b44 | 132 | |
hirokimineshita | 12:438175c58b44 | 133 | /** function |
hirokimineshita | 12:438175c58b44 | 134 | * |
hirokimineshita | 12:438175c58b44 | 135 | * write 16bit to the register and check the register is realy set |
hirokimineshita | 12:438175c58b44 | 136 | * @param reg register number of what you want to write |
hirokimineshita | 12:438175c58b44 | 137 | * @param data register data what you want to write |
hirokimineshita | 12:438175c58b44 | 138 | * @return result 1:error 0:no error |
hirokimineshita | 12:438175c58b44 | 139 | */ |
hirokimineshita | 0:f2770227e24c | 140 | int write_16(uint8_t reg,uint16_t data); |
hirokimineshita | 12:438175c58b44 | 141 | |
hirokimineshita | 12:438175c58b44 | 142 | /** function |
hirokimineshita | 12:438175c58b44 | 143 | * |
hirokimineshita | 12:438175c58b44 | 144 | * now writing |
hirokimineshita | 12:438175c58b44 | 145 | */ |
hirokimineshita | 1:354deb9168c0 | 146 | int get_scale(); |
hirokimineshita | 12:438175c58b44 | 147 | |
hirokimineshita | 12:438175c58b44 | 148 | /** function |
hirokimineshita | 12:438175c58b44 | 149 | * |
hirokimineshita | 12:438175c58b44 | 150 | * now writing |
hirokimineshita | 12:438175c58b44 | 151 | */ |
hirokimineshita | 1:354deb9168c0 | 152 | int set_scale(int dps); |
hirokimineshita | 12:438175c58b44 | 153 | |
hirokimineshita | 12:438175c58b44 | 154 | /** function |
hirokimineshita | 12:438175c58b44 | 155 | * |
hirokimineshita | 12:438175c58b44 | 156 | * now writing |
hirokimineshita | 12:438175c58b44 | 157 | */ |
hirokimineshita | 1:354deb9168c0 | 158 | int get_temp(); |
hirokimineshita | 12:438175c58b44 | 159 | |
hirokimineshita | 12:438175c58b44 | 160 | /** function |
hirokimineshita | 12:438175c58b44 | 161 | * |
hirokimineshita | 12:438175c58b44 | 162 | * now writing |
hirokimineshita | 12:438175c58b44 | 163 | */ |
hirokimineshita | 1:354deb9168c0 | 164 | int16_t get_rotate_by_bit(int axis); |
hirokimineshita | 12:438175c58b44 | 165 | |
hirokimineshita | 12:438175c58b44 | 166 | /** function |
hirokimineshita | 12:438175c58b44 | 167 | * |
hirokimineshita | 12:438175c58b44 | 168 | * now writing |
hirokimineshita | 12:438175c58b44 | 169 | */ |
hirokimineshita | 1:354deb9168c0 | 170 | float get_rotate_by_rad(int axis); |
hirokimineshita | 12:438175c58b44 | 171 | |
hirokimineshita | 12:438175c58b44 | 172 | /** function |
hirokimineshita | 12:438175c58b44 | 173 | * |
hirokimineshita | 12:438175c58b44 | 174 | * now writing |
hirokimineshita | 12:438175c58b44 | 175 | */ |
hirokimineshita | 1:354deb9168c0 | 176 | float get_rotate(int axis); |
hirokimineshita | 12:438175c58b44 | 177 | |
hirokimineshita | 12:438175c58b44 | 178 | /** function |
hirokimineshita | 12:438175c58b44 | 179 | * |
hirokimineshita | 12:438175c58b44 | 180 | * now writing |
hirokimineshita | 12:438175c58b44 | 181 | */ |
hirokimineshita | 1:354deb9168c0 | 182 | float deg_to_rad(float deg); |
hirokimineshita | 0:f2770227e24c | 183 | }; |
hirokimineshita | 0:f2770227e24c | 184 | |
hirokimineshita | 0:f2770227e24c | 185 | #endif |