|
楼主 |
发表于 2012-5-5 11:48:23
|
显示全部楼层
culapple 发表于 2012-5-4 22:20
估计是相序写的不对,学习下步进电机驱动吧
你看看这个程序有没有问题嘛,我觉得我的接线和这上面定义一样的哒
/******************************定义头文件*************************/
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
/*****************************变量定义****************************/
sbit L_A = P1^0; //左侧步进电机A相
sbit L_A_= P1^1; // 左侧步进电机A_相
sbit L_B = P1^2; // 左侧步进电机B相
sbit L_B_= P1^3; // 左侧步进电机B_相
sbit R_A = P1^4; // 右侧步进电机A相
sbit R_A_= P1^5; // 右侧步进电机A_相
sbit R_B = P1^6; // 右侧步进电机B相
sbit R_B_= P1^7; // 右侧步进电机B_相
/***************************变量声明*****************************/
uchar step_round_l[8]={0x80, 0x90, 0x10, 0x30, 0x20, 0x60, 0x40, 0xc0}; //步进电机单步脉冲
uchar step_round_r[8]={0x08, 0x09, 0x01, 0x03, 0x02, 0x06, 0x04, 0x0c}; //步进电机单步脉冲
char i=0,j=0; //步数控制变量
/**************************子函数定义****************************/
void round_l(int speed,step); //前进函数
void round_r(int speed,step); //后退函数
void delay(int k); //延时函数
/***************************主函数*****************************/
void main()
{
round_l(300,500);
round_r(300,500);
}
/**************************子函数****************************/
void round_l(int speed,step) //前进函数
{
while(step)
{
P1=(step_round_l|step_round_r[j]);
i++;
if(i==8)
{
i=0;
}
delay(speed);
step--;
}
}
void round_r(int speed,step) //前进函数
{
while(step)
{
P1=(step_round_r[j]|step_round_l);
j++;
if(j==8)
{
j=0;
}
delay(speed);
step--;
}
}
void delay(int k)
{
while(k--);
} |
|