Using Timers and Counters to Clock Seconds

My current secret project involves cramming a bunch of features into a single microcontroller. The chip I chose to use is an ATMega48. The ATMega 48 is $1.40 each in small quantities and comes crammed packed with features. The chip will be quite busy performing many functions, but its main loop will be executed at least every 50ms (required for USB, did I mention I’m bit-banging USB?!). I desire to have a bit of RTC (real time clock) functionality in that I need to precisely measure seconds, although I don’t need to actually know the time or date. I desire to execute a function once per second, consuming a minimum of resources. The solution was quite simple, but I’m choosing to document it because it’s somewhat convoluted in its explanation elsewhere on the net.

In summary, the way I accomplished this is using the built-in 16-bit timer (most AVRs have such a timer, including the ATTiny series). If I’m clocking the microcontroller at a known rate (determined by my selection of crystal, 12 MHz in my case), I can set the chip to continuously increment a register (timer1) and execute a function every time it overflows. Timer1 overflows at 2^16 (65,536). I enabled a prescaler value of 256 so that it takes 256 clock pulses to increment the timer. 12MHz/256 = 46,875 Timer1 increments each second. Since Timer1 overflows at 65,536, if I initiate Timer1 at 18,661 (65,536-46,875), it will take 1 second exactly to overflow. Upon overflowing, I do something (maybe flip a LED on or off), and reset the Timer1 back to its starting value 18,661. Done! Without using an external RTC module or even an external crystal or asynchronous timer, we managed to execute a function every second on the second with minimal overhead, allowing the chip to do everything it wants in the rest of the time!

The following example is a little more specific, executing a function exactly 15 times a second, and executing another function (to flash an LED) exactly every 1 second. It should be self explanatory:

// This function is called every second on the second
volatile int count; // this should be global
ISR(TIMER1_OVF_vect){
	TCNT1=62411;//Initialize our varriable (set for 1/15th second)
	count++; //increment 1/15th second counter
	if(count==15){
		statusTOGGLE(); // do your event (flash a LED in my case)
		count=0;//reset global variable
		}
	}
// This is for ATMega48, consult datasheet for variations for different chips
// place this just inside main(), before your primary loop
TCCR1B|=(1<<CS12);// prescaler 256
TIMSK1|=(1<<TOIE1); //Enable Overflow Interrupt Enable
TCNT1=62411;//Initialize our varriable (set for 1/15th second)
count=0; //Initialize a global variable
sei(); // enable interrupts

I’m having a lot of fun spending time going through the datasheet of this chip. It has a lot of features, and some I didn’t really dig deeply into. Without giving away too much of my project, I’ll show some photos I’m excited to share. My project interfaces the PC through USB directly attached to 2 pins using no intermediate chips (wow!). The photos demonstrate various steps in the temperature measurement and calibration tests…
DSCN1367
DSCN1372


     

Permeability Tuned Oscillator (PTO) Working Nicely

My last entry described my accidental discovery of the PTO for QRP purposes. I breadboarded it and was amazed at the results! I went ahead and built this carefully in an enclosure and the output is wonderful. It’s strong, it’s stable, and it tunes effortlessly over the same range it did before (about 1MHz). The video describes details of the action, and demonstrates the stability of the oscillator by letting you hear it audibly on a nearby receiver.

The fundamental concept and hardware is straightforward. Two nuts are soldered into an Altoids tin providing much-needed grounding for the screw (reduces shift when it’s touched). Also the wire soldered over the screw is pinched firmly at the base to apply constant pressure to the screw to make it hard to turn and therefore more stable while turning. The inductor is a bunch of turns (no idea how many, about a meter of magnet wire) around a McDonalds straw.
DSCN1350

Alltogether it’s a simple colpitts oscillator with a MPF102 JFET at its heart, using a 74hc240 CMOS buffer as an amplifier. There’s a voltage regulator in there too.DSCN1356

The result? Pretty darn stable (by CW QSO standards).  That’s without any regard to thermal isolation or temperature compensation. I’m quite pleased!  I look forward to MUCH more experimentation now that I’m starting to feel good about designing and building simple, tunable, stable oscillators. It’s always hard to nail all 3 in a single device!
DSCN1357


     

Screwy Oscillator Idea

> > > UPDATE!
>>>CLICK HERE FOR IMPROVED DEVICE

Can you believe it’s been almost 3 months since my last post? A lot’s been going on since then, namely the national board dental exam. I’m happy to report I prepared for it and performed above and beyond my expectations on the exam, and I’m quite satisfied. The last few weeks were quite a strain on my life in my aspects, and during that time I realized that I didn’t appreciate the little things (such as free time) that I would have loved to experience instead of studying. I guess it’s the feeling you have when you’re really sick and think to yourself “remember this moment so that when you’re well again, you can appreciate feeling well”. Now that it’s all behind me, what do I do? I sit at my work station, play some light music, grab an adult beverage, turn on the soldering iron, and make something special.

Update: read the bottom of the post for reflections about the concept discussed below...

I’m resuming work on my simple transmitter/receiver projects, but I’m working at the heart of the device and experimenting with oscillator designs. I built various Colpitts, Hartley, Clapp, and other oscillator designs, and I think I landed on a design I’m most comfortable with replicating. I’m actually creating a voltage controlled oscillator (VCO or VFO), with a frequency that can be adjusted by rotating a dial or two. It’s always a balance between stability and tunability for me. I don’t want to use polyvaricon variable capacitors (expensive!), and LED-based varactor diode configurations only give me a swing of about 20pf. What did I come up with?
DSCN1335

I had tremendous success using a variable inductor for coarse tuning! The inductor is nothing more than a screw entering and exiting the center of an air core inductor. I can’t claim all the credit, because I got the idea from this photo on one of the coolest websites on the planet, Alan Yates’ Lab. It looks like Alan got the idea from this page… This is so useful! Is this common HAM knowledge? Why am I, someone who’s been into RF circuitry for a couple of years now, JUST learning about this? I’m documenting it because I haven’t seen it out there on the web, and I feel it should be represented more! Here’s a video of it in action:

This is the circuit I was using: DSCN1334

This is what it looked like before the glue or screw: DSCN1307

Here’s the variable inductor enveloped in hot glue before it cooled and turned white: DSCN1316

At the end of the day, it looks nice! DSCN1339

Band changes can be accomplished by swapping the capacitor between the inductor and ground. It couldn’t be any easier! I’ll see if I can build this in a more compact manner…

UPDATE (2 days later): Apparently this is called a “Permeability Tuned Oscillator”, or PTO. It’s an early design for radios (earlier than variable capacitors) and I guess therefore not described often on the internet. Knowing it’s official title, searching yielded a few pages describing this action:

Dave, G7UVW did some analytical measurements using a mercury core!
The Tin Ear uses a PTO as its primary tuning method (also McDonalds straw?)
This guy made a PTO out of PVC with a nice screw handle!
This PTO kit seems to be used in many projects.
The Century 21’s VFO is a PTO! I love that rig and had no idea it tuned like that…
This guy used a PTO in his MMR-40 radio.
This guy uses a straw too!
Someone on Hackaday recommended This ARRL Challenge winner with an almost identical design as mine!

I guess this bright idea was so bright, it was thought of by many people long ago…