次はステッピングモータの回転角度を制御します。
リセットを押すたび一周だけ回るプログラムを作ってみます。
MoterAround.c
//反時計回りにモータをちょっきり一周させるプログラム
#include <stdio.h>
#define BRR (*((unsigned char *)0xFFA9))
#define TMA (*((unsigned char *)0xFFA6))
#define PMR1 (*((unsigned char *)0xFFE0))
#define PMR5 (*((unsigned char *)0xFFE1))
#define PCR1 (*((unsigned char *)0xFFE4))
#define PCR5 (*((unsigned char *)0xFFE8))
#define PCR7 (*((unsigned char *)0xFFEA))
#define PCR8 (*((unsigned char *)0xFFEB))
#define PDR1 (*((unsigned char *)0xFFD4))
#define PDR5 (*((unsigned char *)0xFFD8))
#define PDR7 (*((unsigned char *)0xFFDA))
void wait();
void rot_L();
int i,cnt=1,cnt1=0;
int t1=0,t3=0,t4=0;
int t2=-1;
unsigned char Seg1,Seg10;//表示用変数
unsigned char rot;//1の位シフト用
unsigned char rot10;//10の位シフト用
main()
{
BRR=12; //16MHZ時には12を デフォルトは7 ボーレート変更
PMR1 = 0x00; /*ポート1を汎用IOに設定*/
PMR5 = 0x00; /*ポート1を汎用IOに設定*/
TMA = 0x98 ;
PCR1 = 0xff; //アドレス FFE4
PCR5 = 0xff; //FFE8
PCR7 = 0x00;
PCR8 = 0xff; //FFED //00入力 ff出力
if(cnt==1){//初期化
Seg1=0x01;Seg10=0x81;rot=0x01;rot10=0x81;cnt=0;
}
for (;;) {/* 無限ループ */
PDR1=Seg1;
wait(4000);
if(cnt1<485){ //モータによってこの値は変えなければならない 今回使用したモータは485パルスで一周する
cnt1++;
rot_L();
}
}
return 0;
}
void rot_L()//反時計回り用関数
{
//4bit rot
if(rot==0x80){
Seg1=rot;
rot=0x10; //左へ1ビットシフト
}
else{
Seg1=rot;
rot=rot<<1;
}
}
void wait(int n)
{
int i;
for(i=0;i<n;i++);
}
以下の部分はモータ固有の値をとるためモータを変えれば変更が必要です。
if(cnt1<485){ //モータによってこの値は変えなければならない 今回使用したモータは485パルスで一周する
cnt1++;
rot_L();
}
さらに
void rot_L()//反時計回り用関数
{
//4bit rot
if(rot==0x80){
Seg1=rot;
rot=0x10; //左へ1ビットシフト
}
else{
Seg1=rot;
rot=rot<<1;
}
}
反時計回りを行う部分を関数にしました。