2022-03-02 04:51 AM
A 720mV RMS sine wave, biased to +1.25V is connected to the PB2 pin. PB2 is the COMP1 positive input. The COMP1 negative input is connected to the internal reference voltage (+1.22V) The output of the comparator is connected to PA0 which is the input capture channel 1 input of TIM2 (32-bit) timer. The TIM2 runs from 275MHz clock and it is started in input capture channel1 with interrupt mode. It works, but it measures 119.3Hz instead of 120Hz.
I had to set the comparator mode to COMP_POWERMODE_MEDIUMSPEED because with the COMP_POWERMODE_HIGHSPEED I got bouncing signal (spikes, glitches) at the rising/falling edges of the comparator output. The max hysteresis is used.
The error is not large and consequent about 0.6 – 0.7Hz less: The standard deviation also large: 0.07Hz, is should be 0.002Hz.
Maybe the NUCLEO-114 runs from an RC oscillator, not crystal?
As far as I understand the CPU is clocked from HSE clock. It is MCO of STLINK-V3E which is 8MHz. Is this an RC oscillator or crystal? The other acceptable option would be the X3 25MHz crystal, but unfortunately it is not assembled...
Thanks,
Louis
2022-03-02 05:33 AM
Typically, the NUCLEO-144 are clocked by the ST-LINK's MCO, with the 8MHz coming from its HSE, which is using an 8MHz crystal.
Do you measure the period, i.e. not the pulse width?
Regards
/Peter
2022-03-02 05:42 AM
The 8 MHz HSE from the STLINK-V3E is from a quarz via the STLINK's STM32, and usually / on my boards not too bad.
The noise comes probably from the combination of low frequency / not so steep flank input signal and the comparator noise.
Concerning the inaccuracy:
2022-03-02 06:25 AM
Watch the frequency output by the V3, it is selectable as 25/3, ie 8.33 MHz or from the HSI. Bit of a mess.
2022-03-02 10:45 AM
Hi Peter,
Thanks for the answer!
Because my signal is biased to +1.25V and the comparator compares at +1.2 the the positive comparator output is shorter than the negative one. On scope this is 4.8us and 5.2us @100Hz. Therefore I measure the time between two rising edges (period).
Louis
2022-03-02 11:02 AM
Dear LCE,
Thanks for the ideas.
Thanks,
Louis
2022-03-02 11:02 AM
Hi Tesla DeLorean,
I am afraid I do not understand you. What is V3 and how I ca set (and why) the 25/2 = 8.33MHz?
Would you pleas explain a bit more?
Thnaks,
Louis
2022-03-02 11:56 AM
The firmware upgrade tool should show/provide for options as to the source of the clock. None of them are crystal accurate 8 MHz.
Period/Prescale are N-1 numbers. For timing I'd use the TIM in a maximal mode, ie Period=0xFFFF or 0xFFFFFFFF, as it makes the math easier, and less likely to be wrong.
On STLINK-V3E boards: – Possibility added to choose the MCO clock output from ST-LINK to target among 3 values. The default value (HSI / 2 = 8 MHz) can be changed through STLinkUpgrade applications to HSE / 3 = 8.33 MHz (for better clock accuracy) or cut off. The operation is reversible. In case of a switch to 8.33 MHz, the target application must be updated accordingly: a factor × (24 / 25) must be applied in the PLL configuration in order to keep the clock tree unchanged behind (for instance, if PLLN = 400, set PLLN = 384). Moreover the HSE_VALUE constant commonly defined into stm32xxx_ hal_conf.h must be changed to value 8333333.
2022-03-02 12:38 PM
Value is about 1% off (typical HSI error value). Also short term deviations are typical for HSI. Looks like you are running from HSI. Double check your clock source. Simply put fraction of your SYSCLK to MCO (PA8/PC9?) and look by scope on HSE input frequency and your MCO output. If they are, you can be sure that you are running from HSE and you can search elsewhere - for example at comparator output.
2022-03-02 12:39 PM
correction: *If they are in phase.