COMP1721 - Higher Computing 1B

Higher Computing 1B - Week 11 Laboratory Exercises

This lab involves:

This is the program from lectures for displaying a binary counter on the AVR board LED bar.



#include "cse_avr_board.h"

int
main(void) {
    volatile char *portb_control_address = (char *)0x37;
    volatile char *portb_data_address = (char *)0x38;
    
    uart_init();
    uart_puts("8-bit counter\n");

    *portb_control_address = 0xff;       // set PB0..PB7 to be outputs
    for (int i = 0; ; i++) {
        *portb_data_address = i & 0xff;  // write to PB0..PB7
        
        // delay by executing a loop
        for (int j = 0; j < 30000; j++)
            ;
        uart_puts(".");
    }
    return 0;
}

Cut and paste into a file named led.c.

Use /home/cs1721/bin/cse_avr_board_gcc to compile led.c to an ihex file like this:


% /home/cs1721/bin/cse_avr_board_gcc led.c
/home/cs1721/lib/avr-esdk/bin/avr-esdk-gcc -std=gnu99 -O -Wall -mmcu=avr5 -I/home/cs1721/lib/cse-avr-board/ -c led.c
/home/cs1721/lib/avr-esdk/bin/avr-esdk-gcc -std=gnu99 -O -Wall -mmcu=avr5 -I/home/cs1721/lib/cse-avr-board/ -A mega64  led.o/home/cs1721/lib/cse-avr-board/cse_avr_board.a /home/cs1721/lib/cse-avr-board/crt0.s -o led.bin
/home/cs1721/lib/avr-esdk/bin/avr-esdk-objcopy -O ihex  led.bin led.hex
% ls -l led.hex   
-rw-r--r--  1 andrewt andrewt 308 Oct 09 21:05 led.hex

Connect the USB cable between your lab computer and the CSE AVR board.

You should see a green LED on the the board indicating it has power.

Use the wires supplied with AVR board to connect the 8 pins labeled PB0,PB1,...,PB7 to the 8 pins labeled LED0,LED1..LED7

Download the ihex file led.hex to the board using the command /home/cs1721/bin/avr_download like this:


%  /home/cs1721/bin/avr_download led.hex
Using /dev/ttyUSB0

As the message says you'll have to hold the Loader button down and press the Reset button to start downloading.

And then afterwards press the Reset button to start execution.

You should see an 8-bit binary counterdisplayed on the LED bar.

Exercise 1 - 10 bit Counter

The LED bar on the CSE AVR board has 10 LEDs. Using the above program as a starting point write a program led10.c which shows a 10-bit counter

First use 2 single wires to connect the 2 pins labeled PD0,PD1 to the pins labeled LED8,LED9.

To discover what addresses to use for PORT D look at page 9 of the Atmega64 summary datasheet.

If you want to see the desired behaviour download this ihex file to the AVR board.

Exercise 2 - LED Chaser

Using the above program as a starting point write a program chaser.c which shows a "chaser". At any time one LED and only one LED of the bar should be on. The illuminated LED should "shuffle" from from one of the bar to the other and back again.

Increase the delay by nesting the loop which counts to 30000 inside a loop that counts to 5.

If you want to see the desired behaviour download this ihex file to the AVR board.

Exercise 3 - LED Chaser Controlled by Push-buttons

First use 2 single wires to connect the 2 pins labeled PC0,PC1 to the pins for the push buttons. These are labeled PB0,PB1 and are next to the pins labeled Pot and LDR. Don't connect the wires to the Port B pins labeled PB0..PB7.

Using the above program as a starting point write a program pb_chaser.c which shows a LED "chaser".

While push button 0 is pressed the illuminated LED should move up the bar.

While push button 1 is pressed the illuminated LED should move down the bar.

If both or neither buttons are pressed the illuminated LED should not move.

You'll have to set PC0,PC1 to be inputs by writing zeros instead of ones to its control address (DDRC).

If push button 0 is down then bit 0 of the address 33 (PINC) will be one. Similarly for push button 1.

If you want to see the desired behaviour download this ihex file to the AVR board.

Finished?

When you have these 3 exercises working show them to your tutor to receive this week's lab mark.

After your tutor has checked that you answers are correct and given you the lab mark, you must also submit your answers using give. Here is the command to use:


% /home/cs1721/bin/classrun -give lab11 led10.c chaser.c pb_chaser.c


Andrew Taylor (andrewt@cse.unsw.edu.au)
Higher Computing 1B, Computer Science & Engineering, UNSW