a

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers L6470.cpp Source File

L6470.cpp

00001 /*#include"mbed.h"
00002 #include"L6470.h"
00003 
00004 SPI spi(PinName mosi, PinName miso, PinName ssel);
00005 DigitalOut cs(PinName);
00006 
00007 L6470::L6470(const spi_pinmap_t &static_pinmap, PinName ssel)
00008     :
00009     static_pinmap(new SPI(mosi,miso,sclk)),
00010     spi(*static_pinmap),
00011     ssel = DigitalOut(PinName)
00012 {
00013     init();
00014 }
00015 
00016 L6470::~L6470(){}
00017 
00018 
00019 /*L6470::L6470(PinName mosi, PinName miso, PinName sclk, PinName ssel)
00020     :
00021     spi_p(new SPI(mosi, miso, sclk)),
00022     spi(*spi_p),
00023     cs_p(new DigitalOut(ssel)),
00024     cs(*cs_p)
00025 
00026     
00027     
00028 void L6470::init(){
00029     
00030     // L6470のspi通信の有効化
00031     //spi.format(8,2);
00032     //spi.frequency(4960000);
00033     
00034     // nopを送ることで残留している命令を排除
00035     cs = 1;
00036     spi.write(0x00);
00037     spi.write(0x00);
00038     spi.write(0x00);
00039     spi.write(0x00);
00040     cs = 0;
00041         
00042     //リセット
00043     cs = 1;
00044     spi.write(0xC0);
00045     cs = 0;
00046         
00047     // MAX_SPEED設定。
00048     cs = 1;
00049     spi.write(0x07);
00050     // 最大回転スピード値(10bit) 初期値は 0x41
00051     spi.write(0x00);
00052     spi.write(0x23);
00053     cs = 0;
00054 
00055     // KVAL_HOLD設定。
00056     /// レジスタアドレス。
00057     cs = 1;
00058     spi.write(0x09);
00059     spi.write(0xFF);
00060     cs = 0;
00061         
00062     // KVAL_spiUN設定。
00063     /// レジスタアドレス。
00064     cs = 1;
00065     spi.write(0x0A);
00066     spi.write(0xFF);
00067     cs = 0;
00068         
00069     // KVAL_ACC設定。
00070     cs = 1;
00071     spi.write(0x0B);
00072     spi.write(0xFF);
00073     cs = 0;
00074         
00075     // KVAL_DEC設定。
00076     /// レジスタアドレス。
00077     cs= 1;
00078     spi.write(0x0C);
00079     spi.write(0x40);
00080     cs = 0;
00081         
00082     // OCD_TH設定。
00083     /// レジスタアドレス。
00084     cs = 1;
00085     spi.write(0x13);
00086     spi.write(0x0F);
00087     cs = 0;
00088         
00089     // STALL_TH設定。
00090     /// レジスタアドレス。
00091     cs = 1;
00092     spi.write(0x14);
00093     spi.write(0x7F);
00094     cs = 0;
00095 }
00096 
00097 void L6470::writeByte(uint8_t data)
00098 {
00099     cs = 1;
00100     spi.write(data);
00101     cs = 0;
00102 }
00103 
00104     
00105 void L6470::writeBytes(uint8_t add, int bytes, uint32_t value)
00106 {
00107     uint32_t data[3];
00108     for (int i = 0; i < bytes; i++){
00109         data[i] = value & 0xff;
00110         value = value >> 8;
00111     }
00112     if(bytes == 3){
00113         cs = 1;
00114         spi.write(add)
00115         spi.write(data[2]);
00116         spi.write(data[1]);
00117         spi.write(data[0]);
00118         cs_R = 0;
00119         }
00120     else if ( bytes == 2 ) {
00121         cs = 1;
00122         spi.write( add );
00123         spi.write( data[1] ) ;
00124         spi.write( data[0] ) ;
00125         cs = 0;
00126     }
00127     else if ( bytes == 1 ) {
00128         cs = 1;
00129         spi.write( add );
00130         spi.write( data[0] ) ;
00131         cs = 0;
00132     }
00133 }
00134 
00135 
00136 void L6470::L6470_run(uint32_t speed)
00137 {
00138         uint8_t dir;
00139         uint32_t spd;
00140         uint8_t spd_h;
00141         uint8_t spd_m;
00142         uint8_t spd_l;
00143 
00144         // 方向検出。
00145         if (speed < 0)
00146         {
00147             dir = 0x50;
00148             spd = -1 * speed;
00149         }
00150         else
00151         {
00152             dir = 0x51;
00153             spd = speed;
00154         }
00155 
00156         // 送信バイトデータ生成。
00157         spd_h = (uint8_t)((0x000F0000 & spd) >> 16);
00158         spd_m = (uint8_t)((0x0000FF00 & spd) >> 8);
00159         spd_l = (uint8_t)(0x000000FF & spd);
00160         // コマンド(レジスタアドレス)送信。
00161         cs = 1;
00162         spi.write(dir);
00163         spi.write(spd_h);
00164         spi.write(spd_m);
00165         spi.write(spd_l);
00166         cs = 0;
00167 }    
00168     
00169 */