|
#include <reg51.h> //51芯片管腳定義頭文件
#include <intrins.h>//內(nèi)部包含延時(shí)函數(shù) _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};
/********************************************************/
/*
/* 延時(shí)t毫秒
/* 11.0592MHz時(shí)鐘,延時(shí)約1ms
/*
/********************************************************/
void delay(uint t)
{
uint k;
while(t--)
{
for(k=0; k<125; k++)
{ }
}
}
/********************************************************/
/*
/*步進(jìn)電機(jī)正轉(zhuǎn)
/*
/********************************************************/
void motor_ffw(uint n)
{
uchar i;
uint j;
for (j=0; j<12*n; j++) //轉(zhuǎn)1×n圈
{
for (i=0; i<8; i++) //一個(gè)周期轉(zhuǎn)30度
{
P1 = FFW[i]; //取數(shù)據(jù)
delay(15); //調(diào)節(jié)轉(zhuǎn)速
}
}
}
/********************************************************/
/*
/*步進(jìn)電機(jī)反轉(zhuǎn)
/*
/********************************************************/
void motor_rev(uint n)
{
uchar i;
uint j;
for (j=0; j<12*n; j++) //轉(zhuǎn)1×n圈
{
for (i=0; i<8; i++) //一個(gè)周期轉(zhuǎn)30度
{
P1 = REV[i]; //取數(shù)據(jù)
delay(15); //調(diào)節(jié)轉(zhuǎn)速
}
}
}
/********************************************************
*
* 主程序
*
*********************************************************/
main()
{
while(1)
{
motor_ffw(5); //電機(jī)正轉(zhuǎn)
delay(5000); //換向延時(shí)
//motor_rev(5); //電機(jī)反轉(zhuǎn)
//delay(1000); //換向延時(shí)
}
}
/********************************************************/
自己改吧, 這是正反轉(zhuǎn)5S |
|