📄 stepmotor._c
字号:
/********************************************************************
步进电机驱动
说明:用MEGA8+ULN2003驱动四相六线步进电机
MEGA8所用端口为PD0,PD1,PD2,PD3
晶振使用MEGA8内部8M
实现功能:电机快速正转360度,反转360度
慢速正转360度,反转360度
作者:张国旗
2008年12月31号
*******************************************************************/
#define uchar unsigned char
#define motor_DDR DDRD
#define motor_on motor_DDR|=BIT(PD0)|BIT(PD1)|BIT(PD2)|BIT(PD3)
#define motor_out PORTD
#define motor_pin PIND
const uchar time_sequence1[]={0x10,0x30,0x20,0xa0,0x80,0xc0,0x40,0x50}; //正转时序 3.75度
const uchar time_sequence2[]={0x10,0x50,0x40,0xc0,0x80,0xa0,0x20,0x30}; //倒转时序 3.75度
const uchar time_sequence3[]={0x10,0x20,0x80,0x40}; //正转时序 7.5度
const uchar time_sequence4[]={0x10,0x40,0x80,0x20}; //倒转时序 7.5度
//====================四相六线电机驱动函数==================================
//============正转3.75度==========
void corotation375(unsigned int speed)
{
uchar i;
for (i=0;i<8;i++)
{
motor_out=((motor_pin&0x0f)|(time_sequence1[i]|0x0f));
delay(speed);
}
}
//==============倒转3.75度=========
void reversal375(unsigned int speed)
{
uchar i;
for (i=0;i<8;i++)
{
motor_out=((motor_pin&0x0f)|(time_sequence2[i]|0x0f));
delay(speed);
}
}
//=============正转7.5度===========
void corotation75(unsigned int speed)
{
uchar i;
for (i=0;i<4;i++)
{
motor_out=((motor_pin&0x0f)|(time_sequence3[i]|0x0f));
delay(speed);
}
}
//=============倒转7.5度==========
void reversal75(unsigned int speed)
{
uchar i;
for (i=0;i<4;i++)
{
motor_out=((motor_pin&0x0f)|(time_sequence4[i]|0x0f));
delay(speed);
}
}
//=======================================================================
//====================主程序================
void motorstar (void)
{
unsigned int i,time,speeddat;
motor_on;
time=12;
speeddat=5;
for(i=0;i<time;i++)
{
corotation375(speeddat);
}
for(i=0;i<time;i++)
{
reversal375(speeddat);
}
for(i=0;i<time;i++)
{
corotation75(speeddat);
}
for(i=0;i<time;i++)
{
reversal75(speeddat);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -