6 years, 9 months ago.

PWM set up

Hi forum. Hope you are all well.

include the mbed library with this snippet

/*  an attempt at PWM without using the mbed registers

*/

#define PINSEL4     (*(volatile unsigned char *)(0x4002C010))
#define PCONP       (*(volatile unsigned char *)(0x400FC0C4))
#define PCLKSEL0    (*(volatile unsigned char *)(0x400FC1A8))
#define PINSEL0     (*(volatile unsigned char *)(0x4002C000))
#define PINMODE4    (*(volatile unsigned char *)(0x4002C050))
#define PWM1MR0     (*(volatile unsigned char *)(0x40018018))
#define PWM1MCR     (*(volatile unsigned char *)(0x40018014))
#define PWM1TCR     (*(volatile unsigned char *)(0x40018004))
#define PWM1MR5     (*(volatile unsigned char *)(0x40018044))
#define PWM1PCR     (*(volatile unsigned char *)(0x4001804C))

int main(){
    
    while(1){

PCONP |= 1 << 6;        // power up PWM , powered up on reset, not requ
PCLKSEL0 |= 1 << 12;        // setting up the clock for pwm, clock = microcontroller clock i.e the prescaler value is 1.
PINSEL4 |= 1 << 6;      // p2.4 as PWM1 output
PINMODE4 |= 1 << 9;     // enable neither pull-up nor pull-down
PWM1MR0 = 16;   // PWM freq = PWM clock/16
PWM1MCR |= 1 << 1;      // reset timer on Match0
PWM1MR5 = 8;        // PWM duty cycle = 0.5


/*  8 is stored into MR5. therefore now one cycle of PWM waveform consists of 16PWM clock cycles
and for 8 of them the PWM output will be high and for remaining 8 cycles it will be low. 
therefore the effective signal produced is a waveform of frequency 
(PWM clock/6) with a duty cycle of 0.5
*/

PWM1TCR |= (1 << 0) | (1 << 3);     // enable timer/counter

/*
select control mode for each channel(except channel ) and enable the PWM output

*/

PWM1PCR |= 1 << 4; //configure channel 4 as double edge , refer to table 451 pg 519
//settiing bits 2-6 to 0 enables single edge 

PWM1PCR |= 1 << 12; //enable PWM for channel 4 output
PWM1PCR |= 1 << 13; // eanble PWM for channel 5 output , no need to configure channel 5 default is to 0 single side rise
    }
}

I'm trying to get PWM working on any pin 2.4 I've managed to get the pin to light up, but i can't get voltage to vary when i change the value in PWM1MR5. Could someone please point me in the right direction to fix this. Many thanks.

2 Answers

6 years, 9 months ago.

I assume the LPC1768?

First of all, why make your own register definitions? LPC_PWM1->PCR is for the PCR register for example. What you miss for sure is the LER register, you need to write correct bits to cause the match registers to update.

Donald K
poster
6 years, 9 months ago.

i've tried doing that, it gives me an error complaining about the _ and ->

Add #include "mbed.h"

Or just use PwmOut.

posted by Erik - 14 Feb 2014