Lab 8: C programming with AVR Studio 4



CSC 230
Lab 8: C programming with AVR Studio 4
The goal of this lab is to introduce you to “C” programming language. We will repeat the concepts
from previous labs and use “C” instead of assembly programming. There are two parts to this lab.
The first one introduces you to a blink program and the second one is the LCD display program.
Submit lab8.c at the end of your lab.
PART1: LED blinking
I. Create a C project in AVR Studio 4
Start the AVR Studio 4.
 Select “New Project”.
 Select “Project Type” →AVR GCC, name the project “lab8”, click “…” button to choose a
location (H drive)
 Go to → Next.
CSC 230 Fall 2016
 Select a platform: Debug Platform → AVR Simulator 2
 Select a Device: Device → ATmega2560. Click the “Finish” button.
II. Compile C project
After creating a C project, it opens up the project space. There are issues with the avr-gcc compiler
within the AVR Studio. Hence we need to replace this with a gcc compiler located in the WinAVR
directory. This step needs to be done for every C project you create.
 Go to Project → Configuration Options
 Go to the last item “Custom Options” in the left plane.
 Uncheck “Use AVR Toolchain” under External Tools at the bottom
 Select the button for “avr-gcc” to point to avr-gcc.exe in the folder “C:\WinAVR\bin\avrgcc.exe”
 Select the button for “make” to point to make.exe in the folder
CSC 230 Fall 2016
 Copy the contents of lab8.c provided to you in the Lab8 folder on connex
 Read the code and understand

CSC 230 Fall 2016
 Build the code (F7) or click on the Build
 See the build window below to see if there any possible errors
 If the program compiled with no errors it is time to load the binary hex file (lab8.hex) to the
board using avrdude.
III. Exercises:
Download lab8.c.
Realize the following Pseudo-code:
turn the first led on;
1 second delay;
turn the current led off, turn the next led on; //wrap around when appropriate
1 second delay;
Submit lab8.c at the end of your lab.
PART2: LCD Display
Following the steps similar to blink project, create an AVR-gcc project for display. Import the file
main.c. For this purpose we have provided you complete LCD driver software. This driver is
obtained online from
a. Create your avr-gcc project in the AVR Studio 4.
b. Make sure you use WinAVR tools and not the AVR Toolchain as explained above
c. Import the lcd_driver files (lcd_drv.h, main.h, mydefs.h, lcd_drv.c and main.c) into your
project space.
d. Our Lab LCD is a 2×16 (i.e., 2 line display with 16 characters on each line) display.
Therefore the first thing you have to do is that make sure (i.e., uncomment the 2×16 line)
the LCD is set to 2×16 in “main.h” and comment others in the “define wanted LCD type”
section of main.h
e. Our LAB cpu uses a clock rate of 16MHz. Therefore you need to also make sure that the
correct value for F_CPU in “main.h” file. Otherwise the timing used in the code will not
f. The LCD is connected to the board through specific set of digital pins as outlined in the
HD44780 tutorial. These are already defined under “define the LCD connections”
section of “main.h” with the appropriate port pins.
g. Study the simple “main.c” program. It initializes the LCD by calling lcd_init(); sets the
cursor to position (0,0) (first character, first line) using lcd_xy() and puts a string
“Welcome to” using lcd_puts(). Since ours is a two line display, it will continue with
displaying the second line by setting the cursor to (0,1) (first character, second line) and
writes a string “CSc 230”.
CSC 230 Fall 2016
h. Compile your project and upload your hex file to the board by using “avrdude” program
if there are no errors.
i. If everything goes well, you should see “Welcome to CSc 230” on the display.
Modify the code so that you display your name on the second line. Wait for 500ms, then erase it
and display it again in a loop creating a flashing effect.