whiIe(1)
{P0=1;deIayms(5);
P0=2;delayms(5);
P0=4;delayms(5);
P0=8;delayms(5);
}
反转时顺序为8 421即可。
/*本步进电机步进角为 7.5度 . 一圈 360 度 , 需要 48 个脉冲完成!!!
A组线圈对应 P1.4
B组线圈对应 P1.5
C组线圈对应 P1.6
D组线圈对应 P1.7
正转次序: AB组--BC组--CD组--DA组 (即一个脉冲,正转 7.5 度)
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//正转脉冲表
//unsigned char Table_Run[4]={0x3, 0x60, 0xC0, 0x90}
//反转脉冲表
//unsigned char Table_Rev[4]={0x3, 0x90, 0xC0, 0x60}
///没有ULN2003的情况
//unsigned char forword[]={0x06,0x07,0x03,0x0b,0x09,0x0d,0x0c,0x0e}
//unsigned char back[]={0x06,0x0e,0x0c,0x0d,0x09,0x0b,0x03,0x07}
////ULN2003
unsigned char forword[8]={0xc0,0x40,0x60,0x20,0x30,0x10,0x90,0x80}
unsigned char back[8]={0xc0,0x80,0x90,0x10,0x30,0x20,0x60,0x40}
sbit key1=P2^0 //正转
sbit key2=P2^1 //反
sbit key3=P2^2 //停止
uchar count=0
uint time=0
//函数说明
void delay(uint t)
void front_move(uint step)
void back_move()
//定时器0中断
void time0() interrupt 1
{
TH0=(65536-1000)/256
TL0=(65536-1000)%256
//time++
}
//主程序
void main()
{
uchar i
TMOD=0x01
TH0=(65536-1000)/256
TL0=(65536-1000)%256
ET0=1
EA=1
TR0=1
while(1)
{
if(key1==0)
{
delay(10)
if(key1==0)
{
front_move(12)
}
}
if(key2==0)
{
back_move()
}
if(key3==0)
{
P1=0X00
}
}
}
//延时
//电机驱动子程序
void front_move(uint step)
{
uchar i=0,j
for(j=0j<stepj++)
{
for(i=0i<8i++)
{
P1=forword[i]
delay(50)
}
/*if(i<8)
{
i++
}
else
{
i=0
}
*/
}
}
//反转
void back_move()
{
uchar i
for(i=0i<8i++)
{
P1=back[i]
delay(145)
}
}
void delay(uint t) /* 对于12M时钟,约延时1ms */
{
uint i
while(t--)
{
for (i=0i<80i++)
{}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)