Wednesday, January 30, 2008

First Programming/Breadboarding Assignment


Week 4 — First Programming/Breadboarding Assignment:
Simple control of an LED, with a program!

Hardware Materials:
LED.
220 Ohm resistor.
Wire.

Software Materials:
cmdlinetest.c from sixcode/examples


You will connect an LED from Port C Pin 0 (PC0 in the schematic below) to +5V (VCC), with a 220 ohm resistor to limit current flow.  
You will then write a program which tells pin PC0 to turn on or off, in some pattern that pleases you.


This is a schematic for the circuit:

  


According to the schematic from the Atmega32 datasheet, PC0 is pin #22 on the chip (see below).

Pin #22 on our chip corresponds to pin #22 on the breadboard adapter—so we will connect the resistor and then the LED (in series) to that pin on our breadboard, and then the other end of the LED to positive voltage.  Make sure that you orient the LED the proper way, it is polarized!  So the positive side of the LED (rounded) is the one that connects to positive voltage, and the negative side (flat) is the part that connects to the resistor.  

Properly connected, your board should look something like this:
 


This concludes the hardware setup, and we are now ready for the programming part of this project. 

We are going to modify a pre-existing program, cmdlinetest, to allow us to turn the led on with a new command, "flash".

1.  Make a directory for software you will write inside of sixcode.  From your home directory, type: 

cd /sixcode
mkdir twomey

This is the folder that will hold any programs that you write.


2. Copy the folder cmdline from sixcode/examples to your folder.  This will copy the code files (.c) , header files (.h), the make file (makefile), and anything else you need to compile this program.
From a command prompt in the directory ~/sixcode, type:
 
cp -r examples/cmdline twomey/led

And verify that the files were copied:

ls twomey/led

will show

cmdlineconf.h cmdlinetest.eep cmdlinetest.lst global.h
cmdlinetest.bin cmdlinetest.elf cmdlinetest.map makefile
cmdlinetest.c cmdlinetest.hex cmdlinetest.o

or 

cmdlineconf.h cmdlinetest.c global.h makefile

depending on whether or not you compiled the program previously.

3.  Now we are going to modify the program that exists.  In your copy of cmdlinetest.c:

under:  

// functions

add:

void flashLED(void);


in:  

int main(void)

add:


// configure port C for led output

outb(DDRC, 0xFF);

// all LEDs on

outb(PORTC, 0x00);

// wait for hardware to power up

timerPause(100);

// all LEDs off

outb(PORTC, 0xFF);


in: 

void goCmdline(void)

after:

// add commands to the command database

cmdlineAddCommand("exit", exitFunction);

cmdlineAddCommand("help", helpFunction);

cmdlineAddCommand("dumpargs1", dumpArgsStr);

cmdlineAddCommand("dumpargs2", dumpArgsInt);

cmdlineAddCommand("dumpargs3", dumpArgsHex);

add:

cmdlineAddCommand("flash", flashLED);



in:

void helpFunction(void)

after:

rprintf("dumpargs1 - dumps command arguments as stringsrn");

rprintf("dumpargs2 - dumps command arguments as decimal integersrn");

rprintf("dumpargs3 - dumps command arguments as hex integersrn");

add:

rprintf("flash     - flash LEDs attach to PORT Crn");


at bottom of program

add:

void flashLED(void)

{

// all LEDs on

outb(PORTC, 0x00);

// time to keep light on

timerPause(100);

// all LEDs off

outb(PORTC, 0xFF);

}



4.  Save your changes and compile the program.  In Programmers Notepad in Windows, select

Tools --> [WinAVR] Make All

or in OS X

cd ~/sixcode/twomey/led
make

Your computer should display messages about its progress, and should successfully compile, producing a hex file (cmdlinetest.hex) to be sent to the processor.


5. Open the terminal program, turn on your processor in bootloader mode, and send it the text file cmdlinetest.hex.  The program should finish with


XXXX bytes written to FLASH with 00 errors.


And you know the program was uploaded successfully.


6. Restart your processor and see if your program controls the light. 



7. Modify this program for some more interesting behavior.  For instance, you could separate lightOn and lightOff to be two separate commands.  Or you could control more than one light with the processor.  I have seven-segment displays in the lab, if you want to try writing alphanumeric data to a display.  You could also incorporate basic input (with a switch) to trigger behavior.  Look at sixcode/examples/basic_io for a dscription of input and output settings for the microcontroller.



7b.  Modify the makefile (and rename you directory, your header files, and your source code) so that the program compiles under a different name.

You don't really want to call it cmdlinetest, do you?

2 comments:

Anonymous said...

If you're having problems...

In the terminal:
"cd \sixcode"

as opposed to

"cd /sixcode"

then,

mkdir twomey.

Terry said...

It felt unreasonably good seeing the led go on and off.

LOVELY.