lpcware

Problem using PWM on LandTiger LPC1768 for Servo control

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by hrzrfn on Wed Jun 12 07:59:15 MST 2013
I'm currently working on LandTiger LPC1768 board to generate PWM for servo control. I've managed to verify that the PWM is working by channeling the PWM to LED (I'm getting LED brightness changed) & I had also verified that the PWM is working by using an oscilloscope

But i've realized that from the osciloscope that my PWM period and pulse width are not 20ms and 1-2ms respectively. I've tried to change my coding but hadn't got much. In fact, the PWM i've got are much more worst (much more like a noise when viewed on oscilloscope) ..

I'm suspecting I'm wronged on the clock setting. I'm using standard CMSIS library for my code, so I'm guessing that the PCLK for PWM should be 72MHz..am I correct about this? it seems that I'm not getting 50Hz PWM with my setting. The servo doesnt move at all

in the system_LPC17xx.c
#define CLOCK_SETUP           1
#define SCS_Val               0x00000020
#define CLKSRCSEL_Val         0x00000001
#define PLL0_SETUP            1
#define PLL0CFG_Val           0x0002003f
#define PLL1_SETUP            1
#define PLL1CFG_Val           0x00000023
#define CCLKCFG_Val           0x00000007
#define USBCLKCFG_Val         0x00000000
#define PCLKSEL0_Val          0x00000000
#define PCLKSEL1_Val          0x00000000
#define PCONP_Val             0x042887DE
#define CLKOUTCFG_Val         0x00000000


and for my PWM test code
#include "LPC17xx.h"
#include "debug_frmwrk.h"

int c_entry(void)
{
 
 
  LPC_SC->PCONP |= 1 << 6; // enable power for PWM1
  LPC_SC->PCLKSEL0 |= 1 << 12; //PCLK = CCLK = 72MHz (?)
 
  LPC_PWM1->TCR = 0x00;
  LPC_PWM1->CTCR = 0x00;
  LPC_PWM1->MCR = 0x00;
  LPC_PWM1->CCR = 0x00;
  LPC_PWM1->PCR = 0x00;
  LPC_PWM1->LER = 0x00;
  LPC_PWM1->PR = 1 - 1;
 
  LPC_PINCON->PINSEL4 = (LPC_PINCON->PINSEL4 & ~(0x3 << 0)) | (0x1 << 0); //output to pin 2.0
  LPC_PINCON->PINMODE4 = (LPC_PINCON->PINMODE4 & ~0x3) | 0x2;
 
  LPC_PWM1->MR0 = 1440000;  /*set clock cycles for PWM. PWM freq = PCLK/MR0 (?)*/
  LPC_PWM1->MR1 = 108000; // pulse width 1.5ms
  LPC_PWM1->LER = (1 << 0) | (1 << 1); //Apply changes

  LPC_PWM1->MCR |= 1 << 1; // interrupt on match0.Reset timer on Match0;
  LPC_PWM1->PCR = (1<<9); //--- PWM1 output
  LPC_PWM1->TCR = (1<<0)|(1<<3);
       
       
        while(1)
        {
          LPC_PWM1->MR1 = 1500; // pulse width 1.5ms
          LPC_PWM1->LER |=  (1 << 1); //Apply changes
        }
        return (0);
}


int main (void)
{
  return c_entry();
}

#ifdef  DEBUG
/*******************************************************************************
* @brief        Reports the name of the source file and the source line number
*               where the CHECK_PARAM error has occurred.
* @param[in]    file Pointer to the source file name
* @param[in]    line assert_param error line source number
* @return       None
*******************************************************************************/
void check_failed(uint8_t *file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while(1);
}
#endif

/*
* @}
*/


can someone help me on this? really appreciate ur help thx!

Outcomes