<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>LPC1768 - PWM not working</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/39926/lpc1768---pwm-not-working</link><description> 
Hello All, 

 
I&amp;#39;ve developed customized LPC1768 board &amp;amp; want PWM 4 &amp;amp; 6
in Double Edge mode. However it is not working. Can you please guide
after looking into my code as below. O/p pins of PWM3,4,5 &amp;amp; 6 all
shows always HIGH. 
One more observation is</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: LPC1768 - PWM not working</title><link>https://community.arm.com/thread/131123?ContentTypeID=1</link><pubDate>Thu, 11 Oct 2012 01:26:51 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:594ba350-93f6-451b-9d25-d71afed0c3bb</guid><dc:creator>raj d</dc:creator><description>&lt;p&gt;&lt;pre&gt;
/******************************************************************************
**                            Start Of main.c File
******************************************************************************/
#include &amp;quot;lpc17xx.h&amp;quot;
#include &amp;quot;datatype.h&amp;quot;
#include &amp;quot;pwm.h&amp;quot;

extern volatile uint32_t Pwm_Match0_counter;


int main (void)
{
  /* SystemClockUpdate() updates the SystemFrequency variable */
  SystemClockUpdate();

  PWM_Init();
  PWM_Set(PWM4, 3000, 9000);
  PWM_Set(PWM6, 2000, 8000);
  PWM_Start(PWM4);
  PWM_Start(PWM6);

  while(1)
  {
        if(Pwm_Match0_counter != 0)
        {
                Pwm_Match0_counter = 0;
                PWM_Set(PWM4, 3000, 7000);
                PWM_Set(PWM6, 5000, 8000);
        }
  }
}
/******************************************************************************
**                            End Of main.c File
******************************************************************************/


/******************************************************************************
**                            Start Of pwm.c File
******************************************************************************/

#include &amp;quot;lpc17xx.h&amp;quot;
#include &amp;quot;type.h&amp;quot;
#include &amp;quot;datatype.h&amp;quot;
#include &amp;quot;pwm.h&amp;quot;

#define PWM_RATE        10000

volatile uint32_t Pwm_Match0_counter;

/******************************************************************************
** Function name:               PWM1_IRQHandler
**
** Descriptions:                PWM1 interrupt handler
**                                              For now, it only deals with PWM1 match 0
**
** parameters:                  None
** Returned value:      None
**
******************************************************************************/
void PWM1_IRQHandler (void)
{
  uint8 regVal;

  regVal = LPC_PWM1-&amp;gt;IR;
  if ( regVal &amp;amp; MR0_INT )
  {
                Pwm_Match0_counter++;
  }
  LPC_PWM1-&amp;gt;IR |= regVal;            /* clear interrupt flag on match 0 */
  return;
}

/******************************************************************************
** Function name:               PWM_Init
**
** Descriptions:                PWM initialization, setup all GPIOs to PWM0~6,
**                                              reset counter, all latches are enabled, interrupt
**                                              on PWMMR0, install PWM interrupt to the VIC table.
**
** parameters:                  None
** Returned value:      true or false, if VIC table is full, return false
**
******************************************************************************/
void PWM_Init(void)
{
        Pwm_Match0_counter = 0;
        LPC_PINCON-&amp;gt;PINSEL4 = 0x05500550;    /* select PWM3,4,5 &amp;amp; 6 func and P2.10 to 13 as interrupt */

        LPC_PINCON-&amp;gt;PINMODE4 = 0x00000AA0;           /* No Pull-up/down for PWM3,4,5 &amp;amp; 6*/
        LPC_PWM1-&amp;gt;TCR = TCR_RESET;   /* Counter Reset */
        LPC_PWM1-&amp;gt;PR = 0x00;                         /* count frequency:Fpclk */
        LPC_PWM1-&amp;gt;MCR = PWMMR0I | PWMMR0R;           /* interrupt on PWMMR0, reset on PWMMR0, reset TC if PWM matches */

        LPC_PWM1-&amp;gt;PCR = PWMSEL3 | PWMSEL4 | PWMSEL5 | PWMSEL6;       /* Double Edge selection */
        LPC_PWM1-&amp;gt;MR0 = PWM_RATE;                    /* set PWM cycle */

        /* all PWM latch enabled */
        LPC_PWM1-&amp;gt;LER = LER0_EN;

  NVIC_EnableIRQ(PWM1_IRQn);
  return;
}

/******************************************************************************
** Function name:               PWM_Set
**
** Descriptions:                PWM cycle setup
**
** parameters:                  Channel number, PWM cycle, and offset
** Returned value:      None
**
******************************************************************************/
uint8 PWM_Set(uint8 channelnum, uint32 on_period, uint32 off_period)
{
    switch(channelnum)
    {
        case PWM4:  LPC_PWM1-&amp;gt;MR3 = on_period;               /* ON time */
                LPC_PWM1-&amp;gt;MR4 = off_period;          /* OFF time */
                LPC_PWM1-&amp;gt;LER = LER3_EN | LER4_EN;   /* latch new ON/OFF period */
                break;
        case PWM6:  LPC_PWM1-&amp;gt;MR5 = on_period;               /* ON time */
                LPC_PWM1-&amp;gt;MR6 = off_period;          /* OFF time */
                LPC_PWM1-&amp;gt;LER = LER5_EN | LER6_EN;   /* latch new ON/OFF period */
                break;
        default:  channelnum = ERROR;   /* return ERROR incase wrong channalnum */
                break;
    }
  return(channelnum);
}

/******************************************************************************
** Function name:               PWM_Start
**
** Descriptions:                Enable PWM by setting the PCR, PTCR registers
**
** parameters:                  channel number
** Returned value:      None
**
******************************************************************************/
uint8 PWM_Start(uint8 channelnum)
{
    switch(channelnum)
    {
        case PWM4:  LPC_PWM1-&amp;gt;PCR |= PWMENA4;        /* Start PWM4 */
                break;
        case PWM6:  LPC_PWM1-&amp;gt;PCR |= PWMENA6;        /* Start PWM6 */
                break;
        default:  channelnum = ERROR;          /* return ERROR incase wrong channalnum */
                break;
    }
    LPC_PWM1-&amp;gt;TCR = TCR_CNT_EN | TCR_PWM_EN; /* counter enable, PWM enable */ //????
  return(channelnum);
}


/******************************************************************************
** Function name:               PWM_Stop
**
** Descriptions:                Stop PWM channel
**
** parameters:                  channel number
** Returned value:      None
**
******************************************************************************/
uint8 PWM_Stop(uint8 channelnum)
{
    switch(channelnum)
    {
        case PWM4:  LPC_PWM1-&amp;gt;PCR = PWM_DIS4;                /* Stop PWM4 */
                break;
        case PWM6:  LPC_PWM1-&amp;gt;PCR = PWM_DIS6;                /* Stop PWM6 */
                break;
        default:  channelnum = ERROR;           /* return ERROR incase wrong channalnum */
                break;
    }
  return(channelnum);
}

/******************************************************************************
**                            End Of pwm.c File
******************************************************************************/
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 - PWM not working</title><link>https://community.arm.com/thread/127799?ContentTypeID=1</link><pubDate>Thu, 11 Oct 2012 00:15:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:34ced8ac-87ac-4c4f-8c16-8bde556160e3</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
So read the text information you have available directly above the
message input box. Don&amp;#39;t you notice teh &amp;quot;Place source code ...&amp;quot;
part?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 - PWM not working</title><link>https://community.arm.com/thread/117031?ContentTypeID=1</link><pubDate>Wed, 10 Oct 2012 22:22:35 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3783dcf7-deec-4736-9cd7-aff9459b180f</guid><dc:creator>raj d</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hello Eric, Kindly let me know if there is facility to attach
files, i&amp;#39;ll upload my files. I&amp;#39;m new user &amp;amp; don&amp;#39;t much about this
GUI.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 - PWM not working</title><link>https://community.arm.com/thread/106478?ContentTypeID=1</link><pubDate>Wed, 10 Oct 2012 22:18:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:202e8e7b-c41e-45c9-be20-189e02bf3947</guid><dc:creator>raj d</dc:creator><description>&lt;p&gt;&lt;p&gt;
I&amp;#39;m sorry, in the preview it shows all the indent&amp;amp; code
clearly. I don&amp;#39;t why it is happening like this.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 - PWM not working</title><link>https://community.arm.com/thread/80626?ContentTypeID=1</link><pubDate>Wed, 10 Oct 2012 22:16:11 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0b291496-76b7-4e77-9656-fcf589f87f63</guid><dc:creator>raj d</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hello Eric,&lt;br /&gt;
Thanks for your reply &amp;amp; guidance.&lt;br /&gt;
I&amp;#39;ve formatted the code &amp;amp; previewed. Here there are 2 files
main.c- has logic to call PWM init, start etc and pwm.c - this has
all the pwm functionality code like pwm_init, pwm_stop etc.&lt;br /&gt;
Kindly let me know your feedback about the code fault.&lt;/p&gt;

&lt;p&gt;
Thanks &amp;amp; Regards&lt;br /&gt;
Raj&lt;/p&gt;

&lt;p&gt;
/*********************** Start of pwm.c File
********************************/&lt;br /&gt;
#include &amp;quot;lpc17xx.h&amp;quot;&lt;br /&gt;
#include &amp;quot;pwm.h&amp;quot;&lt;/p&gt;

&lt;p&gt;
extern volatile uint32_t Pwm_Match0_counter;&lt;/p&gt;

&lt;p&gt;
int main (void)&lt;br /&gt;
{ /* SystemClockUpdate() updates the SystemFrequency variable */
SystemClockUpdate();&lt;/p&gt;

&lt;p&gt;
PWM_Init(); PWM_Set(PWM4, 3000, 9000); PWM_Set(PWM6, 2000, 8000);
PWM_Start(PWM4); PWM_Start(PWM6);&lt;/p&gt;

&lt;p&gt;
while(1) { if(Pwm_Match0_counter != 0) { Pwm_Match0_counter = 0;
PWM_Set(PWM4, 3000, 7000); PWM_Set(PWM6, 5000, 8000); } }&lt;br /&gt;
}
/******************************************************************************&lt;br /&gt;

** End Of main.c File&lt;br /&gt;
******************************************************************************/&lt;/p&gt;

&lt;p&gt;
/*********************** Start of pwm.c File
********************************/&lt;br /&gt;
#include &amp;quot;lpc17xx.h&amp;quot;&lt;br /&gt;
#include &amp;quot;type.h&amp;quot;&lt;br /&gt;
#include &amp;quot;pwm.h&amp;quot;&lt;/p&gt;

&lt;p&gt;
#define PWM_RATE 10000&lt;/p&gt;

&lt;p&gt;
volatile uint32_t Pwm_Match0_counter;&lt;/p&gt;

&lt;p&gt;

/******************************************************************************&lt;br /&gt;

** Function name: PWM1_IRQHandler&lt;br /&gt;
**&lt;br /&gt;
** Descriptions: PWM1 interrupt handler&lt;br /&gt;
** For now, it only deals with PWM1 match 0&lt;br /&gt;
**&lt;br /&gt;
** parameters: None&lt;br /&gt;
** Returned value: None&lt;br /&gt;
**&lt;br /&gt;
******************************************************************************/&lt;br /&gt;

void PWM1_IRQHandler (void)&lt;br /&gt;
{ uint8 regVal;&lt;/p&gt;

&lt;p&gt;
regVal = LPC_PWM1-&amp;gt;IR; if ( regVal &amp;amp; MR0_INT ) {
Pwm_Match0_counter++; } LPC_PWM1-&amp;gt;IR |= regVal; /* clear interrupt
flag on match 0 */ return;&lt;br /&gt;
}&lt;/p&gt;

&lt;p&gt;

/******************************************************************************&lt;br /&gt;

** Function name: PWM_Init **&lt;br /&gt;
******************************************************************************/&lt;br /&gt;

void PWM_Init(void)&lt;br /&gt;
{ Pwm_Match0_counter = 0; LPC_PINCON-&amp;gt;PINSEL4 = 0x05500550; /*
select PWM3,4,5 &amp;amp; 6 func and P2.10 to 13 as interrupt */&lt;/p&gt;

&lt;p&gt;
LPC_PINCON-&amp;gt;PINMODE4 = 0x00000AA0; /* No Pull-up/down for
PWM3,4,5 &amp;amp; 6*/ LPC_PWM1-&amp;gt;TCR = TCR_RESET; /* Counter Reset */
LPC_PWM1-&amp;gt;PR = 0x00; /* count frequency:Fpclk */ LPC_PWM1-&amp;gt;MCR
= PWMMR0I | PWMMR0R; /* interrupt on PWMMR0, reset on PWMMR0, reset
TC if PWM matches */&lt;/p&gt;

&lt;p&gt;
LPC_PWM1-&amp;gt;PCR = PWMSEL3 | PWMSEL4 | PWMSEL5 | PWMSEL6; /*
Double Edge selection */ LPC_PWM1-&amp;gt;MR0 = PWM_RATE; /* set PWM
cycle */&lt;/p&gt;

&lt;p&gt;
/* all PWM latch enabled */ LPC_PWM1-&amp;gt;LER = LER0_EN;&lt;/p&gt;

&lt;p&gt;
NVIC_EnableIRQ(PWM1_IRQn); return;&lt;br /&gt;
}&lt;/p&gt;

&lt;p&gt;

/******************************************************************************&lt;br /&gt;

** Function name: PWM_Set **&lt;br /&gt;
** Descriptions: PWM cycle setup **&lt;br /&gt;
** parameters: Channel number, PWM cycle, and offset **&lt;br /&gt;
** Returned value: None **&lt;br /&gt;
******************************************************************************/&lt;br /&gt;

uint8_t PWM_Set(uint8_t channelnum, uint32_t on_period, uint32_t
off_period)&lt;br /&gt;
{ switch(channelnum) { case PWM4: LPC_PWM1-&amp;gt;MR3 = on_period; /* ON
time */ LPC_PWM1-&amp;gt;MR4 = off_period; /* OFF time */
LPC_PWM1-&amp;gt;LER = LER3_EN | LER4_EN; /* latch new ON/OFF period */
break; case PWM6: LPC_PWM1-&amp;gt;MR5 = on_period; /* ON time */
LPC_PWM1-&amp;gt;MR6 = off_period; /* OFF time */ LPC_PWM1-&amp;gt;LER =
LER5_EN | LER6_EN; /* latch new ON/OFF period */ break; default:
channelnum = ERROR; /* return ERROR incase wrong channalnum */ break;
} return(channelnum);&lt;br /&gt;
}&lt;/p&gt;

&lt;p&gt;

/******************************************************************************&lt;br /&gt;

** Function name: PWM_Start **&lt;br /&gt;
** Descriptions: Enable PWM by setting the PCR, PTCR registers**&lt;br /&gt;
** parameters: channel number **&lt;br /&gt;
** Returned value: None **
******************************************************************************/&lt;br /&gt;

uint8_t PWM_Start(uint8_t channelnum)&lt;br /&gt;
{ switch(channelnum) { case PWM4: LPC_PWM1-&amp;gt;PCR |= PWMENA4; /*
Start PWM4 */ break; case PWM6: LPC_PWM1-&amp;gt;PCR |= PWMENA6; /* Start
PWM6 */ break; default: channelnum = ERROR; /* return ERROR incase
wrong channalnum */ break; } LPC_PWM1-&amp;gt;TCR = TCR_CNT_EN |
TCR_PWM_EN; /* counter enable, PWM enable */ //????
return(channelnum);&lt;br /&gt;
}&lt;/p&gt;

&lt;p&gt;

/******************************************************************************&lt;br /&gt;

** Function name: PWM_Stop **&lt;br /&gt;
** Descriptions: Stop PWM channel **&lt;br /&gt;
** parameters: channel number **&lt;br /&gt;
** Returned value: None **&lt;br /&gt;
******************************************************************************/&lt;br /&gt;

uint8_t PWM_Stop(uint8_t channelnum)&lt;br /&gt;
{ switch(channelnum) { case PWM4: LPC_PWM1-&amp;gt;PCR = PWM_DIS4; /*
Stop PWM4 */ break; case PWM6: LPC_PWM1-&amp;gt;PCR = PWM_DIS6; /* Stop
PWM6 */ break; default: channelnum = ERROR; /* return ERROR incase
wrong channalnum */ break; } return(channelnum);&lt;br /&gt;
}&lt;/p&gt;

&lt;p&gt;

/******************************************************************************&lt;br /&gt;

** End Of pwm.c File&lt;br /&gt;
******************************************************************************/&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 - PWM not working</title><link>https://community.arm.com/thread/61235?ContentTypeID=1</link><pubDate>Wed, 10 Oct 2012 13:34:49 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:70b6a9a3-c37e-44f8-8636-f7bd37cc5cad</guid><dc:creator>&amp;#178;erik malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
... is to learn to follow directions.&lt;/p&gt;

&lt;p&gt;
such as how to post code. Did you really believe that anybody
could make head or tail from what you saw in preview?&lt;/p&gt;

&lt;p&gt;
thus, if you followed the directions in the litterature concerning
your part, you just might have a working chip.&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>