Saturday, April 18, 2020

0000 0000 0010 1011

1MHz active crystal timer

I've been making stupid electronic clocks for a couple of years now, and they all work fine - but looking around for improvements (!?) I'm thinking about ditching the external DS3231 RTC module and maybe going it alone with an AVR humming along with an external clock signal provided by a crystal.

Cutting down on parts (maybe...)

I've obtained a few different crystal/oscillator types which I will look at over the next few blogs, but I might start with a 1MHz unit which arrived a few days ago.

Unfortunately I couldn't find a great deal of useful information about its use, so I just plugged it in according to datasheets of similar products and hooked up my god-awful DSO138, forgetting (again) that it has a limitation of 200kHz.

Following the same protocols as per a previous blog, I was able to hook up a couple of CD4017 decade counters and using them as frequency dividers I found that the oscillators do indeed kick over at 1MHz. The dividers were chained together and when around 9V is supplied they reduce the oscillator's signal by 10x10 to give 10kHz - which the DSO138 can measure.



Bang on 10.000kHz - nice one unbranded oscillator

Now while it is great to have a reliable clock signal, the real fun would be hooking it up as an external timer for an AVR such as the ATTiny85 and then...er...blink an LED?

Firstly, as I'm using the ATTiny85 I'll need a suitable core to bolt onto the Arduino IDE. So off to Spence Konde's (aka Dr. Azzy) github resource to procure ATTinyCore. There is no 1MHz external clock option in the list - so firing up a text editor I added an entry in boards.txt as follows:


Then I'll need some simple reliable code which makes for pretty lights via an RGB breakout board:


#define BLED PB2
#define RLED PB1
#define GLED PB0

void setup() {

pinMode(BLED, OUTPUT);
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);

}

void loop() {

digitalWrite(BLED, HIGH);
delay(1000);
digitalWrite(BLED, LOW);
digitalWrite(RLED,HIGH);
delay(1000);
digitalWrite(RLED, LOW);
digitalWrite(GLED,HIGH);
delay(1000);
digitalWrite(GLED, LOW);

}

After confirming that the code works on the ATTiny85 with regular internal 8MHz timing, I connected up my USBasp to a programming shield and wired in the 1MHz output from the oscillator to pin 2 (XTAL1), not forgetting a shared ground wire as well.


Selecting the required (new) external 1MHz clock frequency, followed by "Burn Bootloader", and then "Upload using Programmer", gave the much admired RGB LED goodness at 1 cycle per second as timed by the external oscillator. Good one!

I think that I will test this signal with a display to give the time of day, and check it periodically to see if it drifts over days/weeks. Then I can see if the 1.000MHz engraving on the top of the oscillator is justified!

Note that I did mess up a few times during this discovery process (which has happened too many times before and is really the genesis of this blog - "I've got to document this stuff!"). This memory lapse resulted in a bricked ATTiny85, but I was able to bring it back from the dead with an awesome 12V HVPP fuse resetting thingy from Tindie.

I don't believe that the process of changing the clock source is dangerous per se, but I did forget that when you are planning to use an external clock source, you need that source connected to the XTAL pins during fuse setting and uploading (see connections on diagram above and video below). Once I remembered that - all went well.

I am still not sure if the 22pF capacitors on XTAL1 and XTAL2 are required when I am using a single external clock source, but it seems that they didn't cause any problems - so why not?






No comments:

Post a Comment