|
我想通過pc直接發脈沖給步進電機驅動器(不帶串口通信模塊),控制步進電機啟停動作;脈沖經USB端口DATA+和DATA-連接步進驅動器plus+和plus-,方向接頭懸空。結果電機沒有反應。情況可能如下:1)端口地址沒弄對,脈沖沒輸出去;2)步進驅動器不接受經串口協議封裝的信號。請問如何解決?謝謝。代碼如下:
// pulse_emission.cpp : 定義控制臺應用程序的入口點。//
#include "stdafx.h"
#include <stdio.h>//</stdio.h>
#include <time.h>//</time.h>
#include <conio.h>//</conio.h>
//脈沖寬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; // <esc> 退出</esc>
if(nch == ch && nch != '+' && nch != '-') continue;
if(nch == '+') printf("正在輸出正脈沖。\n");
else printf("正在輸出負脈沖。\n");
delay(10 * pulseCycle); // 轉換輸出脈沖極性時,需間隔的時間
ch = nch;
}
}
return 0;
}
|
評分
-
查看全部評分
|