Controlling the direction of the motor

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

HI all, i am trying to control the direction of the motor forward or bacward, i am giving out signal, by using DAC from Potentiometer. i am working on KIT number CY8C29466-24 SXI.

   

If anyone can help me to figure out how to control that would be great.

   

 

   

Thanks

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

It is always not adviseable to connect an inductance (motor coil) directly to any digital circuit, even when the motor is a very small one. There are interfaces named H-Bridges giving you full control over the direction of the movement, strength (voltage) and torque (with PWM), even the start-stop behaveour may be controlled.

   

An H-bridge may be made of 4 separate transistors or may be contained in an IC, look here

   

http://en.wikipedia.org/wiki/H_bridge

   

Bob

View solution in original post

0 Likes
54 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

It is always not adviseable to connect an inductance (motor coil) directly to any digital circuit, even when the motor is a very small one. There are interfaces named H-Bridges giving you full control over the direction of the movement, strength (voltage) and torque (with PWM), even the start-stop behaveour may be controlled.

   

An H-bridge may be made of 4 separate transistors or may be contained in an IC, look here

   

http://en.wikipedia.org/wiki/H_bridge

   

Bob

0 Likes
Anonymous
Not applicable

but how i would use H brige on the KIT?

   

is there anyway to control the signal that i am sending to direction bit, by inverting the bit ?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Take a look at this, and references at bottom of page.

   

 

   

http://en.wikipedia.org/wiki/H_bridge

   

 

   

There are a number of manufacturers that makes H Bridges, which

   

include thermal and overvoltage, over current protection, and include

   

the bias pumps to make them TTL compatible.

   

 

   

Regards, Dana.

Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The direction is a digital signal which you can control by writing to the corresponding port-bit. Take care of the read-modify-write problem, best use a shadow-register. Look here     http://www.cypress.com/?id=4&rID=28251 and here http://www.cypress.com/?rID=39497

   

 

   

When you've got a kit there are usually some LEDs on it. You may use the LED-component to switch a port-bit and have it indicated. You even may use this construct to switch the motor-direction.

   

Bob

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

i tried to do it but i failed, if anyone can take a look at it and let me know what i am doing wrong,

   

 

   

Thanks

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Post a schematic of your interface, and wiring to PSOC.

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

I did see one error, per datasheet both MSB and LSB must have the same column clock per the

   

following note in datasheet -

   

 

   

"An additional consideration, in selecting a placement location, is that the MSB and LSB clocks must be derived from the same source. This happens automatically, if they are placed in the same column in the analog array. If they are placed in two different columns, both column multiplexers must be set to the same source."

   

 

   

Regards, Dana.

lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I put some suggestions into your code, watch out for ********* markings.

   

 

   

Bob

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

sorry for bothering, but i am new with the PSOC, can you look at it and let me know how to get out the loop, because it doesnt detect the switch when its on/off. and stuck in one direction.

   

 

   

Thanks

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Since it is not 100% clear to me what signals you exactly expect I made a digital approach at first.

   

I generated one signal for the direction and one for motor on/off. You may turn the second signal at need into a PWM to control the motor speed.

   

 

   

Happy coding

   

Bob

lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

(§&%$!!!) and here's the project - file

   

 

   

I WANT TO HAVE AN "EDIT" FEATURE!

   

 

   

Bob

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi Bob, i used what you sent me and used it in my code but still i am missing something.

   

so you can understand what i am trying to do in this project. I am using CY8C29466-24 SXI to control a motor unit. i am trying to control the speed of the motor by connecting the potentiometer to input PORT 0-7, and the output speed on PORT 0-5.

   

Controlling it by swtiches RUN/STOP connected to PORT 0-1 , and CW/CCW ( direction ) to PORT 0-0.

   

i am trying to be able to change speed while motor is running or while its not running. and by the right direction.

   

I attached the project, can you take a look at it and see what am i missing in it.

   

 

   

Thanks

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are using the variable "iData" as the loop-control and you are assignig a value to it within the loop, thus the loop will never terminate correctly.

   

On the other hand I suspect that you are connecting your motor directly to the DAC output of the PSoC which will destroy it immediately!!

   

Controlling a motor is usually done with a PWM which has a digital output with a varying duty-cycle to control the speed. Although the PSoC is able to drive a few mA to drive a (very) small motor you may NOT connect the motor directly to it because the running motor will create (like a dynamo) voltages that are grater than the allowed 5V and so blowing immediately the protection circuits at the pins.

   

Before continuing with the programming of your PSoC try to make a schematic and post it here, we'll have a look at it.

   

 

   

Bob

Anonymous
Not applicable

if it will be helpful i will use it, because after i am done with that, i have to capture the time input and compare it to the output capture PRT.

   

But how i use it ? because i have to vary the input manually using the potentiometer using DAC9 (iData)

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I am not quite clear what you exactly want do do. English is not my native language, so please be so kind to explain a bit more to me so that I can understand your needs. Helpful for you (and for me) would be a block-diagram to have a look at your concept, so that we are talking about the same thing.

   

 

   

Bob

Anonymous
Not applicable

ok, i attached my diagram. basically i have two digital inputs, one for power and other for direction. and the output will be for enable to let motor run, direction, speed, and monitor back the timer input capture which take it and compare the frequency of it to the output RPM frequency. i control the speed by Potentiometer.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

OK, I think I understood. Will be back tomorrow (it's here 23:00h)

   

Bob

Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Since you are using a PSoC1 and you did not tell us about having got an ICE (In-Circuit-Emulator) you will have to set up a minimum man-machine-communication to get a little bit of insight what is going on within your chip. The minimum is a couple of LEDs you may turn on or off, if your kit has got an LCD-module, you can use that.

   

Best will be to work in little steps, realizing one block after the other, testing it and integrating it. Sometimes it will be easier to build a part of the project separately, get it tested and then integrate it into the project (and test it again).

   

Since you still have to learn some programming skills, best is to frequently upload your project here, so that we can have a look at and having something solid to diskuss on.

   

So at first i suggest you to set up your test-environment (LEDs/LCD).

   

then Connect your input-signals and verify each of them getting seen in your program

   

Connect your output-signals and see that you can set them progammatically.

   

DOCUMENT ALL CONNECTIONS !!!

   

Since we have to read your program, try to make it look easy, use names that speak for themselfs (iData is not as good as ADC_Value), use clear indentation and comments to explain what you're doing. Using function calls makes the whole project easier to overlook and even may save precious RAM: using a function as "void InitializeHardware(void)" and calling this from main() will show where all related stuff has to go to and shortens main() to the very essential part.

   

Happy coding

   

Bob

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi Bob, i attached the file.

   

i tested the switches seprate with LEDs and they were function right, but when i put the code in my project, it is not function all correctly. i think because i always getting data from ADC. can you take a look at it. now the problem are when i switch to Stop, it doesnt decrease the speed till Zero as it should do. it just stop the motor. and when i switch to Run mode, it starts as iData value is. not speeding up from zero up to the value as it should be doing.

   

can you advice me in that ?

   

Thanks

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Have a look at that, maybe you can start from that...

   

 

   

Bob

Anonymous
Not applicable

hi Bob, can you help me more to figure out what am i doing wrong or what should i use to solve it, i looked at code you sent me, and i donot think it will help me, because i have to control the motor my switches, RUN/STOP, and direction CW/CCW.

   

Thanks

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Here is the code, the thing is that it stuck in the while loop, i dont know how to get the motor react when i change the switch.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The code I supplied acted on your RUN and CW switches, but probably you didn't see how.

   

Your learning curve for C-programming seems to be at the beginning, so a couple of hints you should obey:

   

Declare variables in the functions where you need them. Do not use global (outside of main() ) vars, they open the door for unwanted side-effects.

   

Have a look at the meaning of "static" storage-class

   

You are not yet thinking binary: you defined RUN as 0x02 and STOP as 0xfd (which would have been better declared as ~RUN)

   

First if(POWER & RUN) which will result in the binary AND and will give a correct result.

   

Second if(POWER & STOP) which will result in the binary AND of PORT0DR and 0xFD which you do not want.

   

so, it is (POWER & RUN) for running condition and !(POWER & RUN) for stop condition. Follow the logic my example shows.

   

The same applies for your CW/CCW-logic.

   

Turning on and off a bit of an I/O_port as you have started by defining your LED-constants will not act as expected. for beginners I would suggest to use the LED-Component and the LED_On(), LED_Off() APIs.

   

 

   

Bob

ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Some additional thoughts on variables, first a summary of static vs global -

   

 

   

Global variables are variables defined outside of any function. Their scope starts at the point where they are defined and

   

lasts to the end of the file. They have external linkage, which means that in other source files, the same name refers to the

   

same location in memory. Static global variables are private to the source file where they are defined and do not conflict

   

with other variables in other source files which would have the same name.

   

 

   

I use globals outside of main(), ones that are used thruout code base, because generally speaking lessens copying

   

operations inherent in variable passing. Especially structures that are at heart of program. Also I use pointers to globals to

   

limit copying. These have the significant advantage code size reduced, especially for variables not machine native, like

   

longs and floats. And speed advantage, the less copying the more MIPS for other needs.

   

 

   

Regards, Dana.

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi Bob, yes i looked at your code and i did the same concept, but my problem is when i try to use a switch, it get stuck in the first loop and never come out from it, so what should i use to make it exit out the loop condition (POWER&CW) to go to (!(POWER&CW) for example?

   

also i want to speed up untill it gets to POT value in a slope like increase slidelly. and when i switch to different mode, i want it to decrease slidlly to zero then increase slidelly.

   

Take a look at what i have been done and let me know.

   

Thanks

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Looked over your file and left comments with ****

   

 

   

Bob

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

i fixed it, and it works with CW/CCW but once i switch to STOP, it stops the motor but doesnt go to my else function.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

thats the updated one, the STOP condition still not function right. it should be anytime i switch to stop (!(POWER&RUN)) should increase to the POT value. not resume the iData.

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

It is now the third time i make some corrections at the very same places, you did not take the all the advices I gave you, you did not correct the timer clock and period..... you must be a bit more precise.

   

Take a look at this. As a hint: everything that occurs more than twice in the code is often something that can be optimized.

   

And have a look at the two new macros.

   

 

   

Bob

Anonymous
Not applicable

Hi Bob, i am sorry but i tried the way you told me and didnt function like it should be thats why i kept everything the way it was.

   

in the Code you sent me last, it only function with one direction, its not working when i switch direction or when i turn Run to Stop. i tried to get it fix but i dont understand why it doesnt let me.

   

Thanks for helping.

   

 

   

while(forever)if(POWER & RUN)do// Set the Direction outputwhile(ADCINCVR_fIsDataAvailable() == 0) Wait(); // Wait for data to be ready.// Get Data and set motor-speedwhile (( POWER & CW )==1);do // Set the Direction outputwhile(ADCINCVR_fIsDataAvailable() == 0) Wait(); // Wait for data to be ready.// Get Data and set motor-speedwhile ((!(POWER & CW)) ==1 );else0); // set it to zero // set it to zero

   

 

   

{

   

 

   

{

   

 

   

 

   

{

   

DirectionPin_Switch(POWER & CW);

   

 

   

RunPin_On();

   

 

   

 

   

 

   

MoveMotor(ADCINCVR_iGetDataClearFlag());

   

 

   

}

   

 

   

 

   

{

   

DirectionPin_Switch(!(POWER & CW));

   

 

   

RunPin_On();

   

 

   

 

   

 

   

MoveMotor(ADCINCVR_iGetDataClearFlag());

   

 

   

}

   

 

   

}

   

 

   

 

   

{

   

MoveMotor(

   

 

   

DirectionPin_Off();

   

 

   

RunPin_Off();

   

}

   

}

   

}

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

As I asked a while ago, can you please supply us with a schematic (Drawinng, picture) of your external connections to the motor (or however you emulate the motor). I think there may be some misunderstanding.

   

With your supplied code , sorry but this forum-software sometimes is not able at all to handle code correctly there seems a lot to be out of order so I will not comment it.

   

Bob

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

i attached the project and how should look like, basically i am controlling a motor through the KIT. i have 2 switches, one is for RUN or STOP the motor which is connected to P01, and other is for CW or CWW to control the direction of the motor connected to P00. i have POT connected to PORT 0-7 and the output to PORT 0-5.

   

i want to measure the frequency of the speed of the motor and compare the output frequency to the PRM out. so when the switch on RUN, it starts from zero slidly increase until value of POT, and when i switch CW to CCW, the motor's speed should decrease till it gets to zero then switch the direction and then from Zero it starts increase till the POT value,, while the motor is running or stop i can be able to change the value of the POT. and when i switch to STOP, it should decrease the speed to Zero.

   

if you look at the code that i had and attached, you will see that i want to get control of the speed by incrementing the speed or decrement the speed and also switches and be able to change the value of the POT while its running, may be that why i was keep using my code because i am almost there just little bugs.

   

please look at the attachments.

   

 

   

Thanks

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Now with the diargam there is a lot more clear to me! I'll come back with some more info later.

   

Boib

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Great, and here is my lastest code i have.

   

Thanks Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

So, back again. The drawback of the software design used is that during the slow-down or accelerate of the motor the switches and poit are not read. When principially the design runs, you should update the DAC in an interrupt-routine independently. Since your schematic shows a tach-input I suppose you'll implement a closed-loop to control the motor-speed.

   

What is the reason to slope the motor-speed? Is that not done by the interface? Are you going to use the break-input?

   

Bob

Anonymous
Not applicable

we need the slope becasue the project requires to make motor slow down till reach zero, or start from zero and speed up till it reaches to the POT value. yes we will use the break-input but that will be the last thing i will do.

   

after i get motor function right with the switches, i have to figure out how to measure the frequency of the motor compare to the input frequency.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

There is a Tach-Timer usermodule which can be used in measuring the speed of a motor. This tends to start into something like a control-loop calculating (Must-Is) * Factor to change the speed...

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Although PSOC 3 oriented some techniques for motor control videos

   

here -

   

 

   

http://www.cypress.com/?rID=60521

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

i dont think the code that you sent is helping me, i want to control the speed by incremting or decreamnting untill it get to the value of the POT, but i have to check first if it is in RUN mode then if it is CW or CCW then change direction of motor then increament the speed of the motor, and once it is on STOP mode, it wont matter CW or CCW so it will decreament till Zero.

0 Likes