jeevanmgp
Newbie level 2
Hi, I am having some trouble with PWM on LPC2148 microprocessors. Pulses are not generated. It is a complete high signal. Here is the code I am using.
/* Oscillator frequency 12.000 Mhz */
/* Target board Keil MCB2100 */
/************************************************************/
#include "LPC214X.H"
#include "FreeRTOSConfig.h"
static int pwmPulseWidth = 2000;
static int pwmPercentage = 50;
int pwmDutyCycle (int percentage);
int pwmPulseWidthInMicroseconds (int us);
int pwmFrequency (int frequency);
void pwmInit (void);
//
//
//
int pwmDutyCycle (int percentage)
{
if ((percentage < 0) || (percentage > 100))
return -1;
PWM_MR5 = ((configCPU_CLOCK_HZ / pwmPulseWidth) * (pwmPercentage = percentage)) / 100;
PWM_LER = PWM_LER_M5L;
return 0;
}
//
//
//
int pwmPulseWidthInMicroseconds (int us)
{
unsigned int count = (configCPU_CLOCK_HZ / 1000000) * us;
if (count > PWM_MR0)
return -1;
PWM_MR5 = count;
PWM_LER = PWM_LER_M5L;
return 0;
}
//
//
//
int pwmFrequency (int frequency)
{
if ((frequency < 1) || (frequency >= (int) configCPU_CLOCK_HZ))
return -1;
PWM_MR0 = (configCPU_CLOCK_HZ / (pwmPulseWidth = frequency));
PWM_MR5 = ((configCPU_CLOCK_HZ / pwmPulseWidth) * pwmPercentage) / 100;
PWM_LER = PWM_LER_M5L | PWM_LER_M0L;
return 0;
}
void pwmInit (void)
{
SCB_PCONP |= SCB_PCONP_PCPWM0;
PCB_PINSEL1 = (PCB_PINSEL1 & ~PCB_PINSEL1_P021_MASK) | PCB_PINSEL1_P021_PWM5;
PWM_TCR = PWM_TCR_CR;
PWM_PR = 0;
PWM_MR0 = (configCPU_CLOCK_HZ / pwmPulseWidth);
PWM_MCR |= PWM_MCR_MR0R;
PWM_PCR |= PWM_PCR_ENA5;
PWM_TCR = (PWM_TCR_CE | PWM_TCR_PWME);
pwmDutyCycle (50);
}
void init_pll()
{
PLL0CFG = 0x00000025;
PLL0CON = 0x00000001;
PLL0FEED = 0x0000000AA;
PLL0FEED = 0x000000055;
while (!(PLL0STAT & 0x00000400));
PLL0CON = 0x00000003;
PLL0FEED = 0x0000000AA;
PLL0FEED = 0x000000055;
VPBDIV = 0x00000001;
}
void main()
{
init_pll();
pwmInit();
pwmFrequency (50);
pwmPulseWidthInMicroseconds(2000);
pwmDutyCycle(50);
while(1)
{
}
}
/* Oscillator frequency 12.000 Mhz */
/* Target board Keil MCB2100 */
/************************************************************/
#include "LPC214X.H"
#include "FreeRTOSConfig.h"
static int pwmPulseWidth = 2000;
static int pwmPercentage = 50;
int pwmDutyCycle (int percentage);
int pwmPulseWidthInMicroseconds (int us);
int pwmFrequency (int frequency);
void pwmInit (void);
//
//
//
int pwmDutyCycle (int percentage)
{
if ((percentage < 0) || (percentage > 100))
return -1;
PWM_MR5 = ((configCPU_CLOCK_HZ / pwmPulseWidth) * (pwmPercentage = percentage)) / 100;
PWM_LER = PWM_LER_M5L;
return 0;
}
//
//
//
int pwmPulseWidthInMicroseconds (int us)
{
unsigned int count = (configCPU_CLOCK_HZ / 1000000) * us;
if (count > PWM_MR0)
return -1;
PWM_MR5 = count;
PWM_LER = PWM_LER_M5L;
return 0;
}
//
//
//
int pwmFrequency (int frequency)
{
if ((frequency < 1) || (frequency >= (int) configCPU_CLOCK_HZ))
return -1;
PWM_MR0 = (configCPU_CLOCK_HZ / (pwmPulseWidth = frequency));
PWM_MR5 = ((configCPU_CLOCK_HZ / pwmPulseWidth) * pwmPercentage) / 100;
PWM_LER = PWM_LER_M5L | PWM_LER_M0L;
return 0;
}
void pwmInit (void)
{
SCB_PCONP |= SCB_PCONP_PCPWM0;
PCB_PINSEL1 = (PCB_PINSEL1 & ~PCB_PINSEL1_P021_MASK) | PCB_PINSEL1_P021_PWM5;
PWM_TCR = PWM_TCR_CR;
PWM_PR = 0;
PWM_MR0 = (configCPU_CLOCK_HZ / pwmPulseWidth);
PWM_MCR |= PWM_MCR_MR0R;
PWM_PCR |= PWM_PCR_ENA5;
PWM_TCR = (PWM_TCR_CE | PWM_TCR_PWME);
pwmDutyCycle (50);
}
void init_pll()
{
PLL0CFG = 0x00000025;
PLL0CON = 0x00000001;
PLL0FEED = 0x0000000AA;
PLL0FEED = 0x000000055;
while (!(PLL0STAT & 0x00000400));
PLL0CON = 0x00000003;
PLL0FEED = 0x0000000AA;
PLL0FEED = 0x000000055;
VPBDIV = 0x00000001;
}
void main()
{
init_pll();
pwmInit();
pwmFrequency (50);
pwmPulseWidthInMicroseconds(2000);
pwmDutyCycle(50);
while(1)
{
}
}