7 years, 9 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

7 years, 9 months ago.

Ticker does not affect Timer in any way. However I would replace the Ticker with PWM here.

Accepted Answer

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 Narshil tibbers 21 Jul 2016

It 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 Erik - 21 Jul 2016

Yes i am using the F411, if this is the case this would be amazing, i will look it up and test it out on my Red Pitaya (great little cheap Oscilloscope). If i have any more questions i will start another topic to keep this one on topic. :)

posted by Narshil tibbers 21 Jul 2016