Problem using PWM on LandTiger LPC1768 for Servo control

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

Problem using PWM on LandTiger LPC1768 for Servo control

800 Views
lpcware
NXP Employee
NXP Employee
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!
0 Kudos
13 Replies

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Thu Jun 13 02:36:13 MST 2013

Quote: hrzrfn
It's kenwood osciloscope.. I've measured the period to be 3.2ms and high time to be 0.24ms .. this is when using MR0=20000, MR1=1500 and no prescaling.. the signal is somewhat odd, its like series of pulses over a wave (as per attachment)..



No Ground? 0.24ms is 7.5% of 3.2 ms. So PWM is working.



Quote: hrzrfn
btw, what's the clkout code function? I've measured the output at p1.27 but got nothing..



Described in UM10360. And again you are not able to measure correctly. Read the manual of your scope and learn how to use it.
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hrzrfn on Thu Jun 13 01:54:08 MST 2013

Quote: R2D2
And what are you measuring? Use your scope (looks like a HAMEG) to measure cycle time and high time (especially 'HOLD OFF'). The picture you are posting is showing wrong triggering.

Use CLKOUT to output main clock and measure it to ensure that you are really using 64MHz.
//clkout (P1.27) to cpu clock
 LPC_PINCON->PINSEL3 &=~(3<<22);
 LPC_PINCON->PINSEL3 |= (1<<22);
 LPC_SC->CLKOUTCFG = (1<<8)|(9<<4)| 0; //enable and divide by 10 (max 16)
The code is generating correct PWM if main clock is set as expected (at least with LPCXpresso standard CMSIS setup).



It's kenwood osciloscope.. I've measured the period to be 3.2ms and high time to be 0.24ms .. this is when using MR0=20000, MR1=1500 and no prescaling.. the signal is somewhat odd, its like series of pulses over a wave (as per attachment)..

btw, what's the clkout code function? I've measured the output at p1.27 but got nothing..
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Wed Jun 12 17:12:49 MST 2013

Quote: hrzrfn
the cycle time should be 20ms period (50Hz) with high time ranged from 1-2ms (5%-10% duty cycle), but for the sake of testing I'm assuming 1.5ms for the high time..

so the setting that I choose was:
LPC_PWM1->PR = 64-1; // prescale to 1us
LPC_PWM1->MR0 = 20000 // (1/1us)/20000 = 50Hz
LPC_PWM1->MR1 = 1500 // (7.5% duty cycle)*MR0 = 1500



And what are you measuring? Use your scope (looks like a HAMEG) to measure cycle time and high time (especially 'HOLD OFF'). The picture you are posting is showing wrong triggering.

Use CLKOUT to output main clock and measure it to ensure that you are really using 64MHz.
//clkout (P1.27) to cpu clock
 LPC_PINCON->PINSEL3 &=~(3<<22);
 LPC_PINCON->PINSEL3 |= (1<<22);
 LPC_SC->CLKOUTCFG = (1<<8)|(9<<4)| 0; //enable and divide by 10 (max 16)
The code is generating correct PWM if main clock is set as expected (at least with LPCXpresso standard CMSIS setup).
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hrzrfn on Wed Jun 12 16:13:50 MST 2013

Quote: R2D2
:confused:

And now you are expecting that I guess what your scope / picture is showing?

What's the cycle time (in xx ms)?

What's the high time (in xx.x ms)?

Which values did you expect?

Which actual settings did you choose ?

LPC_PWM1->PR =?
LPC_PWM1->MR0 =?
LPC_PWM1->MR1 =?



the cycle time should be 20ms period (50Hz) with high time ranged from 1-2ms (5%-10% duty cycle), but for the sake of testing I'm assuming 1.5ms for the high time..

so the setting that I choose was:
LPC_PWM1->PR = 64-1; // prescale to 1us
LPC_PWM1->MR0 = 20000 // (1/1us)/20000 = 50Hz
LPC_PWM1->MR1 = 1500 // (7.5% duty cycle)*MR0 = 1500
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Wed Jun 12 14:00:29 MST 2013

Quote: hrzrfn
sorry but I dont really get your question.. did you mean you wanted to see the PWM waveform that I've measured on the scope?

1)http://mbed.org/media/uploads/hrzrfn/photo.jpg
heres the waveform on oscilloscope with setting 5v/div at y-axis and 1ms/div with 10x magnification at x-axis

2)
im expecting to see somewhat like this [IMG]http://4.bp.blogspot.com/-qXAyEtbPTLE/UP74Hsux0KI/AAAAAAAAA0s/L-K-ldjKx_c/s600/hobby_pwm_single_chan...

hope I'm answering your question this time.. :o



:confused:

And now you are expecting that I guess what your scope / picture is showing?

What's the cycle time (in xx ms)?

What's the high time (in xx.x ms)?

Which values did you expect?

Which actual settings did you choose ?

LPC_PWM1->PR =?
LPC_PWM1->MR0 =?
LPC_PWM1->MR1 =?
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hrzrfn on Wed Jun 12 13:45:28 MST 2013

Quote: R2D2
:confused:

Could be useful if you would answer my questions since I'm doing your job :)

1. Which signal is your scope showing you?

2. Which signal do you expect?



sorry but I dont really get your question.. did you mean you wanted to see the PWM waveform that I've measured on the scope?

1)http://mbed.org/media/uploads/hrzrfn/photo.jpg
heres the waveform on oscilloscope with setting 5v/div at y-axis and 1ms/div with 10x magnification at x-axis

2)
im expecting to see somewhat like this [IMG]http://4.bp.blogspot.com/-qXAyEtbPTLE/UP74Hsux0KI/AAAAAAAAA0s/L-K-ldjKx_c/s600/hobby_pwm_single_chan...

hope I'm answering your question this time.. :o
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Wed Jun 12 12:39:54 MST 2013

Quote: hrzrfn
sorry about that..
I've only check the PWM signal on P2.0 since
I'm expecting PWM signal to be output on pin P2.0 on the board
thus the code >> LPC_PINCON->PINSEL4 = (LPC_PINCON->PINSEL4 & ~(0x3 << 0)) | (0x1 << 0); //output to pin 2.0
I hope I'm answering your questions..



:confused:

Could be useful if you would answer my questions since I'm doing your job :)

1. Which signal is your scope showing you?

2. Which signal do you expect?
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hrzrfn on Wed Jun 12 12:15:21 MST 2013

Quote: R2D2
Could be useful if you would answer my questions since I'm doing your job :)

1. Which signal is your scope showing you?

2. Which signal do you expect?



sorry about that..
I've only check the PWM signal on P2.0 since
I'm expecting PWM signal to be output on pin P2.0 on the board
thus the code >> LPC_PINCON->PINSEL4 = (LPC_PINCON->PINSEL4 & ~(0x3 << 0)) | (0x1 << 0); //output to pin 2.0
I hope I'm answering your questions..
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Wed Jun 12 12:07:37 MST 2013

Quote: hrzrfn
still doesnt work.. :confused:



Could be useful if you would answer my questions since I'm doing your job :)

1. Which signal is your scope showing you?

2. Which signal do you expect?
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hrzrfn on Wed Jun 12 12:02:57 MST 2013

Quote: R2D2
Delete this part, that's nonsense:
while(1) 
 {
  LPC_PWM1->MR1 = 1500; // pulse width 1.5ms 
  LPC_PWM1->LER |=  (1 << 1); //Apply changes
 }
 return (0);
Which signal are you scoping?

Debugger settings don't change your code.

Prescaler is:
LPC_PWM1->PR = 1 - 1;
Set it to:
LPC_PWM1->PR = 64 - 1;
and PWM is prescaling 64, so your PWM time base is 1µs.



still doesnt work.. :confused:
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Wed Jun 12 11:32:38 MST 2013

Quote: hrzrfn
I've tried the code but servo still doesnt move. Is there anything I missed? is it prescaling or anything?

I'm using JLINK debugger with clock setting set to 64.0Mhz and SWO clock to auto.. does this have anything related to the problem?



Delete this part, that's nonsense:
while(1) 
 {
  LPC_PWM1->MR1 = 1500; // pulse width 1.5ms 
  LPC_PWM1->LER |=  (1 << 1); //Apply changes
 }
 return (0);
Which signal are you scoping?

Debugger settings don't change your code.

Prescaler is:
LPC_PWM1->PR = 1 - 1;
Set it to:
LPC_PWM1->PR = 64 - 1;
and PWM is prescaling 64, so your PWM time base is 1µs.
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hrzrfn on Wed Jun 12 10:54:20 MST 2013

Quote: R2D2
UM10360 is describing how clock setup is working

Your CPU clock setup is:

CCLK = Fin * 2 * 0x40 / 3 / 8

with a 12MHz crystal (Fin) that's[COLOR=Red] 64MHz[/COLOR], which is also the clock for your PWM with PCLKSEL =1.



hi thx for your response

so my CCLK is actually 64MHz

so changing my PWM setting to;

LPC_SC->PCLKSEL0 |= 1 << 12; // PCLK = CCLK = 64MHz
LPC_PWM1->MR0 = 1280000; // PCLK/1280000 = 50Hz
LPC_PWM1->MR1 = 640000; // PCLK/(1+1ms) = 640000


I've tried the code but servo still doesnt move. Is there anything I missed? is it prescaling or anything?

I'm using JLINK debugger with clock setting set to 64.0Mhz and SWO clock to auto.. does this have anything related to the problem?
0 Kudos

698 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Wed Jun 12 08:35:42 MST 2013

Quote: hrzrfn
...can someone help me on this?



UM10360 is describing how clock setup is working :)


Quote:

#define PLL0CFG_Val           0x0002003f
#define CCLKCFG_Val           0x00000007

Your CPU clock setup is:

CCLK = Fin * 2 * 0x40 / 3 / 8

with a 12MHz crystal (Fin) that's[COLOR=Red] 64MHz[/COLOR], which is also the clock for your PWM with PCLKSEL =1.
0 Kudos