広告 ArduinoからAllegro MicroSystemsのA4960を使ってみるA4960は外部にNch FETを付けてセンサーレスのブラシレスモータを回すことができる。 Arduinoでは設定するのにSPIライブラリを使う。 ただ回すだけなら
とRun Registerしか使わない。 でも、単にRun Registerを設定しただけでは動かなかったら他のレジスタもいじる必要があると思う。 ArduinoのプログラムSPIの通信に必要そうな部分のみ。SPIライブラリのExampleやフォーラムを参考にした。 まずはSPIのヘッダーをインクルードする。 #include <SPI.h> 使いそうな変数を定義。わざわざ定義する必要ないと思うけど const int WRITE = 0x10; const int wait=1; チップセレクトピン(CSまたはSS)を定義 const int chipSelectPin = 10; いろいろなレジスタのアドレスを定義 const byte BLANK = 0b00000000; const byte VREF = 0b00100000; const byte PWM = 0b01000000; const byte HOLD = 0b01100000; const byte START = 0b10000000; const byte RAMP = 0b10100000; const byte MASK = 0b11000000; const byte RUN = 0b11100000; const byte DIAG = 0b11000000; //Read only レジスタ書き込み関数 void writeRegister(byte thisRegister, byte dataH, byte dataL) { byte dataToSend = thisRegister | WRITE; dataToSend = dataToSend | dataH; digitalWrite(chipSelectPin, LOW); //delay(10); SPI.transfer(dataToSend); //Send register location SPI.transfer(dataL); //Send value to record into register // take the chip select high to de-select: digitalWrite(chipSelectPin, HIGH); } レジスタ読み込み関数 unsigned int readRegister(byte thisRegister ) { byte inByte = 0; // incoming byte from the SPI unsigned int result = 0; // result to return byte dataToSend = thisRegister; digitalWrite(chipSelectPin, LOW); delay(wait); // send the device the register you want to read: result = SPI.transfer(dataToSend); result = result << 8; inByte = SPI.transfer(0x00); // combine the byte you just got with the previous one: result = result | inByte; // take the chip select high to de-select: digitalWrite(chipSelectPin, HIGH); return(result); } で、RUNビットを変更するために変数を定義しておいて byte run_h=0b00000010; byte run_l=0b00001001; //RUN RUNレジスタに書き込む。(RUNビットを1にする) writeRegister(RUN, run_h, run_l); RUNレジスタを確認 ret=readRegister(RUN); Serial.println(ret, BIN); //要シリアル通信の設定 確認すると下位12ビット(レジスタアドレスを引いたもの)ぐらいしか表示されない。 注意点以前モータが回るときの電流でロジック電圧が降下して動かなかったと書いたが、ロジック電圧を別電源にしてもダメだった。 広告 |