L6470 use library

Dependents:   CatPot_Main_ver1 CatPot_Main_F

Committer:
ryuna
Date:
Sat Dec 27 07:32:25 2014 +0000
Revision:
2:63e59749ff49
Parent:
0:f2854fe95fdd
???????????????????busy_flag???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ryuna 0:f2854fe95fdd 1 /*
ryuna 0:f2854fe95fdd 2 *mbed L6470 SPI Library
ryuna 0:f2854fe95fdd 3 */
ryuna 0:f2854fe95fdd 4
ryuna 0:f2854fe95fdd 5 #include "mbed.h"
ryuna 0:f2854fe95fdd 6 #include "L6470.h"
ryuna 0:f2854fe95fdd 7
ryuna 0:f2854fe95fdd 8
ryuna 0:f2854fe95fdd 9 L6470::L6470(PinName mosi, PinName miso, PinName sclk, PinName cs,PinName busy)
ryuna 0:f2854fe95fdd 10 : m_spi(mosi, miso, sclk), m_cs(cs),m_busy(busy,PullUp)
ryuna 0:f2854fe95fdd 11 {
ryuna 0:f2854fe95fdd 12 //定義時に行う
ryuna 0:f2854fe95fdd 13 m_cs = 1;
ryuna 0:f2854fe95fdd 14 m_spi.format(8,3);
ryuna 0:f2854fe95fdd 15 m_spi.frequency(1000000);
ryuna 0:f2854fe95fdd 16
ryuna 0:f2854fe95fdd 17 /*///前回の送りデータを流す
ryuna 0:f2854fe95fdd 18 send(0x00);
ryuna 0:f2854fe95fdd 19 send(0x00);
ryuna 0:f2854fe95fdd 20 send(0x00);
ryuna 0:f2854fe95fdd 21 send(0x00);
ryuna 0:f2854fe95fdd 22 /**/
ryuna 0:f2854fe95fdd 23
ryuna 0:f2854fe95fdd 24 }
ryuna 0:f2854fe95fdd 25 void L6470::send(uint8_t temp)
ryuna 0:f2854fe95fdd 26 {
ryuna 0:f2854fe95fdd 27 //while(!m_busy){
ryuna 0:f2854fe95fdd 28 //}
ryuna 0:f2854fe95fdd 29 m_cs = 0;//#cs = 1;
ryuna 0:f2854fe95fdd 30 temp = m_spi.write((uint8_t)temp);
ryuna 0:f2854fe95fdd 31 m_cs = 1;//#cs = 0;
ryuna 0:f2854fe95fdd 32
ryuna 0:f2854fe95fdd 33 wait_us(0.5);
ryuna 0:f2854fe95fdd 34
ryuna 0:f2854fe95fdd 35 }
ryuna 0:f2854fe95fdd 36
ryuna 0:f2854fe95fdd 37
ryuna 0:f2854fe95fdd 38 uint8_t L6470::send_r(uint8_t temp)
ryuna 0:f2854fe95fdd 39 {
ryuna 0:f2854fe95fdd 40 //while(!m_busy){
ryuna 0:f2854fe95fdd 41 //}
ryuna 0:f2854fe95fdd 42 m_cs = 0;//#cs = 1;
ryuna 0:f2854fe95fdd 43 temp = m_spi.write((uint8_t)temp);
ryuna 0:f2854fe95fdd 44 m_cs = 1;//#cs = 0;
ryuna 0:f2854fe95fdd 45
ryuna 0:f2854fe95fdd 46 wait_us(1);
ryuna 0:f2854fe95fdd 47 return temp;
ryuna 0:f2854fe95fdd 48 }
ryuna 0:f2854fe95fdd 49
ryuna 0:f2854fe95fdd 50 void L6470::send_bytes(uint8_t temp[], int i)
ryuna 0:f2854fe95fdd 51 {
ryuna 0:f2854fe95fdd 52 while(0< i--)
ryuna 0:f2854fe95fdd 53 {
ryuna 0:f2854fe95fdd 54 send(temp[i]);
ryuna 0:f2854fe95fdd 55 }
ryuna 0:f2854fe95fdd 56 }
ryuna 0:f2854fe95fdd 57
ryuna 0:f2854fe95fdd 58 void L6470::send_bytes_r(uint8_t temp[], int i)
ryuna 0:f2854fe95fdd 59 {
ryuna 0:f2854fe95fdd 60 while(0< i--)
ryuna 0:f2854fe95fdd 61 {
ryuna 0:f2854fe95fdd 62 temp[i] = send_r(temp[i]);
ryuna 0:f2854fe95fdd 63 }
ryuna 0:f2854fe95fdd 64 }
ryuna 0:f2854fe95fdd 65
ryuna 0:f2854fe95fdd 66
ryuna 0:f2854fe95fdd 67
ryuna 0:f2854fe95fdd 68
ryuna 0:f2854fe95fdd 69 void L6470::NOP()
ryuna 0:f2854fe95fdd 70 {
ryuna 0:f2854fe95fdd 71 send(0x00);
ryuna 0:f2854fe95fdd 72
ryuna 0:f2854fe95fdd 73 }
ryuna 0:f2854fe95fdd 74
ryuna 0:f2854fe95fdd 75 void L6470::SetParam(int param, int value)
ryuna 0:f2854fe95fdd 76 {
ryuna 0:f2854fe95fdd 77 int n = (param>>8)/8;//配列の数
ryuna 0:f2854fe95fdd 78 int m = (param>>8)%8;//nは割り算だからあまりの分の値が取れないためこちらをif文に活用
ryuna 0:f2854fe95fdd 79 if(!m){
ryuna 0:f2854fe95fdd 80 uint8_t temp[n+1];
ryuna 0:f2854fe95fdd 81 temp[n] = 0x00 | (uint8_t)(param & 0xFF);
ryuna 0:f2854fe95fdd 82 while(0 < n--){//比較後マイナス
ryuna 0:f2854fe95fdd 83 temp[n] = (uint8_t)(value >> 8*n )&0xFF;
ryuna 0:f2854fe95fdd 84 }
ryuna 0:f2854fe95fdd 85 send_bytes(temp,sizeof temp/sizeof temp[0]);
ryuna 0:f2854fe95fdd 86
ryuna 0:f2854fe95fdd 87 }else{//余りが出た場合. 例 22とか7
ryuna 0:f2854fe95fdd 88 uint8_t temp[n+2];
ryuna 0:f2854fe95fdd 89 temp[n+1] = 0x00 | (uint8_t)(param & 0xFF);
ryuna 0:f2854fe95fdd 90 temp[n] = (uint8_t)(value >> 8*n)&~(0xFF<<m);//FFをずらしたあとで1の補数変換で反転.
ryuna 0:f2854fe95fdd 91 while(0 < n--){
ryuna 0:f2854fe95fdd 92 temp[n]= (uint8_t)(value >> 8*n)&0xFF;
ryuna 0:f2854fe95fdd 93 }
ryuna 0:f2854fe95fdd 94 send_bytes(temp,sizeof temp/sizeof temp[0]);
ryuna 0:f2854fe95fdd 95 }
ryuna 0:f2854fe95fdd 96
ryuna 0:f2854fe95fdd 97 }
ryuna 0:f2854fe95fdd 98 int L6470::GetParam(int param)
ryuna 0:f2854fe95fdd 99 {
ryuna 0:f2854fe95fdd 100 int value = 0;
ryuna 0:f2854fe95fdd 101 int n = (param>>8)/8;//配列の数
ryuna 0:f2854fe95fdd 102 int m = (param>>8)%8;//nは割り算だからあまりの分の値が取れないためこちらをif文に活用
ryuna 0:f2854fe95fdd 103
ryuna 0:f2854fe95fdd 104 if(!m){
ryuna 0:f2854fe95fdd 105 uint8_t temp[n+1];
ryuna 0:f2854fe95fdd 106 for(int i = 0; i < n+1; i++){
ryuna 0:f2854fe95fdd 107 temp[i] = 0;
ryuna 0:f2854fe95fdd 108 }
ryuna 0:f2854fe95fdd 109 temp[n] = 0x20|(uint8_t)(param&0xFF);
ryuna 0:f2854fe95fdd 110 send_bytes_r(temp,sizeof temp/sizeof temp[0]);
ryuna 0:f2854fe95fdd 111 while(0 < n--){
ryuna 0:f2854fe95fdd 112 value |= (int)temp[n] << 8*n;
ryuna 0:f2854fe95fdd 113 }
ryuna 0:f2854fe95fdd 114
ryuna 0:f2854fe95fdd 115 }else{
ryuna 0:f2854fe95fdd 116 n++;
ryuna 0:f2854fe95fdd 117 uint8_t temp[n+1];
ryuna 0:f2854fe95fdd 118 for(int i = 0; i < n+2; i++){
ryuna 0:f2854fe95fdd 119 temp[i] = 0;
ryuna 0:f2854fe95fdd 120 }
ryuna 0:f2854fe95fdd 121 temp[n] = 0x20|(uint8_t)(param&0xFF);
ryuna 0:f2854fe95fdd 122 send_bytes_r(temp,sizeof temp/sizeof temp[0]);
ryuna 0:f2854fe95fdd 123 while(0 < n--){
ryuna 0:f2854fe95fdd 124 value |= (int)temp[n]<< 8*n;
ryuna 0:f2854fe95fdd 125 }
ryuna 0:f2854fe95fdd 126
ryuna 0:f2854fe95fdd 127 }
ryuna 0:f2854fe95fdd 128
ryuna 0:f2854fe95fdd 129 return value;
ryuna 0:f2854fe95fdd 130 }
ryuna 0:f2854fe95fdd 131
ryuna 0:f2854fe95fdd 132
ryuna 0:f2854fe95fdd 133
ryuna 0:f2854fe95fdd 134 void L6470::Run(bool dir, int speed)
ryuna 0:f2854fe95fdd 135 {
ryuna 0:f2854fe95fdd 136 uint8_t temp[4];
ryuna 0:f2854fe95fdd 137 temp[3] = 0x50|dir;
ryuna 0:f2854fe95fdd 138 temp[2] = (uint8_t)(speed >> 16)&0x0F;
ryuna 0:f2854fe95fdd 139 temp[1] = (uint8_t)(speed >> 8)&0xFF;
ryuna 0:f2854fe95fdd 140 temp[0] = (uint8_t)(speed >> 0)&0xFF;
ryuna 0:f2854fe95fdd 141 send_bytes(temp,sizeof temp/sizeof temp[0]);
ryuna 0:f2854fe95fdd 142
ryuna 0:f2854fe95fdd 143 }
ryuna 0:f2854fe95fdd 144
ryuna 0:f2854fe95fdd 145 void L6470::StepClock(bool dir)
ryuna 0:f2854fe95fdd 146 {
ryuna 0:f2854fe95fdd 147 send(0x58|dir);
ryuna 0:f2854fe95fdd 148 }
ryuna 0:f2854fe95fdd 149
ryuna 0:f2854fe95fdd 150 void L6470::Move(bool dir,int n_step )
ryuna 0:f2854fe95fdd 151 {
ryuna 0:f2854fe95fdd 152 uint8_t temp[4];
ryuna 0:f2854fe95fdd 153 temp[3] = 0x40|dir;
ryuna 0:f2854fe95fdd 154 temp[2] = (uint8_t)(n_step >> 16)&0x3F;
ryuna 0:f2854fe95fdd 155 temp[1] = (uint8_t)(n_step >> 8)&0xFF;
ryuna 0:f2854fe95fdd 156 temp[0] = (uint8_t)(n_step >> 0)&0xFF;
ryuna 0:f2854fe95fdd 157 send_bytes(temp,sizeof temp/sizeof temp[0]);
ryuna 0:f2854fe95fdd 158
ryuna 0:f2854fe95fdd 159 }
ryuna 0:f2854fe95fdd 160 void L6470::GoTo(int abs_pos)
ryuna 0:f2854fe95fdd 161 {
ryuna 0:f2854fe95fdd 162 uint8_t temp[4];
ryuna 0:f2854fe95fdd 163 temp[3] = 0x60;
ryuna 0:f2854fe95fdd 164 temp[2] = (uint8_t)(abs_pos >> 16)&0x3F;
ryuna 0:f2854fe95fdd 165 temp[1] = (uint8_t)(abs_pos >> 8)&0xFF;
ryuna 0:f2854fe95fdd 166 temp[0] = (uint8_t)(abs_pos >> 0)&0xFF;
ryuna 0:f2854fe95fdd 167 send_bytes(temp,sizeof temp/sizeof temp[0]);
ryuna 0:f2854fe95fdd 168
ryuna 0:f2854fe95fdd 169 }
ryuna 0:f2854fe95fdd 170
ryuna 0:f2854fe95fdd 171 void L6470::GoTo_dir(bool dir, int abs_pos)
ryuna 0:f2854fe95fdd 172 {
ryuna 0:f2854fe95fdd 173 uint8_t temp[4];
ryuna 0:f2854fe95fdd 174 temp[3] = 0x68|dir;
ryuna 0:f2854fe95fdd 175 temp[2] = (uint8_t)(abs_pos >> 16)&0x3F;
ryuna 0:f2854fe95fdd 176 temp[1] = (uint8_t)(abs_pos >> 8)&0xFF;
ryuna 0:f2854fe95fdd 177 temp[0] = (uint8_t)(abs_pos >> 0)&0xFF;
ryuna 0:f2854fe95fdd 178 send_bytes(temp,sizeof temp/sizeof temp[0]);
ryuna 0:f2854fe95fdd 179
ryuna 0:f2854fe95fdd 180 }
ryuna 0:f2854fe95fdd 181
ryuna 0:f2854fe95fdd 182 void L6470::GoUntill(bool act, bool dir, int speed)
ryuna 0:f2854fe95fdd 183 {
ryuna 0:f2854fe95fdd 184 uint8_t temp[4];
ryuna 0:f2854fe95fdd 185 temp[3] = 0x82|(act<<8)|dir;
ryuna 0:f2854fe95fdd 186 temp[2] = (uint8_t)(speed >> 16)&0x3F;
ryuna 0:f2854fe95fdd 187 temp[1] = (uint8_t)(speed >> 8)&0xFF;
ryuna 0:f2854fe95fdd 188 temp[0] = (uint8_t)(speed >> 0)&0xFF;
ryuna 0:f2854fe95fdd 189 send_bytes(temp,sizeof temp/sizeof temp[0]);
ryuna 0:f2854fe95fdd 190 }
ryuna 0:f2854fe95fdd 191
ryuna 0:f2854fe95fdd 192 void L6470::ReleseSW(bool act,bool dir)
ryuna 0:f2854fe95fdd 193 {
ryuna 0:f2854fe95fdd 194 send(0x92|(act <<3)|dir);
ryuna 0:f2854fe95fdd 195 }
ryuna 0:f2854fe95fdd 196 void L6470::GoHome()
ryuna 0:f2854fe95fdd 197 {
ryuna 0:f2854fe95fdd 198 send(0x70);
ryuna 0:f2854fe95fdd 199 }
ryuna 0:f2854fe95fdd 200
ryuna 0:f2854fe95fdd 201 void L6470::GoMark()
ryuna 0:f2854fe95fdd 202 {
ryuna 0:f2854fe95fdd 203 send(0x78);
ryuna 0:f2854fe95fdd 204 }
ryuna 0:f2854fe95fdd 205
ryuna 0:f2854fe95fdd 206 void L6470::ResetPos()
ryuna 0:f2854fe95fdd 207 {
ryuna 0:f2854fe95fdd 208 send(0xD8);
ryuna 0:f2854fe95fdd 209 }
ryuna 0:f2854fe95fdd 210
ryuna 0:f2854fe95fdd 211 void L6470::ResetDevice()
ryuna 0:f2854fe95fdd 212 {
ryuna 0:f2854fe95fdd 213 send(0xC0);
ryuna 0:f2854fe95fdd 214 }
ryuna 0:f2854fe95fdd 215
ryuna 0:f2854fe95fdd 216 void L6470::SoftStop()
ryuna 0:f2854fe95fdd 217 {
ryuna 0:f2854fe95fdd 218 send(0xB0);
ryuna 0:f2854fe95fdd 219 }
ryuna 0:f2854fe95fdd 220
ryuna 0:f2854fe95fdd 221 void L6470::HardStop()
ryuna 0:f2854fe95fdd 222 {
ryuna 0:f2854fe95fdd 223 send(0xB8);
ryuna 0:f2854fe95fdd 224 }
ryuna 0:f2854fe95fdd 225
ryuna 0:f2854fe95fdd 226 void L6470::SoftHiz()
ryuna 0:f2854fe95fdd 227 {
ryuna 0:f2854fe95fdd 228 send(0xA0);
ryuna 0:f2854fe95fdd 229 }
ryuna 0:f2854fe95fdd 230
ryuna 0:f2854fe95fdd 231 void L6470::HardHiz()
ryuna 0:f2854fe95fdd 232 {
ryuna 0:f2854fe95fdd 233 send(0xA8);
ryuna 0:f2854fe95fdd 234 }
ryuna 0:f2854fe95fdd 235
ryuna 0:f2854fe95fdd 236 void L6470::Resets()
ryuna 0:f2854fe95fdd 237 {
ryuna 0:f2854fe95fdd 238 SoftStop();
ryuna 0:f2854fe95fdd 239 wait(0.5);
ryuna 0:f2854fe95fdd 240 ResetDevice();
ryuna 0:f2854fe95fdd 241 SetParam(ABS_POS,INI_ABS_POS);
ryuna 0:f2854fe95fdd 242 SetParam(EL_POS,INI_EL_POS);
ryuna 0:f2854fe95fdd 243 SetParam(MARK,INI_MARK);
ryuna 0:f2854fe95fdd 244 SetParam(SPEED,INI_SPEED);
ryuna 0:f2854fe95fdd 245 SetParam(ACC,INI_ACC);
ryuna 0:f2854fe95fdd 246 SetParam(DEC,INI_DEC);
ryuna 0:f2854fe95fdd 247 SetParam(MAX_SPEED,INI_MAX_SPEED);
ryuna 0:f2854fe95fdd 248 SetParam(MIN_SPEED,INI_MIN_SPEED);
ryuna 0:f2854fe95fdd 249 SetParam(KVAL_HOLD,INI_KVAL_HOLD);
ryuna 0:f2854fe95fdd 250 SetParam(KVAL_RUN,INI_KVAL_RUN);
ryuna 0:f2854fe95fdd 251 SetParam(KVAL_ACC,INI_KVAL_ACC);
ryuna 0:f2854fe95fdd 252 SetParam(KVAL_DEC,INI_KVAL_DEC);
ryuna 0:f2854fe95fdd 253 SetParam(INT_SPD,INI_INT_SPD);
ryuna 0:f2854fe95fdd 254 SetParam(ST_SLP,INI_ST_SLP);
ryuna 0:f2854fe95fdd 255 SetParam(FN_SLP_ACC,INI_FN_SLP_ACC);
ryuna 0:f2854fe95fdd 256 SetParam(FN_SLP_DEC,INI_FN_SLP_DEC);
ryuna 0:f2854fe95fdd 257 SetParam(K_THERM,INI_K_THERM);
ryuna 0:f2854fe95fdd 258 SetParam(OCD_TH,INI_OCD_TH);
ryuna 0:f2854fe95fdd 259 SetParam(STALL_TH,INI_STALL_TH);
ryuna 0:f2854fe95fdd 260 SetParam(FS_SPD,INI_FS_SPD);
ryuna 0:f2854fe95fdd 261 SetParam(STEP_MODE,INI_STEP_MODE);
ryuna 0:f2854fe95fdd 262 SetParam(ALARM_EN,INI_ALARM_EN);
ryuna 0:f2854fe95fdd 263 SetParam(CONFIG,INI_CONFIG);
ryuna 0:f2854fe95fdd 264
ryuna 0:f2854fe95fdd 265 }
ryuna 0:f2854fe95fdd 266
ryuna 0:f2854fe95fdd 267 void L6470::BusyWait(unsigned int time)
ryuna 0:f2854fe95fdd 268 {//BESYが解除されるまで待機
ryuna 0:f2854fe95fdd 269 while(!m_busy){
ryuna 0:f2854fe95fdd 270 }
ryuna 0:f2854fe95fdd 271
ryuna 0:f2854fe95fdd 272 wait_ms(time);
ryuna 0:f2854fe95fdd 273 }
ryuna 0:f2854fe95fdd 274
ryuna 0:f2854fe95fdd 275 void L6470::Step(int dosu)
ryuna 0:f2854fe95fdd 276 {
ryuna 0:f2854fe95fdd 277 //初期値180度とする
ryuna 0:f2854fe95fdd 278 int temp;
ryuna 0:f2854fe95fdd 279
ryuna 0:f2854fe95fdd 280 temp = dosu*200/360;
ryuna 0:f2854fe95fdd 281
ryuna 2:63e59749ff49 282 GoTo(temp);
ryuna 0:f2854fe95fdd 283
ryuna 0:f2854fe95fdd 284
ryuna 0:f2854fe95fdd 285
ryuna 0:f2854fe95fdd 286 }
ryuna 0:f2854fe95fdd 287
ryuna 2:63e59749ff49 288
ryuna 2:63e59749ff49 289 bool L6470::BusyCheck()
ryuna 2:63e59749ff49 290 {//BESYによりステッピングが動いているかどうか確認する。
ryuna 2:63e59749ff49 291 return (!m_busy);
ryuna 2:63e59749ff49 292 }
ryuna 0:f2854fe95fdd 293
ryuna 0:f2854fe95fdd 294
ryuna 0:f2854fe95fdd 295
ryuna 0:f2854fe95fdd 296
ryuna 0:f2854fe95fdd 297
ryuna 0:f2854fe95fdd 298
ryuna 0:f2854fe95fdd 299
ryuna 0:f2854fe95fdd 300