#include "reg52.h"
//電機IO
#define GPIO_MOTOR P1 //宏定義 對端口進行操作,定義電機接口為P1
//sbit F1 = P1^0;
//sbit F2 = P1^1;
//sbit F3 = P1^2;
//sbit F4 = P1^3;
//sbit F5 = P1^4;
//sbit F6 = P1^5;
//sbit F7 = P1^6;
//sbit F8 = P1^7; //另種操作,位操作,定義電機接口為P1.0~p1.7
//按鍵IO
sbit K1=P2^0; //正轉
sbit K2=P2^1; //反轉
sbit K3=P2^2; // 減速
sbit K4=P2^3; // 加速
sbit K5=P2^4; // 電機切換
sbit K6=P2^5; // 停止
sbit led1=P3^3;
sbit led3=P3^0;
unsigned char code FFW_X[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //X反轉順序
unsigned char code FFZ_X[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正轉順序
unsigned char code FFW_Y[8]={0x1f,0x3f,0x2f,0x6f,0x4f,0xcf,0x8f,0x9f}; //Y反轉順序
unsigned char code FFZ_Y[8]={0x9f,0x8f,0xcf,0x4f,0x6f,0x2f,0x3f,0x1f}; //Y正轉順序
unsigned char code table0[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
unsigned char Direction,Speed,X_Y=0,stop=0; // Direction 轉向標志位,Speed 延時函數的參數,通過改變它大小來控制電機轉速
//X_Y 電機的參數 X_Y=0為Y電機 X_Y=1為X電機
void Delay(unsigned int t); //stop為控制電機停止的標志位,但它為1時,電機停止
void Motor(); //電機旋轉函數
/*******************************************************************************
* 函 數 名 : main
* 函數功能 : 主函數
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void main(void)
{
unsigned char i;
unsigned char num=0;
led1=0;
led3=0;
P0=0x3f;//初始數字
Speed=30; //Speed 延時函數的參數,通過改變它大小來控制電機轉速
while(1)
{
/********************檢測按鍵K1是否按下***********************/
if(K1==0) //檢測按鍵K1是否按下
{
Delay(1); //消除抖動
if(K1==0)
led1=1;
P0=0x06;
Speed=60;
{
Direction=1; //Direction 轉向標志位,為1時為正轉
stop=0; //按下K1時,對stop停止標志位清零,如果之前按下停止鍵,清零之后才能啟動
}
while((i<200)&&(K1==0))//檢測按鍵是否松開
{
Delay(1);
i++;
}
i=0;
}
/********************檢測按鍵K2是否按下***********************/
if(K2==0) //檢測按鍵K2是否按下
{
Delay(1); //消除抖動
if(K2==0)
{
Direction=2; //Direction 轉向標志位,為2時為反轉
stop=0; //按下K1時,也對stop停止標志位清零,如果之前按下停止鍵,清零之后才能啟動
}
while((i<200)&&(K2==0))//檢測按鍵是否松開
{
Delay(1);
i++;
}
i=0;
}
/********************檢測按鍵K3是否按下***********************/
if(K3==0)//檢測按鍵K3是否按下
{
Delay(1);//消除抖動
if(K3==0)
{
num=2;
P0=table0[num];
Speed=30; //Speed 延時函數的參數,通過改變它大小來控制電機轉速
//87行代碼定義Speed=30;但按鍵K3按下后,Speed=60,這時轉速就會降下來
}
while((i<200)&&(K3==0))//檢測按鍵是否松開
{
Delay(1);
i++;
}
i=0;
}
/********************檢測按鍵K4是否按下***********************/
if(K4==0) //檢測按鍵K4是否按下
{
Delay(1); //消除抖動
if(K4==0)
{
num=3;
P0=table0[num];
Speed=20; //Speed 延時函數的參數,通過改變它大小來控制電機轉速
//87行代碼定義Speed=30;但按鍵K4按下后,Speed=20,這時轉速就會升上去
}
while((i<200)&&(K4==0))//檢測按鍵是否松開
{
Delay(1);
i++;
}
i=0;
}
if(K5==0)//檢測按鍵K1是否按下
{
Delay(1);//消除抖動
if(K5==0)
led3=~led3;
{
Direction=0; //X_Y 電機的參數 X_Y=0為Y電機 X_Y=1為X電機
if(X_Y==0) //當切換電機時,對Direction=0;對標志位清零,防止上個電機的轉向影響本次所控電機
X_Y=1;
else
X_Y=0;
}
while((i<200)&&(K5==0))//檢測按鍵是否松開
{
Delay(1);
i++;
}
i=0;
}
if(K6==0)//檢測按鍵K1是否按下
{
Delay(1);//消除抖動
if(K6==0)
led1=0 ;
led3=0;
{
stop=1; //stop為控制電機停止的標志位,但它為1時,電機停止
}
while((i<200)&&(K6==0))//檢測按鍵是否松開
{
Delay(1);
i++;
}
i=0;
}
Motor();
}
}
/*******************************************************************************
* 函 數 名: Motor
* 函數功能: 電機旋轉函數
* 輸 入: 無
* 輸 出: 無
*******************************************************************************/
void Motor()
{
unsigned char i;
for(i=0;i<8;i++)
{
if(Direction==1&&X_Y==0&&stop==0) //正轉&Y電機&電機沒按下停止
GPIO_MOTOR = FFW_Y[i]&0xf0;//取數據,‘&’按位與
if(Direction==2&&X_Y==0&&stop==0) //反轉&Y電機&電機沒按下停止
GPIO_MOTOR = FFZ_Y[i]&0xf0;
if(Direction==1&&X_Y!=0&&stop==0) //正轉&X電機&電機沒按下停止
GPIO_MOTOR = FFW_X[i]&0x0F;//取數據
if(Direction==2&&X_Y!=0&&stop==0) //反轉&X電機&電機沒按下停止
GPIO_MOTOR = FFZ_X[i]&0x0f;
Delay(Speed); //調節轉速
}
}
/*******************************************************************************
* 函 數 名 : Delay
* 函數功能 : 延時
* 輸 入 : t
* 輸 出 : 無
*******************************************************************************/
void Delay(unsigned int speed)
{
unsigned int k;
while(speed--)
{
for(k=0; k<80; k++)
{ }
}
}
|