Kevin Reid's blog

Latest Posts

Info

stone wall, glasses, neutral
Name
Kevin Reid
Website
My Website

View

Advertisement

October 1st, 2008

This is not a complete tutorial; it is intended to collect the information that I needed to get started and didn't find obvious, particularly explaining the operations needed to compile a program, and the most basic of syntax, operators, and IO operations to use microcontroller-specific operations from C/avr-gcc/avr-libc. It does not cover designing circuits or choosing a programmer.

Needed software

Get avr-gcc, avr-binutils, avr-libc, and avrdude. If you're on Mac OS X, all three are available through MacPorts.

Setup

Create ~/.avrduderc to specify what kind of programmer you have and where it's connected. Mine looks like this:

  default_serial = "/dev/cu.KeySerial1";
  default_programmer = "stk500v1";

default_serial is the device for the serial port the programmer is attached to; default_programmer is the type of programmer; see the manual section on command line options for programmer type names. You can also specify these on the command line, but I figure since these are specific to your computer it's better to leave them unspecified in makefiles.

Writing code

  • char is 8 bits and int is 16.
  • The entry point is the normal int main(void); you don't have to do any special initialization.
  • Special registers are exposed as variables — e.g. PORTD = 0 to do output.
  • All normal variables take up scarce RAM — for constant data, use the facilities in <avr/pgmspace.h> to store it in program space.
  • Basic IO: Each port has three registers. For some port x, DDRx specifies direction: a 1 bit makes the corresponding pin an output, whereas 0 makes it an input. Reading PINx takes input from the pins; writing PORTx sets output. If DDRx has a 1 bit, then PORTx controls internal pullups on those pins.
  • For further info on the functions of the chip, read the datasheet for it; the translation into C is mostly straightforward.
  • The avr-libc manual contains info on its avr-specific features.
  • The avr-libc demo projects are informative and well-explained.

Compiling

  avr-gcc -mmcu=at90s8515 -Werror foo.c bar.c -o foo.elf

Substitute the part number of your target chip for at90s8515. I use -Werror to reduce the chances I'll run broken code on real hardware.

  avr-objcopy -O ihex foo.elf foo.hex

This step discards the metadata the .elf file contains, leaving just the code, suitable for programming.

Programming (downloading)

avrdude -p 8515 -U flash:w:foo.hex

Substitute the part number of your target chip for 8515.

October 7th, 2007

One of the requirements for the timer project is that the alarm sound be not too unpleasant.

So: What can I do with the current prototype hardware, a piezo element taken from a watch connected directly to a (digital) output pin on the PIC?

Trivially, fixed-volume square waves, by either toggling the output pin in software or setting up the PIC's PWM feature to drive it automatically. (Note that the PWM produces a minimum frequency of 1/16384 of the main oscillator.)

(I've moved the piezo output on the prototype to the RC2/CCP1 pin. This change is not yet reflected in the published circuit and program.)

However, I want software volume control, so I tried something more elaborate. I set the PWM to a very high frequency, and produce the audio-frequency signal by varying the PWM's duty cycle parameter. The characteristics of the piezo crystal filter out the high frequency, producing an effectively analog output.

After much twiddling getting this to work (CCPR1L is not the least-significant bits of the duty cycle), I found that square waves work well, sine waves are very quiet, and changing the amplitude has the expected effect. So while it won't sound great, I can at least program it to increase the volume gradually.

I haven't yet integrated this into the main timer program, so I don't know whether I'll be able to produce decent tones despite (or making use of) the timer program's counting interrupt.

July 22nd, 2007

Timer project

Add to Memories Tell a Friend
stone wall, glasses, neutral

This is my first nontrivial electronics project. The goal is to create a better kitchen timer; over the years I’ve had many break, and the model I’m currently using has an annoying UI and is usually too loud.

Ben Jackson sent me a PIC16F877 microcontroller and a bunch of parts, and I've finally gotten around to doing something with them. (Some of the parts in the photo I already had.)

I've written a separate page with more details on the timer prototype and my plans.

Steps to get to this point: )

The big things remaining are a multi-digit LCD display, calibration to real time, better input devices, and building it into a case.

(Please let me know if you'd like more information about some part.)

Powered by LiveJournal.com