Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Zero crossing detector in PIC16F18856

Status
Not open for further replies.
On the diagram on page 315 of micro datasheet, there appear to be two current sources which act sop as to regulate the ZCD pin to 0.75V. Is this true?

Presumably at the zero crossing, the current sources cannot regulate the ZCD pin to 0.75V, and therefore it drops below 0.75V, and then the comparator trips to flag up the zero crossing…that is the modus operandi?
 

Hello,

Please can you confirm that the Zero Cross Detector inside the PIC16F18856 can only work with a mains input (as in the attached schematic and LTspice simulation) if there is also a resistor to ground from the ZCDxIN pin? (R5 in the attached schematic).

If the resistor to ground is absent, then the current source internal to the PIC16F18856 will not operate, and therefore zero cross will not be detected….-[Both current source and current sink inside the micro must operate (alternately) in order for zero cross detection to happen].

Section 21.2 of page 316 of the PIC16F18856 datasheet makes it clear that the zero cross detection needs both the internal current source, and also the internal current sink, inside the micro, to operate alternately. If one never operates then there will be no zero cross detection.
As such, the resistor to ground (R5 in the attached schematic, is necessary for zero cross detection).
Do you agree?

The schematic (page 315) in the PIC16F18856 datasheet shows a situation where the AC source and the microcontroller are both referenced to the same “ground”. But with the AC Mains setup of the attached schematic and simulation, this situation is not prevalent…and therefore the resistor to ground is needed (ie from ZCDxIN pin to ground)…do you agree?

PIC16F18856 datasheet
**broken link removed**
 

Attachments

  • ZCD on PIC16F18856.txt
    3.8 KB · Views: 76
  • ZCD in PIC16F18856.jpg
    ZCD in PIC16F18856.jpg
    69.4 KB · Views: 93

I've never used that device but from the data sheet it seems to behave rather like window comparator with the thresholds set by the gain of the internal current source amplifier. It's own feedback tries to maintain 0.75v on the sense pin but given sufficient 'push or pull' from outside, the feedback is overcome and the comparator 'trips' causing an interrupt. The external series resistor value is chosen to control how much influence the AC has on the 0.75V and hence it's sensitivity.

The schematic (page 315) in the PIC16F18856 datasheet shows a situation where the AC source and the microcontroller are both referenced to the same “ground”. But with the AC Mains setup of the attached schematic and simulation, this situation is not prevalent…and therefore the resistor to ground is needed (ie from ZCDxIN pin to ground)…do you agree?

All the pull-up and pull-down resistors do is add a degree of preference to tripping in one polarity. I'm not sure they are essential but that depends on the needs of your application.

Brian.
 

Thanks, the datasheet is saying that the microcontroller actually detects current flow from its internal current sink and current source. As such, it is current detection which is done to detect Zero cross. When the micro's current sink is operating , the OUT bit of the ZXDxCON register is set, and when the current sourse is operating , the OUT bit of the ZXDxCON register bit is cleared. So what needs to be done, is to arrange for the AC waveform to alternately set and clear the OUT bit...then you can get zero cross detection.

With the schematic of post#3 above, the only way to get the current source and sink to work alternately (so as to get Zero cross detection), is to add the resistor to ground.

The problem is, the datasheet doesnt say what is the threshold for current detection inside the microcontroller. Do you know?

The LTspice simulation of the zero cross detect circuit in post#3 shows how the zcd circuit in the micro works. The ZXDxIN pin is always regulated to 0.75V.

- - - Updated - - -

The datasheet seems to suggest that a series capacitor is needed. However, the datasheet doesn’t state whether or not its ok to just avoid the capacitor and just get the current source and sink to alternately operate, and then kind of “calculate” where the ZX occurs….(some point in between the changing of the state of the OUT bit of the ZXDxCON register.)

The problem is, if only resistors are used, then you get this state change of the OUT bit, but how out of phase is that with the actual mains zero crosses?
-If we don’t know the threshold of current detection, then we cant know when the zero cross occurs?
 
Last edited by a moderator:

If the current source doesnt get activated, then the OUT bit of the ZXDxCON register never gets cleared, and thus zero cross would never be detected. Therefore R5 is needed in the top post schematic.
The problem is, the datasheet doesnt say what current has to flow from the internal current source, or to the internal current sink, in order for current flow to be acknowledged by the micro.
That is, the PIC18F18856 datasheet doesnt tell what is the detection threshold of current. Do you know?

Even microchips app note on this, fails to say what is the current detection threshold...
https://ww1.microchip.com/downloads/en/AppNotes/90003138A.pdf
 

The formula for working out the resistor value is on the data sheet.

The reason the simulation needs the resistor is that you are not feeding AC into the circuit. In fact you are only feeding it half AC cycles because D5 is directly across the input. If fed with true AC it shouldn't need the resistor. What R5 does is provide a false negative (below 0.75V) which is then overcome by the current through R1 & R2 to make it go above 0.75V and be seen as AC.

Brian.
 

Thanks, yes i agree...As you know we wouldnt feed it AC as its easier to do it like weve done it there......We should still be able to get the OUT bit to set and clear...however, no matter how its done, we do need to know the thresholds.
I assume it needs to source/sink at least 100uA to "know" that it is actually sourcing/sinking current....since it says that as voltage peak varies, you should ensure the current always peaks up to at least 100uA.
Their given formula is for a single peak value, but we are addressing a range of mains peak voltages as our mains varies from 210VAC to 285VAC.
Even worse, we have transients above that and it says 600uA is the max, but doesnt say what happens if in short term it increases above that.

It would be good if the datasheet gave the minimum current source/sink value peak for current detection.....this is needed to see when in the sine half wave the OUT bit actually sets/clears...which with a sine wave tells how out of phase the setting/clearing of the OUT bit is with the actual mains zero crossings.

It seems to me that this zero crossing detector was made for square waves.

There is a microchip app note on this but it isnt that helpful....(not slating microchip, who i think generally are the best at app notes)
 

We are trying to do Zero Cross detection with the PIC16F18856. The schematic and LTspice sim of what we are doing is as attached.


OUT bit:
Section 21.2, page 316 of the PIC16F18856 datasheet clearly states that zero cross detection is gotten from the setting and clearing of the OUT bit. Section 21.2 states that the OUT bit is set when the current sink is active, and cleared when the current source is active. The problem is that it doesn’t say at what current threshold the sink/source is considered to be “active”.

Square wave:
If the waveform being checked for zero cross is a square wave, then the zero cross circuitry is obviously going to work. If a square wave, then the OUT bit is obviously going to set/clear right at the zero crossing. However, if its a sine wave, then we don’t know where on the rising sine wave the current is going to be great enough for the source or sink to be regarded as “active”. As such, we cant work out where exactly will be the zero crossing.

Detecting zero current:
We could pretend to ourselves that the micro will faithfully detect the exact zero of current. However, if it was capable of doing this, then why does it need the peak of the current to be 300uA? Also, concerning section 21.6, page 318 of the PIC16F18856 datasheet, why, if it is capable of detecting zero current, does it need the peak current to be at least 100uA high?
Does this mean that for the detection of current in the current sink or source, at least 100uA of current flow is needed? –If not, then why is there this 100uA minimum figure?

If the micro can detect the zero of current, then being sensitive to that low level of current would mean that it could easily be messed up by noise…chattering of the OUT bit. Microchip’s TB3138 document recommends a capacitor at the ZCDxIN pin to mitigate noise, but the datasheet makes no mention of this.
So if the ZCD circuitry in the micro can really detect zero current, then is it sub 1uA that it detects as zero Amps?, or sub 2uA?, …or sub 3uA……?.....And if it really is sensitive enough to be able to detect these low levels, then it would be very easily triggered by noise…specially with a signal originating from the mains like in our case.

Sine waves:
This is all relevant for sine waves, because if the detection of current is at some point above zero amps, then how far above zero amps is the detection threshold?.....because with a sine wave, if the current detection point is relatively high, then the OUT bit setting/clearing may well be a significant time interval away from the zero crossing…..and so we will not be accurately pinpointing the zero crossing. All this confusion never arises with bipolar square wave zero crossing…because a square wave immediately rises to its peak at the zero crossing. It is my impression that the ZCD circuit in the PIC16F18856 was designed for bipolar square waves. The datasheet fails to elucidate on the required parameters needed to properly use this ZCD circuit with sine waves. We actually have a microcontroller with nothing more than an “RSERIES” resistor connecting to the ZCDxIN pin and use it with sine waves as in the attached schematic. (ltspice simulation also included). The circuit appears to be working. However, it may be just that we’ve been lucky. –Maybe we have been narrowly operating at the edge of a “tolerance cliff” and are just lucky to not have gone over it……maybe with a different batch of microcontrollers we would not be so lucky, as maybe they would not be able to so closely detect the zero current point. As you can see, our current circuit is interfaced to the mains which is at the other side of a bridge rectifier, so for us, the current source barely operates at all….(it only sources some 2uA). According to section 21.2, page 316 of the PIC16F18556 datasheet, the OUT bit needs the current source to operate in order to be able to get cleared. For our situation, the ZCD circuitry would need to be able to detect 2uA of current flow in order to be able to clear the OUT bit. –Maybe the batchs of microcontrollers that we have so far used have been able to do this, but it is obviously going to be suspected that other batches will not be able to do this….and so we are running the risk that the ZCD circuit will not work.

Contradiction 1:
Another problem is that Microchip’s “Technical Brief” document (TB3138) contradicts the ZCD section of the PIC16F18856 datasheet. TB3138, on page 1, states that the peak current that can be sourced/sinked is 300uA. Wheras the PIC16F18556 datasheet states that this figure is 600uA. Which is it? We need to know because our mains varies from 210VAC to 265VAC, and we need to be sure that at high mains we don’t go above the maximum allowable current.

Contradiction 2:
Also, there is another contradiction…… the datasheet specifically states that it is current flow from the source that clears the OUT bit, and current flow into the sink that sets the OUT bit.
However, TB3138, on page 6, specifically states that the OUT bit changes state when the current into the ZCDxIN pin is zero….Current into the ZXDxIN pin can become zero if only one of the current source or current sink operates. This is a contradiction. According to the TB3138, only the current source or the sink need to be monitored..not both. –This is not in accordance with the datasheet.

Sine wave reference point:
All of the discussion in the datasheet and the TB3138 document concerns detection of zero cross in sine waves that are referenced to the same ground as the microcontroller. In such a case, the current source and sink would obviously alternately operate….and provide similar waveforms in the current source/sink. This is not our application case, because our microcontroller is not referenced to the same ground as the mains, and the mains is at the other side of the diode bridge. The datasheet/TB3138 document makes no mention of our application case.

I don’t think we can be sure of reliable operation of the ZCD circuitry in the micro, and therefore believe we should revert to using a anti-parallel diode optocoupler circuit instead.

The attached simulation shows our application case in the LTspice simulator.
(please note i believe microchip are usually the best at explaining tech points)

TB3138 app note on ZCD
https://ww1.microchip.com/downloads/en/AppNotes/90003138A.pdf

PIC16F18856 datasheet (page 314- discusses ZCD)
https://ww1.microchip.com/downloads/en/DeviceDoc/40001824B.pdf
 

Attachments

  • ZCD on PIC16F18856.txt
    3.4 KB · Views: 46
  • ZCD schematic.jpg
    ZCD schematic.jpg
    67.1 KB · Views: 69

These are all points you should ask Microchip, their tech guys are very helpful and they inevitably have more experience than readers here.

If you are actually doing what your schematic suggests and connecting VSS to the negative side of the bridge there is no reason why you can't just use the presence of a positive voltage to detect zero crossing. The simplest way to do this is to use a single resistor (or more in series to build enough voltage rating) of say 1M between the rectified positive and almost any input pin. There is no need to check for a negative AC half cycle, you can assume it by the absence of a positive cycle and the high resistance will limit the current to the input pin with internal protection diodes preventing over-voltage.

True, it doesn't exactly detect zero, it will detect approximately half VDD instead but you can easily work out the error angle, it is constant and equal to the point where the sine passes half VDD. I would anticipate that to be very small, for example 240V RMS and VDD of 3.3V would give an angle of approximately 0.25 degrees which you could either ignore or simply factor out of the result. All you do is look for the state of the input pin changing, each change will be at zero crossing.

For an even more precise measurement, use a PIC with built in comparators and program the threshold to the lowest practical voltage.

Brian.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Thanks, i fear though that the method of allowing conduction through the esd diodes of the micro is ruled out by microchip now...as stated on page 1 of the following....

TB3138
https://ww1.microchip.com/downloads/en/AppNotes/90003138A.pdf

...Apparently the newer micros have circuitry which means that allowing flow of current through the esd diodes is not permitted any longer.
 

I think that only applies to pins that can be used as analog inputs but regardless, adding a small Schottky diode (BAT85 for example) between the pin and VDD will make it safe. The negative half cycle will be removed by the bridge rectifier so it isn't necessary to protect against voltages below VSS.

You can either poll the port bit, looking for change or use the external interrupts or 'interrupt on change' to find zero crossing. Try to keep capacitance as small as possible across the input pin to VSS or alternatively factor in the phase shift a known capacitance would introduce.

Brian.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Fig 21.2 on page 315 of the PIC16F18856 datasheet shows optional externally attached pull-up and pull-down resistors.
Please can you confirm that in our setup, with AC mains, the pullup resistor (pulling up to Vdd) would never be needed?
This is because the pullup resistor would simply bring the current source into action, and that would be pointless, because the high-going half cycle of the AC mains will already have brought the current source into action (the “current source” being the current source inside the PIC16f18856).

Schematic and LTspice simulation of our setup is as attached.

PIC16F18856 datasheet:
https://ww1.microchip.com/downloads/en/DeviceDoc/40001824B.pdf
 

Attachments

  • ZCD on PIC16F18856.txt
    3.6 KB · Views: 42
  • ZCD schematic.jpg
    ZCD schematic.jpg
    70.5 KB · Views: 83

Correct.
The ZCD in the PIC expects to alternately sink or source current but you are only feeding it half cycles of positive polarity so it can sink but has nothing to source to except ground. In your configuration the pull-down IS needed to make the input pin think it is seeing a negative half cycle by sinking some of the 0.75V from the comparator to ground and the current through R1 & R2 brings it up above 0.75V to indicate a positive half cycle. The pull towards positive should come only from R1 & R2 so no pull-up should be fitted.

I go back to my previous posts, if you only have positive half cycles to detect anyway, why are you using a circuit that detects both polarities?

Brian.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
I go back to my previous posts, if you only have positive half cycles to detect anyway, why are you using a circuit that detects both polarities?
Thanks.
Section 21.2 of the PIC16F18856 datasheet (page 316) states that the OUT bit is cleared by the internal current souce operating, and set by the current sink operating.
As such, both the current source and current sink must be brought into play to detect zero cross.
The OUT bit is what the micro “sees” so as to detect zero cross.
 

It WOULD see the internal source operating because the pull-down resistor would sink current from 0.75V to VSS.

Have you considered another alternative: That PIC has internal comparators and a precision voltage reference module. If you program the reference to 1.024V and route it to the comparator, you get a simple "greater than/less than" 1.024V output. It can even be used to trigger an interrupt as the threshold is crossed.

I use a different PIC using the reference and comparator method to analyze AC coupled audio tones.

Brian.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
It WOULD see the internal source operating because the pull-down resistor would sink current from 0.75V to VSS.
Thanks, we are talking at cross purposes.... i agree with that entirely

- - - Updated - - -

Have you considered another alternative: That PIC has internal comparators and a precision voltage reference module. If you program the reference to 1.024V and route it to the comparator, you get a simple "greater than/less than" 1.024V output. It can even be used to trigger an interrupt as the threshold is crossed.
Thanks, thats one for the future....yes, the thing is, the current source setup for ZCD is good because it avoids having conduction through the ESD diode of the PIC (during HV transients on the mains).....I know ATMEL say its ok if this is kept below 1mA, but then its not even on the table when you have the current source.

- - - Updated - - -

i know we can add an external sSchottky to prevent esd diode conduction, but how would we know if the esd diode might have a lower vf than the Schottky
 

I picked BAT85 (or it's smd equivalent) because at the currents you would be asking it to pass, it's Vf would be less than 0.3V. The ESD diode or other junction behind the pin would be in normal silicon and have a Vf of around twice that. Bear in mind it isn't the current flowing into the pin that causes damage (within reason) it's the susceptibility to latch-up that presents the more serious problem. The Schottky diode trick also gives it protection from line AC voltage before the PIC supply has stabilized.

Brian.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top