Simple PWM timer setup for LPC1343

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

Simple PWM timer setup for LPC1343

1,386 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Brinkand on Wed Feb 17 05:47:26 MST 2010
I wanted a simple piece of setup code for the Xpresso demo board to test PWM. Starting with the blinky project and cutting out the pieces I did not need, I arrived at the below code.

Most of You probably do this in minutes, but maybe I can help a single soul by posting my resulting code.

The timer is set up with PWM on MAT0 of TMR16B1 and a toggling output on MAT1. MR3 used to set cycle length.

I noted that the init_timer16() actually sets pull up on outputs:-)

  // PWM output: 16bit_timer1
  //init_timer16(1, 14399); // 14399 gives 5kHz with 72MHz clock.
  // Timer set up for 5KHz 20% DC PWM on MAT0 and
  // 2.5kHz toggling signal with phase offset at MAT1.
  // This is not for any specific application, just for testing.
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);  // Enable clock to timer 1
  //LPC_IOCON->PIO1_8           &= ~0x07;    /*  Timer1_16 I/O config  */
  //LPC_IOCON->PIO1_8           |= 0x01;        /* Timer1_16 CAP0    - set bit 1*/
  // default: 0xD0 = 0b11010000 (at boot) - after this: 0b11010001 - means MAT0 enabled + pull up + some undocumented bits.
  // Frankly there is no reason for pull-up on output.
  //LPC_IOCON->PIO1_9           &= ~0x07;                               // - remove bits 0,1 & 2
  LPC_IOCON->PIO1_9           &= ~0x1F;                               // - remove bits 0,1 & 2 and pull resistors
  LPC_IOCON->PIO1_9           |= 0x01;        /* Timer1_16 MAT0 */ //- set bit 1
  //LPC_IOCON->PIO1_10          &= ~0x07;
  LPC_IOCON->PIO1_10          &= ~0x1F;     // - remove bits 0,1 & 2 and pull resistors
  LPC_IOCON->PIO1_10          |= 0x02;        /* Timer1_16 MAT1 */

  /* Enable the TIMER1 Interrupt */
  // NVIC_EnableIRQ(TIMER_16_1_IRQn);  // No interrupt needed.

  LPC_TMR16B1->MCR = 0x0400;        /* Reset at MR3*/
  //LPC_TMR16B1->MCR |= 0x0400;        /* MR1 setup*/
  LPC_TMR16B1->MR0 = 11421;            /* Match register 0 - Turn on time: 2879 = 20% duty cycle */
  LPC_TMR16B1->MR1 = 7000;            /* Match register 1 - no PWM - toggle test */
  //LPC_TMR16B1->MR2 = 0;                /* Match register 2 - not used */
  LPC_TMR16B1->MR3 = 14399;          /* Match register 3 - cycle length */
  LPC_TMR16B1->EMR = 0xC2;            /* External Match register Settings for PWM channel have no effect.*/
  LPC_TMR16B1->PWMC = 0x01;             /* PWMC register -MAT0 is PWM. */
  // PIO1_9 = TC16B1_MAT0
  GPIOSetDir( 1, 9, 1 );  // Set as output
  // PIO1_10 = TC16B1_MAT1
  GPIOSetDir( 1, 10, 1 ); // set as output
  LPC_TMR16B1->TCR = 1;   // Enable timer 1
0 Kudos
11 Replies

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stefi on Thu Apr 10 03:45:12 MST 2014
I want to increase intensity gradually...please help me...please
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stefi on Wed Mar 19 22:58:27 MST 2014
how can i change intensity of led using pwm gradually?
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stefi on Mon Feb 03 03:11:15 MST 2014
how can i read simple pir sensor using lpc1343 in lpcxpresso? & how can i get the printed
readings of pir sensor in console window?
please help me ....
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stefi on Tue Jan 28 02:24:22 MST 2014
thanx
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by serge on Tue Jan 28 02:22:49 MST 2014
user manual 10375
chapter 15
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Jan 28 01:26:03 MST 2014

Quote: stefi
...make the above code simper?????



You're kidding. That sample[color=#f00] is[/color] simple.


Quote: stefi
In above code i didnt get that..what are thoes values...14399,14211 assigned to the match registers... and how it comes??



Could be useful to read timer description of UM.

14399 is obviously the cycle length in MR3: 72MHz / (14399 +1) = 5 kHz

11421 should be the switching point (80%)in MR0.

That's obviously a typo, this value should be 11520...
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stefi on Tue Jan 28 00:48:17 MST 2014
actually i m a student of b.e 4th year. And i m doing my project using this software. But i found this too much tough.Can u help me to make the above code simper?????

In above code i didnt get that..what are thoes values...14399,14211 assigned to the match registers... and how it comes??
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Jan 28 00:23:02 MST 2014

Quote: stefi
Please,help me in coding for changing the intensity of led with the help of pwm in lpc1343 in lpcxpresso...please...please



What's wrong with the PWM sample above ????????????  :quest:
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stefi on Tue Jan 28 00:18:53 MST 2014
Please,help me in coding for changing the intensity of led with the help of pwm in lpc1343 in lpcxpresso...please...please
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ub3r on Sat Oct 08 09:00:58 MST 2011
[SIZE=2]void setPWM(void)
{
// PWM output: 16bit_timer1
// init_timer16(1, 14399); // 14399 gives 5kHz with 72MHz clock.
// Timer set up for 5KHz 20% DC PWM on MAT0 and
// 2.5kHz toggling signal with phase offset at MAT1.
// This is not for any specific application, just for testing.

LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);  // Enable clock to timer 1
LPC_IOCON->PIO1_9           |= 0x01;        /* Timer1_16 MAT0 */ //- set bit 1

/* Enable the TIMER1 Interrupt */
// NVIC_EnableIRQ(TIMER_16_1_IRQn);  // No interrupt needed.

LPC_TMR16B1->MCR = 0x0400;       /* Reset at MR3*/
LPC_TMR16B1->MR0 = 11421;        /* Match register 0 - Turn on time: 2879 = 20% duty cycle */
LPC_TMR16B1->MR3 = 14399;        /* Match register 3 - cycle length */
LPC_TMR16B1->PWMC = 0x01;         /* PWMC register -MAT0 is PWM. */
GPIOSetDir( 1, 9, 1 );  // Set as output
LPC_TMR16B1->TCR = 1;   // Enable timer 1
}[/SIZE]
0 Kudos

936 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sakka.haythem.esim on Wed Aug 17 05:29:53 MST 2011
hello,
Could someone help to set PIO1_10 as Timer1_16 MAT0 and PIO2_3 as Timer1_16 MAT1?
Thinks!
Haythem
0 Kudos