CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Details on set_pwmX_duty values

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

Details on set_pwmX_duty values
PostPosted: Sat Feb 10, 2007 9:45 am     Reply with quote

I am totally confused regarding the value I should put in set_pwmX_duty()

PCM Programmer had put forward that 50% duty cycle means 50% of 249 = 124.

But I got only 8% of the output value by setting the value to 124.

However, I get approx 50% duty cycle by keeping the value as 500, which is exactly the value suggested by PWMWizard.

My code is as follows :

Code:
 //CCS 3.249
#include <16F877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
//#fuses HS,NOWDT,PROTECT,NOLVP

#use delay(clock=20000000)

#define SET_LEFT_MOTOR_DC   set_pwm1_duty
#define SET_RIGHT_MOTOR_DC   set_pwm2_duty

/* LUT containing values computes as : (index * 249), where index denotes the duty cule percent. For example, the  0th value (0%) is 0, the 10th entry is 10 * 249 = 24, etc */
const unsigned int8 uiDutyCycleLUT[] = {0, 2, 4, 7, 9, 12, 14, 17, 19, 22, 24, 27, 29, 32, 34, 37, 39, 42, 44, 47, 49, 52, 54, 57, 59, 62, 64, 67, 69, 72, 74, 77, 79, 82, 84, 87, 89, 92, 94, 97, 99, 102, 104, 107, 109, 112, 114, 117, 119, 122, 124, 126, 129, 131, 134, 136, 139, 141, 144, 146, 149, 151, 154, 156, 159, 161, 164, 166, 169, 171, 174, 176, 179, 181, 184, 186, 189, 191, 194, 196, 199, 201, 204, 206, 209, 211, 214, 216, 219, 221, 224, 226, 229, 231, 234, 236, 239, 241, 244, 246, 249};

void main()
{
   disable_interrupts(GLOBAL);

   output_low(PIN_C1); // Set CCP2 output low
   output_low(PIN_C2); // Set CCP1 output low

   setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
   setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM

   //setup_timer_2(T2_DIV_BY_4, 249, 1); // This gives 5 kHz

   setup_timer_2(T2_DIV_BY_16, 249, 1); // This gives 1250 Hz
   /* Start with normal state */

   SET_LEFT_MOTOR_DC(uiDutyCycleLUT[50]);
   SET_RIGHT_MOTOR_DC(uiDutyCycleLUT[50]);
}


Could someone (maybe PCM Programmer himself, write a small detailed description of how to choose the value ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Feb 10, 2007 11:28 am     Reply with quote

You need to put a while(1); statement at the end of your program.
If you don't have it, the PIC will go to sleep and PWM will stop working.

If you need more help (from me), I can do it on tomorrow, on Sunday.
Then I'll have a scope available, etc.
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 10, 2007 11:55 am     Reply with quote

Oh no... the code did not end there... i just copy pasted te relevant portion... you can very well put a while(1); at the end.

Yes... to play around with the timer values and the duty cycle values.

PWM is a section which I see is a PITA...

Most questions that surface are either PWM or LCD or ADC as far as I can see ...

And thanks PCM Programmer... you have been the most helpful.
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 10, 2007 11:59 am     Reply with quote

@PCM

Also do comment on the snippet in general... for example... my idea of putting the LUT in ROM and so on..

I heard that you need to follow a certain init pattern to setup PWM ?
Do you know about it ?

But one thing is for sure... I want to put an end to all the PWM confusion I have... this duty cycle calculation is getting on my nerves...

- the formula that Microchip gives in their datasheet - does it yelid the value in time period or count/timer values ?
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

PostPosted: Sun Feb 11, 2007 7:18 am     Reply with quote

@PCM

I think that the idea of using the percentage value of the timer overflow value in setup_timer is valid ONLY when using a scalar value of 1.

For values of 4 and 16, the duty percent values need to be scaled accordingly...

Could you verify it on the scope... I may well work with you on it ...but I will have to work for it after office hours as I don't have a scope at home...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 11, 2007 2:22 pm     Reply with quote

I took the code that you posted, added a while(1); statement to the end,
and ran it on a PicDem2-Plus board, with a 16F877A and 20 MHz crystal.
I'm looking at pins C1 and C2 with my scope.

I see a 1.25 KHz signal on pins C1 and C2.

Both pins are at 50% duty cycle.

I have the scope set on 100 us/division. Each half cycle of the square
wave takes 4 major divisions, which is 400 us. Then, 2 x 400 us = 800 us,
which is 1.25 KHz.

I'm using PCM vs. 3.249 with MPLAB, and a Microchip ICD2 programmer.
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

PostPosted: Sun Feb 11, 2007 8:13 pm     Reply with quote

@PCM

Could you just run the duty cycle index from 0 to 100% (right now the index is set to 50%) and get back to me...

I always test my code before burning it and am sure I got the firmware right, but maybe I think the problem lies in the hardware I am using.. but still, it's reassuring to know my code is ok...

Do have a program ro run through the LUT from 0 to 100%

I have a favour to ask - could you PM me with your email address so I can send you in details the application I am applying the codee to ? It's basically a DC MCS with the LMD18201 and a 877A.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 11, 2007 8:32 pm     Reply with quote

I can do the test with incrementing the index on Monday. I don't want
to send my email address. I only want to help people through the forum.

--------
Edited to add:
On 2nd thought, I don't want to test running the LUT. My goal on this
forum is to help people get past a sticking point on their project, where
some piece of hardware doesn't work and the driver needs a few tweaks,
or perhaps a CCS function doesn't work properly and it needs some
work-around code. I don't want to be a co-partner on a project.
I don't want to be asked to "approve" someone's code. I don't want
to be asked to test someones code and tell them that it works. For those
reasons I'm going to decline your request.
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 12, 2007 1:02 am     Reply with quote

PCM programmer wrote:
My goal on this
forum is to help people get past a sticking point on their project, where
some piece of hardware doesn't work and the driver needs a few tweaks,
or perhaps a CCS function doesn't work properly and it needs some
work-around code. I don't want to be a co-partner on a project.
I don't want to be asked to "approve" someone's code. I don't want
to be asked to test someones code and tell them that it works. For those
reasons I'm going to decline your request.


Very good points.

I will test my code and hardware in this week and get back to you.
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

PostPosted: Fri Feb 16, 2007 9:46 am     Reply with quote

@PCM

You were right at the start... from the VERY beginning....

To summarize what you said :

The duty cycle value is EXACTLY the percentage of DC you want of the timer count value.

Now I got to have the bit resolution worked out... do you have good docs on PIC PWM except the datasheet ?

If not... how should I start with the PWM datasheet ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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