标题:
怎么修改单片机程序可以使步进电机在满足条件后正转和反转180°
[打印本页]
作者:
树叶飘
时间:
2020-5-22 00:47
标题:
怎么修改单片机程序可以使步进电机在满足条件后正转和反转180°
#include "include.h"
#define uchar unsigned char
#define uint unsigned int
#define DataPort P2
#define MotorData P0
#define GapValue 305
uchar phasecw[4] ={0x08,0x04,0x02,0x01};
uchar phaseccw[4]={0x01,0x02,0x04,0x08};
sbit LCM_RS=P0^7;
sbit LCM_RW=P0^6;
sbit LCM_EN=P0^5;
sbit IN1 = P3^4;
sbit IN2 = P3^5;
sbit IN3 = P3^6;
sbit IN4 = P3^7;
sbit HX711_DOUT=P3^3;
sbit HX711_SCK=P3^2;
unsigned long Time_Cont = 0;
unsigned char PollCount = 0;
unsigned int qupi=0;
long weight_wuti = 0;
long weight_wutinew = 0;
unsigned long weight_maopi = 0;
unsigned long weight_maopi_0 = 0;
unsigned char tx_buf[5];
void Get_maopi();
void Get_weight();
void Delay__hx711_us(void);
unsigned long HX711_Read(void);
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<121;j++);
}
void Delay__hx711_us(void)
{
_nop_();
_nop_();
}
unsigned long HX711_Read(void)
{
unsigned long count;
unsigned char i;
HX711_DOUT=1;
Delay__hx711_us();
HX711_SCK=0;
count=0;
while(HX711_DOUT);
for(i=0;i<24;i++)
{
HX711_SCK=1;
count=count<<1;
HX711_SCK=0;
if(HX711_DOUT)
count++;
}
HX711_SCK=1;
count=count^0x800000;
Delay__hx711_us();
HX711_SCK=0;
return(count);
}
void Get_Weight()
{
weight_wuti = HX711_Read();
weight_wuti = weight_wuti - weight_maopi;
weight_wuti = (unsigned int)((weight_wuti)/GapValue);
if(weight_wuti>5000)weight_wuti=0;
tx_buf[0]= weight_wuti/1000;
tx_buf[1]= weight_wuti/100%10;
tx_buf[2]= weight_wuti%100/10;
tx_buf[3]= weight_wuti%10;
}
void Get_maopi()
{
unsigned char clear;
mm: weight_maopi_0 = HX711_Read();
for(clear=0;clear<2;clear++)
{
Delay_ms(200);
}
weight_maopi = HX711_Read();
if(weight_maopi/GapValue!=weight_maopi_0/GapValue)
goto mm;
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = (65536 - 20000)/256;
TL0 = (65536 - 20000)%256;
PollCount++;
if(PollCount==50)
{
PollCount=0;
}
}
void positive(unsigned int time)
{
IN1 = 1;IN2 = 0; IN3 = 0;IN4 = 0;
Delay_ms(time);
IN1 = 0;IN2 = 1; IN3 = 0;IN4 = 0;
Delay_ms(time);
IN1 = 0;IN2 = 0; IN3 = 1;IN4 = 0;
Delay_ms(time);
IN1 = 0;IN2 = 0; IN3 = 0;IN4 = 1;
Delay_ms(time);
}
unsigned int count;
void run(unsigned int time,unsigned int k)
{
for(count=0;count<time;count++)
positive(k);
}
void MotorCW(void)
{
uchar i;
for(i=0;i<4;i++)
{
MotorData=phasecw[i];
Delay_ms(4);
}
}
void MotorCCW(void)
{
uchar i;
for(i=0;i<4;i++)
{
MotorData=phaseccw[i];
Delay_ms(4);
}
}
void MotorStop(void)
{
MotorData=0x00;
}
void main()
{
InitLcd();
Get_maopi();
DisplayOneChar(0,0,'W');
DisplayOneChar(1,0,'E');
DisplayOneChar(2,0,'I');
DisplayOneChar(3,0,'G');
DisplayOneChar(4,0,':');
DisplayOneChar(0,1,'N');
DisplayOneChar(1,1,'U');
DisplayOneChar(2,1,'M');
DisplayOneChar(3,1,'B');
DisplayOneChar(4,1,':');
while(1)
{
Get_weight();
DisplayOneChar(5,0,'0'+tx_buf[0]);
DisplayOneChar(6,0,'0'+tx_buf[1]);
DisplayOneChar(7,0,'0'+tx_buf[2]);
DisplayOneChar(8,0,'0'+tx_buf[3]);
DisplayOneChar(9,0,'G');
if((weight_wutinew-weight_wuti>10)||(weight_wuti-weight_wutinew>10))
{
weight_wutinew = weight_wuti;
if((weight_wuti>0)&&(weight_wuti<100)){DisplayOneChar(5,1,'1');MotorCW();
MotorStop();
else if(weight_wuti>100){DisplayOneChar(5,1,'2'); MotorCCW(); }
MotorStop();
}
Delay_ms(2);
}
}
/*******************************/
void WaitForEnable(void)
{
DataPort=0xff;
LCM_RS=0;LCM_RW=1;_nop_();
LCM_EN=1;_nop_();_nop_();
while(DataPort&0x80);
LCM_EN=0;
}
/*******************************/
void WriteCommandLCM(uchar CMD,uchar Attribc)
{
if(Attribc)WaitForEnable();
LCM_RS=0;LCM_RW=0;_nop_();
DataPort=CMD;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
/*******************************/
void WriteDataLCM(uchar dataW)
{
WaitForEnable();
LCM_RS=1;LCM_RW=0;_nop_();
DataPort=dataW;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
/***********************************/
void InitLcd()
{
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0c,1);
}
/***********************************/
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}
复制代码
作者:
两仪式
时间:
2020-5-22 08:13
具体要看步进电机上面的驱动器,一步是多少度,一个脉冲是半步还是一步,单片机只是负责发脉冲
作者:
树叶飘
时间:
2020-5-22 10:49
两仪式 发表于 2020-5-22 08:13
具体要看步进电机上面的驱动器,一步是多少度,一个脉冲是半步还是一步,单片机只是负责发脉冲
看了一下,只需解决正反转就行,度数可以随时调整
作者:
xuyaqi
时间:
2020-5-22 13:13
把你现在的现象描述一下。这一句MotorStop(); 要屏蔽掉,否则每次都要停一下。
作者:
ERERQ
时间:
2020-5-23 12:08
MotorStop(); 要屏蔽掉
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1