Step 3A Closer Look at the ATmega8
Image 1 is the pinout diagram for the ATMega8 (exactly the same as the 168/48/88, the only difference is the amount of onboard memory and interrupt options).
Pin 1 - Reset, should be held at VCC voltage (or at least logical 1). If grounded, the device will soft-reset
Pin 2-6 - Port D, general input/output
Pin 7 - VCC, supply voltage (+5V for us)
Pin 8 - Ground
Pin 9,10 - XTAL, external clock inputs (part of Port B)
Pin 11 - 13 Port D, general input/output
Pin 14 - 19 Port B, general input/output
Pin 20 - AVCC, analogue supply voltage (same as VCC)
Pin 21 - AREF, analogue voltage reference
Pin 22 - Ground
Pin 23-28 Port C, general input/output
Usable i/o ports: D = 8, C = 6, B = 6
A total of 20 usable ports is great, for simplicity you should group your outputs either into ports (say, D as the output port) or into groups on the board - you might want the LCD to run from Port C just to keep the wires tidy in that corner.
There are three extra pins that are required for programming. Those are MISO (18), MOSI (17) and SCK(19). These will happily act as i/o pins if needed though.
Clocking
The signal that we send to the camera needs to be precisely timed (accurate to around a microsecond) so it's important we choose a good clock source. All AVRs have an internal oscillator that the chip can get its clock from. The downside of this is that they can fluctuate around 10% with temperature/pressure/humidity. What we can do to combat this is use an external quartz crystal. These are available in anything from 32768kHz (watch) to 20MHz. I've chosen to use a 4Mhz crystal as it provides a decent amount of speed yet is fairly power conservative compared to perhaps 8Mhz+.
Onboard Power Management
I really wanted to use sleep routines in my code. In fact i wrote the first version to rely heavily on idling the processor while time lapsing. Unfortnuately, due to time constraints, i ran into some issues with running the clock externally and interrupting using the timers. In essence i'd have to rewrite the code to deal with the controller simply not waking up - which i could do, but time is against me. As such, the device only draws 20mA ish so you can get away with it. If you're really up for it, then by all means fiddle with the code, all you need to do is to clock internally and then run Timer 2 in asynchronous mode using the 4MHz crystal for the more accurate delays. It's simple to do, but time consuming.
ADC
The swiss army knife in the AVR toolset, the ADC stands for Analogue to Digital Converter. How it works is relatively simple from the outside. A voltage is sampled on a pin (from some sensor or other input), the voltage gets converted into a digital value between 0 and 1024. A value of 1024 will be observed when the input voltage is equal to the ADC reference voltage. If we set our reference to be VCC (+5V) then each division is 5/1024 V or around 5mV. Thus an increase of 5mV on the pin will increase the ADC value by 1. We can take the ADC output value as a variable and then fiddle with it, compare it with things, etc in the code. The ADC is an incredibly useful function and allows you do lots of cool things like turn your AVR into an oscilloscope. The sampling frequency is around 125kHz and must be set in proportion to the main clock frequency.
Registers
You may have heard of registers before, but fear not! A register is simply a collection of addresses (locations) in the AVR memory. Registers are classed by their bit size. A 7 bit register has 8 locations, as we start from 0. There are registers for just about everything and well take a look at them in much more detail later. Some examples include the PORTx registers (where x is B, C or D) that control whether a pin is set high or low and sets pull up resistors for inputs, the DDRx registers which set whether a pin is output or input and so on.
The Datasheet
A behemoth of literature, weighing in at around 400 pages; the AVR datasheets are an invaluable reference to your processor. They contain details of every register, every pin, how timers work, what fuses should be set to what and much more. They are free of charge and you will need it sooner or later, so download a copy!
www.atmel.com/dyn/resources/prod_documents/doc2486.pdf
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|

























































