Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: Sample "c" code

2007-06-18 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "David VanHorn" <microbrix@...> 
wrote:
> One easy way to code it, is to use IN and OUT everywhere,
> and fix them when the assembler complains.

A more efficient alternative is to use a macro that expands to 
either IN/OUT or LDS/STS depending on the port address.  This method 
has the additional advantage of automatically using the shortest 
instruction for the AVR you're using.

An example GNU Assembler macro for input is shown below.  The 
corresponding macro for output is similar.  If you are using the AVR 
assembler you may be able to implement something like this.

// Macro to input from an I/O port to a register.  Example use:
//
// inPort r24, UDR0
//
.macro inPort  _reg, _port
  .if (_SFR_IO_ADDR(\_port) < 64)
    in   \_reg, _SFR_IO_ADDR(\_port)
  .else
    lds  \_reg, \_port
  .endif
.endm

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.