How fast does an interrupted respond - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: December 30, 2014, 02:27:17 pm
content: I was tasked by one of my dad's friends to come up with a circuit that can drive his clock. I think it's called a synchronous clock but it's basically an old clock that used to be used in a fire station connected to many other clocks all in series with a single timepiece to control them generating a pulse which made all of the clocks tick together.My first attempt used logic gates and counters, at the time somebody on this forum very generously gave me part numbers and even the diagram required.Although that works fine I felt I could make a more compact version and as I was learning to program atmel microcontrollers in C I turned to a microcontroller solution. I used a clock crystal to clock the microcontroller and set up a program which ran a counter and every time the counter hit the top it would fire an interrupt and update a variable counter (a software counter) so that every time the number hit the final count a pulse would be generated to click clock and everything would reset. However much to my surprise although I worked it out very mathematically I found that the clock ran slightly slow. So I reduced one of the counter limits I forget if it was the one on the actual physical counter or the software counter but either way it put the time back in keeping. I never have figured out though why this happened. And I have not tested the clock and the circuit for enough time to establish if it will be stable over the long time. Is there perhaps a delay in a microcontroller responding to an interrupt so small that it is never of consequence in most practical uses but where timekeeping is required slowly builds up to an error. What I am now considering doing is going back to running the clock crystal on a logic gate as an oscillator on its own and connecting the output to an interrupted pin on the microcontroller so that the microcontroller can run at a much higher speed and will easily stay in sync with the slower clock. Was the problem before perhaps that I was using the same crystal as the system clock and as the timekeeping reference?

author: tggzzz
author_rank: Super Contributor
timestamp: December 30, 2014, 03:04:16 pm
content: Quote from: Simon on December 30, 2014, 02:27:17 pmConclusion: the clock input is not at the frequency you expect.what is the type of the resonator?what is its specified frequency range? (best expressed as +-ppm)how slow is your analogue clock, expressed in ppm?QuoteThat would get to the heart of the problem, provided you have a sufficiently accurate frequency counter. Don't forget that mains frequency can vary noticably from 50Hz but should be 50Hz on average over 24 hour period. Similarly note there is a time delay between the "o'clock pips" on FM and DAB radios, due to the decoding delay.QuoteYes.QuoteNo, unless the delay is so long that it prevents other processing occurring, e.g. missing the next interrupt. Highly unlikely in your case.QuoteThat would only be worthwhile if the new clock is more accurate and stable than the existing clock.QuoteNo.Fundamentally your derived clock will have the same frequency error as your system clock. There will be a little bit extra jitter, but that isn't relevant to your case.

author: langwadt
author_rank: Super Contributor
timestamp: December 30, 2014, 03:13:56 pm
content: an interrupt generally responds very fast, as long as it isn't waiting for a higher priority to finish etc.if I understand how you implemented it, the time it takes to get into an interrupt shouldn't add up to an error. The clock to you MCU might just be a bit slow

author: Simon
author_rank: Unknown
timestamp: December 30, 2014, 03:15:30 pm
content: I will go back to using a logic gate as an oscillator with the crystal and inputting it as an external interrupt and see if that is more accurate.

author: rob77
author_rank: Super Contributor
timestamp: December 30, 2014, 03:32:49 pm
content: i would rather add a RTC... or even more hardcore - add GPS receiver module and intterupt the MCU with the 1pps output of the GPS.

author: Simon
author_rank: Unknown
timestamp: December 30, 2014, 03:39:35 pm
content: its only to drive a crusty old clock for a one off

author: rob77
author_rank: Super Contributor
timestamp: December 30, 2014, 04:03:39 pm
content: Quote from: Simon on December 30, 2014, 03:39:35 pmeven thou... a small RTC is $3 - interface it with the MCU is very easy. and the time saved is priceless

author: Simon
author_rank: Unknown
timestamp: December 30, 2014, 04:07:03 pm
content: Well it's something I've had for over a year so I want to facet together and get it done this afternoon. I'm going to try again with the crystal clocking the microcontroller itself. I was working on a 192 count being equivalent to 6 seconds when the clock is pre-scaled by 1024. Changing the count to 191 was what seemed to fix the problem. If it doesn't work after a few days running I will run the crystal as a separate oscillator.

author: ElektroQuark
author_rank: Unknown
timestamp: December 30, 2014, 04:08:47 pm
content: Feed the micro with a 32.768 KHz crystal. Setup a counter interrupt and drive a pin with it.

author: tggzzz
author_rank: Super Contributor
timestamp: December 30, 2014, 04:44:10 pm
content: Quote from: tggzzz on December 30, 2014, 03:04:16 pmDong! It should bereducedjitter, since the divider inherently sums many input clocks to produce one output clock - which is an averaging operation."Jitter" which has a period greater than the divided ckock, won't be averaged out, of course. Such "jitter" is more usually called "drift".

author: tggzzz
author_rank: Super Contributor
timestamp: December 30, 2014, 04:46:47 pm
content: Quote from: EdoNork on December 30, 2014, 04:08:47 pmWhy bother with an MCU? Just use a 15 bit divider! I'm sure it would be possible to find a standard IC containing a 15 bit divider and an unbuffered inverted that can be used as a crystal oscillator.

author: Simon
author_rank: Unknown
timestamp: December 30, 2014, 05:54:02 pm
content: Well the microcontroller is the way I chose to go, it was part of me learning how to program and Atmel microcontroller and use interrupts on the timer. The idea was to be able to later change the program to get any timing steps I needed and I also have the small problem of needing to produce a pulse of the correct length to drive the clock mechanism. Two shorts and a clock mechanism does not actually take as it relies on a solenoid pulling a lever. Too long and I risk burning out the coil and my original plan was to make it battery-powered so control over the pulse was very important although batteries only last a couple of days so I don't think that is going to be a very good idea.I think I have realised the source of my error. Having reduced the crystal clock by 1024 it then requires 192 ticks to time a six second period however I was forgetting the zero it still takes a tick to go from 192 to 0 as the counter needs to overflow. So that was actually 193 ticks. When I made it 191 ticks I corrected it but I did not understand what the source of the problem was and I was unhappy with making an arbitrary correction without understanding why.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: December 30, 2014, 05:58:43 pm
content: If all you need is a regular pulse, and you're not doing anything else there's no need to use an interrupt - just poll the timer interrupt flag til it sets, then increment your count & clear the flag (ISTR you write a 1 to the flag to clear it on AVRs)

author: Simon
author_rank: Unknown
timestamp: December 30, 2014, 06:14:23 pm
content: Quote from: mikeselectricstuff on December 30, 2014, 05:58:43 pmWell be interrupted just made it easier. Either way the problem seems to have occurred because I did not take into account how the timer works and I inadvertently added an extra account which caused the delay which caused the clock to run slightly slower.The other thing is the pulse that I have to generate to drive the clock mechanism has to last for anything up to one second so actually I don't think I could do as you suggest in all circumstances although here possibly because the timer is running on a six second cycle. As things are at the moment it takes a fraction of a second to update the internal program counter variable from the main counter so it can also occur whilst the clock mechanism is being pulsed.I'm going to try and make the circuit up and see what happens.

author: nctnico
author_rank: Super Contributor
timestamp: December 30, 2014, 08:44:00 pm
content: Quote from: Simon on December 30, 2014, 05:54:02 pmIn these cases I usually toggle an IO pin from the (timer) interrupt and verify the frequency using a frequency counter.

author: Simon
author_rank: Unknown
timestamp: December 30, 2014, 09:07:09 pm
content: Well I am now struggling to talk to a chip with the programmer. Although I have the crystal clock running and I have a 1 V peak to peak clean sine wave the programmer refuses to talk to the chip whereas it did before when it was running on internal clock. This is getting to be rather a pain of a project.

author: Mr.B
author_rank: Unknown
timestamp: December 30, 2014, 10:04:23 pm
content: I have done this sort of thing a number of times in the past.I have found the only easy solution to be a RTC.I use the Maxim DS3231M.In the MCU, set the DS3231M alarm time.The DS3231M fires an interrupt on your MCU.Set the new alarm time to 1 minute more than the last time.Now your code has about 1 minute to do whatever you like.The clock pulse to the field devices can take say one whole second... It can even be blocking, it doesn't matter.Flash a heartbeat LED..Do whatever... Just have all operations completed in less than 1 minute when the interrupt will fire again.

author: Simon
author_rank: Unknown
timestamp: December 30, 2014, 10:09:02 pm
content: Well at the moment it's a challenge just to get the MCU to talk back to me.

author: Simon
author_rank: Unknown
timestamp: December 30, 2014, 10:55:04 pm
content: I just cut the crystal and the capacitors off and low and behold now it will talk to the programmer. So I think after careful consideration I'm going to be running the crystal on an external oscillator using a gate and interrupting the microcontroller. Thank God for internal clocks!

author: Psi
author_rank: Super Contributor
timestamp: December 30, 2014, 11:15:50 pm
content: Quote from: EdoNork on December 30, 2014, 04:08:47 pm+ 1 for this.Most micros have a frew special pins intended for use with a clock xtal.The main xtal drives the cpu and the clock xtal drives the hw timer.Of cause you can clock the main cpu from a clock xtal if you want, its just slow to execute things.If you know your xtal is out by 31 sec every month you can add code to skip counts every so often to compensate.Or buy a more accurate xtal.One thing you might want to watch though is the fact that 8bit micros with mixed length instruction cant do an interrupts in the middle of a 16/32bit instruction. They must wait for it to finish. So depending on what instruction is currently active at the time the int flag is set the interrupt delay might be one cycle or two cycles, 4 cycles etc... Because the 8 bit cpu has to clock multiple times to finish the extra long instruction.You can cheat by having nothing in void main at all, just the while(1) loop, so its only doing 8bit instructions when the interupt happens. But then you have to put your actual code in an interrupt handler to be run when you know you time critical handler wont be about to trigger. Eg right after you have delt with it.

author: Simon
author_rank: Unknown
timestamp: December 31, 2014, 09:23:12 am
content: Well unfortunately trying to clock the microcontroller straight from the clock Crystal has proved very unsuccessful. Even putting a new chip on the board which would be working on the internal 1 MHz clock proved unsuccessful. The programmer would not recognise the chip. But as soon as I cut the clock Crystal and its capacitors off the programmer was able to recognise the chip so I am at a bit of a loss as to how I select the crystal clock in the first place. The crystal need to be connected so that when I changed the fuse is clock source can switch over but with the crystal connected the chip won't work. The chip also would not work when I found a previous one that I had been using successfully that was already running on the crystal clock despite the crystal oscillator in which it would not work with the programmer. I'm using an attiny25v.If the system clock is running faster than the counter clock then the delay that would occur with a 32 bit instruction should not be a problem. Because providing each interrupted is responded to on average there will be the correct amount of pulses, for example if it takes a couple of clocks every now and then to enact the interrupts that is not a problem providing it is completed before the next interrupt is received.I'm going to crack on with it again today and I will use a logic gate with the crystal to create an oscillator and interrupts one of the microcontrollers interrupted pins. That seems to be the most robust way of doing it at the moment. The chip will be working at a far higher frequency than the crystal so will be able to cope with ease with carrying out instructions between each interrupted. I will probably also as a button or 2 to tell the program to generate an extra pulse to the clock or to delay the next pulse so that the time can be corrected from time to time.

author: bobcat
author_rank: Regular Contributor
timestamp: December 31, 2014, 12:16:09 pm
content: Two things:First, make sure that the clock speed setting on your programmer is set to less than 1/4 the clock speed on the microcontroller. The programmers are sensitive to microcontroller clock speeds. I usually start with a very low number when there is a connect problem.Second, in avr-c there is lot of overhead when entering and leaving an interrupt, pushing registers and such. That will affect your timing.

author: Simon
author_rank: Unknown
timestamp: December 31, 2014, 12:40:47 pm
content: I was working down to the lowest speeds the programmers support which was way below 32 KHz, I think the ICE was working at a little over 2 KHz

author: Mr Smiley
author_rank: Frequent Contributor
timestamp: January 01, 2015, 08:54:37 pm
content: Simple solution.Go down to the £ shop and get a battery operated clock.Rip out the guts, you now have a very accurate 1second pulse.Pulse clocks normally pulse every 30 seconds.Feed that 1 second clock into a divide by 30, feed the output of the divide by 30 into a monostable for your fixed pulse width.*4 C cells should last you over a year.

author: Simon
author_rank: Unknown
timestamp: January 01, 2015, 09:23:21 pm
content: Too late and I'm not resorting to that sort of shabby trick.Part of the idea was a learning exercise, I have now got this to work by using a not gate with the crystal to produce an oscillator which interrupts the microcontroller which effectively acts as the counter. The good thing being I will be able to fine tune that counter to any number I want with the accuracy of the 32 kHz crystal pulses. The clock takes nearly 300 mA so even with short pulses of one second or half a second the amount of current required from batteries mounts up quickly. I am using a 5 V wall plug to run it.

author: Mr Smiley
author_rank: Frequent Contributor
timestamp: January 02, 2015, 05:04:30 am
content: Quote from: Simon on January 01, 2015, 09:23:21 pmWhich clock, the £1 mechanical wall/alarm clock ?A 32khz oven oscillator is a bit overkill hereAlso, what are you going to do if you have a power failure or someone wants to use the mains socket for a moment or two. The clock stops.Now with a normal clock that wouldn't be a problem, you just reset the hands with the dial on the back. BUT the pulse clock has no adjustment on the back, you either pulse it manually from a battery or an option on your circuit; or you wait 12 hours for the time to come back round to the hand settings on the clock. ( oh the fun you have with UK summertime changes )

author: Simon
author_rank: Unknown
timestamp: January 02, 2015, 07:11:59 am
content: Well in this case I'm looking to get the damn thing sorted and gone. I can't use batteries I've tried, you will have a bigger problem changing batteries every 2-3 days unless I have dual battery holders and make a monstrosity. I could add some buttons to skip or advance a click

author: Mr Smiley
author_rank: Frequent Contributor
timestamp: January 02, 2015, 04:47:04 pm
content: http://www.vintage-radio.net/forum/showthread.php?t=26040&page=2post No26http://www.bmumford.com/clocks/driver/driver.htmlhttp://www.ebay.co.uk/itm/Slave-Clock-Impulser-run-your-clock-w-original-mech-/201253998828?pt=LH_DefaultDomain_0&hash=item2edbac50ecI have one of thesehttp://www.hvtesla.com/masters/gents_intro.htmlThe vid is'nt mine.Picked it up at a car boot a few years ago for a £5, guy didn't know what he hadThe only problem is that they have to be bolted to a solid wall with *6, 1/2" bolts because the pendulum is so heavy and loaded with lead shot that any movement of the case stops the clock after a few hours. Also you can hear the clunk through the walls every 30 seconds. They are a joy to behold, and they do make the sounds as in the video.It drives a clock in every room and i have it down to 1s/month

author: Mr Smiley
author_rank: Frequent Contributor
timestamp: January 02, 2015, 05:01:11 pm
content: Just had another quick and dirty idea.That mechanical clock (small travel alarm clock) from the £ shop. They are very accurate. Get one with a second hand, put an opto at the 12 and 6 o'clock positions and every time the second hand cuts the beam, pulse the clock.If your still going the micro way, and powering from the mains, use 50hz, rectify it, square it and use it as your clock into your counter. If i remember there is a legal requirement that the mains, even though the frequency varies, has to have a zero frequency error over a set period. That's why synchronous ac clocks are so accurate here in the UK

author: Mr Smiley
author_rank: Frequent Contributor
timestamp: January 02, 2015, 05:42:57 pm
content: http://www.palebluedot.nl/jml/projects/electronics/29-mains-frequency-clock-time-base

author: Simon
author_rank: Unknown
timestamp: January 02, 2015, 07:24:21 pm
content: Quote from: Mr Smiley on January 02, 2015, 05:42:57 pmThat is pretty much what I have done. Although I have used a microcontroller to eliminate a lot of circuitry and give me much flexibility should I need it. Clock is ticking away now I will just have to see if it's keeping time if it does not I can fine tune it to an accuracy of 1/32768 of a second. And if that does not work I can add a fudge factor to the programme.My setup is run off of a 5 V adapter one of a box of about 100 I bought on a car boot sale and have a few of left after selling most of them. It powers a not logic gate chip and a microcontroller and of course the clock via a MOSFET switch. The logic gate based oscillator and the microcontroller are behind a diode and bulk capacitor so that they are not affected by the surge in power to the clock which takes up to 270 mA from the 300 mA power adapter. I don't think the mechanism was meant to work on 5 V but main power is delivered to it through a 1 ohm resistor which keeps a 1000 micro farad capacitor topped up on standby ready to supply power to the MOSFET and the clock. A half second pulse seems to be doing it but it has only been running for a few minutes. I think there may be a bit of a problem with the spring inside as last time I had it running it would keep time as the hand came down the left-hand side but then it would skip a check when it had to climb up the right side. Again this is something I can compensate in software if necessary over time.I feel is a big advantage to using the microcontroller is that if my dad's friend rings me up and says that it is going a bit fast or a bit slow and he can tell me by how much in a day or a week then it should be a small order to calculate the required adjustment and pop over there with my laptop and programmer and reflash the microcontroller.