Skip to main content

LPT1 stepper motor control

Last Sunday, I was coding some modules for our RDBMS project and playing Dungeon Siege: Legend of Aranna (quite old but it would be a waste of time if I did not) in parallel. I received a phone call asking a favor to write a program for sending signals through the parallel port. It sounds a little scary at first because I did not have any idea on parallel port interfacing.


Let me explain the project.


The project is a Car Control System. They have 2 bipolar stepper motors. One is responsible for the forward-reverse function and the other one is for the left-right function.


Signals recognized by the floppy drive stepper motor:


Clockwise 18-degree turn


1001      or      0x9
0110 or 0x6

Counterclockwise 18-degree turn


0110      or      0x6
1001 or 0x9

Now, how is it possible to send these signals using the parallel port? Not all 25 pins are needed. For this project, only the data pins are needed.


Pin         Function
2 D0
3 D1
4 D2
5 D3
6 D4
7 D5
8 D6

LPT1 is usually 0x378; having this knowledge, everything comes in trivial.


In file lptcontrol.c


#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
short data;

if(argc<2) {
printf("Usage\n\n");
printf("lptcontrol.exe [option] [data]\n\n");
return 0;
}

/* a read function is not necessary for this project
included for additional info */

if(!strcmp(argv[1],"read")) {
data = _inp(0x378);
printf("Data from parallel port: ");
printf("%d",data);
}

if(!strcmp(argv[1],"write")) {
_outp(0x378,atoi(argv[2]));
printf("Data written to parallel port: ");
printf("%s",argv[2]);
}
return 0;
}

If you want a constant turn you can achieve it through a loop.



while(1) {
_outp(0x378, 0x9);
_outp(0x378, 0x6);
}

This will result to a motor spinning clockwise. C program above is written for Windows OS.

Comments

Popular posts from this blog

Architecture Complexity

Here are the items to consider: Coding to an interface Service Oriented Architecture Automated Testing Domain Driven Design Custom Data Access Layer Layered architecture Complexity is relatively equal the number of lines of code. Note that complexity is not bad. It must be justified.

Moving on

So I bought my self a 2.5″ hard drive enclosure for about 750 bucks. I wish I have my own tools for constructing a device as such. Instead of going for a 2GB USB flash drive, I chose to salvage the 30GB hard drive of an incapacitated laptop. Cool isn’t it? Though relatively slower than a typical USB flash drive, the storage capacity outweighs it. [To God] Thanks for being there always.

Bin to hex converter in 16-bit DOS assembly

Just a quick code like putting your keyboard where your brain is. There should be another way of doing this. I hate this code. start: xor al, al xor bl, bl xor cl, cl mov dl, 4 ; use dl as counter input: mov ah, 00 int 16h cmp ah, 1ch je exit cmp al, '0' jb input cmp al, '1' ja input convert: mov cl, dl dec cl ; dl - 1 sub al, 30h ; get original value shl al, cl ; place in the appropriate bit position or bl, al ; save in bl dec dl ; prepare for the next bit jnz input ; must be 4 bits cmp bl, 9 ja letter jbe number letter: add bl, 37h ; because 'A' - 37h is 0Ah jmp here number: add bl, 30h ; because '0' - 30h is 00h here: call display jmp start display: mov ah, 02h ...