Path: Home => AVR-overview => Assembler intro => Project planning    (Diese Seite in Deutsch: Flag DE) Logo

Project planning in assembler

How to plan an AVR project in assembler

Here are the basics on how to plan a simple project, to be programmed in assembler. Because the hardware components determine a lot, the hardware considerations are discussed first. Then a chapter on interrupt follows and after that timing issues are discussed.

Hardware considerations

The decision, which type of AVR fits best to your needs, a number of considerations can play a role. Here are the most relevant ones:
  1. Which port connections with a fixed location are needed? Fixed locations are I/O ports of internal components that are only available on certain pins, and cannot be moved to another portpin. Components and connections of this kind are:
    1. If the processor should be programmable within the circuit (ISP interface), the pins SCK, MOSI and MISO have to be assigned for this purpose. If your peripheral allows that those can be used as inputs (e.g. SCK and MOSI) or as outputs (by decoupling them via resistors or multiplexers).
    2. If a serial interface is needed, RXD and TXD have to be reserved for that purpose. If the RTS/CTS hardware handshake protocol shall be implemented as well, two additional portpins are required, but can be placed at any other free location.
    3. If an analog comparator is needed, AIN0 and AIN1 have to be reserved for that.
    4. If external signals are to be monitored for level changes, INT0 and/or INT1 have to be reserved for that.
    5. If AD converters arew to be used, the ADC inputs have to be placed and reserved for that purpose. If the converter has the external AVCC and AREF connections, those should be used and wired accordingly.
    6. If external pulses are to be counted, the timer input pins T0, T1 and T2 are fixed and exclusive for that use.
    7. If external SRAM is to be attached, the respective address and data ports together with ALE, RD and WR have to be reserved.
    8. If the processor clock should be generated from an external crystal oscillator, XTAL1 has to be reserved. If an external crystal or ceramic resonator shall control the clock frequency, XTAL1 and XTAL2 are fixed for that purpose.
  2. Are there any external components that require more than one portpin (e.g. 2, 4 or 8) to be written or read? These should be defined in an appropriate way (in the same port, in the right order).
    1. If controlling of an external device requires writing or reading of more than one bit at once, e.g. a four- or eight-bit LCD interface, the necessary port bits should be in the right order. If it isn't possible to place the whole interface in a single port, the interface can be divided into two pieces. The software is easier, if the resulting portions are left- or right-adjusted in the port.
    2. If two or more ADC channels are required, the software is easier, if those are placed in an order (e.g. ADC2+ADC3+ADC4).
  3. At the end, all external components are placed that do not require fixed pins.
    1. If only a single pin causes you to select a larger device, you can consider using the RESET pin for that purpose. This can be used as an input pin if a certain fuse is set. Setting of that fuse disables further ISP programming, the chip can only be programmed in high-voltage programming modes. For final productions with a large number of identical devices, this is acceptable, but not for prototyping. In case of prototyping a high-voltage programmer interface on the ISP pin can be used, if the component on the RESET pin is protected against the 12 V on this pin during ISP programming, e.g. with a resistor and a zener diode.
Further considerations for the decision, which processor type fits best, are: To the top of this page

Considerations on interrupt operation

Very simple tasks work fine without interrupts. If power consumption is an issue, this ain't true either. Nearly all projects require interrupts. And this should be planned thoroughly.

Basic requirements of interrupt-driven operation

If not aware any more, here are the basics. To the top of this page

Example for an interrupt-driven assembler program

The following provides an example for an interrupt-driven assembler program, that utilizes all the above mentioned rules.
; Register definitions
.EQU rsreg = R15 ; saving the status during interrupts
.EQU rmp = R16 ; Temporary register outside interrupts
.EQU rimp = R17 ; Temporary register inside interrupts
.EQU rflg = R18 ; Flag register for communication
.EQU bint0 = 0 ; Flag bit for signaling INT0-Service
.EQU btc0 = 1 ; Flag bit for signaling TC0-Overflow
; ...
; ISR-Table
.ORG $0000
	rjmp main ; Reset vector, executed at start-up
	rjmp isr_int0 ; INT0-vector, executed on level changes on the INT0 input line 
	reti ; unused interrupt
	reti ; unused interrupt
	rjmp isr_tc0_Overflow ; TC0-Overflow-vector, executed in case of a TC0 overflow
	reti ; unused interrupt
	reti ; unused interrupt
	; ... other int vectors
; Interrupt service routines
isr_int0: ; INT0-Service Routine
	in rsreg,SREG ; safe status
	in rimp,PINB ; read port B to temp register
	out PORTC,rimp ; write temp register to port C 
	; ... do other things
	sbr rflg,1<

To the top of this page

Considerations on timing

If an AVR project goes beyond polling an I/O port and, depending from that result, doing something, considerations on timing are necessary. Timing

Selection of the clock frequency of the processor

The main question is on the necessary precision of the processor clock.

Is it unnecessary in the application to perform times less than a few percent inaccurate, the internal RC oscillator of most of the AVR types is sufficient. In the tiny and mega types, a oscillator calibration is built in, so that differences between the nominal and the effective frequency are reduced. Note that the default internal calibration byte was selected at a certain operating voltage. If your operating voltage is fixed at a different level, rewriting the calibration byte brings more accuracy. If the operating voltage is fluctuating, the error can be too large.

If the internal RC clock is too slow or too large, some device types have a clock prescaler on board. This feature allows to optimize the clock frequency, and different clock frequencies can be selected. This is either done once with changing a hardware fuse (the DIV8 fuse) or within the software (e.g. to reduce the supply power during pauses). But be aware that some devices with a limited clock specification (V types) should not be set to beyond their limit, otherwise they won't work correct any more.

If the internal RC oscillator is too inaccurate, fuses can be set for external RC combination, an external oscillator, a crystal (Xtal) or a ceramic device. Because false fuse setting can cause a catastophe, a rescue board with an external oscillator might be the last chance to get the device working and the fuse resetted again.

The absolute clock frequency should be appropriate for the application. As an indicator, the repeat frequency of the most often performed work package can be used. If a key has to be polled any 2 ms, and, after 20 times, should be debounced long enough, there is plenty of time, if a 1 MHz clock is used (2,000 clock cycles between any two polls, 20,000 clocks for repeated execution of the key command).

It's only getting narrower, if a pulse width modulated signal with high resolution and a high PWM frequency has to be reached. With a PWM frequency of 10 kHz and 8 bits resolution 2.56 MHz are too slow for a software-driven solution. If a timer with some software-overhead can take over that burden, that's better.

To the top of this page

©2011 by