PWM help

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

PWM help

3,005 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fplesinger on Thu Sep 23 06:33:04 MST 2010
Hi,
I would appreciate any links for PWM demos for use with LPC 1343. The one in default examples is not enough (for me. I jumped from Arduino and the change is ... quite difficult).

I managed how to control one PWM channel with 16bit timer. My project will need four PWM channels with 16bit timers and I am not able to switch them on simultaneously.

If anyone knows how to do this, please let me know.

Thanks!
Filip
0 Kudos
Reply
18 Replies

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cesimkaol on Mon Jun 20 07:42:17 MST 2011
Thank you zero ....problem solved.I will be here again with problems :)
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Fri Jun 17 18:32:15 MST 2011

Quote: cesimkaol
...but I dont have an idea or knowledge about that %50 duty cycle...how do we calculate it or in which formula calculate it...for example if I want to have %10 duty - cycle then what should I do?



What's not clear there?


Quote: Zero
Are you kidding me?

Just add a prescaler:
 LPC_TMR16B0->PR  = 71;     //timer base: 72MHz / (Prescaler+1) = 1MHz

and select match register:
LPC_TMR16B0->MR0 = 2000;                // A duty cycle - TMR16B0 Match reg0 2000*1µs = 2ms
LPC_TMR16B0->MR1 = 2000;                // C duty cycle - TMR16B0 Match reg1
LPC_TMR16B0->MR3 = 4000;                // Cycle length 4000*1µs = 4ms

0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cesimkaol on Fri Jun 17 17:47:54 MST 2011
Same as me,I set my PR register to 71 and set MR0 to 500000 for 0.5ms  period and  %50 duty cycle.However I dont know how you set that duty cycle?

its clear that give me an information about how to calculate period but I dont have an idea or knowledge about that %50 duty cycle...how do we calculate it or in which formula calculate it...for example if I want to have %10 duty - cycle then what should I do?
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bobi-one on Sun Apr 10 02:25:53 MST 2011

Quote: Zero
Yes, it's different. It doesn't run at all  :)

PIO1_0 is capture input (CT32B1_CAP0).

If you want to use PWM you need Match output (of 16bit or 32bit timer).

This sample uses 16 bit timer0/1 to generate PWM at
[INDENT]PIO0_8 = CT16B0_MAT0
PIO0_9 = CT16B0_MAT1
PIO1_9 = CT16B1_MAT0
PIO1_10= CT16B1_MAT1
[/INDENT]


Оh Oh, this means I have to redesign my board( x-acto + kynar wire :)).
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Apr 09 16:03:43 MST 2011

Quote: bobi-one
How about GPIO 1.0 on the lpc1343? Is pwm on it run by different  way?



Yes, it's different. It doesn't run at all  :)

PIO1_0 is capture input (CT32B1_CAP0).

If you want to use PWM you need Match output (of 16bit or 32bit timer).

This sample uses 16 bit timer0/1 to generate PWM at
[INDENT]PIO0_8 = CT16B0_MAT0
PIO0_9 = CT16B0_MAT1
PIO1_9 = CT16B1_MAT0
PIO1_10= CT16B1_MAT1
[/INDENT]
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bobi-one on Sat Apr 09 15:46:55 MST 2011
How about GPIO 1.0 on the lpc1343? Is pwm on it run by different  way?
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Fri Apr 08 01:29:16 MST 2011
Cool that works perfectly :)
The prescaler did the job.

Thank you :)
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Apr 07 14:29:41 MST 2011

Quote:

I need to have a pwm signal with 2ms high at 50% duty cycle.


Are you kidding me?

Just add a prescaler:
 LPC_TMR16B0->PR  = 71;     //timer base: 72MHz / (Prescaler+1) = 1MHz

and select match register:
LPC_TMR16B0->MR0 = 2000;                // A duty cycle - TMR16B0 Match reg0 2000*1µs = 2ms
LPC_TMR16B0->MR1 = 2000;                // C duty cycle - TMR16B0 Match reg1
LPC_TMR16B0->MR3 = 4000;                // Cycle length 4000*1µs = 4ms
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Thu Apr 07 13:19:29 MST 2011
I need to have a pwm signal with 2ms high at 50% duty cycle.
The program given above has a frequency of 17.8khz. That is way to fast.
Most drivers can only read signals about 2ms.

Yes indeed, the ESC drives the motor, got 4 of them.
I did not tried to make a signal about 2ms, it is difficult for me to use interrupt etc.
I was looking for an working program and found this.

I was thinking how to make my own pwm output. Hope you could help me with it.
Make a timer until the 2ms max for example.
Use 4 var's where i can set the a number.
If the counter is greater then that number, put it high. Is it lower then put it low.
That could give me a pwm ouput.
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Apr 07 13:15:13 MST 2011

Quote: Lithium
I measured the pwm out from the receiver. The signal is 2ms high and 2ms low.



And what's happening then :confused::confused:

So you are trying to use an LPC1343 to generate a signal for an ATMega8L, which drives your motor?

Anyway, did you try a standard signal (50Hz, 1-2ms pulse)?
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Thu Apr 07 12:35:36 MST 2011
I am using this driver: http://www.hobbyking.com/hobbyking/store/uh_viewItem.asp?idProduct=6458

I need a signal at 400hz for example.
I measured the pwm out from the receiver. The signal is 2ms high and 2ms low.
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Apr 07 11:53:24 MST 2011
Which Power Driver did you use?
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Thu Apr 07 11:24:43 MST 2011
I was also looking for 4 pwm out on the lpc1343.
It works, i got a nice signal on my scope.

I wanted to connect the output to an servo motor or brushless motor.
The motor will not turn on any value at all, could it be the frequency of the signal?
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fplesinger on Wed Sep 29 06:53:28 MST 2010
Hi mhjerde,
many thanks for the example! It is very helpfull. Now I can see how it works..no more alchemy!:) ... So thanks!

Filip
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mhjerde on Wed Sep 29 04:26:43 MST 2010
Hi
Here is an example of running 4 16-bit PWM outputs. Using the 32 bit counters works exactly the same... you would use LPC_TMR32B0 and LPC_TMR32B1, and connect to their respective pins.

/*************************************************************************//**
  Set up 16 bit counters CT16B0 and CT16B1 to run as PWM with a cycle
  length of 4096 (17.8kHz)

*****************************************************************************/
void PWM16_init(){


    LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 7);    // Enable clock to timer 0, CT16B0
    LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8);    // Enable clock to timer 1, CT16B1

    // 16 bit timer 0 - CT16B0
    // PIO0_8 IS A
    LPC_IOCON->PIO0_8        |= 0x02;        // - set bit 1 to connect pin to CT16B0_MAT0
    LPC_IOCON->PIO0_8        &= ~(1 << 3);    // - clear bit 3 (and set 4: pull-up resistor)
    LPC_IOCON->PIO0_8        |= (1 << 4);    // - set bit 4

    // PIO0_9 IS C
    LPC_IOCON->PIO0_9        |= 0x02;        // - set bit 1 to connect pin to CT16B0_MAT1
    LPC_IOCON->PIO0_9        &= ~(1 << 3);    // - clear bit 3 (and set 4: pull-up resistor)
    LPC_IOCON->PIO0_9        |= (1 << 4);    // - set bit 4

    // 16 bit timer 1 - CT16B1
    // PIO1_9 IS B
    LPC_IOCON->PIO1_9        |= 0x01;        // - set bit 0 to connect pin to CT16B1_MAT0
    LPC_IOCON->PIO1_9        &= ~(1 << 3);    // - clear bit 3 (and set 4: pull-up resistor)
    LPC_IOCON->PIO1_9        |= (1 << 4);    // - set bit 4

    // PIO1_10 IS D
    LPC_IOCON->PIO1_10        |= 0x02;        // - set bit 1 to connect pin to CT16B1_MAT1
    LPC_IOCON->PIO1_10        &= ~(1 << 3);    // - clear bit 3 (and set 4: pull-up resistor)
    LPC_IOCON->PIO1_10        |= (1 << 4);    // - set bit 4

    LPC_TMR16B0->MCR     |= (1 << 10);        // Reset TC counter when it reaches Match Register 3 value
    LPC_TMR16B1->MCR     |= (1 << 10);        // Reset TC counter when it reaches Match Register 3 value

    LPC_TMR16B0->MR0 = 0;                // A duty cycle - TMR16B0 Match register 0
    LPC_TMR16B0->MR1 = 0;                // C duty cycle - TMR16B0 Match register 1
    LPC_TMR16B0->MR3 = 4096;            // Cycle length 4096 (= 17.8kHz) - Match register 3

    LPC_TMR16B1->MR0 = 0;                // B duty cycle - TMR16B1 Match register 0
    LPC_TMR16B1->MR1 = 0;                // D duty cycle - TMR16B1 Match register 1
    LPC_TMR16B1->MR3 = 4096;            // Cycle length 4096 (= 17.8kHz) - Match register 3

    LPC_TMR16B0->PWMC |= (1 << 0);        // A    Timer 0 MAT0 is PWM
    LPC_TMR16B0->PWMC |= (1 << 1);        // C    Timer 0 MAT1 is PWM
    LPC_TMR16B1->PWMC |= (1 << 0);        // B    Timer 1 MAT0 is PWM
    LPC_TMR16B1->PWMC |= (1 << 1);        // D    Timer 1 MAT1 is PWM

    LPC_GPIO0->DIR     |= (1 << 8);        // - select OUTPUT
    LPC_GPIO0->DIR     |= (1 << 9);        // - select OUTPUT
    LPC_GPIO1->DIR     |= (1 << 9);        // - select OUTPUT
    LPC_GPIO1->DIR     |= (1 << 10);        // - select OUTPUT
    
    LPC_TMR16B0->TCR = 1;    //enable timer 0
    LPC_TMR16B1->TCR = 1;    //enable timer 1

    return;
}



For the above example, you change the duty cycle by setting the four match registers MR0 add MR1 on TMR16B0 and TMR16B1 to any value between 0 and 4096.

Hope this helps.

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Jesse.Rosenberg on Fri Sep 24 15:46:56 MST 2010
Flip,

Glad to hear it. I realize I only referred to 16 bit timers, but the 32bit timers would work equally well.
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fplesinger on Fri Sep 24 01:46:29 MST 2010
Jesse, thank you for Your reply, it was helpfull.

Two PWM on one timer are now runnig, so ...  it is on the good way :).

Thanks!
Filip
0 Kudos
Reply

2,931 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Jesse.Rosenberg on Thu Sep 23 15:01:31 MST 2010
Each of the timers in the LPC11xx/LPC13xx has an option to drive up to 3 Match/PWM outputs. If you need a total of 4 PWM outputs you will have to use both of the 16 bit timers.
0 Kudos
Reply