单片机型号是:STC90C516RD+,步进电机型号:NMB-PM42S-096-SYH6,四线双极性
下载到单片机里面,步进电机只有刚刚开始转一小下,然后就不转了,一直发热,修改了延时时间,由10000改到了50000,还是不转。
求大神帮忙~
代码:
#include <reg52.h>
#include <intrins.h>
#define NOP() _nop_() /* 定义空指令 */
#define uchar unsigned char
#define uint unsigned int
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
sta
tic unsigned int count; //计数
static int step_index; //步进索引数,值为0-7
static bit turn; //步进电机转动方向
static bit stop_flag;//步进电机停止标志
static int speedlevel;//步进电机转速参数,数值越大转速越慢
static int spcount; //步进电机转速参数计数
void delay(unsigned int endcount);//延时函数,延时为endcount*0.5ms
void gorun(); //步进电机控制步进函数
//定义管脚
sbit IRIN1 = P3^5; //红外接收器数据线(红,绿,黄)
sbit IRIN2 = P3^6;
sbit IRIN3 = P3^7;
sbit PH1 = P0^0; //定义管脚
sbit PH2 = P0^1;
sbit I01 = P0^2;
sbit I11 = P0^3;
sbit I02 = P0^4;
sbit I12 = P0^5;
void main(void)
{
count=0;
step_index=0;
spcount=0;
stop_flag=1; //不允许转
PH1=0;
PH2=0;
I01=0;
I11=0;
I02=0;
I12=0;
EA=1; //允许cpu中断
TMOD=0x11; //设定时器0和1为16位模式1
ET0=1; //定时器0中断允许
TCON=0x01; //触发方式为脉冲负边沿触发
IRIN1=1;
IRIN2=1;
IRIN3=1;
TH0=0xFE;
TL0=0x0C; //设定时每隔0.5ms中断一次
TR0=1; //开始计数
turn = 0;
speedlevel = 2;
delay(10000); //控制转动的角度
// stop_flag=1; //一段时间后停止转动
speedlevel = 1;
do{
speedlevel=2; //数值越大转的越慢
delay(10000);
speedlevel=1; //快速
delay(10000);
speedlevel=2;
delay(10000);
stop_flag=1; //停止
}while(1);
}
//定时器0中断处理
void timeint(void) interrupt 1 //相当于这个函数每隔0.5ms执行一次
{
TH0=0xFE;
TL0=0x0C; //设定时每隔0.5ms中断一次
count++;
if(!IRIN1&&!IRIN2)
{
turn=0; //正传
stop_flag=0;
}
if(!IRIN3&&!IRIN2)
{
turn=1;
stop_flag=0;
}
spcount--; //步进电机转速参数计数
if(spcount<=0) //speedlevel=2表示两次中断转动一下
{
spcount=speedlevel;
gorun();
}
}
void delay(unsigned int endcount)
{
count=0;
do{}while( count<endcount);
}
void gorun()
{
if(stop_flag==1)
{
PH1=0;
PH2=0;
I01=0;
I11=0;
I02=0;
I12=0;
return;
}
switch(step_index)
{
case 0: // A/
PH1 = 0; //PH1为0 A组线圈为反向电流
I01 = 0;
I11 = 0; //以最大电流输出(没细分) 以下相同 均是此意思
PH2 = 1; //PH2为1 B组线圈为正向电流
I02 = 1;
I12 = 1; //关闭电流输出,以下相同均是此意思
break;
case 1: // A/B
PH1 = 0; //PH1为0 A组线圈为反向电流
I01 = 0;
I11 = 0;
PH2 = 1; //PH2为1 B组线圈为正向电流
I02 = 0;
I12 = 0;
break;
case 2: // B
PH1 = 1;
I01 = 1;
I11 = 1;
PH2 = 1;
I02 = 0;
I12 = 0;
break;
case 3: // AB
PH1 = 1;
I01 = 0;
I11 = 0;
PH2 = 1;
I02 = 0;
I12 = 0;
break;
case 4: // A
PH1 = 1;
I01 = 0;
I11 = 0;
PH2 = 0;
I02 = 1;
I12 = 1;
break;
case 5: // AB/
PH1 = 1;
I01 = 0;
I11 = 0;
PH2 = 0;
I02 = 0;
I12 = 0;
break;
case 6: // B/
PH1 = 0;
I01 = 1;
I11 = 1;
PH2 = 0;
I02 = 0;
I12 = 0;
break;
case 7: // A/B/
PH1 = 0;
I01 = 0;
I11 = 0;
PH2 = 0;
I02 = 0;
I12 = 0;
break;
if(turn==0)
{
step_index++;
if(step_index>7)
step_index=0;
}
else
{
step_index--;
if(step_index<0)
step_index=7;
}
}
}
一周热门 更多>