| Writing a Program | ||
| Links Mentioned APP-IV resources Online Library Reference |
![]() NOTICE: THIS IS AN ARCHIVAL COPY OF THIS COURSE. For the latest version, please visit our interactive classroom. Creating your program will involve two parts. First, you have to create your source files (that is, .C files and perhaps some headers or .H files). In addition, you must build a makefile that will control the build process. This isn't hard because you can start with the prototype makefile included with the APP-IV demo program. When you write your C program, you may wonder what C library calls are available to you. The AVR library is surprisingly complete. You'll find the online documentation in the links section of this page (top left). There are some sections of the library that are only useful in certain circumstances (for example, the bootloader functions aren't much use unless you are writing another bootloader). In addition, some items (such as the program-space strings) are probably only interesting once you start worrying about optimizing your code. For now, stick to the normal C stuff and you'll do fine. You can place the app4 library headers in one of the system header directories, or just copy them into each project you want to use. It is possible to compile the modules and make them into libraries, but since you might want to compile with special options, we provide them as source and it is perfectly OK to compile them separately for each project. So starting with a blank directory, I issued the following shell commands (remember, even under Windows you have the shell thanks to Cygwin): mkdir app4tut cd app4tut cp /app4lib/app4*.[ch] . cp /app4lib/makefile . Here's an example program I entered into tutor.c: #include <stdio.h>
#include "app4uart.h"
int main(int argc, char * argv[])
{
int i,n;
UartInit(BAUD_19200); // 19200 baud, 8 bits, 1 stop, no parity
UartSetStdio(); // Make UART stdin/stdout
printf("\014"); // Clear screen
while (1)
{
printf("How high should I count? ");
scanf("%d",&n);
for (i=1;i<=n;i++)
printf("\n%d",i);
printf("\nDone!\n");
}
return 0;
}
You might find it surprising to use printf and scanf in a microcontroller program. In truth, these functions are not very efficient, but for a quick example, they are certainly handy and they demonstrate how complete the library is. You can use any text editor you like. WinAVR has a text editor that is set up to act like an IDE, but you can use any editor you like. You also have to modify the prototype makefile to successfully build the project. If you open the makefile in your editor, you'll find all the changes you need to make are near the top. The first line you need to change looks like this: TARGET = demo Change the demo to tutor since that's the name of this project (that is, the main file is tutor.c). So the line looks like: TARGET = tutor The next line you should change looks like this: SRC += app4uart.c app4adc.c app4lcd.c app4delay.c You don't actually need to change this line, but since you aren't using all of these libraries, there is no point in compiling them. You are using app4uart.c, though, so leave it in: SRC += app4uart.c Next, you should change two items further down in the file. In fact, you really should make these changes in your "master copy" of the makefile so you don't have to change them again. These specify the serial port that connects to the APP-IV and the programming software you want to use. You probably won't change these often. Here's the relevant part of the file: PROGPORT = /dev/com1 # Select programming software: uisp, avrdude, avrprog PROGRAMMER = uisp Some versions of Cygwin want the port in the format "COM1" and some want /dev/com1. You may have to experiment. Of course, if you are using a different COM port, change the number appropriately. In Linux, the serial device is probably /dev/cua0 (check your documentation). You might want to make symbolic link named /dev/app4 to the correct COM port and then you can change the link if you ever need to switch ports (this doesn't work in Linux though). You can use any of the three programming drivers you like although we recommend you stick with uisp unless you have a specific reason not to use it. There are many other options in the makefile, but most of them you will never need to change. Now you are ready to build your project!
|