| Author |
Message |
|
|
Posted: Nov 20, 2008 - 08:26 PM |
|

Joined: Nov 17, 2004
Posts: 13828
Location: Vancouver, BC
|
|
|
Quote:
When does OC1A and OC1B gets set or clear?
This depends on what mode you are in and how you have the COMx bits set. In your code (Phase correct and COMx bits set to 0b11) they will be set on compare match when the timer is counting up, and cleared on compare match when the timer is counting down.
Quote:
Also how do I set duty cycle?
By setting OCRxx registers.
Quote:
Suppose I want to use the duty cycle of 50%, that means i put half the value of 0x03FF into OCR1A...is that right?
Yes.
Quote:
Why would I need it?
In the case of your motors, the higher the duty cycle, the faster the motor turns.
Quote:
I want my robot to turn 90 degrees. I think this should be done by stopping one motor and rotating the other one. But for how long should I rotate the other one so that it turns 90 degrees. As in how do I fix the time for which a particular motor runs?
This depends purely on the mechanics of how your robot is set up (how big the wheels are, how far they are apart, speed range of the motors, etc).
Quote:
How does prescaling affect PWM? I saw that changing prescaler values made the code not run any more.
Changing the prescaler will only affect the frequency of the PWM. What frequency you want depends on what it is driving. |
_________________ Regards,
Steve A.
The Board helps those that help themselves.
|
| |
|
|
|
|
|
Posted: Nov 20, 2008 - 10:02 PM |
|

Joined: Sep 25, 2006
Posts: 51
Location: 28638
|
|
Sid,
I am no expert on this but basically what is going on is that the motor acts as inductance (a coil in electronics) and "changes" the waveform that you feed it into a "DC voltage". How effectively it does that for any given frequency is determined by the motor itself, i.e. the size of the wire, the number of turns, and possibly even the standing magnetic fields caused by the motor magnets.
Thus for any given motor, there is an inductance caused by the the specific items mentioned above for that exact motor.
Now, you apply a square wave of some frequency to that motor. If the frequency is too low, the motor just passes the wave form through and does not perform the "AC to DC conversion".
As the frequency goes up, the inductance of the motor is more and more efficient at converting the waveform to a DC voltage, so the wave starts to deform from a square wave to a sawtooth, and eventually (at a high enough frequency) the sawtooth turns into AC ripple on top of a DC voltage.
Because of the DV voltage, a DC current starts to flow in one direction through the motor and the motor tries to turn.
Now... what determines how fast the motor turns is the "on" time vs the "off" time. Obviously if you are feeding a square wave with only a millisecond of on time and 999 milliseconds of off time, the "DC VOLTAGE" applied to the motor is going to be very low. As you increase the ON time of the waveform, the DC voltage gets higher and higher, and so the DC current gets higher and higher.
This is in VERY "layman" terms. But essentially, at least up to a point, the higher the frequency, the more efficiently the motor will turn the signal from a square wave to a DC voltage. The higher the percentage of on time, the higher the DC voltage created, and the faster the motor turns.
There does reach a point of diminishing returns IIRC where higher frequencies actually causes losses in the wire of the motor, and instead of being turned into a DC voltage, the wire just becomes a big radio transmitter radiating significant amounts of power out as a radio signal. At too low a frequency, there is heating in the wire and power is lost to heat.
Thus there is a balancing act where you try to find out what frequency your specific motor most efficiently transforms the frequency you apply to the motor into rotational power (DC voltage).
Someone with more knowledge of motors will no doubt jump in and tell me I am all wet (which to some extent is no doubt true) but at least this should get you started in understanding what the parts the frequency and on / off times do for you. |
_________________ JWColby
AVRNubee
www.ColbyConsulting.com
|
| |
|
|
|
|
|
Posted: Nov 21, 2008 - 02:29 PM |
|

Joined: Sep 24, 2008
Posts: 12
|
|
Dear Koshchi,
Thank you for your detailed response. Had a few questions:
Quote:
This depends on what mode you are in and how you have the COMx bits set. In your code (Phase correct and COMx bits set to 0b11) they will be set on compare match when the timer is counting up, and cleared on compare match when the timer is counting down.
-But when you say compare match do you mean a compare match between TCNT1 and OCR1A? For example to get a duty cycle of 50%, I will put 0x01FF in OCR1A. So TCNT1 will count from 0 to 0x03FF while upcounting and as soon at it reaches 0x01FF the OC1A output will be set.I have attached an image to show my understanding of the above.Is this right? If it is then what is the difference between this mode (i.e PWM Phase correct with TOP as 0x03FF) and mode 11 (i.e PWM Phase correct with TOP as OCR1A).
-Also to make a motor run for a certain time, say 10 seconds, and then stop it, how will I implement this in code? Should I put delays up to 10 seconds?
Again thank you so much for your kind help and time. I really appreciate.
Regards.
SID |
|
|
| |
|
|
|
|
|
Posted: Nov 21, 2008 - 02:34 PM |
|

Joined: Sep 24, 2008
Posts: 12
|
|
Dear jwcolby,
Thank you so much for your response.
Quote:
This is in VERY "layman" terms. But essentially, at least up to a point, the higher the frequency, the more efficiently the motor will turn the signal from a square wave to a DC voltage. The higher the percentage of on time, the higher the DC voltage created, and the faster the motor turns.
This is exactly what I wanted to clear my basic understanding. Thank you so much once again
Regards.
SID |
|
|
| |
|
|
|
|
|
Posted: Nov 21, 2008 - 03:38 PM |
|

Joined: Nov 17, 2004
Posts: 13828
Location: Vancouver, BC
|
|
|
Quote:
I have attached an image to show my understanding of the above.Is this right?
Yes.
Quote:
If it is then what is the difference between this mode (i.e PWM Phase correct with TOP as 0x03FF) and mode 11 (i.e PWM Phase correct with TOP as OCR1A).
In this mode OCR1A is TOP, therefore it can not be used to set the duty cycle. You will only be able to get PWM on OC1B (using OCR1B to set the duty cycle). In the modes with a fixed number for TOP, both OC1A and OC1B may be used as PWM outputs (with independent duty cycles, but using the same frequency). |
_________________ Regards,
Steve A.
The Board helps those that help themselves.
|
| |
|
|
|
|
|
Posted: Nov 22, 2008 - 10:03 AM |
|


Joined: Oct 23, 2005
Posts: 55
Location: BELGIUM
|
|
Sorry, I was in the wrong topic.
I've found my solution in the ADC tutorial
( to the Mod. You may delete this if you want )  |
Last edited by ERELCE on Nov 26, 2008 - 10:50 PM; edited 1 time in total
|
| |
|
|
|
|
|
Posted: Nov 26, 2008 - 04:32 PM |
|

Joined: Sep 09, 2008
Posts: 297
|
|
Dean, my man, you are awesome. If I had a sister she would be yours for the taking
Instead of that I shall try to express my appreciation with words. Thank you. |
_________________ Code, justify, code - Pitr Dubovich
|
| |
|
|
|
|
|
Posted: Nov 26, 2008 - 09:50 PM |
|


Joined: Jan 23, 2004
Posts: 9825
Location: Trondheim, Norway
|
|
|
Quote:
Dean, my man, you are awesome. If I had a sister she would be yours for the taking
How about this - you take mine away, and we call it even?
Must remember to finish this during my holidays.
- Dean  |
_________________ Atmel Studio 6.1 is now released, grab it here.
Report AS6/ASF bugs here.
|
| |
|
|
|
|
|
Posted: Dec 01, 2008 - 02:28 PM |
|

Joined: Sep 09, 2008
Posts: 297
|
|
I'd feel tempted, but I'd anticipate problems as well.
Other then that, if things keep going the way I want them to go, I might be able to actually finish this lasertag project. |
_________________ Code, justify, code - Pitr Dubovich
|
| |
|
|
|
|
|
Posted: Feb 09, 2009 - 11:44 AM |
|

Joined: Feb 09, 2009
Posts: 18
Location: Auckland, New Zealand
|
|
| Will this ever be completed? This and the preceding tutorial are by far the best I've found and it's a shame it isn't finished. |
|
|
| |
|
|
|
|
|
Posted: Feb 09, 2009 - 11:49 AM |
|


Joined: Jan 23, 2004
Posts: 9825
Location: Trondheim, Norway
|
|
|
Quote:
Will this ever be completed? This and the preceding tutorial are by far the best I've found and it's a shame it isn't finished.
One day! I've had a lot more on my plate these holidays than I expected (read: I actually managed to get a job) which hasn't left me the motivation to finish it in the spare time I've had.
Perhaps I'll have a crack at it tomorrow, since I haven't got anything scheduled.
- Dean  |
_________________ Atmel Studio 6.1 is now released, grab it here.
Report AS6/ASF bugs here.
|
| |
|
|
|
|
|
Posted: Feb 09, 2009 - 08:51 PM |
|

Joined: Feb 09, 2009
Posts: 18
Location: Auckland, New Zealand
|
|
*holds breath in anticipation*
good on you for finding a job - I left finding my summer job until after exams, which was right when the so-called recession began and now no-one is looking to hire. |
|
|
| |
|
|
|
|
|
Posted: Feb 16, 2009 - 06:45 AM |
|

Joined: Feb 09, 2009
Posts: 18
Location: Auckland, New Zealand
|
|
well after a bit of reading the datasheet, PWM is in fact as straightforward as hoped - took as long to find the typo in my code as it did to write it
just one question: frequency and phase correct mode - are not all modes frequency correct? It may take a bit of fiddling to get the desired frequency without too great a compromise in resolution, but I fail to see how we can have a phase (but not frequency) correct PWM signal |
|
|
| |
|
|
|
|
|
Posted: Mar 09, 2009 - 03:01 AM |
|

Joined: Jan 25, 2007
Posts: 1
|
|
As of getting the robot to turn you can do as you said, but another way of making a robot turn is, to reverse the direction of one of the motors... The your robot will truely turn in a 90 degree angle  |
|
|
| |
|
|
|
|
|
Posted: Mar 18, 2009 - 03:37 AM |
|

Joined: Dec 31, 2006
Posts: 168
|
|
| Pardon my ignorance, but I cannot figure out how to set the PMW to 8-bit rather than 10. Also, the duty cycle can be set as an integer... right? |
|
|
| |
|
|
|
|
|
Posted: Mar 18, 2009 - 05:19 AM |
|

Joined: Nov 17, 2004
Posts: 13828
Location: Vancouver, BC
|
|
|
Quote:
Pardon my ignorance, but I cannot figure out how to set the PMW to 8-bit rather than 10.
This depends somewhat on which AVR and which timer you are using. But it is really just a matter of setting the correct mode.
Quote:
Also, the duty cycle can be set as an integer... right?
Of course, what else would it be? |
_________________ Regards,
Steve A.
The Board helps those that help themselves.
|
| |
|
|
|
|
|
Posted: Mar 18, 2009 - 05:04 PM |
|

Joined: Dec 31, 2006
Posts: 168
|
|
| I just assumed it had be be set as a hex rather than a 'counting number' |
|
|
| |
|
|
|
|
|
Posted: Mar 18, 2009 - 05:05 PM |
|

Joined: Dec 31, 2006
Posts: 168
|
|
| I just assumed it had be be set as a hex rather than a 'counting number' |
|
|
| |
|
|
|
|
|
Posted: Mar 18, 2009 - 07:53 PM |
|

Joined: Nov 17, 2004
Posts: 13828
Location: Vancouver, BC
|
|
|
Quote:
I just assumed it had be be set as a hex rather than a 'counting number'
But hex is a counting number, it just goes up to 15 instead of 9 for each digit.
The AVR only knows binary and the compiler will always transform whatever human readable form the number is in to binary. |
_________________ Regards,
Steve A.
The Board helps those that help themselves.
|
| |
|
|
|
|
|
Posted: Mar 19, 2009 - 09:26 AM |
|

Joined: Nov 30, 2008
Posts: 174
Location: India
|
|
| How do I generate PWM from two Pins which go to a H-Bridge to stall a DC motor?What care is to be taken when motor is to be stalled?(I would refer to this as 1-1 state :stalled or brake,where 1-0 corresponds to say clock wise,0-1 to anti and 0-0 to off) |
|
|
| |
|
|
|
|
|