|
我想通过pc直接发脉冲给步进电机驱动器(不带串口通信模块),控制步进电机启停动作;脉冲经USB端口DATA+和DATA-连接步进驱动器plus+和plus-,方向接头悬空。结果电机没有反应。情况可能如下:1)端口地址没弄对,脉冲没输出去;2)步进驱动器不接受经串口协议封装的信号。请问如何解决?谢谢。代码如下: // pulse_emission.cpp : 定义控制台应用程序的入口点。// #include "stdafx.h" #include // #include // #include //
//脉冲宽50ms; const unsigned pulseWidthPlus = 50; //脉冲槽宽50ms; const unsigned pulseWidthMinus = 50; //脉冲周期宽度100ms; const unsigned pulseCycle = pulseWidthPlus + pulseWidthMinus;
void delay(clock_t nms) { clock_t start; start = clock(); while((clock() - start) * CLOCKS_PER_SEC / 1000 < nms); }
void output(int *port, char polarity) { int peak = 0; if(polarity == '+') peak = 1; else if(polarity == '-') peak = -1; *port = peak; }
void writeWaveform(int *port,unsigned pulseWidth, unsigned cycle, char polarity) { output(port,polarity); delay(pulseWidth); output(port,0); delay(cycle - pulseWidth); }
int _tmain(int argc, _TCHAR* argv[]) { char ch = 0,nch; int p = 5; // p的值端口地址(这里用内存单元表示) int *port = &p; while(1) { writeWaveform(port,pulseWidthPlus,pulseCycle,ch); if(kbhit()) { nch = _getch(); if(nch == 0X1B) break; // 退出 if(nch == ch && nch != '+' && nch != '-') continue; if(nch == '+') printf("正在输出正脉冲。\n"); else printf("正在输出负脉冲。\n"); delay(10 * pulseCycle); // 转换输出脉冲极性时,需间隔的时间 ch = nch; } } return 0; }
|
评分
-
查看全部评分
|