a
Embed:
(wiki syntax)
Show/hide line numbers
L6470.h
00001 /*#ifndef _L6470_H_ 00002 #define _L6470_H_ 00003 00004 #include"mbed.h" 00005 00006 //L6470 Registers 00007 #define ABS_POS 0x01 // Current position 00008 #define EL_POS 0x02 // Electrical position 00009 #define MARK 0x03 00010 #define SPEED 0x04 00011 #define ACC 0x05 00012 #define DEC 0x06 00013 #define MAX_SPEED 0x07 00014 #define MIN_SPEED 0x08 00015 #define FS_SPD 0x15 00016 #define KVAL_HOLD 0x09 00017 #define KVAL_RUN 0x0A 00018 #define KVAL_ACC 0x0B 00019 #define KVAL_DEC 0x0C 00020 #define INT_SPEED 0x0D 00021 #define ST_SLP 0x0E 00022 #define FN_SLP_ACC 0x0F 00023 #define FN_SLP_DEC 0x10 00024 #define K_THERM 0x11 00025 #define ADC_OUT 0x12 00026 #define OCD_TH 0x13 00027 #define STALL_TH 0x14 00028 #define STEP_MODE 0x16 00029 #define ALARM_EN 0x17 00030 #define CONFIG 0x18 00031 #define STATUS 0x19 00032 00033 //L6470 Commands 00034 #define NOP 0x000 00035 #define SetParam 0x0<<5 // ( | PARAM ) 00036 #define GetParam 0x01<<5 // ( | PARAM ) 00037 #define Run_Forward 0x50 00038 #define Run_Back 0x51 00039 #define Step_Clock_Forward 0x58 00040 #define Step_Clock_Back 0x59 00041 #define Move_Forward 0x40 00042 #define Move_Back 0x41 00043 #define GoTo 0x60 00044 #define GoTo_DIR_Forward 0x68 00045 #define GoTo_DIR_Back 0x69 00046 //#define GoUntill 00047 //#define ReleseSW 00048 #define GoHome 0x70 00049 #define GoMark 0x78 00050 #define ResetPos 0xD8 00051 #define ResetDevice 0xC0 00052 #define SoftStop 0xB0 00053 #define HardStop 0xB8 00054 #define SoftHiZ 0xA0 00055 #define HardHiz 0xA8 00056 #define GetStatus 0xD8 00057 00058 00059 00060 00061 class L6470 00062 { 00063 SPI _spi; 00064 DigitalOut _cs; 00065 00066 public: 00067 L6470(PinName mosi, PinName miso, PinName sclk, PinName cs):_spi(mosi, miso, sclk),_cs(cs) 00068 { 00069 _spi.format(8,2); 00070 _spi.frequency(4960000); 00071 } 00072 //~L6470(); 00073 00074 00075 //L6470(PinName mosi, PinName miso, PinName sclk, PinName ssel); 00076 00077 void init() 00078 { 00079 // L6470のspi通信の有効化 00080 //spi.format(8,2); 00081 //spi.frequency(4960000); 00082 00083 // nopを送ることで残留している命令を排除 00084 _cs = 1; 00085 _spi.write(0x00); 00086 _spi.write(0x00); 00087 _spi.write(0x00); 00088 _spi.write(0x00); 00089 _cs = 0; 00090 00091 //リセット 00092 _cs = 1; 00093 _spi.write(0xC0); 00094 _cs = 0; 00095 00096 // MAX_SPEED設定。 00097 _cs = 1; 00098 _spi.write(0x07); 00099 // 最大回転スピード値(10bit) 初期値は 0x41 00100 _spi.write(0x00); 00101 _spi.write(0x23); 00102 _cs = 0; 00103 00104 // KVAL_HOLD設定。 00105 /// レジスタアドレス。 00106 _cs = 1; 00107 _spi.write(0x09); 00108 _spi.write(0xFF); 00109 _cs = 0; 00110 00111 // KVAL_spiUN設定。 00112 /// レジスタアドレス。 00113 _cs = 1; 00114 _spi.write(0x0A); 00115 _spi.write(0xFF); 00116 _cs = 0; 00117 00118 // KVAL_ACC設定。 00119 _cs = 1; 00120 _spi.write(0x0B); 00121 _spi.write(0xFF); 00122 _cs = 0; 00123 00124 // KVAL_DEC設定。 00125 /// レジスタアドレス。 00126 _cs= 1; 00127 _spi.write(0x0C); 00128 _spi.write(0x40); 00129 _cs = 0; 00130 00131 // OCD_TH設定。 00132 /// レジスタアドレス。 00133 _cs = 1; 00134 _spi.write(0x13); 00135 _spi.write(0x0F); 00136 _cs = 0; 00137 00138 // STALL_TH設定。 00139 /// レジスタアドレス。 00140 _cs = 1; 00141 _spi.write(0x14); 00142 _spi.write(0x7F); 00143 _cs = 0; 00144 }; 00145 //void writeByte(uint8_t); 00146 void writeByte(uint8_t data) 00147 { 00148 _cs = 1; 00149 _spi.write(data); 00150 _cs = 0; 00151 } 00152 00153 void writeBytes(uint8_t add, int bytes, uint32_t value) 00154 { 00155 uint32_t data[3]; 00156 for (int i = 0; i < bytes; i++){ 00157 data[i] = value & 0xff; 00158 value = value >> 8; 00159 } 00160 if(bytes == 3){ 00161 _cs = 1; 00162 _spi.write(add); 00163 _spi.write(data[2]); 00164 _spi.write(data[1]); 00165 _spi.write(data[0]); 00166 _cs = 0; 00167 } 00168 else if ( bytes == 2 ) { 00169 _cs = 1; 00170 _spi.write( add ); 00171 _spi.write( data[1] ) ; 00172 _spi.write( data[0] ) ; 00173 _cs = 0; 00174 } 00175 else if ( bytes == 1 ) { 00176 _cs = 1; 00177 _spi.write( add ); 00178 _spi.write( data[0] ) ; 00179 _cs = 0; 00180 } 00181 } 00182 00183 //void writeBytes(uint8_t, int, uint32_t); 00184 void L6470_run(uint32_t speed) 00185 { 00186 uint8_t dir; 00187 uint32_t spd; 00188 uint8_t spd_h; 00189 uint8_t spd_m; 00190 uint8_t spd_l; 00191 00192 // 方向検出。 00193 if (speed < 0) 00194 { 00195 dir = 0x50; 00196 spd = -1 * speed; 00197 } 00198 else 00199 { 00200 dir = 0x51; 00201 spd = speed; 00202 } 00203 00204 // 送信バイトデータ生成。 00205 spd_h = (uint8_t)((0x000F0000 & spd) >> 16); 00206 spd_m = (uint8_t)((0x0000FF00 & spd) >> 8); 00207 spd_l = (uint8_t)(0x000000FF & spd); 00208 // コマンド(レジスタアドレス)送信。 00209 _cs = 1; 00210 _spi.write(dir); 00211 _spi.write(spd_h); 00212 _spi.write(spd_m); 00213 _spi.write(spd_l); 00214 _cs = 0; 00215 } 00216 00217 //void L6470_run(uint32_t); 00218 00219 }; 00220 00221 00222 #endif
Generated on Fri Aug 26 2022 11:56:35 by
1.7.2