im having a problem controlling a stepper motor using a microcontroller..what i did is that i test it with my board coz it has led's to check whether its working or not..we are tasked to make a program in c++ that would make a variable reluctance stepper motor work. If you press the the R button the rotor of the stepper motor will turn forward (clockwise), if you press L it will turn backward (counter clockwise), and if you press S the program will stop.. so how would you be able to make a program out of this conditions?

Recommended Answers

All 24 Replies

heres a sample code...

#include <reg420.h>
#include <stdio.h>
void main ()
{
TMOD = 0x20 ;
TCON = 0x40 ;
TH1 = 0xf9 ;
TL1 = 0xf9 ;
PCON = 0x80 ;
SCON = 0x50 ;
while (1) /*continues loop */
{ 
printf("a"); /* transmit a along with CR & LF.
}

what this does is it simply moves the motor in a forward direction..so how would you put controls to this? the R, L and S?

This looks more like C than C++... Anyway, to be able to do something like you described, you'll need real-time I/O. This isn't supported by standard C/C++, so you'll have to look into your compiler documentation for more information regarding this (or get a third-party library to handle it). Oh, and speaking of standards, you should change

void main()
{
.
.
.
}

to

int main()
{
.
.
.
return 0;
}

Finally, please use code tags. It makes reading code much easier.

// controlling a stepper motor 

#include <stdio.h>
#include <reg420.h>
#include <ctype.h>
#include "serial.h"

void main()
{
	char o;
	int i;
	InitSerialHardware();
	
	do{
	o = getchar();
	
	if(isspace(o)) continue;
	o = toupper(o); 
	
	if(o == 'S')
	{
		puts(" Stop");
		P1 = 0;
	}

	if (o == 'L')
		{
			puts(" Left");
			{
				TMOD = 0x20;
				TCON = 0x40;
				TH1 = 0xF9;
				TL1 = 0xF9;
				PCON = 0x80;
				SCON0 = 0x50;
			}
		}

		if (o == 'R')
		{
			puts(" Right");
			{
				SCON0 = 0x50;
				PCON = 0x80;
				TL1 = 0xF9;
				TH1 = 0xF9;
				TCON = 0x40;
				TMOD = 0x20;
			}
		}else	continue;
		
	}while (1); /*continues loop */

	{
		printf("a"); // transmit a along with CR & LF.
	}
}

*heres the code i made, but it didnt work..i try to put it in port one coz our port one has leds to check if its working..so what went wrong here?

Why didn't you use CODE-TAGS as Joey mentioned? Why did you use void main ()?

*heres the code i made, but it didnt work..

What doesn't work? Does it compile/run/..?

Niek

void main() is fine. There isn't an OS. The code runs in an infinite loop until the power is removed.

ok, il also try that..im sorry, there was nothing wrong with the code, it compiled, there were no errors however, the problem is this program didnt function the way it must function..when you press the R,L and S nothing happened...so how will i revise this? also how would you know the pins of a stepper motor? tnx...

you should be able to simulate the computer input "manually" to verify it's functioning before frustrating yourself by chasing a phantom.

That's troubleshooting 101. Keep cutting the problem in half. Verify what works vs. what doesn't.
Then continue to break down the components of the non-working section, etc.

In your case you can easily separate the electronics issue from programming issue.
Who knows? Maybe neither side works!

i don't have any problem with the electronic side, my board works perfectly, my only problem is the programming part...i cannot make it the way it must function..

plz help..i need the R,L and S work...thanks...

Well, like I said, you can't get real time input using standard C. You could, for instance, create a menu for your program:

Please choose an option

[B]r[/B]: Move forward 10 rotations
[B]l[/B]: Move backwards 10 rotations
[B]s[/B]: Exit program
Please enter your input:

But it'd always have to come back to this menu after it does its 10 rotations or whatever, because it's not real time input. If you want real time input, go study your compiler documentation.

uhmm...i dont think we need the menu thing...coz our main output is the stepper motor itself...we are not limited to how many rotations, maybe an infinite rotation or 1 rotation, it doesnt matter..as long as it moves the right way when you press the corresponding button to it... the R, L, and S refers to the letters in the keyboard..so we use it to control the said motor...

How are you interfacing with the motor?
Are you using a computer port, pci card, or a special built micro controller?

If you are sure about motor drive side of things, then test the output from the port(s).

Without knowing the specifics of the entire setup, there is no way of making judgments about the correctness of the code.
PIA's need initialization, so I don't think you are using one of those...


Again, break the problem down into small pieces.
This can as simple as writing a loop to send highs and lows to the addresses and monitor for the desired result.

uhmm...i dont think we need the menu thing...coz our main output is the stepper motor itself...we are not limited to how many rotations, maybe an infinite rotation or 1 rotation, it doesnt matter..as long as it moves the right way when you press the corresponding button to it... the R, L, and S refers to the letters in the keyboard..so we use it to control the said motor...

Well, you don't have to print out the menu, but you'll still have to use a menu-like interface. Also of note is that you'll be required to input a newline every time you want to give it an instruction.

im using a computer port to program the code into the microcontroller...im trying to test it using the ports that i have in my microcontroller but the problem is..it still doesnt give me the right results...

What code are you using now? Remember to use code tags when posting code.

heres a sample code...

#include <reg420.h>
#include <stdio.h>
void main ()
{
TMOD = 0x20 ;
TCON = 0x40 ;
TH1 = 0xf9 ;
TL1 = 0xf9 ;
PCON = 0x80 ;
SCON = 0x50 ;
while (1) /*continues loop */
{ 
printf("a"); /* transmit a along with CR & LF.
}

what this does is it simply moves the motor in a forward direction..so how would you put controls to this? the R, L and S?

Did you verify that this code above does do the job?
For instance, according to that header file those are bit registers that are SET or UNSET to perform some task.
I believe to need to be sending o's or 1's to those addresses to set the registers.
I don't understand why sending an "a" with a CR and LF is important. besides is printf something you can do with a microcontroller? You are almost at machine code simplicity with those, aren't you? I mean with just basic address and data functions.

The program should not "run" the motor, it just controls it.
Think of an elevator analogy. You are the microcontroller pushing the buttons to make it open the door, go up or down and where. Otherwise the elevator processes are transparent.

here is a snippet form the header

/*  BIT Registers  */
/*  TCON */
sbit IT0    = 0x88;
sbit IE0    = 0x89;
sbit IT1    = 0x8A;
sbit IE1    = 0x8B;
sbit TR0    = 0x8C;
sbit TF0    = 0x8D;
sbit TR1    = 0x8E;
sbit TF1    = 0x8F;

This is an 8 bit register starting at 0x88 and ending at 0x8f

how would you make the rotation of the motor continuous? because my program rotates at a certain angle..i want it to be rotating continuously without stopping..tnx..

also it must be rotating smoothly. meaning there are no certain stops or clicking action...

did you get it do anything yet?
I took a quick look at a data sheet for a microcontroller like what you are using. You need to do the same!

As i suspected, those addresses are registers within the two Uarts in the IC.
Those registers control the functionality of the serial port.

Is this how you are to communicate with the motor drive?
What commands data does it need to spin CW , CW or stop?

You need to get the Uart working with proper initialization and just send data to the driver to verify that the communications is working, then work on the menu stuff.

The first task is to define the problem, then try to write the code.

how to code unipolar stepper motor with 4 push button to control their degree n angle?

Hye, i want to ask. how to make a program stepper motor driver that with pulse??. it is for whiteboard. i do that project for our final year. please help ~~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.