AVR code

Work in progress...

What's here is not guaranteed to work, nor even to remain the same between two looks at it. All of it is work in progress, except for the stuff that hasn't/won't progress due to lack of time, need, or interest...


The Atmel range of RISC microcontrollers is something I'm becoming quite attached to. They have a reasonably sensible range of instructions (if you ignore things like no add immediate and the few instructions which only operate on half the register set) and they're fast. I've used one on the nixie clock, but the code there is in Bascom AVR basic.

Here are some routines I've found useful...

Software asynchronous serial port

Most - if not all - of the Atmel range of AVR micros contain serial interfaces - UARTs. These are easily driven in hardware and make communication easy and fast to implement. However, there are times when it's convenient to use the serial pins for some other purpose, or when you need more than one serial data link

Atmel provide an interrupt driven serial line, but it can only work on the two external interrupt pins. Sometimes it's nice to be able to use any of the available pins; not necessarily on the same port.

These routines provide an easy to use implementation of a 2400 baud serial link, no parity, eight bits, on an 8MHz ATMega32. It will easily work at lower baud rates; the limiting factor is the number of interrupts that need to be handled; four per bit period. Doubling the clock to 16MHz will allow 4800 baud operation - you'll need to decide for yourself whether your application can handle the overhead for higher rates.

It also works for other chips in the AVR range but you'll need to change some of the names of the registers and bits therein.

As an added bonus, there's a clock also available, as accurate as the crystal, which might be useful, though it's not a real-time-clock and it's not maintained during power-down.

Soft serial interface - C code

Soft serial interface - assembly code

Interfacing with a VK5121 LCD

The Vikay VK5121 dot-matrix LCD display turns up from time to time on eBay and similar surplus outlets. With a size of 120 by 32 dots it's a good size to use as a display - it's easy to produce a 4*20 character display and simultaneously use it as a graphics device. The main snag is that the chipset includes no character generator and so this has to be done in the memory space of the controlling processor

A secondary snag is that the controller chips used with this display each handle half the display - sixty columns - though they can display sixty-one columns. The following code shows one way to use the device, in both C and assembly.

VK5121 LCD driver - C code

VK5121 LCD driver - assembly code.

Video clock

There have been several versions of PAL and NTSC video generators published on the web, most apparently derived from Alberto Ricci Bitti's original design using an AT1200. This adapts that design and runs on a ATMega8 at 16MHz, and generates four independent monochrome outputs with a pixel resolution of 32 columns of 48 rows. Not brilliant, but sufficient to display a single digit at quite high resolution on each of four small video displays - viewfinder CRTs or the like - to make a clock.

This is untested except in simulation...

Video Clock

Copyright © 1995-2005 Neil Barnes - 4 April 2005