Clock monitor and switch automatically when the primary is missing - Page 1
Forum section: electronics / beginners

author: juvann
author_rank: Contributor
timestamp: November 19, 2022, 04:16:01 pm
content: I have a microcontroller atmega 328 which can receive 2 clocks:1. fromthiscircuit (primary clock) about 14MHz2. 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.

author: ataradov
author_rank: Super Contributor
timestamp: November 19, 2022, 06:13:23 pm
content: 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.

author: juvann
author_rank: Contributor
timestamp: November 19, 2022, 07:14:12 pm
content: Quote from: ataradov on November 19, 2022, 06:13:23 pmNo, it's only important understand if is active or no. If is not active, from pic16f active secondary clock generated with a PWM.Quote from: ataradov on November 19, 2022, 06:13:23 pmI 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.Quote from: ataradov on November 19, 2022, 06:13:23 pmHave you some examples to see?

author: ataradov
author_rank: Super Contributor
timestamp: November 19, 2022, 07:51:50 pm
content: Quote from: juvann on November 19, 2022, 07:14:12 pmDon't do counting. Just use edge detection interrupt. It has to detect at least some edges.Quote from: juvann on November 19, 2022, 07:14:12 pmHere is a quick and dirty concepthttps://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.

author: juvann
author_rank: Contributor
timestamp: November 19, 2022, 08:20:40 pm
content: Quote from: ataradov on November 19, 2022, 07:51:50 pmThis my actual situation: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?Quote from: ataradov on November 19, 2022, 07:51:50 pmSure I tring to use timer0 which should be more fast and edge detection interrupt.

author: ataradov
author_rank: Super Contributor
timestamp: November 19, 2022, 08:52:43 pm
content: Quote from: juvann on November 19, 2022, 08:20:40 pmYes, but there are single gate versions, you don't need 4.Quote from: juvann on November 19, 2022, 08:20:40 pmNo, 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.

author: juvann
author_rank: Contributor
timestamp: November 19, 2022, 09:05:28 pm
content: Quote from: ataradov on November 19, 2022, 08:52:43 pmSure 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.

author: juvann
author_rank: Contributor
timestamp: November 20, 2022, 10:23:42 am
content: 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.Quote from: ataradov on November 19, 2022, 08:52:43 pmIs it possible remove OR port? Have you some suggestions for improve the connection of 2 clocks?

author: ledtester
author_rank: Super Contributor
timestamp: November 20, 2022, 10:55:04 am
content: Perhaps instead of the OR gate you should use an XOR gate in case the 14 MHz clock gets stuck outputting a 1.

author: ataradov
author_rank: Super Contributor
timestamp: November 20, 2022, 06:04:34 pm
content: Quote from: juvann on November 20, 2022, 10:23:42 amThe 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.

author: juvann
author_rank: Contributor
timestamp: November 21, 2022, 08:25:49 pm
content: Quote from: ataradov on November 20, 2022, 06:04:34 pmHave 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.

author: ataradov
author_rank: Super Contributor
timestamp: November 21, 2022, 08:41:54 pm
content: 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.

author: Swake
author_rank: Frequent Contributor
timestamp: November 22, 2022, 07:21:24 am
content: 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

author: ataradov
author_rank: Super Contributor
timestamp: November 22, 2022, 07:24:38 am
content: 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.

author: Swake
author_rank: Frequent Contributor
timestamp: November 22, 2022, 07:30:25 am
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: November 22, 2022, 07:33:08 am
content: 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.

author: Swake
author_rank: Frequent Contributor
timestamp: November 22, 2022, 07:39:30 am
content: 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.

author: juvann
author_rank: Contributor
timestamp: January 19, 2023, 08:15:17 pm
content: 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,herea simulation, I haven't tried it.I report it, if a day will be useful to someone.

author: mvs
author_rank: Frequent Contributor
timestamp: January 19, 2023, 09:18:57 pm
content: ATmega is not designed for an external clock switch. Datasheet limits cycle-by-cycle frequency variation to just 2%.Quote