Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Speed of a stepper motor

Status
Not open for further replies.

Zwilorg

Member level 5
Joined
Oct 13, 2010
Messages
89
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
1,906
Hello,

i am in need of some help trying to control the speed of a stepper motor...

Explanation:

1-receive a string from rs232
2-write it to a variable ("speed")
3-get it to work on a loop that increases the delay of each step on the motor.

i am posting a sample of my work to explain more or less what i am trying to to.

Code:
unsigned int speed;

void System_init(void)
{
TRISB = 0b00000000;	//set B1-B7 as output, B0 as input
}


void delay_manualms(unsigned int ms)
{
    while (ms--) __delay_ms(1);
}  






void main(void)
{

	System_init();	
	
	
while(1)
{
			
				PORTB=0b00000001;	//Step 0
				delay_manualms(speed);
				PORTB=0b00000011;	//Step 1
				delay_manualms(speed);
				PORTB=0b00000010;	//Step 2
				delay_manualms(speed);
				PORTB=0b00000110;	//Step 3
				delay_manualms(speed);
				PORTB=0b00000100;	//Step 4
				delay_manualms(speed);
				PORTB=0b00010100;	//Step 5
				delay_manualms(speed);
				PORTB=0b00010000;	//Step 6
				delay_manualms(speed);
				PORTB=0b00010001;	//Step 7
				delay_manualms(speed);
			
			
}
}

This works for 1 to 2 seconds before entering in an endless loop...

and being the while loop i need to insert numbers lower than 1 on the speed!

if i can´t get it working like this i would like someone to help get it to work using a for statement or something of the kind.

Decreasing the value just like an usual for cycle

Code:
for(speed; i = 0;i--){
 
        __delay_ms(1);
 
    }

i prefer this idea... because i would only need to get the value received from rs232 to the variable and then get it decreased from the value (for example 100) to 0 and each time causing a delay on the motor of 1second

but i can´t get it working and i don´t have sufficient knowledge in C to make the for loop a subroutine to be called after each step.


I hope you understand my problem :|
 

**broken link removed**


here it is.

I made a mistake while writing the code and jumped from RB3 to RB4, it is proper on the schematic ;)
 

Well the first thing you should be aware of is the current draw:

Input current
100 mA max (65 mA typical) with no output load

This exceeds the 25mA sink/source per I/O of the PIC16F877A.

PIC16F877A Datasheet

You would be wise to purchase of build a stepper moter driver.

The PIC most likely is resetting when the current draw ramps up. Besides you could easily damage the PIC with these current draws.

---------- Post added at 00:44 ---------- Previous post was at 00:42 ----------

You could construct a simple stepper motor driver out of a few transistors.

I'll see if I have a design lying around to post.
 
i have already built the schematic with the transistors in real life! (but with a little stepper motor and i had no trouble with it) ;)

but that part:

"This exceeds the 25mA sink/source per I/O of the PIC16F877A." is completely new to me... i have to check another PIC to make it work or get something to lower the mA before reaching the pic hmmmm


Thanks for this tip :O

and about the code can you help me?
 

Last edited:

Thanks for the links ;) i had indeed check 2 of them already :p


i can pretty easily control the position/movement orientation of the motor but the speed.... it is giving me a headache :S
 

The the current limits of the PIC almost currently have something to do with the speed limitation.

I'll dig the code up after I get something to eat. Low blood sugar!
 

This works for 1 to 2 seconds before entering in an endless loop...
What do you mean by that?
Your code uses an endless while loop anyway, what exactly is the problem?

for lower delay check if there is a delay_us function available.
There is also no need to call a function that uses the __delay_ms(1), you can use it directly __delay_ms(speed).

The motor current is not enough to drive the motor as bigdogguru said and also I see a 9v power supply connected to the motor, you can't use a voltage that high and expect the pic output to sink the current , it will damage the mcu because your voltage is way too high.

You need a bridge to use any voltage higher that the mcu supply and you need it anyway to provide the needed current , an example schematic is A simple H-Bridge design « PocketMagic

Alex

---------- Post added at 11:23 ---------- Previous post was at 10:11 ----------

Another note, the datasheet you mentioned shows a 4 wire bipolar motor but the schematic you gave uses a unipolar motor, which one is correct?
There is a very big difference between the two types.

Alex
 

i know that it doesn´t run the new motor i need the 48 or the 24V... the proteus simulation was just an example :S the proteus version i have does not have the unipolar design of the motor.

i tried the program for real with a unipolar 5 wire stepper motor ;) (a little one that would go from 5 to 9V)

And i use the lite version of hi tech compiler.... and one of the limitations of it is no variables inside __delay_ms()

it gives you the error:

Error [1387] C:\test\test.c; 35. inline delay argument must be constant.


with the Pro version this does not happen ^^

Best regards!

---------- Post added at 10:41 ---------- Previous post was at 10:17 ----------

So i solved my problem with the For cycle just now ;)

if anyone could help me creating a simple function :S

i have something like

Code:
		PORTB=0b00010001;	//Step 7

    for(i = 0; i < max;i++){
 
        __delay_ms(1);
 
    }
		PORTB=0b00010000;	//Step 6
    for(i = 0; i < max;i++){
 
        __delay_ms(1);
 
    }

i in each of the following steps i have the same for cycle but i would like to have a subroutine calling it out just like

Code:
void for_veloc_manual (void)
{ 
for(i = 0; i < max;i++){
 
        __delay_ms(1);
 
    }
}

void main (void){

while(1){
	PORTB=0b00010001;	//Step 7
for_veloc_manual(max);

	PORTB=0b00010000;	//Step 6
for_veloc_manual(max);

}}


The last one is a non working code it is just to explain what i want to do ^^
 

You mean something like


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
void for_veloc_manual (unsigned int x)
{ 
unsigned int i;
 
for(i = 0; i < x;i++){
 
        __delay_ms(1);
 
    }
}



if the values are up to 255 you can use unsigned char.

Alex
 
gotta try that out right now :O thanks m8!

that was exactly what i was searching ;)

i did something like that but i kept getting a build error... i got so used coding in matlab and vb that C almost seems like a new world...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top