Yahoo Groups archive

AVR-Chat

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

Thread

Sample "c" code

Sample "c" code

2007-06-18 by joe_magee44

Hello I have just acquired a mega128, and intend using it for a
software  radio control. I have dabbled in the past in Uprocessors,
(Z80) a long time ago. I used machine code, and hand assembled it....
I dropped it at that point and havent done any since. I am looking for
some serial routines in , preferably "C" or assembly. I need to teach
myself allover again :-(
Any pointers to some source code would be appreciated.
Thanks in advance.
Joe

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

Re: [AVR-Chat] Sample "c" code

2007-06-18 by David VanHorn

On 6/18/07, joe_magee44 <joe_magee44@yahoo.com> wrote:
> Hello I have just acquired a mega128, and intend using it for a
> software  radio control. I have dabbled in the past in Uprocessors,
> (Z80) a long time ago. I used machine code, and hand assembled it....
> I dropped it at that point and havent done any since. I am looking for
> some serial routines in , preferably "C" or assembly. I need to teach
> myself allover again :-(
> Any pointers to some source code would be appreciated.
> Thanks in advance.
> Joe

You'll find the AVR a lot more "friendly" than the Z-80 environment.

One thing to watch for, is that the control registers for some of
USART0, and all of USART1 are in SRAM, so you'll need LDS and STS to
access them instead of IN and OUT.   One easy way to code it, is to
use IN and OUT everywhere, and fix them when the assembler complains.
:)

Another is the UDRIE int, which will int you whenever the transmit reg is empty.
Normally I have a routine that checks my serial out buffer, and turns
on UDRIE if there's stuff to send, and another one in the UDRIE int
that turns OFF udrie when the buffer goes empty.  Otherwise you'll be
getting into the UDRIE ISR every other instruction!

Other than that, it's pretty straightforward, I can send you routines,
but mine are written for cooperative multitasking. I would recommend
that you walk through it in simulation, so that you see the whole
process.

RE: [AVR-Chat] Sample "c" code

2007-06-18 by Phillip Vogel

You might have a look at avrlib. There are many example programs, including
one that controls up to eight RC servos.
http://hubbard.engr.scu.edu/embedded/avr/avrlib

Phillip
________________________________
Show quoted textHide quoted text
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of joe_magee44
Sent: Monday, June 18, 2007 7:16 AM
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] Sample "c" code



Hello I have just acquired a mega128, and intend using it for a
software radio control. I have dabbled in the past in Uprocessors,
(Z80) a long time ago. I used machine code, and hand assembled it....
I dropped it at that point and havent done any since. I am looking for
some serial routines in , preferably "C" or assembly. I need to teach
myself allover again :-(
Any pointers to some source code would be appreciated.
Thanks in advance.
Joe

Re: [AVR-Chat] Re: Sample "c" code

2007-06-18 by microbrix@gmail.com

i've never been a big fan of macros.  i like to see the actual code.

normally there's only a few points in the code that actually talk to
the i/o directly anyway, so just fixing them by hand is easy.
Show quoted textHide quoted text
On 6/18/07, Don Kinzer <dkinzer@easystreet.com> wrote:
> --- 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
>
>
>
>
> Yahoo! Groups Links
>
>
>
>

Re: [AVR-Chat] Re: Sample "c" code

2007-06-18 by John Samperi

At 01:34 AM 19/06/2007, you wrote:
>A more efficient alternative is to use a macro
>An example GNU Assembler macro for input is shown below.

Atmel's macros.inc (AVR001 app notes) contains a few useful
macros for the AVR assembler. The zip file is only 1k.
It has become one of my favorite bit of code :-)

I will paste the code here as it is very small:

;*****************************************************************
;*      file: macros.inc
;*
;*      Description:
;*      Source file for application note AVR001 - Conditional Assembly
;*      and Portability Macros.
;*
;*      Defines a number of macros that makes it easier to access
;*      IO registers and extended IO registers (or SRAM locations up
;*  to adress $FF if applicable).
;*      The macros can be used to produce code that assembles to
;*      any target AVR, without considering if the accessed IO
;*      registers are located in low, standard or extended IO space
;*
;* $Revision: 2.2 $
;* $Author: jllassen $
;* $Date: Wednesday, January 26, 2005 10:55:18 UTC $
;*****************************************************************

;*********************************************************
;*      BIT access anywhere in IO or lower $FF of data space
;*      SETB - SET Bit in IO of data space
;*      CLRB - CLeaR Bit in IO of data space
;*********************************************************
.listmac

.MACRO SETB             ;Arguments: Address, Bit, Register
         .if @1>7
                 .message "Only values 0-7 allowed for Bit parameter"
         .endif
         .if @0>0x3F
                 lds  @2, @0
                 sbr  @2, (1<<@1)
                 sts  @0, @2
         .elif @0>0x1F
                 in   @2, @0
                 sbr  @2, (1<<@1)
                 out  @0, @2
         .else
                 sbi  @0, @1
         .endif
.ENDMACRO

.MACRO CLRB             ;Arguments: Address, Bit, Register
         .if @1>7
                 .message "Only values 0-7 allowed for Bit parameter"
         .endif
         .if @0>0x3F
                 lds  @2, @0
                 cbr  @2, (1<<@1)
                 sts  @0, @2
         .elif @0>0x1F
                 in   @2, @0
                 cbr  @2, (1<<@1)
                 out  @0, @2
         .else
                 cbi  @0, @1
         .endif
.ENDMACRO

;*********************************************************
;*      Bit test anywhere in IO or in lower $FF of data space
;*  SKBS : SKip if Bit Set
;*  SKBC : SKip if Bit Cleared
;*********************************************************
.MACRO SKBS             ;Arguments: Address, Bit, Register
         .if @1>7
                 .message "Only values 0-7 allowed for Bit parameter"
         .endif
         .if @0>0x3F
                 lds  @2, @0
                 sbrs @2, @1
         .elif @0>0x1F
                 in   @2, @0
                 sbrs @2, @1
         .else
                 sbis @0, @1
         .endif
.ENDMACRO

.MACRO SKBC             ;Arguments: Address, Bit, Register
         .if @1>7
                 .message "Only values 0-7 allowed for Bit parameter"
         .endif
         .if @0>0x3F
                 lds      @2, @0
                 sbrc @2, @1
         .elif @0>0x1F
                 in       @2, @0
                 sbrc @2, @1
         .else
                 sbic @0, @1
         .endif
.ENDMACRO

;*********************************************************
;*      Byte access anywhere in IO or lower $FF of data space
;*      STORE - Store register in IO or data space
;*      LOAD  - Load register from IO or data space
;*********************************************************

.MACRO STORE            ;Arguments: Address, Register
         .if     @0>0x3F
                 sts     @0, @1
         .else
                 out     @0, @1
         .endif
.ENDMACRO

.MACRO LOAD             ;Arguments: Register, Address
         .if     @1>0x3F
                 lds     @0, @1
         .else
                 in      @0, @1
         .endif
.ENDMACRO


Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495       Fax (02) 9674-8745
Email: john@ampertronics.com.au
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************


-- 
No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.472 / Virus Database: 269.9.0/852 - Release Date: 17/06/2007 8:23 AM

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.