10 years, 8 months ago.  This question has been closed. Reason: I resolved myself thank you

What's most efficient way to generate a 500Hz Pulse & cope with the rest of the code?

Dear all,

I'm trying to generate a 500Hz(2ms) pulse to control my step motors but I would like it to be independent from the rest of the code as it gets delayed. What would be the most efficient way of doing it please? Shall I just create a function or try to use an interrupt.

Thank you very much.

2 Answers

10 years, 8 months ago.

You can easily create a continous 500Hz wave by using a Ticker object that calls a function to flip a DigitalOut every ms. See handbook. Alternatively you can use the PWM pins.

Accepted Answer

Hi! I've tried to use the Ticker_HelloWorld example as a template but it isn't reading the sensor. Whatever I do it stops the clock. Would you tell if the syntax is wrong please? Thank you

/void flip() { Clock.output(); Clock.write(0); wait_ms(2); Clock.write(1); wait_ms(2); }void

int main(){

forward.attach(&flip, 0.5); the address of the function to be attached (forward) and the interval (1/2 >>second)

while(1) { usensor.start(); wait_ms(500); dist=usensor.get_dist_cm(); lcd.cls(); lcd.locate(3,3); lcd.printf("cm: %ld",dist ); } /}

posted by lincoln almeida 23 Aug 2013
10 years, 8 months ago.

I've managed thanks!