EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: juvann on November 19, 2022, 04:16:01 pm

Title: Clock monitor and switch automatically when the primary is missing
Post by: juvann on November 19, 2022, 04:16:01 pm
I have a microcontroller atmega 328 which can receive 2 clocks:
1. from this (https://www.eevblog.com/forum/beginners/sine-wave-to-square-wave-input-about-14mhz-10vpp-and-output-05v/?action=dlattach;attach=1616122;image) circuit (primary clock) about 14MHz
2. from a square wave generated by a pic16f PWM (secondary clock) about 6MHz.

Primary clock may be missing, so the second clock should start running.
I tried to develope a software solution on pic16f, but the frequency is too high (about 14MHz) for count pulses, so it's not work very well.
I hope to find a hardware solution.
My question is: can I switch automatically/manually the clock when the primary clock is missing with a circuit or some IC?
I tried to see flip-flop but I don't understand as to use them.

Thank you for suggestions.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: ataradov on November 19, 2022, 06:13:23 pm
Do you actually need to detect that the frequency is 14 MHz? Why not just detect that there is an edge transition and if there is not one in some time, enable the secondary clock.

The easiest fixed component solution may be to use a multiplexer IC and use an RC filter to filter 14 MHz clock and use that as a selector pin for the mux.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: juvann on November 19, 2022, 07:14:12 pm
Do you actually need to detect that the frequency is 14 MHz?
No, it's only important understand if is active or no. If is not active, from pic16f active secondary clock generated with a PWM.

Why not just detect that there is an edge transition and if there is not one in some time, enable the secondary clock.
I tried to connect the external clock to a PIC16F1455 pin T1CKI, so I can count pulses with Timer1, but is not stable, sometimes it's not detect clock while is active. I configured for overflow every about 1uS, 8 pulses before 16 bit timer, but not always go in overflow, so when it don't go in overflow I active pwm, but is wrong because sometimes pwm is active while the primary clock is active, so I would like test a hardware solution.

The easiest fixed component solution may be to use a multiplexer IC and use an RC filter to filter 14 MHz clock and use that as a selector pin for the mux.
Have you some examples to see?
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: ataradov on November 19, 2022, 07:51:50 pm
I tried to connect the external clock to a PIC16F1455 pin T1CKI, so I can count pulses with Timer1
Don't do counting. Just use edge detection interrupt. It has to detect at least some edges.

Have you some examples to see?
Here is a quick and dirty concept https://tinyurl.com/2n4xumcb (falstad simulation). I bet this can be improved with a bit of though.

But also I'm a bit confused. How do you switch the clocks in your case? Do they just go to different pins or something?

In any case, I personally would work with a digital solution on the PIC side, it would be better in all possible ways.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: juvann on November 19, 2022, 08:20:40 pm
But also I'm a bit confused. How do you switch the clocks in your case? Do they just go to different pins or something?

This my actual situation:
(https://www.eevblog.com/forum/beginners/clock-monitor-and-switch-automatically-when-the-primary-is-missing/?action=dlattach;attach=1643471;image)
if I detect external clock I switch off pwm on pic16f and set pin low, so external clock on OR port win.
I would like test your solution, so I can replace OR port.
Could you suggest me a name of IC ? (EDIT) 74HC157 should be good. Rigth?

In any case, I personally would work with a digital solution on the PIC side, it would be better in all possible ways.
Sure I tring to use timer0 which should be more fast and edge detection interrupt.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: ataradov on November 19, 2022, 08:52:43 pm
Could you suggest me a name of IC ? (EDIT) 74HC157 should be good. Rigth?
Yes, but there are single gate versions, you don't need 4.

Sure I tring to use timer0 which should be more fast and edge detection interrupt.
No, it is not. Timer has to count, which involves a lot of logic. Edge detect is simple and generally would be more reliable than a counter.

And even if this pic can't handle 14 MHz input at all, it is still better to use external clock divider for detection than my "analog" switch. If you are going to be adding more ICs into the mix, then adding some chained D triggers would be much better.

But I feel like your whole thing is sub-optimal. You are already involving PIC, AVR and random ICs.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: juvann on November 19, 2022, 09:05:28 pm
But I feel like your whole thing is sub-optimal. You are already involving PIC, AVR and random ICs.
Sure my project is not very good. I would like switch a solution with only one microcontroller.
It was born as test project and is growing in a bad way.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: juvann on November 20, 2022, 10:23:42 am
I tested solution with Timer0 (can count 14MHz with prescaler 1:2 without issue) and interrupt rising edge both works, so I can control switch clock in digital mode from PIC.
I will test also you analog solution.

random ICs.
Is it possible remove OR port? Have you some suggestions for improve the connection of 2 clocks?
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: ledtester on November 20, 2022, 10:55:04 am
Perhaps instead of the OR gate you should use an XOR gate in case the 14 MHz clock gets stuck outputting a 1.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: ataradov on November 20, 2022, 06:04:34 pm
Is it possible remove OR port? Have you some suggestions for improve the connection of 2 clocks?
The only easy thing that comes to mind is to use a buffer IC with an output enable pin and at least 2 buffers per package. This was the first stage could be always enabled and provide the output for detection and the second stage could be controlled by the PIC. This way PIC could disable the other output before enabling its own and disable its own before enabling the other one. And you can use wired-OR of the outputs, since you are guaranteed that one one is active at the same time.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: juvann on November 21, 2022, 08:25:49 pm
The only easy thing that comes to mind is to use a buffer IC with an output enable pin and at least 2 buffers per package. This was the first stage could be always enabled and provide the output for detection and the second stage could be controlled by the PIC. This way PIC could disable the other output before enabling its own and disable its own before enabling the other one. And you can use wired-OR of the outputs, since you are guaranteed that one one is active at the same time.
Have you an example? Multiplexer IC with selection from PIC instead of RC filter, is not good?
I'm testing the solution software and sometimes some bytes are corrupted in AVR SRAM, so OR gate is a bad idea. I think sometimes there is an overclock on AVR when both clock are active and maybe SRAM don't work correctly.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: ataradov on November 21, 2022, 08:41:54 pm
Glitch-free clock switching is not easy. At the same time I would not expect this reaction from AVR, so I would investigate this further.

Or gate or any simple switches would not work because at the switch over time you can't guarantee that period would not be too short.

Can you describe the full system? I feel like you are doing something really strange the solution is to do something entirely different.

The way I see it, it would be so much easier to use an MCU with a PLL. Take the 14 MHz as an input and pass it through the PLL. If the reference signal is missing, PLL would still continue to generate reasonable clock in an open loop mode. I predict that something like SAMD11 would be able to replace both PIC and AVR here.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: Swake on November 22, 2022, 07:21:24 am
Can't the atmega detect if the clock is or not present on its own? And for example start using a simple x-tal from another pin if it fails to detect it or that other 6Mhz signal
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: ataradov on November 22, 2022, 07:24:38 am
Not really. If external clock is selected, it would expect external clock. This is pretty old stuff. That's why you need to use a whole lot of ICs to achieve the same result as a single modern MCU would to for a fraction of a price.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: Swake on November 22, 2022, 07:30:25 am
Is it the clock for the uC itself or is it the clock of some signal? (I was in the assumption of the second)
If it is the clock for the uC itself then this is about redundancy/component failure detection. And that is not something to be solved with just another signal as anything else can fail too.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: ataradov on November 22, 2022, 07:33:08 am
I would assume it is the clock to the MCU. But the whole thing is bizarre. I assume there is a simpler solution if we knew what is actually going on.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: Swake on November 22, 2022, 07:39:30 am
Yes, you are right, the unknown is to large to allow equation.
 @juvann please provide some more context. Also tell us if the clock is expected to fail as normal high or low.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: juvann on January 19, 2023, 08:15:17 pm
Sorry for the delay, I abandoned the project.
I tried to implement a switch clock glitch free with flip-flop (not automatic switch), suggested by Altera, here (https://tinyurl.com/2kq6bo9m) a simulation, I haven't tried it.
I report it, if a day will be useful to someone.
Title: Re: Clock monitor and switch automatically when the primary is missing
Post by: mvs on January 19, 2023, 09:18:57 pm
ATmega is not designed for an external clock switch. Datasheet limits cycle-by-cycle frequency variation to just 2%.

Quote
When applying an external clock, it is required to avoid sudden changes in the applied clock frequency to ensure stable
operation of the MCU. A variation in frequency of more than 2% from one clock cycle to the next can lead to unpredictable
behavior. If changes of more than 2% is required, ensure that the MCU is kept in reset during the changes.