?? s_ motor-key.c
字號:
/******************************************************
//write by liwei
//QQ 329044941
//liwei329044941
******************************************************/
#include <reg51.h> //51芯片管腳定義頭文件
#include <intrins.h> //內部包含延時函數 _nop_();
#define uchar unsigned char
#define uint unsigned int
uchar code FFW[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
uchar code REV[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};
sbit K1 = P1^4; //正轉
sbit K2 = P1^5; //反轉
sbit K3 = P1^6;
sbit K4 = P1^7; //停止
sbit BEEP = P3^7; //蜂鳴器
/********************************************************/
/*
/* 延時t毫秒
/* 11.0592MHz時鐘,延時約1ms
/*
/********************************************************/
void delay(uint t)
{
uint k;
while(t--)
{
for(k=0; k<125; k++)
{ }
}
}
/**********************************************************/
void delayB(uchar x) //x*0.14MS
{
uchar i;
while(x--)
{
for (i=0; i<13; i++)
{ }
}
}
/**********************************************************/
void beep()
{
uchar i;
for (i=0;i<100;i++)
{
delayB(4);
BEEP=!BEEP; //BEEP取反
}
BEEP=1; //關閉蜂鳴器
}
/********************************************************/
/*
/*步進電機正轉
/*
/********************************************************/
void motor_ffw()
{
uchar i;
uint j;
for (j=0; j<12; j++) //轉1*n圈
{
if(K4==0)
{break;} //退出此循環程序
for (i=0; i<8; i++) //一個周期轉30度
{
P1 = FFW[i]; //取數據
delay(100); //調節轉速
}
}
}
/********************************************************/
/*
/*步進電機反轉
/*
/********************************************************/
void motor_rev()
{
uchar i;
uint j;
for (j=0; j<12; j++) //轉1×n圈
{
if(K4==0)
{break;} //退出此循環程序
for (i=0; i<8; i++) //一個周期轉30度
{
P1 = REV[i]; //取數據
delay(100); //調節轉速
}
}
}
/********************************************************
*
* 主程序
*
*********************************************************/
main()
{
uchar r,N=5; //N 步進電機運轉圈數
while(1)
{
if(K1==0)
{
beep();
for(r=0;r<N;r++)
{
motor_ffw(); //電機正轉
if(K4==0)
{beep();break;} //退出此循環程序
}
}
else if(K2==0)
{
beep();
for(r=0;r<N;r++)
{
motor_rev(); //電機反轉
if(K4==0)
{beep();break;} //退出此循環程序
}
}
else
P1 = 0xf0;
}
}
/********************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -