航空研究会 / L6470_status_check
Committer:
imanomadao
Date:
Thu Feb 20 03:48:07 2020 +0000
Revision:
1:6cd6cd00aabe
Parent:
0:2f3be153718a
Child:
2:09ae6c1aab1a
aiueo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
imanomadao 1:6cd6cd00aabe 1 #ifndef _L6470_H_
imanomadao 0:2f3be153718a 2 #define _L6470_H_
imanomadao 0:2f3be153718a 3
imanomadao 0:2f3be153718a 4 #include"mbed.h"
imanomadao 0:2f3be153718a 5
imanomadao 0:2f3be153718a 6 //L6470 Registers
imanomadao 0:2f3be153718a 7 #define ABS_POS 0x01 // Current position
imanomadao 0:2f3be153718a 8 #define EL_POS 0x02 // Electrical position
imanomadao 0:2f3be153718a 9 #define MARK 0x03
imanomadao 0:2f3be153718a 10 #define SPEED 0x04
imanomadao 0:2f3be153718a 11 #define ACC 0x05
imanomadao 0:2f3be153718a 12 #define DEC 0x06
imanomadao 0:2f3be153718a 13 #define MAX_SPEED 0x07
imanomadao 0:2f3be153718a 14 #define MIN_SPEED 0x08
imanomadao 0:2f3be153718a 15 #define FS_SPD 0x15
imanomadao 0:2f3be153718a 16 #define KVAL_HOLD 0x09
imanomadao 0:2f3be153718a 17 #define KVAL_RUN 0x0A
imanomadao 0:2f3be153718a 18 #define KVAL_ACC 0x0B
imanomadao 0:2f3be153718a 19 #define KVAL_DEC 0x0C
imanomadao 0:2f3be153718a 20 #define INT_SPEED 0x0D
imanomadao 0:2f3be153718a 21 #define ST_SLP 0x0E
imanomadao 0:2f3be153718a 22 #define FN_SLP_ACC 0x0F
imanomadao 0:2f3be153718a 23 #define FN_SLP_DEC 0x10
imanomadao 0:2f3be153718a 24 #define K_THERM 0x11
imanomadao 0:2f3be153718a 25 #define ADC_OUT 0x12
imanomadao 0:2f3be153718a 26 #define OCD_TH 0x13
imanomadao 0:2f3be153718a 27 #define STALL_TH 0x14
imanomadao 0:2f3be153718a 28 #define STEP_MODE 0x16
imanomadao 0:2f3be153718a 29 #define ALARM_EN 0x17
imanomadao 0:2f3be153718a 30 #define CONFIG 0x18
imanomadao 0:2f3be153718a 31 #define STATUS 0x19
imanomadao 0:2f3be153718a 32
imanomadao 0:2f3be153718a 33 //L6470 Commands
imanomadao 0:2f3be153718a 34 #define NOP 0x000
imanomadao 0:2f3be153718a 35 #define SetParam 0x0<<5 // ( | PARAM )
imanomadao 0:2f3be153718a 36 #define GetParam 0x01<<5 // ( | PARAM )
imanomadao 0:2f3be153718a 37 #define Run_Forward 0x50
imanomadao 0:2f3be153718a 38 #define Run_Back 0x51
imanomadao 0:2f3be153718a 39 #define Step_Clock_Forward 0x58
imanomadao 0:2f3be153718a 40 #define Step_Clock_Back 0x59
imanomadao 0:2f3be153718a 41 #define Move_Forward 0x40
imanomadao 0:2f3be153718a 42 #define Move_Back 0x41
imanomadao 0:2f3be153718a 43 #define GoTo 0x60
imanomadao 0:2f3be153718a 44 #define GoTo_DIR_Forward 0x68
imanomadao 0:2f3be153718a 45 #define GoTo_DIR_Back 0x69
imanomadao 0:2f3be153718a 46 //#define GoUntill
imanomadao 0:2f3be153718a 47 //#define ReleseSW
imanomadao 0:2f3be153718a 48 #define GoHome 0x70
imanomadao 0:2f3be153718a 49 #define GoMark 0x78
imanomadao 0:2f3be153718a 50 #define ResetPos 0xD8
imanomadao 0:2f3be153718a 51 #define ResetDevice 0xC0
imanomadao 0:2f3be153718a 52 #define SoftStop 0xB0
imanomadao 0:2f3be153718a 53 #define HardStop 0xB8
imanomadao 0:2f3be153718a 54 #define SoftHiZ 0xA0
imanomadao 0:2f3be153718a 55 #define HardHiz 0xA8
imanomadao 0:2f3be153718a 56 #define GetStatus 0xD8
imanomadao 0:2f3be153718a 57
imanomadao 0:2f3be153718a 58
imanomadao 0:2f3be153718a 59
imanomadao 0:2f3be153718a 60
imanomadao 0:2f3be153718a 61 class L6470
imanomadao 0:2f3be153718a 62 {
imanomadao 0:2f3be153718a 63 SPI _spi;
imanomadao 0:2f3be153718a 64 DigitalOut _cs;
imanomadao 0:2f3be153718a 65
imanomadao 0:2f3be153718a 66 public:
imanomadao 0:2f3be153718a 67 L6470(PinName mosi, PinName miso, PinName sclk, PinName cs):_spi(mosi, miso, sclk),_cs(cs)
imanomadao 0:2f3be153718a 68 {
imanomadao 0:2f3be153718a 69 _spi.format(8,2);
imanomadao 0:2f3be153718a 70 _spi.frequency(4960000);
imanomadao 0:2f3be153718a 71 }
imanomadao 0:2f3be153718a 72 //~L6470();
imanomadao 0:2f3be153718a 73
imanomadao 0:2f3be153718a 74
imanomadao 0:2f3be153718a 75 //L6470(PinName mosi, PinName miso, PinName sclk, PinName ssel);
imanomadao 0:2f3be153718a 76
imanomadao 0:2f3be153718a 77 void init()
imanomadao 0:2f3be153718a 78 {
imanomadao 0:2f3be153718a 79 // L6470のspi通信の有効化
imanomadao 1:6cd6cd00aabe 80 //_spi.format(8,2);
imanomadao 1:6cd6cd00aabe 81 //_spi.frequency(4960000);
imanomadao 0:2f3be153718a 82
imanomadao 0:2f3be153718a 83 // nopを送ることで残留している命令を排除
imanomadao 0:2f3be153718a 84 _cs = 1;
imanomadao 0:2f3be153718a 85 _spi.write(0x00);
imanomadao 0:2f3be153718a 86 _spi.write(0x00);
imanomadao 0:2f3be153718a 87 _spi.write(0x00);
imanomadao 0:2f3be153718a 88 _spi.write(0x00);
imanomadao 0:2f3be153718a 89 _cs = 0;
imanomadao 0:2f3be153718a 90
imanomadao 0:2f3be153718a 91 //リセット
imanomadao 0:2f3be153718a 92 _cs = 1;
imanomadao 0:2f3be153718a 93 _spi.write(0xC0);
imanomadao 0:2f3be153718a 94 _cs = 0;
imanomadao 0:2f3be153718a 95
imanomadao 0:2f3be153718a 96 // MAX_SPEED設定。
imanomadao 0:2f3be153718a 97 _cs = 1;
imanomadao 0:2f3be153718a 98 _spi.write(0x07);
imanomadao 0:2f3be153718a 99 // 最大回転スピード値(10bit) 初期値は 0x41
imanomadao 0:2f3be153718a 100 _spi.write(0x00);
imanomadao 0:2f3be153718a 101 _spi.write(0x23);
imanomadao 0:2f3be153718a 102 _cs = 0;
imanomadao 0:2f3be153718a 103
imanomadao 0:2f3be153718a 104 // KVAL_HOLD設定。
imanomadao 0:2f3be153718a 105 /// レジスタアドレス。
imanomadao 0:2f3be153718a 106 _cs = 1;
imanomadao 0:2f3be153718a 107 _spi.write(0x09);
imanomadao 0:2f3be153718a 108 _spi.write(0xFF);
imanomadao 0:2f3be153718a 109 _cs = 0;
imanomadao 0:2f3be153718a 110
imanomadao 0:2f3be153718a 111 // KVAL_spiUN設定。
imanomadao 0:2f3be153718a 112 /// レジスタアドレス。
imanomadao 0:2f3be153718a 113 _cs = 1;
imanomadao 0:2f3be153718a 114 _spi.write(0x0A);
imanomadao 0:2f3be153718a 115 _spi.write(0xFF);
imanomadao 0:2f3be153718a 116 _cs = 0;
imanomadao 0:2f3be153718a 117
imanomadao 0:2f3be153718a 118 // KVAL_ACC設定。
imanomadao 0:2f3be153718a 119 _cs = 1;
imanomadao 0:2f3be153718a 120 _spi.write(0x0B);
imanomadao 0:2f3be153718a 121 _spi.write(0xFF);
imanomadao 0:2f3be153718a 122 _cs = 0;
imanomadao 0:2f3be153718a 123
imanomadao 0:2f3be153718a 124 // KVAL_DEC設定。
imanomadao 0:2f3be153718a 125 /// レジスタアドレス。
imanomadao 0:2f3be153718a 126 _cs= 1;
imanomadao 0:2f3be153718a 127 _spi.write(0x0C);
imanomadao 0:2f3be153718a 128 _spi.write(0x40);
imanomadao 0:2f3be153718a 129 _cs = 0;
imanomadao 0:2f3be153718a 130
imanomadao 0:2f3be153718a 131 // OCD_TH設定。
imanomadao 0:2f3be153718a 132 /// レジスタアドレス。
imanomadao 0:2f3be153718a 133 _cs = 1;
imanomadao 0:2f3be153718a 134 _spi.write(0x13);
imanomadao 0:2f3be153718a 135 _spi.write(0x0F);
imanomadao 0:2f3be153718a 136 _cs = 0;
imanomadao 0:2f3be153718a 137
imanomadao 0:2f3be153718a 138 // STALL_TH設定。
imanomadao 0:2f3be153718a 139 /// レジスタアドレス。
imanomadao 0:2f3be153718a 140 _cs = 1;
imanomadao 0:2f3be153718a 141 _spi.write(0x14);
imanomadao 0:2f3be153718a 142 _spi.write(0x7F);
imanomadao 0:2f3be153718a 143 _cs = 0;
imanomadao 0:2f3be153718a 144 };
imanomadao 0:2f3be153718a 145 //void writeByte(uint8_t);
imanomadao 0:2f3be153718a 146 void writeByte(uint8_t data)
imanomadao 0:2f3be153718a 147 {
imanomadao 0:2f3be153718a 148 _cs = 1;
imanomadao 0:2f3be153718a 149 _spi.write(data);
imanomadao 0:2f3be153718a 150 _cs = 0;
imanomadao 0:2f3be153718a 151 }
imanomadao 0:2f3be153718a 152
imanomadao 0:2f3be153718a 153 void writeBytes(uint8_t add, int bytes, uint32_t value)
imanomadao 0:2f3be153718a 154 {
imanomadao 0:2f3be153718a 155 uint32_t data[3];
imanomadao 0:2f3be153718a 156 for (int i = 0; i < bytes; i++){
imanomadao 0:2f3be153718a 157 data[i] = value & 0xff;
imanomadao 0:2f3be153718a 158 value = value >> 8;
imanomadao 0:2f3be153718a 159 }
imanomadao 0:2f3be153718a 160 if(bytes == 3){
imanomadao 0:2f3be153718a 161 _cs = 1;
imanomadao 0:2f3be153718a 162 _spi.write(add);
imanomadao 0:2f3be153718a 163 _spi.write(data[2]);
imanomadao 0:2f3be153718a 164 _spi.write(data[1]);
imanomadao 0:2f3be153718a 165 _spi.write(data[0]);
imanomadao 0:2f3be153718a 166 _cs = 0;
imanomadao 0:2f3be153718a 167 }
imanomadao 0:2f3be153718a 168 else if ( bytes == 2 ) {
imanomadao 0:2f3be153718a 169 _cs = 1;
imanomadao 0:2f3be153718a 170 _spi.write( add );
imanomadao 0:2f3be153718a 171 _spi.write( data[1] ) ;
imanomadao 0:2f3be153718a 172 _spi.write( data[0] ) ;
imanomadao 0:2f3be153718a 173 _cs = 0;
imanomadao 0:2f3be153718a 174 }
imanomadao 0:2f3be153718a 175 else if ( bytes == 1 ) {
imanomadao 0:2f3be153718a 176 _cs = 1;
imanomadao 0:2f3be153718a 177 _spi.write( add );
imanomadao 0:2f3be153718a 178 _spi.write( data[0] ) ;
imanomadao 0:2f3be153718a 179 _cs = 0;
imanomadao 0:2f3be153718a 180 }
imanomadao 0:2f3be153718a 181 }
imanomadao 0:2f3be153718a 182
imanomadao 0:2f3be153718a 183 //void writeBytes(uint8_t, int, uint32_t);
imanomadao 0:2f3be153718a 184 void L6470_run(uint32_t speed)
imanomadao 0:2f3be153718a 185 {
imanomadao 0:2f3be153718a 186 uint8_t dir;
imanomadao 0:2f3be153718a 187 uint32_t spd;
imanomadao 0:2f3be153718a 188 uint8_t spd_h;
imanomadao 0:2f3be153718a 189 uint8_t spd_m;
imanomadao 0:2f3be153718a 190 uint8_t spd_l;
imanomadao 0:2f3be153718a 191
imanomadao 0:2f3be153718a 192 // 方向検出。
imanomadao 0:2f3be153718a 193 if (speed < 0)
imanomadao 0:2f3be153718a 194 {
imanomadao 0:2f3be153718a 195 dir = 0x50;
imanomadao 0:2f3be153718a 196 spd = -1 * speed;
imanomadao 0:2f3be153718a 197 }
imanomadao 0:2f3be153718a 198 else
imanomadao 0:2f3be153718a 199 {
imanomadao 0:2f3be153718a 200 dir = 0x51;
imanomadao 0:2f3be153718a 201 spd = speed;
imanomadao 0:2f3be153718a 202 }
imanomadao 0:2f3be153718a 203
imanomadao 0:2f3be153718a 204 // 送信バイトデータ生成。
imanomadao 0:2f3be153718a 205 spd_h = (uint8_t)((0x000F0000 & spd) >> 16);
imanomadao 0:2f3be153718a 206 spd_m = (uint8_t)((0x0000FF00 & spd) >> 8);
imanomadao 0:2f3be153718a 207 spd_l = (uint8_t)(0x000000FF & spd);
imanomadao 0:2f3be153718a 208 // コマンド(レジスタアドレス)送信。
imanomadao 0:2f3be153718a 209 _cs = 1;
imanomadao 0:2f3be153718a 210 _spi.write(dir);
imanomadao 0:2f3be153718a 211 _spi.write(spd_h);
imanomadao 0:2f3be153718a 212 _spi.write(spd_m);
imanomadao 0:2f3be153718a 213 _spi.write(spd_l);
imanomadao 0:2f3be153718a 214 _cs = 0;
imanomadao 0:2f3be153718a 215 }
imanomadao 0:2f3be153718a 216
imanomadao 0:2f3be153718a 217 //void L6470_run(uint32_t);
imanomadao 0:2f3be153718a 218
imanomadao 0:2f3be153718a 219 };
imanomadao 0:2f3be153718a 220
imanomadao 0:2f3be153718a 221
imanomadao 0:2f3be153718a 222 #endif