MCU's comparator ignoring Hysteresis - Page 1
Forum section: electronics / beginners

author: Saimoun
author_rank: Frequent Contributor
timestamp: March 12, 2023, 09:31:45 am
content: HiI am using a GD32 MCU, and its internal comparator CMP0.I set a trigger on the comparator which is rising edge only. It works about 99% of the time, but sometimes it triggers while the signal is falling as shown in the oscilloscope's screenshot.I attached:* the comparator's datasheet* the CMP peripheral's register CMP_CS* My config of the CMP_CS register (from the debugger - note that the trigger is using CMP0, CMP1 is also used but for something else)* A screenshort of the oscilloscope showing the wrongly triggered falling edgeI am guessing this happens because of noise, but the comparator has an hysteresis setting which is 36 mV (typical value), so the noise would need to go up by 72mV during a falling edge for it to trigger! Which does not seem to happen looking at the oscilloscope? (the oscillo is max 100Mhz)What is strange is that the error disappears completely if I slow down the comparator (low power mode and ultra-low power mode seem to give no errors).I might have missed something, maybe something to do with the propagation delay?Thank you!Simon

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 12, 2023, 10:00:08 am
content: 72mv? I don't think so?The noise need to go down by 36mv for the input going "low", then 36mv up to become "high".

author: Siwastaja
author_rank: Super Contributor
timestamp: March 12, 2023, 10:47:20 am
content: And the scope trace shows some 35-40mVp-p of noise right there. Could be more internally at the comparator. Result is as expected.

author: pcprogrammer
author_rank: Super Contributor
timestamp: March 12, 2023, 10:48:57 am
content: 36mV is not that much. Might even be caused by something on the power supply.The scope image does show some noise on the input signal, but that is after the event already happened, and might even be due to the event happening.

author: Saimoun
author_rank: Frequent Contributor
timestamp: March 12, 2023, 01:27:02 pm
content: Ok thanks guys.I guess it depends how you understand the hysteresis, but I would say it needs to go to 300-36mV (to be marked as low) and then back up to 300+36mV (to be marked as high and trigger a rising edge).See attached picture from the datasheet.The scope is showing when the event happens - not sure what you mean pcprogrammer with "after the event already happened"? you can see it happens when the yellow line crosses the 300mV (more or less). The actual rising edge of the signal also matches, more or less at 380mV (which makes sense with the hysteresis - my guess is the actual threshold is about 350, making the low threshold 315 and the high 385).But I guess you all make a good point, there is the noise on the actual signal, maybe a bit of noise on the ground, maybe a bit of noise on the 300mV threshold, all of that combined making it enough to go over the threshold even with the hysteresis.

author: pcprogrammer
author_rank: Super Contributor
timestamp: March 12, 2023, 03:51:45 pm
content: On the scope image you see the blue line go high, which is when the comparator output changes. The noise blips on the yellow line, two of them, are seen later on the screen. So the noise seen there can be the result of the processor taking some actions.To lazy right now to copy the image and draw circles in itBut you are right with the hysteresis in this case. It is what the datasheet shows, but still < 0.1V is not that much.

author: Saimoun
author_rank: Frequent Contributor
timestamp: March 13, 2023, 05:35:54 pm
content: Ok this MCU has this wonderful ability to route the comparator's output to a GPIO, so I could investigate more what is happening.This scope screenshot shows now the comparator's output (not the detected rising edges like before, the raw CMP_OUT)And indeed now it's pretty clear: because the signal's rising time is a bit slow, when it comes close to the threshold, if there is at that moment a little bit of noise, then yes the output can go up down up down.In particular I noticed if I speak to the device over USB, then I have these little clumps of noise (probably the host regularly asking for packages), and if one of those clumps of noise happens precisely at the transition time, then it triggers twice.Anyways - gotta sort it out, but at least now I know what's happening and clearly the hysteresis is working as it shouldThanks everybody for the help!Simon

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 17, 2023, 10:01:03 pm
content: Do you need extremely fast response?Otherwise add a simple RC filter to remove these glitches, like 10K 100nF (1ms) or 10nF (100us).