|
#include <reg51.h> //51芯片管腳定義頭文件
#include <intrins.h>//內(nèi)部包含延時函數(shù) _nop_();
#define uchar unsigned char
#define uint unsignedint
uchar code FFW[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
uchar code REV[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};
/**************************************************/
/* 延時t毫秒
/* 11.0592MHz時鐘,延時約1ms
/*************************************************/
void delay(uint t)
{
uint k;
while(t--)
{
for(k=0; k<125; k++)
{ }
}
}
/******************步進電機正轉(zhuǎn)********************/
void motor_ffw(uint n)
{
uchari;
uint j;
for (j=0; j<12*n; j++) //轉(zhuǎn)1×n圈
{
for (i=0; i<8; i++) //一個周期轉(zhuǎn)30度
{
P1 = FFW[i]; //取數(shù)據(jù)
delay(15); //調(diào)節(jié)轉(zhuǎn)速
}
}
}
/****************步進電機反轉(zhuǎn)************************/
void motor_rev(uint n)
{
uchari;
uint j;
for (j=0; j<12*n; j++) //轉(zhuǎn)1×n圈
{
for (i=0; i<8; i++) //一個周期轉(zhuǎn)30度
{
P1 = REV[i]; //取數(shù)據(jù)
delay(15); //調(diào)節(jié)轉(zhuǎn)速
}
}
}
/*********************主程序**************************/
main()
{
while(1)
{
motor_ffw(5); //電機正轉(zhuǎn)
delay(1000); //換向延時
motor_rev(5); //電機反轉(zhuǎn)
delay(1000); //換向延時
}
} |
|