02 Jun 2010 @ 11:06 PM 

There is some scewyness with the compiler and enhanced pwm, so just to get all the p1a,b,c,d to work i had to use the code in this ccs post.

From there I wanted to know how the values are set to actually make the frequency you want. First you have to realize that the pwm is really just a result of a interrupt on overflow for timer2. So, whatever you set timer2 to be will be your fq of your pwm. To set up timer2 you use the command

setup_timer_2 (mode, period, postscale)

The mode is simply a divisor of your main clock frequency it can be 1,4 or 16. You should also note that since this is timer2 it also automatically divides your main clock by 4, each timer has its own “dividend” of the main clock, in timer2’s case it is 4. The postscale is the amount of times you will allow timer2 to overflow before you interrupt, so if you had everything setup for 500Hz and you wanted to actually use 1kHz you could just change postscale to 2 and it would interrupt every 2 times it overflows.

The hard one to figure out is the period, we all know what the period is but how do we set it in ccs? Well the max value is 255 and the min is 0 being that it is a 8 bit value (i think). So, 255 represents the max period which would be Tosc/(4*mode), you can figure out what that is. Just playing around half of 255 would represent half of Tosc/(4*mode). Anyway forget loosing yourself in all this there is a nice page here that oultines the whole thing and gives a nice formula

How to calculate the required values of the TIMER2: TIMER2 FORMULA

that (PR2-TMR2) is really the value you put for “period” in the function for ccs.  A quick example  at 4MHz main clock on the 887 to get 1kHz we would use

setup_timer_2(T2_DIV_BY_16, 124, 1);  // Set 1000 Hz PWM freq

so with that example we have a 1kHz frequency and this is where our timer2 would interrupt and our pwm would turn on/off to drive our motors. But, thats just part of the equation how about the duty cycle ? Turns out thats pretty easy to do (hard to find info on) you just take your period (for timer2 interrupts) and multipy it by the % you want of the duty cycle. So, if we have 1kHz and we want 25% thats (1/1000)*.25 which gives you 250uS then you times that by (1/Tosc) * “mode divisor” in our case 250uS*(1/4M)*16 which gives me 125.  I’m not sure if their value is wrong or my calculations but this is what they (ccs) give in the help file

value*(1/clock)*t2div

But you can just forget all that, since you’ve already calculated the value for “period” in setup_timer_2 you can just multiply that value by the desired % … for example 25% of our period 124 is 31 so we can set the duty at 25% with the instruction

set_pwm1_duty(31);  // Set PWM for 25% duty cycle

With this code I got the pwm working great, it would wait a few seconds then increase in increments to 90% afterwards it would go to 0% duty then start over again. Here is my code

code here . . .

after hooking it up to the hbridge circuit I made a short video of it switching speeds, at the end of the video there is a unhappy ending when the hbridges burn again. I really need to go back and redesign the Hbridge circuit to make sure shootthrough is never a possibility. The lines float or something happens and it burns up the hbrigdges quickly. I almost want to make my own hbridge so i would have a better idea whats going on but I dont have any mosfets that will handle the motors.
 

So enhanced mode pwm, and switching lines on/off pwm worked great ! Hbridge still burns up :(

Posted By: admin
Last Edit: 02 Jun 2010 @ 11:06 PM

EmailPermalink
Tags


 

Responses to this post » (None)

 
Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Change Theme...
  • Users » 5
  • Posts/Pages » 71
  • Comments » 62
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Contact Me



    No Child Pages.

Front



    No Child Pages.