A PWM library for LPC1768 that enables the sending of n number of PWM impulses through registry editing of LPC1768.

Revision:
0:b3a0656d2b6e
Child:
2:881243185b6a
diff -r 000000000000 -r b3a0656d2b6e TruePWM.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TruePWM.cpp	Mon Nov 30 02:41:40 2015 +0000
@@ -0,0 +1,135 @@
+#include "TruePWM.h"
+
+
+TruePWM::TruePWM(PinName pin) {    //Constructor that can target any pwm pin on MBED LPC1768 and LED1 for testing purposes
+    initTruePWM();
+    switch ( pin ) {
+    case LED1:
+    LPC_GPIO1->FIODIR = 1 << 18;            
+    LPC_PINCON->PINSEL3 = 0x00000020;
+    LPC_PINCON->PINMODE3 = 0x00000010;
+    break;
+    case p26:
+    LPC_PWM1->PCR |= 1 << 9;                 //Seting PRC register bit to 1 to enable single-egde mode 
+    LPC_PINCON->PINSEL4 |= 1;                //Enables pin as a PWM pin
+    LPC_PINCON->PINMODE4 |= (1<<1);          //Disables pullups and pulldowns for pin
+    pulsewidth_us_ind (getPulsewidth(), 1);
+    break;
+    case p25:
+    LPC_PWM1->PCR |= 1 << 10;
+    LPC_PINCON->PINSEL4 |= (1<<2);
+    LPC_PINCON->PINMODE4 |= (1<<3);
+    pulsewidth_us_ind (getPulsewidth(), 2);
+    break;
+    case p24:
+    LPC_PWM1->PCR = 1 << 11;
+    LPC_PINCON->PINSEL4 |= (1<<4);
+    LPC_PINCON->PINMODE4 |= (1<<5);
+    pulsewidth_us_ind (getPulsewidth(), 3);
+    break;
+    case p23:
+    LPC_PWM1->PCR = 1 << 12;
+    LPC_PINCON->PINSEL4 |= (1<<6);
+    LPC_PINCON->PINMODE4 |= (1<<7);
+    pulsewidth_us_ind (getPulsewidth(), 4);
+    break;
+    case p22:
+    LPC_PWM1->PCR = 1 << 13;
+    LPC_PINCON->PINSEL4 |= (1<<8);
+    LPC_PINCON->PINMODE4 |= (1<<9);
+    pulsewidth_us_ind (getPulsewidth(), 5);
+    break;
+    case p21:
+    LPC_PWM1->PCR = 1 << 14;
+    LPC_PINCON->PINSEL4 |= (1<<10);
+    LPC_PINCON->PINMODE4 |= (1<<11);
+    pulsewidth_us_ind (getPulsewidth(), 6);
+    break;
+    default:
+  // Code
+    break;
+}
+}
+
+
+
+void TruePWM::initTruePWM() {   
+
+    LPC_SC->PCONP |= 1 << 6;    //Tests if the pwm module is working
+    LPC_PWM1->TCR = 2;          //Puts pwm module into reset mode                           
+    LPC_PWM1->IR = 0xff;        //Clears interrupts            
+    LPC_PWM1->PR = SystemCoreClock / (4 * 1000000) - 1;  //Sets Prescale register valiue 
+    LPC_PWM1->MR1 = 1000;       //Sets a defoult pulsewidth if any other pin than p26 is selected
+}
+
+void TruePWM::pulsewidth_us_ind (uint32_t ticks, char n){ //Internal function sets pulsewidth for coresponding pin
+    switch (n){
+        case 1:  //For P26
+        LPC_PWM1->MR1 = ticks;          
+        LPC_PWM1->LER = 2;
+        break;
+        case 2:  //For P25
+        LPC_PWM1->MR2 = ticks;
+        LPC_PWM1->LER |= 1 << 3;
+        break;
+        case 3:  //For P24
+        LPC_PWM1->MR3 = ticks;
+        LPC_PWM1->LER |= 1 << 4;
+        break;
+        case 4:  //For P23
+        LPC_PWM1->MR4 = ticks;
+        LPC_PWM1->LER |= 1 << 5;
+        break;
+        case 5:  //For P22
+        LPC_PWM1->MR5 = ticks;
+        LPC_PWM1->LER |= 1 << 6;
+        break;
+        case 6:  //For P21
+        LPC_PWM1->MR6 = ticks;
+        LPC_PWM1->LER |= 1 << 7;
+        break;
+    } 
+}
+
+void TruePWM::pulsewidth_us( uint32_t ticks ) {   //sets pulsewidth for p26
+    LPC_PWM1->MR1 = ticks;  
+    LPC_PWM1->LER = 2;  
+}
+
+void TruePWM::period_us( uint32_t ticks ) {                       
+    LPC_PWM1->MR0 = ticks;                  //Fills pariod register with valiue
+    LPC_PWM1->LER = 1;                     //Enables the writen valiue
+}
+
+void TruePWM::setDuty(float a){           //sets dity cycle
+    if (a<=1){
+    LPC_PWM1->MR1 = LPC_PWM1->MR1*a;  
+    LPC_PWM1->LER = 2;  
+    }}
+
+uint32_t TruePWM::getPeriod() {
+    return LPC_PWM1->MR0;
+}
+
+uint32_t TruePWM::getPulsewidth() {
+    return LPC_PWM1->MR1;
+}
+
+void TruePWM::startPWM() {
+    LPC_PWM1->TCR = 0x00000009;           //Enables CE and PWMEN bits
+}
+
+void TruePWM::stopPWM() {
+    LPC_PWM1->MCR = 0x00000004;           /*Enables PWMMR0S bit that stops 
+                                            PWM output and diaables counter bit*/  
+}    
+
+void TruePWM::nImpulse(int n) {            //Sends n number of impulses 
+    if(LPC_PWM1->TCR != 0x00000009)
+        LPC_PWM1->TCR = 0x00000009;
+    float m= LPC_PWM1->MR0*n;
+    t0.attach_us(this,&TruePWM::stopPWM, m);
+}
+  
+  
+