1 2 3 4 84 Replies Latest reply on Feb 22, 2013 7:07 AM by userc_38527 Go to original post

• ###### 16. Re: Simple PWM

PWM Properties, atatched.

Regards, Dana.

• ###### 17. Re: Simple PWM

if the parameter of the servo are :

In order to drive a servo :-

1. It should be sent a pulse every 20ms
2.
3. The width of the pulse should 1.5ms to zero the servo
4.
5. A width of 1ms will make it turn fully to one side
6.
7. A width of 2ms will make it turn fully to the other side
8.

What's the pulse width and period for it ?

thanks

• ###### 18. Re: Simple PWM

You have the numbers listed .....

It should be sent a pulse every 20ms

1. The width of the pulse should 1.5ms to zero the servo
2.
3. A width of 1ms will make it turn fully to one side
4.
5. A width of 2ms will make it turn fully to the other side
6.

So the period is 20 mS, or 50 Hz. So you start with your CPU clock, and the product

of all divisors to the clock you select, have to produce 50 Hz / 20 mS. One other consideration,

is the resolution you want for servo position, eg. duty cycle. So say that equates to

100 uS. Example -

24 Mhz clock

VC1 you choose for what ever reason to be 4 Mhz = 24/4 = 6 as divisor

You choose VC2 to be PWM clock

PWM width = 20 mS / 100 uS = 200, so 8 bit PWM OK

The clock for PWM must have a resolution of 100 uS, 10 Khz.

To get 10 Khz period we have to divide by = 4 Mhz / .01 Mhz = 400. Since divisor for

VC2 limited to /16, so we will have to use VC3 as PWM clock to get a total divisor of 400

So let VC2 = 4, VC3 = 100 as divisors

So now we have 10 Khz into PWM, wnat period to be 20 mS, so period value = 200 - 1 = 199

I will let uoi compute from equations compare values to get 1, 1.5, 2 mS.

Regards, Dana.

• ###### 19. Re: Simple PWM

so it will be like in the picture ?

where can I put 1ms and 2ms ?

in a code ?

how ?

thanks

• ###### 20. Re: Simple PWM

Could it be like this ?

it's taken from MCS51 code

//motor begin
count=10;
//rotate left every 45 degree begin
for(i=0;i<50;i++)  // 10 equal to 5 degree
{
output=1;
timer(count);
output=0;
timer(100);

}
delay(30000);

//rotate left every 45 degree end

• ###### 21. Re: Simple PWM

The picture showing you the properties window is where you set

properties for when PSOC starts up, for that module, and the

global chip properties.

In your case you will be changing PWM pulese dutry cycle with software

as well. In datasheet are functionb call you can use to write the compare

register which will change the duty cycle.

Regards, Dana.

• ###### 22. Re: Simple PWM

The picture showing you the properties window is where you set

properties for when PSOC starts up, for that module, and the

global chip properties.

In your case you will be changing PWM pulese dutry cycle with software

as well. In datasheet are function call you can use to write the compare

register which will change the duty cycle.

Regards, Dana.

• ###### 23. Re: Simple PWM

I have created :

void GenerateOneThirdDutyCycle(void)
{
/* set period to eight clocks */
PWM8_WritePeriod(199);

/* set pulse width to generate a 33% duty cycle */
PWM8_WritePulseWidth(200);

/* ensure interrupt is disabled */
PWM8_DisableInt();

/* start the PWM8! */
PWM8_Start();
}

but how can I include :

#include  "PWM8_1.h"

always fail....

Should I include it manually ?

because I saw it's already there

• ###### 24. Re: Simple PWM

I got :

!ERROR {linker} file 'main.o': undefined symbol '_PWM8_WritePeriod'
!ERROR {linker} file 'main.o': undefined symbol '_PWM8_DisableInt'
!ERROR {linker} file 'main.o': undefined symbol '_PWM8_Start'
!ERROR {linker} file 'main.o': undefined symbol '_PWM8_WritePulseWidth'

• ###### 25. Re: Simple PWM

From the device editor screenshot (pwm3.jpg), it looks like the instance name of the PWM is PWM8_1. So, the APIs should be like this -

PWM8_1_WritePeriod, PWM8_1_DisableInt() and so on.

The APIs are always prefixed with its instance name.

• ###### 26. Re: Simple PWM

Correct me if I'm wrong ....

How can I fix stall servo motor ? Thanks

/* Divide by eight function */
void GenerateOneThirdDutyCycle(void)
{
/* set period to eight clocks */
PWM8_1_WritePeriod(199);

/* set pulse width to generate a 33% duty cycle */
PWM8_1_WritePulseWidth(200);

/* ensure interrupt is disabled */
PWM8_1_DisableInt();

/* start the PWM8! */
PWM8_1_Start();
}

/* Divide by eight function */
void GenerateHalfDutyCycle(void)
{
/* set period to eight clocks */
PWM8_1_WritePeriod(14);

/* set pulse width to generate a 33% duty cycle */
PWM8_1_WritePulseWidth(15);

/* ensure interrupt is disabled */
PWM8_1_DisableInt();

/* start the PWM8! */
PWM8_1_Start();
}

• ###### 27. Re: Simple PWM

How can I develope this spec?
Operating Speed (4.8V no load) : 0.131sec / 60 degrees

• ###### 28. Re: Simple PWM

Please correnct me if I'm wrong,

I wanna move 180 degree , just alike car window wiper
/* Divide by eight function */
void GenerateOneThirdDutyCycle(void)
{
/* set period to eight clocks */
PWM8_1_WritePeriod(100);

/* set pulse width to generate a 33% duty cycle */
PWM8_1_WritePulseWidth(101);

/* ensure interrupt is disabled */
PWM8_1_DisableInt();

/* start the PWM8! */
PWM8_1_Start();
}

/* Divide by eight function */
void GenerateHalfDutyCycle(void)
{
/* set period to eight clocks */
PWM8_1_WritePeriod(1299);

/* set pulse width to generate a 33% duty cycle */
PWM8_1_WritePulseWidth(1300);

/* ensure interrupt is disabled */
PWM8_1_DisableInt();

/* start the PWM8! */
PWM8_1_Start();
}

/* Divide by eight function */
void GenerateZeroCycle(void)
{
/* set period to eight clocks */
PWM8_1_WritePeriod(1);

/* set pulse width to generate a 33% duty cycle */
PWM8_1_WritePulseWidth(2);

/* ensure interrupt is disabled */
PWM8_1_DisableInt();

/* start the PWM8! */
PWM8_1_Start();
}

main()

{

GenerateOneThirdDutyCycle();
Delay10msTimes(40);
GenerateHalfDutyCycle();
Delay10msTimes(40);

}

• ###### 29. Re: Simple PWM

Hello Bianchi,

In order to obtain a 20 ms rate pulse, as mentioned by Dana - you can set use the VC1, VC2 and VC3 dividers. The divider properties should be -

VC1 = 6, VC2 = 4, VC3 source = VC2 and VC3 = 100 --> this will generate a 10 KHz (or 100 us) clock output from VC3 .

Setting your 8-bit PWMs input clock as VC3 and PWM Period as 199, gives a PWM period of 20 ms (= 100 uS * (199+1));

Now you have obtained the rate at which you want to send pulse to your servo. Next step would be to define the pulse width.

With a PWM compare type of  "Less than" and a Pulse width of 10 - you should be able to get a 1 ms pulsewidth every 20 ms (PWM8 output will be high when the counter value is less than 10 and low otherwise ==> 10/200 * 20ms ==> 1 ms).

Similarly with a pulsewidth of 20, you get a 2 ms high pulse every 20 ms (20/200 * 20 ms ==> 2 ms)

So you don't have to change the period of the PWM, just changing the pulsewidth to these values will help you send the respective pulses to your servo.

Again, this PWM has a step size of 0.1 ms - if you want a finer step size; You can use the below parameters -

VC1,  VC2 and VC3 source same, VC3 divider = 10 ==> VC3 clock period = 10 uS (your step size;

Use a PWM16 module and set the period as 2000 ( ==> 2000 * 0 uS = 20 ms)

Use pulsewidth as 200 for 2 ms and 100 for 1 ms; your step size is 0.01 ms - a finer step;

Hope this helps,

Regards,

MSUR

1 2 3 4