Generate 2 PWM signal on LPC1114 using 1 timer 16 bit

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

Generate 2 PWM signal on LPC1114 using 1 timer 16 bit

1,018 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tungsys on Tue Jan 03 23:47:05 MST 2012
Hi,

Can I use one timer (CT16B1) to generate 2 PWM at the same time.
According to User Manual. Timer CT16B1_MAT[1:0] has two out put such as MAT0 and MAT1 and can be configured independently.
But after I try , only either MAT0 or MAT1 can out put at a time. Cannot generate 2 PWM at the same time.
Below is my code:

==================================================
volatile uint32_t period = 1000;  //48Khz PWM frequency


/* Main Program */

int main (void) {
   
    /* Initialize the PWM in timer16_1 enabling match1 output */
    init_timer16PWM(1, period, MATCH1, 0);
    //init_timer16PWM(1, period, MATCH0, 0);
    setMatch_timer16PWM (1, 1, period/8); // PIO 1.10
    //setMatch_timer16PWM (1, 0, period/4); // PIO 1.9
    enable_timer16(1);

}
=====================================================
0 Kudos
13 Replies

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed May 23 02:34:39 MST 2012
I don't know what your Timer1 init is doing, but you should ask your friend if EM1 bit in EMR of Timer1 is set :rolleyes:
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sophio on Wed May 23 02:18:42 MST 2012

Quote: Zero
Did you ask your new friend if timer clock is running (Peripheral View -> SYSCTL ->SYSAHBCLKCTRL ->CT32B1)?

No clock, no fun :)


Thank you for your patience to answer my questions~~~

My friend said that clock is running, but on the oscilloscope, only a low level straight line, no clock signal can be seen:eek:.
Corresponding to the timer32_1_MAT0 pin, the timer32_0_MAT0 pin has the clock signal on the oscilloscope.
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed May 23 01:25:36 MST 2012
Did you ask your new friend if timer clock is running (Peripheral View -> SYSCTL ->SYSAHBCLKCTRL ->CT32B1)?

No clock, no fun :)
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sophio on Wed May 23 01:11:32 MST 2012
+
Quote: Zero
Debugger is your friend



Aha, i am not familiar with this friend:p.
Thanks for your advise, the friend told me that the function of the pin has already changed to CTB1_MAT0, but the timer32_1 is not runing
void enable_timer32(uint8_t timer_num)
{
  if ( timer_num == 0 )
  {
    LPC_CT32B0->TCR = 1;
  }
  else
  {
    LPC_CT32B1->TCR = 1;
  }
  return;
}
The TCR of timer32_1 can not be set to 1.
This problem is not happened on timer32_0
Don't know why...:confused:
I also test another board, the same problem exits.
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue May 22 08:49:52 MST 2012

Quote: Zero
What's your debugger telling you about IOCON of this pins? Is it really switched to CT32Bx_MAT0 and is the timer running?



Debugger is your friend :)
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sophio on Tue May 22 08:47:04 MST 2012

Quote: Zero

But pins at LPC11U14 are named different :mad:



Of cource i have changed the pin:D and also the function.
...
        /* Setup the outputs */
        /* If match0 is enabled, set the output */
        if (match_enable & 0x01)
        {
              LPC_IOCON->TDO_PIO0_13  &= ~0x07;
              LPC_IOCON->TDO_PIO0_13  |= 0x03;        /* Timer1_32 MAT0 */
        }
...


From user manual if bit 2:0 is 0x03, the function should be CT32B1_MAT0.
And i test the CT32B1_MAT1 too (pin 0_14), it does not work either...
From the oscilloscope i can see nothing :eek:
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue May 22 08:28:18 MST 2012

Quote: sophio
Do you know why?



Yes, that's an U problem :p

This sample is working fine here with LPC1114 (PIO1_6 & PIO1_1). But pins at LPC11U14 are named different :mad:

Thanks God I've no LPC11U14 :) I would guess that's a pin function problem :rolleyes:
What's your debugger telling you about IOCON of this pins? Is it really switched to CT32Bx_MAT0 and is the timer running?
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sophio on Tue May 22 07:43:00 MST 2012

Quote: Zero
Of course you can

Your sample is nearly doing it :rolleyes:

Just init MATCH0 and MATCH1 and you can generate 2 PWM with same frequency.

/* Main Program */

volatile uint32_t period = 1000; //48Khz PWM frequency

int main (void)
{
/* Initialize the PWM in timer16_1 enabling match1 output */
 init_timer16PWM(1, period, [COLOR=Red]MATCH0| MATCH1[/COLOR] , 0);
 setMatch_timer16PWM (1, 1, period/8); // PIO 1.10
 setMatch_timer16PWM (1, 0, period/4); // PIO 1.9
 enable_timer16(1);
}



Really smart! It works well, thanks! But i have another question.

I am working with LPC11U14, and i want to use timer32_0 to generate PWM1 and timer32_1 to generate PWM2. For timer32_0, it works fine and i can see the pulse width changes in the oscilloscope, however for timer32_1 it does not work. Do you know why?

The main code is really simple
int main(void) {

    uint8_t b2=1;
    /* Initialize the PWM in timer32_0 enabling match0 output
     * pin0_18 for led 2*/
    init_timer32PWM(0, period, MATCH0);
    setMatch_timer32PWM (0, 0, period*b2/16);
    enable_timer32(0);

    uint8_t b1=8;
    /* Initialize the PWM in timer32_1 enabling match0 output
     * pin0_13 for led 1*/
    init_timer32PWM(1, period, MATCH0);
    setMatch_timer32PWM (1, 0, period*b1/16);
    enable_timer32(1);
    return 0;
}
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed Jan 04 03:10:30 MST 2012

Quote: tungsys
Can these two PWM run with difference frequency ?



No :mad:

One PWM = one frequency.

 LPC_TMR16B1->MR3   = timer16_1_period;
is setting your period = restarting timer 1. So different PWM outputs can be used to change pulse width at same frequency.
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tungsys on Wed Jan 04 02:51:07 MST 2012
Hi Zero,

Thank you.:):):)
Just one more question. Can these two PWM run with difference frequency ?

Regards,
Tung
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed Jan 04 01:53:20 MST 2012

Quote: tungsys
Can I use one timer (CT16B1) to generate 2 PWM at the same time.



Of course you can

Your sample is nearly doing it :rolleyes:

Just init MATCH0 and MATCH1 and you can generate 2 PWM with same frequency.

/* Main Program */

volatile uint32_t period = 1000; //48Khz PWM frequency

int main (void)
{
/* Initialize the PWM in timer16_1 enabling match1 output */
 init_timer16PWM(1, period, [COLOR=Red]MATCH0| MATCH1[/COLOR] , 0);
 setMatch_timer16PWM (1, 1, period/8); // PIO 1.10
 setMatch_timer16PWM (1, 0, period/4); // PIO 1.9
 enable_timer16(1);
}
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tungsys on Wed Jan 04 00:13:55 MST 2012
Hi Rob,

I just want to know weather it can be done or not ?
I will take a look into the driver fucntion (timer16.c)

Thanks
0 Kudos

738 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Tue Jan 03 23:57:41 MST 2012
There is a nice saying:[INDENT]If you don't know where you are going, you might end up somewhere else
[/INDENT]:eek:

This means that if you do not know what init_timer16PWM() and setMatch_timer16PWM() are doing, something else may happen than expected :confused:

So go ahead and look at what both functions do, single step through the code and check after each step if the registers of the timer are still what you expect them to be.
And then, if you still want us to look at this. Provide the content of all relevant functions - without this we have no idea what your program is doing.

Regards,[INDENT]Rob
[/INDENT]
0 Kudos