5 years, 11 months ago.

how to output a timed block of PwmOut on LPC1768

I'm pretty new to mbed and have been trying to have it output a 0.5s block of 1kHz square wave. My problem is that once the PwmOut is turned on, I can't seem to turn it off. Any ideas would be great.

This is what I have:

pwm

#include "mbed.h"

Timer timer1;
DigitalIn enable(p5);

int p, w, x;

int main() {
   
   while(1){
   
   x = enable;
   
   if(x == 1){
           
           PwmOut *pulse = new PwmOut(p26); //pin 26 pwm out
           timer1.start();
           
           if(timer1.read_ms() <= 500){
               
           p = 1; 
           w = 500;
           pulse ->period_ms(p);          //period
           pulse ->pulsewidth_us(w);    //pulsewidth  
       }
       
       delete pulse;
       timer1.reset(); 
   
   }
}
}

Got it to work with much simpler use of wait() and pulse = 0 :)

posted by Beth B 24 Oct 2014

1 Answer

5 years, 11 months ago.

Good that you got it working :)

Also in general don't bother with using new and delete, but rather use it just like you got DigitalIn for example. New and delete certainly have their uses, but are also incredibly easy to make mistakes with.

Accepted Answer

Yeah, I was leery of new/delete for sure, and am glad to avoid pointers!

posted by Beth B 24 Oct 2014