Main Menu
Home
AVR News
Atmel AVR
AVR Development Tools
Valuable Tools
WinAVR toolset
Makefile for WinAVR
AVR Virtual Simulators
Hardware for prototyping
GCC and AVR-GCC
Short introduction to C
AVR-GCC Tutorial
Example AVR Projects
AVR-GCC articles
WinAVR Site Map
Scienceprog FORUM
ScienceProg BLOG
Disclaimer
WinARM Tutorial
Latest comments
Measuring motor speed and...
Dear Administrator The code above...
More...

Programming AVR ADC module...
hi please send me example usart with...
More...

Measuring motor speed and...
main.c: In function...
More...

Running TX433 and RX433 RF...
(Hopefully this is not a re-post). I am...
More...

Running TX433 and RX433 RF...
I just bnought a pair of the RX/TX 433...
More...

Friendly sites

Related Items:

Recommended sites


How to combine C program with external ASM PDF Print E-mail
Written by Administrator   
Wednesday, 09 May 2007
Sometimes it is useful to program some parts in assembly language while whole program is written in C. There are two methods how to inject assembler parts in C program. One of them is to write in-line ASM and another is to link existing assembler program. For instance some parts of program may require critical timings and you are not sure if C compiler can be as optimal as you expect. Other reasons according to avr-libc

documentation may be as follows:

  • Code for AVR's that have no RAM;

  • Very timing critical applications;

  • Special tweaks that cannot be done in C.

As I mentioned all this can be also done using in-line assembler feature. For somebody in-line assembler may look too complicated because of special syntax requirements. Before starting to write assembler program first read (What Registers are used by the C compiler) to ensure you are using right registers in your assembler program and won't mess up the normal program flow by occupying registers that are storing values from other routines. In most cases push and pop may do the job.

OK lets have simple example to clear things out. Dont pay attention to fact that I am writing program which can be easily written in C. This is only for demonstration purposes how to relate C and ASM. Project consists of 3 parts:

  • mainC.c – C program;

  • extasm.S – ASM program;

  • Makefile – makefileneeded to make a project.

Note, that assembler file must have capital extension .S, otherwise program wont compile and maybe cleared during make clear command.

Lets write program:

<<<<<<<<<<<<,mainC.c>>>>>>>>>>>>>>>

#include <avr/io.h>
volatile uint8_t pinbits; //define poer pins
extern void InitPort(void);//init port for output
extern void sendpinbits(void);//send to port
int main()
{
	pinbits=1;//set pin 1 as output
	InitPort();//call subroutine from assembler file
	while(1)//repeat for ever
	{
	pinbits=1;//Pin High
	sendpinbits();//call subroutine from assembler file
	pinbits=0;//Pin Low
	sendpinbits();//call subroutine from assembler file
	}
}


Then assembler program:

<<<<<<<<<<<<extasm.S>>>>>>>>>>>

#include <avr/io.h>				;required for register definitions
.extern pinbits					;external variable
.global InitPort				;make accesible globally
InitPort:					;function name  
	push r18				;save register value
	lds r18, pinbits			;load variable to r18
	out _SFR_IO_ADDR(DDRD), r18	;set pins as output
	pop r18				;restore register
	ret					;return from subroutine
.global sendpinbits				;make global
sendpinbits:					;function name
	push r18				;save register value
	lds r18, pinbits			;load variable to r18
	out _SFR_IO_ADDR(PORTD), r18	;pins to High
	pop r18				;restore register
	ret					;return from subroutine


Makefile is generated with Mfile which comes with WinAVR. You only have to change these parts:

<<<<<<Makefile>>>>>>>>

# MCU name

MCU = atmega8

# Processor frequency.

F_CPU = 8000000

# Output format. (can be srec, ihex, binary)

FORMAT = ihex

# Target file name (without extension).

TARGET = mainC

# List Assembler source files here.

ASRC = extasm.S


Other parts of Makefile leave as they are. Then call command make all (Programmers Notepad->Tools->Make All) and program should compile without errors. I have tested this example with AVRStudio debugger and it works properly.

Good luck.


Related Items:

   

Users' Comments  
 

Average user rating

 


Add your comment
Only registered users can comment an article. Please login or register.

No comment posted



mXcomment 1.0.6 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
< Prev   Next >

© 2008 WinAVR AVR-GCC Tutorial
Joomla! is Free Software released under the GNU/GPL License.