Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 4 months ago.
Can i use Timer and Ticker together?
I am using the ticker to make a square wave that can be used as a step input for a stepper motor. I am working in the range of 1kHz to 25 kHz and i wanted to see how this effects the rest of the main thread. Can i use timer for this or will using ticker and timer together give me unreliable results?
I attached a part of the code below that may explain my question better.
Note: The POT is just for testing, i don't plan on keeping it there.
example
DigitalOut pin7(D7); AnalogIn readPOT(A0); void trigger_step() { pin7 = !pin7; } while (true) { timer.start(); //pc.printf("This program runs since seconds.\n"); pc.printf("A0 reads: %f \n", readPOT.read()); //analog read returns between 0-1 at 12bit resolution stepspeed = (readPOT.read() * 22000); timeperiodmicro = (1/stepspeed)*0.5*1000000; //Multiply by 0.5 for half period for 50% duty cycle toggle_step_ticker.attach_us(&trigger_step, timeperiodmicro); pc.printf("Time Period: %f \n", timeperiodmicro); pc.printf("Time in micro seconds : %f \n", timer.read_us()); timer.stop(); }
1 Answer
8 years, 4 months ago.
Ticker does not affect Timer in any way. However I would replace the Ticker with PWM here.
Thank you and I need to have 2 independent signals, from what i have seen from the forums changing the frequency on PWM changes the frequency for all PWM outputs?
posted by 21 Jul 2016It depends on your MCU how it works exactly. I see you got a F411? On the pinout the PWMs are labbeled as PWM1/2, where the first number is the timer and the second the channel. If you use two pins on different timers, they can have independent frequencies. Otherwise they are indeed equal.
posted by 21 Jul 2016