Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
motor.cpp
- Committer:
- duchung2603
- Date:
- 2018-08-20
- Revision:
- 2:ff609fd0c51a
- Parent:
- 1:c002cfb55315
- Child:
- 3:6898c5bd7fef
File content as of revision 2:ff609fd0c51a:
#include "mbed.h"
#include "motor.h"
// ################################################################
// SPI通信フォーマットに合わせた供用体定義
// bit |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
// function | KeyBit | ChNumber | Motor Thlotle |
// ################################################################
typedef union{
UINT16 spiCmd;
struct{
UINT16 val : 8;//bit[ 7: 0]
UINT16 ch : 4;//bit[11: 8]
UINT16 key : 4;//bit[15:12]
}bf;
}SPI_CMD;
// ###############################################################
// グローバル変数
// ###############################################################
static SPI spi (p5,p6,p7); // mosi, miso, clk
static DigitalOut CS_n(p8); //SPIのチップセレクト
static Serial pc (USBTX , USBRX );
MOTOR_THROTTLE mt={0,};
static UCHAR value=0;
//================================================================
//モーター制御モジュール初期化
//================================================================
void motorInit(){
spi.format(16,3) ;//SPIのフォーマット指定(bit長、極性)
spi.frequency(500000) ;//クロック周波数
//Low有意なのでHighにしておく
CS_n =1;
}
//----------------------------------------------------------------
//SPI送信
//----------------------------------------------------------------
void motorSpiSend
(UCHAR ch //チャンネル番号
,UCHAR val //モータ設定値0~255
)
{
SPI_CMD cmd;
//引数を送信するコマンドに成形
cmd.bf.key = 0xA; //キーbit :常に0xA
cmd.bf.ch = ch; //チャンネル番号 :1~12が有効
cmd.bf.val = val; //モータースロットル :0~255
//チップセレクトアサート
CS_n= 0;
//データ出力
spi.write(cmd.spiCmd);
//チップセレクトネゲート
CS_n = 1;
pc.printf("ch[%d] %d\r\n" , cmd.bf.ch , cmd.bf.val);
}
//================================================================
//
//================================================================
void motorUp(){
int ch ,i;
for (i=0; i<10; i++){
if (value != 0xFF) {
value++;
}
for(ch=1; ch<3; ch++){
motorSpiSend(ch,value);
wait(0.01);
}
}
/*
for(ch=5; ch<9; ch++){
for(val=0; val<250; val +=5){
motorSpiSend(ch,val);
wait(0.01);
}
}
*/
}
void motorStop(){
int ch, v;
/*
for(ch=5; ch<9; ch++){
motorSpiSend(ch,0);
wait(0.01);
}
}
*/
for(ch=1; ch<3; ch++){
for (v=value; v>0; v--){
motorSpiSend(ch,v);
wait(0.01);
}
}
value = 0;
}