11 years, 6 months ago.

Problem using PWM on LandTiger LPC1768 for Servo

Hi

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 /media/uploads/hrzrfn/photo.jpg (5v/div at y-axis and 1ms/div with 10x magnification at x-axis)

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

here's my test code.

Landtiger LPC1768 PWM

#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? Its my first time working with this board & PWM so I appreciate any of your help thanks!

side note: I'm using IAR embedded workbench

Question: Why not just use the mbed PWM libraries?

posted by Erik - 11 Jun 2013

I've tried to use the mbed libraries,directly loading the libraries using IAR toolchain. but there seems to be problem when I've tried to compile the program..there are errors.. something like c/c++ compatibility,.. but I've just come across d handbook on exporting to iar toolchain so maybe I'll try with that later

posted by hrz rfn 11 Jun 2013

2 Answers

hrz rfn
poster
11 years, 6 months ago.

I've tried to use the mbed libraries,directly loading the libraries using IAR toolchain. but there seems to be problem when I've tried to compile the program..there are errors.. something like c/c++ compatibility,.. but I've just come across d handbook on exporting to iar toolchain so maybe I'll try with that later

Easiest should be just grabbing helloworld program, exporting that to IAR, import it in IAR, and make your project in there.

posted by Erik - 11 Jun 2013
11 years, 6 months ago.

The easiest way to get results is to use the mbed libs and compiler. The mbed supports PWM and there are even some libs available to drive standard servo's. You compile the mbed code using the online compiler and download the bin to you local disk. You then have two options: 1) use a bin2hex utility on the bin file and use flashmagic to download the resulting hex code to the landtiger over the serialport or 2) use the landtiger onboard JLINK programmer to download the code to the board. Both options are described in the landtiger manual that I posted on the landtiger notebook page. Both options work well, JLINK is faster. Just note that some lpc1768 pins have connections to onboard hardware on the landtiger.

I dont have experience with the IAR toolchain and would have to check on the details of your code to help you with any issues regarding PWM.

Hi thanks for y'all reply .. I've tried the mbed online compiler. Unfortunately I dont have mbed lpc1768 board, thus i'm not able to export the program due to target board unavailable... hmm

posted by hrz rfn 12 Jun 2013

can anyone export helloworld program to IAR for me & send it over my email? wajahrahsiahati(at)gmail(dot)com ? really appreciate ur help!

posted by hrz rfn 12 Jun 2013