Go Down

Topic: Blinking LED without any includes (Read 1 time) previous topic - next topic


Aug 14, 2010, 05:18 pm Last Edit: Aug 14, 2010, 05:19 pm by glynton Reason: 1
Hi all,

My background is Mehcanical Engineering but I'm currently working as a project manager for an Embedded Computing company so to better understand the kinds of tasks, issues and risks associated with the industry I have decided to buy an Arduino Duemilanove and get cracking on some AVR programming.

I have successfully written a blinking LED sketch using the Arduino IDE, however this was not very satisfying.

I have also successfully set up XCode as an IDE and have written a blinking LED programme that only uses the avr-libc libraries as includes.

However, to really get to grips with Embedded Programming I would like to be able to write a Blinking LED programme with out any includes from third parties.

I'm a little stuck at the moment with the processes involved with the whole read-modify-write operations of actual device registers to control digital IO pins.

Does anyone have any good links for me to read up that are specific to the Arduino above?

I've so far been able to put the following code together below, but I'm unsure how to replace the _SFR_IO8 part without any includes. I believe it may require some assembly code to utilise the AVR instructions CBI and SBI but I'm not sure how to use these within a C programme.

Code: [Select]

* Function:            Main
* Description:            Blink LED connected to pin 13 on arduino board (PB5)
* Notes:                  This routine contains an infinite loop
* Returns:                  None

#define      PORTB _SFR_IO8(0x05) //address of Data 8bit Register
#define      DDRB _SFR_IO8(0x04) //address of Data Direction 8-bit Register for PORTB
#define      PORTB5 5 //PB5 (or pin 13) is bit 5 of the PORTB register

Many thanks,



The _SFR_IO8 macro expands to a specific memory address that depends upon the particular AVR in use. Have a look at the sfr_defs.h file in the include directory of the AVR-LIBC distribution.

For example, PORTB on at ATmega328 is at memory address 0x25 and _SFR_IO8() for this processor just adds 0x20 to its argument as that's where the special registers start in RAM.

Then, _SFR_IO8() wraps this all up in a _MMIO_BYTE() macro which actually treats this address as a pointer to a raw memory location. So the full expression for accessing PORTB at the lowest level would be:

Code: [Select]
(*(volatile unsigned char *)0x25) = 7; // Write 7 to PORTB

I bet you won't be programming this way for long but will, at some point, rely on include files :)

Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html


You could try asm instead of C, then there would be no includes, only you and the datasheet to discover registers adress's

Go Up