NOTE: Following code is for Wiring but it can be translated to /ardu with few changes.
Once you have all requirements for working with AVRLIB and AVR-GCC you should create a Wiring working directory on your home directory. On Linux and OS-X you can do this by typing on the command line on a terminal window:
|
|
|
|
|
From now on, all work should be done in the blink directory. Use Emacs, Vi or your favorite editor to copy-and-paste contents of makefile.blink and global.h.blink, each them on a separate file. Save them separately as 'makefile' and 'global.h'. Your 'blink' directory should now contain these files.
Again, using Emacs, Vi or your favorite editor copy-and-paste the following code into a file and save it as 'blink.c'.
|
You should now have the files 'makefile', 'global.h' and 'blink.c' in your 'blink' directory. You can make sure about this by issuing the 'ls' command on the terminal window:
|
Make sure you read and understand this code on 'blink.c'. Several points should be make here:
This program causes Wiring built-in LED to blink on/off.
A timer functions allows to blink every 500ms.
Built-in LED is on Wiring 'pin-48'. By looking at the table in (§) we can see that 'pin-48' is 'pin-0' of 'PORTG'.
The function 'outb(DDRG, 0xFF)' sets PORTG as OUTPUT, and is equivalent to the portMode() function on the Wiring IDE.
The function outb(PORTG, 0xF0) sets the low 4 bits HIGH of PORTG, thus the low pins of PORTG have 5 Volts. 'cbi(PORTG,PG0)', clears the bit on PORTG pin-0 to turn LED on. 'sbi(PORTG,PG0)' sets bit to turn LED off. 'sbi()' and 'cbi()' functions can be thought of like digitalWrite() function on Wiring .
The line timerPause(500) from the timer library causes a delay of 500ms before the next blink.
Further information about this functions can be seen in the file: 'avrlib/examples/basic_io/basiciotest.c'. Also on the CCRMA's PID wiki: Anatomy of a C program for AVR.
To compile the 'blink.c' program, make sure you have AVR-GCC and all the AVR-tools install on your computer(see §2). Once ready and on the blink directory type:
|
|
If you are not satisfied with a compilation or if you edit the '.c' file you can do a:
|
After the compiler has generated a '.hex' file, it must be loaded onto the micro controller. There are a variety of ways of doing this, using different programming hardware and/or protocols for transmitting the data. In this case we are going to use again our 'makefile' also in theblink directory.
Connect a USB cord to the board and the other end to a USB port on your computer. Make sure the board gets powered and that it has communication through FTDI. On Fedora systems communication port device should be somenthing like: '/dev/ttyUSB0'. If it is not you might have to edit your 'makefile' on the 'UISP_PORT=/dev/ttyUSB0' line or on the 'AVRDUDE_PORT = /dev/ttyUSB0' line. If all is working! just press the '<reset>' button on the Wiring board, wait for three(3) seconds and on the command line type:
|
On this 'makefile', we are using UISP to load the '.hex' int the ATMega128. If communication is right you should see some progress output on the terminal window:
|
Press the '<reset>' button again and wait for two(2) or three(3) seconds. The internal LED should blink now.
It is advisable to make changes to the 'blink.c' file, for instance change duration of blink or wait. If you do so, don't forget to recompile again: do a 'make clean', a 'make' and when you are ready to load again '<reset>' button and 'make load'. After the loading process is complete don't forget to press the '<reset>' button again. Read 'blink.c' to understand more about variables and hexadecimal values.
© Copyright 2001-2008 CCRMA, Stanford University. All rights reserved.
Created and Mantained by Juan Reyes