Frequency meter with STM32

Does anybody know some good code for a frequency meter (with period and duty cicle) using the STM32F103 hardware timers ? Thanks for any advice. Antonio

AntonioTesta:
Does anybody know some good code for a frequency meter (with period and duty cicle) using the STM32F103 hardware timers ? Thanks for any advice. Antonio

I forgot to mention that my hardware is prepaired to use Timer2, T2C1E as input. Thanks again

Google didn't turn up anything?

aarg:
Google didn't turn up anything?

No. I have found only code generated by CubeMx and other complicated ST tools. I wonder if I can do that under Arduino IDE...

AntonioTesta:
No. I have found only code generated by CubeMx and other complicated ST tools. I wonder if I can do that under Arduino IDE...

Why? Arduino IDE has such a limited HAL that timer code has to be coded manually. That means the timer libraries are third party, and mostly processor specific to AVR.

What I recommend, is to take the Cube code and port it. By the way, you can call Cube complicated if you like, but since it can auto generate low level driver code for all the on chip peripherals, I disagree. I've never seen anything else that is as easy to use. Arduino is easy at high level, but anything that goes beyond what the core and core libraries have, becomes a very customized task. Also with that, the resulting code remains processor specific. That problem is actually what brings you here.

Thanks aarg for you explanation. The problem is that during "all my life" i solved all my problems using the Arduino IDE and that is why I did not feel animated to learn or study one new thing, specilly why at first look the CubeMx really seams to be complicated for me. Perhaps the Arduino IDE code bellow would works if I found where the mistake are !!! It gives the correct frequency and duty only from 2Hz to 5KHz. For higher or lower frequencies the result is completely wrong !!! :frowning: I think it is close to be ok... Any idea about what is wrong ?

// 
// input signal to be measured at PA0 (T2C1E)
//

float freq, duty, thigh, ttotal;

int prefactor=72; //72 ok for 16Hz to 5KHz;
                  //720 ok for 2Hz to 1KHz;
                  //noway to get an accurated measuring bellow 2Hz or above 5KHz !!! ??? 
long tout;

void setup()
{
  Serial1.begin(9600);
  iniT2();
}

void loop()
{
  tout=millis()+1100;
  while (!Timer2.getInputCaptureFlag(TIMER_CH2) && (millis()<tout)) {} thigh=Timer2.getCompare(TIMER_CH2);
  tout=millis()+1100;
  while (!Timer2.getInputCaptureFlag(TIMER_CH4) && (millis()<tout)) {} ttotal=Timer2.getCompare(TIMER_CH4);
  
  duty=thigh/ttotal*100.0; 
  freq=1.0/ttotal;
  if (prefactor==72){freq=freq*1000000.0;}
  if (prefactor==720){freq=freq*100000.0;}
  Serial1.print("Freq=");Serial1.print(freq,3);Serial1.print(" - Duty=");Serial1.println(duty,1);
  delay(1000);
}


void iniT2()
{
  Timer2.pause();
  Timer2.setPrescaleFactor(prefactor);
  Timer2.setInputCaptureMode(TIMER_CH4, TIMER_IC_INPUT_DEFAULT);
  Timer2.setInputCaptureMode(TIMER_CH2, TIMER_IC_INPUT_SWITCH);
  Timer2.setPolarity(TIMER_CH2, 1);
  Timer2.setSlaveFlags( TIMER_SMCR_TS_TI1FP1 | TIMER_SMCR_SMS_RESET );
  Timer2.refresh();
  Timer2.getCompare(TIMER_CH4);
  Timer2.getCompare(TIMER_CH2); 
  Timer2.resume();
  Timer2.getCompare(TIMER_CH4);
}