[TAG] Stepper motor driver in Issue 122
Jason Creighton
jcreigh at gmail.com
Tue Apr 18 07:10:26 MSD 2006
[ Reply CC'd to querent, TAG, and the author of the article in question ]
On Sun, Apr 16, 2006 at 08:20:36AM -0400, jovliegen at gmail.com wrote:
> Hello,
> First of all, I like to thank you guys for this great "Gazette". I
> realy do enjoy it !!!
Thanks! It's great to hear from readers.
> I'm trying to build the stepper motor driver, that you published in
> issue 122.
> While reading the code, I have a little remark.
> I'm not quite sure that I'm right, but ... what do I have to loose :D
> On line 20 : "static int pattern[2][8][8] = {"
> Shouldn't that be pattern[2][2][8] ?
I went and looked at that author's code, and it sure looks like you're
right. Note that it doesn't hurt anything to declare the array as larger
than you need it, but it's probably not the best style.
There's a few other things in that code that I might have done
differently. For instance, his "step" function looks like this:
``
int step()
{
if(k<8) {
// if(pattern[i][j][k]==0) {
// k=0;
// printk("%d\n",pattern[i][j][k]);
// k++;
// }
// else {
printk("%d\n",pattern[i][j][k]);
k++;
// }
}
else {
k=0;
printk("%d\n",pattern[i][j][k]); /*#####*/
k++; /*#####*/
}
return 0;
}
''
Whereas I feel it might have been written more clearly like so:
``
int step() {
printk("%d\n", pattern[i][j][k]);
k = (k + 1) % 8;
return 0;
}
''
Note that this does not have the exact same behavior as the original
function. In the original function, after step() returns, k is in range
[1,8], inclusive. In the version I give, k is kept in the range [0,7],
inclusive, which is the correct range if k is being used as the index of
an array of length 8.
Unless I'm really missing something, I don't think the actual code to
write the parallel port was included in that article.
[ Aside to the Answer Gang: I am by no means a C expert, but I know a
little bit about the language. ISTR some reader volunteering to
proofread C code that comes up in future articles, but if not, I could
step in and try to catch the obvious technical stuff. ]
> Like I said before, I'm not sure of this ... just wondering ...
Thanks for taking the time to point this out.
Jason Creighton
More information about the TAG
mailing list