Just tried to program the PWM register of LPC1768. The code has been well commented for use with LPC1768 based mBed
main.cpp@0:0b19df35d533, 2013-02-03 (annotated)
- Committer:
- Neel
- Date:
- Sun Feb 03 21:08:53 2013 +0000
- Revision:
- 0:0b19df35d533
PWM Register Level program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Neel | 0:0b19df35d533 | 1 | #include "LPC17xx.h" |
Neel | 0:0b19df35d533 | 2 | #include "mbed.h" |
Neel | 0:0b19df35d533 | 3 | |
Neel | 0:0b19df35d533 | 4 | Serial pc(USBTX,USBRX); |
Neel | 0:0b19df35d533 | 5 | |
Neel | 0:0b19df35d533 | 6 | int main() |
Neel | 0:0b19df35d533 | 7 | { |
Neel | 0:0b19df35d533 | 8 | // initialize the LED pins as outputs |
Neel | 0:0b19df35d533 | 9 | //LPC_GPIO1->FIODIR = 1 << 18 | 1 << 27; |
Neel | 0:0b19df35d533 | 10 | // PIN P26 is the output pin! |
Neel | 0:0b19df35d533 | 11 | pc.printf("Welcome to LPC1768 PWM demo\n\r"); |
Neel | 0:0b19df35d533 | 12 | // ensure PWM peripheral is powered on (it is powered on by default) |
Neel | 0:0b19df35d533 | 13 | LPC_SC->PCONP |= 1 << 6; |
Neel | 0:0b19df35d533 | 14 | LPC_PWM1->TCR = 2; // bring PWM module into reset |
Neel | 0:0b19df35d533 | 15 | LPC_PWM1->IR = 0xff; // clear any pending interrupts |
Neel | 0:0b19df35d533 | 16 | // configure P2.0 for PWM1.1 - O - Pulse Width Modulator 1, channel 1 output. |
Neel | 0:0b19df35d533 | 17 | LPC_PINCON->PINSEL4 = (LPC_PINCON->PINSEL4 & ~(0x3 << 0)) | (0x1 << 0); |
Neel | 0:0b19df35d533 | 18 | // Disable pullups for P2.0 |
Neel | 0:0b19df35d533 | 19 | LPC_PINCON->PINMODE4 = (LPC_PINCON->PINMODE4 & ~0x3) | 0x2; |
Neel | 0:0b19df35d533 | 20 | // Set prescale so we have a resolution of 1us |
Neel | 0:0b19df35d533 | 21 | LPC_PWM1->PR = SystemCoreClock / (4 * 1000000) - 1; |
Neel | 0:0b19df35d533 | 22 | LPC_PWM1->MR0 = 20000; // set the period in us. 50Hz rate |
Neel | 0:0b19df35d533 | 23 | LPC_PWM1->MR1 = 1500; // set duty of 1.5ms |
Neel | 0:0b19df35d533 | 24 | LPC_PWM1->MR2 = 19000; // set a match that occurs 1ms before the TC resets. |
Neel | 0:0b19df35d533 | 25 | LPC_PWM1->LER = 0x07; // set latch-enable register |
Neel | 0:0b19df35d533 | 26 | LPC_PWM1->MCR = 0x02 | (1 << 6); // reset on MR0, interrupt on MR2 |
Neel | 0:0b19df35d533 | 27 | LPC_PWM1->PCR = 1 << 9; // enable PWM1 with single-edge operation |
Neel | 0:0b19df35d533 | 28 | LPC_PWM1->TCR = (1 << 3) | 1; // enable PWM mode and counting |
Neel | 0:0b19df35d533 | 29 | while(1) { |
Neel | 0:0b19df35d533 | 30 | // wait for MR2 interrupt flag to be raised |
Neel | 0:0b19df35d533 | 31 | while((LPC_PWM1->IR & (1 << 2)) == 0); |
Neel | 0:0b19df35d533 | 32 | LPC_PWM1->IR = 0xff; // clear interrupt flags |
Neel | 0:0b19df35d533 | 33 | // write duty cycle value and set Latch-enable register |
Neel | 0:0b19df35d533 | 34 | LPC_PWM1->MR1 = 10000; //from 200 = 1% and 20000 = 100% |
Neel | 0:0b19df35d533 | 35 | LPC_PWM1->LER = 1 << 1; |
Neel | 0:0b19df35d533 | 36 | } |
Neel | 0:0b19df35d533 | 37 | |
Neel | 0:0b19df35d533 | 38 | } |