![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
jemly
Joined: 13 May 2007 Posts: 34
|
Newbie PWM question |
Posted: Sun May 13, 2007 2:36 pm |
|
|
I'm using a PIC18f452 and am new to CCS C. I've written some code which produces 2 PWM outputs but I don't entirely understand how to use timer2 and all the settings to get the accuracy I require. The 2 PWM outputs will be fed into a motor drive chip which will enable us to control motor position by varying the PWM duty cycle.
I need to be as accurate as I can when positioning the motors (used for x acis and y axis control). My system clock is 4MHz........ can anyone give me some tips here on how I calculate the correct values for Timer 2?
Am I correct in saying that I've got up to 10bits which means I could effectively move either motor 1023 increments on each axis? Therefore if I wanted to move one of the motors to the mid position I would set the PWM to 50%.
Have I understood this correctly, and if so what would the optimum set up for Timer2 be? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sun May 13, 2007 2:53 pm |
|
|
Basically yes.
The smallest 'step', that the pwm can be set to, is the master oscillator frequency (unless a prescaler is used), so the highest frequency PWM, which offers the full ten bit resolution, is the master oscillator/1024. Hence for your 4MHz clock, you would need to use a PWM frequency of:
4000000/1024 = 3906.25Hz
This is low enough, that there may well be some 'buzzing' on the motors, so you mght want to see if you could use a higher master clock. If you used the 4*PLL on the chip (so the processor actually runs at 16MHz), then the frequency would jump to 15625Hz, which is more likely to be an acceptable value.
With any clock, you can run at a higher frequency, but at the cost of the number of available steps.
For this maximum frequency, you would setup timer2, with:
setup_timer_2(T2_DIV_BY_1,255,1);
The last number doesn't matter (it is only needed if you want to use the ability to interrupt from this timer). The fist number is the prescaler (for the highest output frequency, this needs to be '1'), and the second number is the number of available steps in the PWM, over 4, minus 1.
For the PWM, with 0 to 1023 counts (1024 steps), you get:
1024/4 = 256
256-1 = 255
The maximum value for this number is 255.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
jemly
Joined: 13 May 2007 Posts: 34
|
|
Posted: Mon May 14, 2007 1:44 am |
|
|
Thanks to both of you for such useful information!! One question for Ttelmah is what do you mean by using the 4*PLL on the chip - I haven't heard of this before! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon May 14, 2007 2:29 am |
|
|
Eeek!.
Read the data sheet.
Look at the 'oscillator' section.
The chip you are using, supports having an internal *4 PLL (enabled using the fuse 'H4' in CCS), which takes the external clock, and multiplies it by four, before feeding the internal operations. This allows the chip to run with a 4MHz external clock, and perform 4MIPS, instead of the 'normal' 1MIPS from the 4MHz clock. The increase in frequency, also affects everything else using the clock (such as the PWM), so with this enabled, your chip, with no hardware changes, would run off a 16Mhz internal clock.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
jemly
Joined: 13 May 2007 Posts: 34
|
|
Posted: Mon May 14, 2007 2:49 am |
|
|
!! I guess I should have known that!! Thanks for explaining so clearly. |
|
![](templates/subSilver/images/spacer.gif) |
windz
Joined: 19 Aug 2007 Posts: 7
|
|
Posted: Sun Aug 26, 2007 8:23 pm |
|
|
Let's say if i want to make the motor rotate for 10 complete revolutions, does that means for a 500 steps/revolution stepper, 100% duty, i just have to include a counter just like this?
Code: | for (i=0; i<10; i++){
setup_timer_2(T2_DIV_BY_1, 124, 1);
set_pwm1_duty(100);
setup_ccp1(CCP_PWM);
}
|
Will the motor stop to rotate after 10 revolutions? if not, what else should i include in the code? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Aug 27, 2007 2:39 am |
|
|
PWM, has nothing to do with revolutions of the motor.....
The PWM, controls the percentage of the 'time' that power is applied to the motor. How far the motor will rotate in a given time, with the power on for a given percentage, will change according the load on the motor, the design of the motor itself, etc. etc..
Think of a modern battery electric drill. Most change speed, as you pull the trigger button in/out. This is PWM control. However the actual speed at the shaft, will also change as you load it, and with different drill models. You can pull the trigger half way for a second, and according to the drill model, and the load, the chuck could turn a vast range of different distances. This is all that PWM gives you.
To stop after ten revolutions, requires you to add feedback from the shaft position to the processor, and actually 'count' the revolutions (or better, smaller units), and then adjust the PWM, to 'home in' on the required target position. This is 'servo' control, and requires a lot more hardware, and code...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
windz
Joined: 19 Aug 2007 Posts: 7
|
|
Posted: Mon Aug 27, 2007 3:27 am |
|
|
Hi, Ttelmah. Thanks for your reply. I get a better picture of PWM now.
I am using a 0.72 degrees stepper motor, so a complete revolution would be 360/0.72 = 500 steps per revolution. Say, 10 revolutions would be 5000 steps, which in other words mean, 5000 pulses generated from PWM.
My question is, how am i going to "count" the pulses been sent and eventually "terminate" the pulses when i no longer required them in the program? Are there any functions, commands or magics could be done on that using CCS? Could you please suggest me one of them? I can't see harry potter in CCS. If the functions of CCS allowed me to count and stop the pulses, i think, the positioning problem could be solved by trial and error on the distance. (lousy way without PID)
I'm stuck in the stepper maze... mind showing me another way out? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Aug 27, 2007 5:05 am |
|
|
Whoa again....
For a simple stepper motor, PWM, does not come into the equation.
You can use PWM control, to adjust the current fed to a winding, for half stepping, or to allow higher voltage drive (and therefore faster stepping), but the movement of the motor, is not done using PWM itself. For a stepper motor, _you_ have to generate the step patterns into the windings, and move through these the rght number of times, to get the motion required. None of this involves PWM....
The other possibility, is that you have a step/direction controller running the motor?. If so, then you need a pulse train, at the right rate, to step the motor. You can use the PWM, to generate this train, but you would normally want to be a bit more ingenious, and change the rate, to give motor acceleration/deceleration ramping. If you use such a train, then PWM itself, gives no inherent ability to 'count' the pulses. For this, you would want to use the CCP. Given the relatively slow rates needed for a stepper, you have two choices. The first is to use the CCP, to generate the output pulses (using the set/reset functions of this), and count the pulses sent, in the CCP interrupt. The other, is to feed the PWM signal, into the other CCP input (most chips have two CCP/PWM channels), and have this second channel, interrupt when the required count is reached.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|