This section shows the test suite I used to ensure that the PIC was correctly executing the commands I sent to it.
It is possible to just type in the PIC commands, using cu or even something as simple as cat >/dev/stepper provided you have set up the serial port beforehand. However it gets tedious, typing in the same test commands every time you change something in the code
/* Test Suite for Stepper Motor Controller with serial interface */ #include#include #include #include #include #include #include #include #define STEPPER "/dev/stepper" int fd; /* file descriptor for comm port */ FILE *fp; /* same as a FILE * */ /* format of input command is */ /* A+xxxxx:nnn */ /* A = chip ident */ /* + or - is step direction */ /* xxxxx is 5 digits of step count */ /* : delimits step count from step rate */ /* nnn is step rate in milliseconds */ /* the string is terminated with a newline char */ /* define a set of "difficult" commands */ struct step { char dir; int count; int dly; }; struct step s[] = { '+',1,2, '+',10,2, '-',11,20, '+',96,2, /* one revolution is 96 steps (PJL) */ '-',96,20, '+',48,10, '-',48,2, '-',48,10, '-',48,10, '+',255,10, '-',255,10, '+',256,10, '-',256,10, '+',257,10, '-',257,10, '+',999,5, '-',999,2, '+',960,4, '-',960,10, '+',1000,5, '+',1000,20, '-',1000,50, '-',1000,30, '+',10000,2, '-',10000,50 }; main() { char a[20]; int i; int j; int d; int w; int n = 1; open_comm(STEPPER); for(i = 0; i < sizeof(s)/sizeof(struct step); i++) { a[0] = 'A'; a[1] = s[i].dir; sprintf(&a[2], "%05d", s[i].count); sprintf(&a[7], ":%03d", s[i].dly); a[11] = '\n'; a[12] = '\0'; printf("%05d sending: ", i); fflush(stdout); write(fd, a, 12); a[11] = '\0'; printf("%s (%d mSec)\n", a, s[i].count * s[i].dly); usleep(s[i].count * s[i].dly * 1010); } } int open_comm(char *dev) { struct termios t; unsigned char ret_code; if((fd = open(dev, O_RDWR | O_NONBLOCK)) < 0) { printf("Can't open port %s, %s\n", dev, sys_errlist[errno]); exit(1); } fp = fdopen(fd, "w"); cfsetispeed(&t, B38400); cfsetospeed(&t, B38400); tcgetattr(fd, &t); cfmakeraw(&t); t.c_iflag = (ISTRIP|IXON|IXOFF); tcsetattr(fd, TCSANOW, &t); return; }
The code exercises the stepper with a series of "interesting"
commands to test the accuracy of the BCD to binary conversion
and the logic for the direction reversal code. Since there
are the same total number of steps in both directions, your
motor should (if you are lucky) end up back at the position it
started.
The whole test will take about 15 minutes to run, go and make
some tea