You are on page 1of 7

1/28/13

ee476 Lab 1

ECE 4760: Laboratory 1 Human reaction time.


Introduction. You will produce a reaction time measurement device which signals a user by flashing a light and making a short sound, then recording the time it takes for the user to push a button. The user's reaction time will be displayed on an LCD and the minimum measured time recorded in eeprom. The site Human Benchmark suggests that the mean reaction time is around 220 millseconds, with a standard deviation of around 60 mSec. But first you need to set up a board, arrange jumpers, learn how to connect peripheral devices and use the compiler. You will first do a quick exercise to compile an existing program and watch the blinking lights. The LEDs on the board are the embedded programmers low-level, fast, debug tool. Hardware The hardware you will be using to support the MEGA-series is the Flash MCU evaluation board, a small board providing: A target microcontroller (mcu) with onboard flash program memory. A programmer for the target flash memory, including a programmer serial connection to a PC. A separate serial connection for applications. Eight general pushbuttons, eight LEDs and a RESET button. For the testing phase of this lab, connect the LEDs and pushbuttons with jumpers as shown at the left below. You will need a 2-pin jumper for serial communication. You will also need to have a programming jumper as shown on the bottomcenter. The crystal socket is shown with a crystal mounted. Make sure your board has a 16 MHz crystal. The top serial connector (left image) is the programming connector, the bottom one the application connector. For more detail refer to the hardware manual. The rightmost image shows a closeup of programming, reset, power, analog reference and crystal select jumpers.

people.ece.cornell.edu/land/courses/ece4760/labs/s2012/lab1.html

1/7

1/28/13

ee476 Lab 1

For most labs, you will use 10-pin connectors connectors for bringing out the microcontroller signals to user-designed peripheral equipment. Note that there is no protection on these lines. You are connecting directly to the mcu. Prudent design suggests you might want to use buffers when you are uncertain of voltage levels or other conditions. The pin definitions for the connectors are shown below. The inner squares represent pins. In the image above, pin 0 of each connector is at the top-left.

There are cables connecting the 10-pin header plugs shown above to DIP plugs to make it easy to connect to the usual white protoboards. The connection pin-out is given below along with a picture of the cable attached to PORTB of the STK500 and to a white board. In this lab you will use these cables to add an LCD display and to measure capacitance (see circuits below).

people.ece.cornell.edu/land/courses/ece4760/labs/s2012/lab1.html

2/7

1/28/13

ee476 Lab 1

Software Software you will use is freely downloadable and consists of: AVR Studio which simulates the execution of an object file. The course will be using version 4.15 The WINAVR GCC C complier. The course will be using WinAVR 20080610. This utility looks very useful. HAPSIM allows AVRstudio to simulate LCD, buttons, LEDs, keypad and a serial terminal. More information There are examples at AVR Libc and Procyon AVRlib A useful tutorial on AVRLibc, and more. MEGA644 register, bit, and interrupt vector definitions See also Debugging GCC and STK500. Hints on debugging are here.
people.ece.cornell.edu/land/courses/ece4760/labs/s2012/lab1.html 3/7

1/28/13

ee476 Lab 1

Calling assembly language from C http://winavr.scienceprog.com/comment/101 Inline assembly Register use http://people.ece.cornell.edu/land/courses/ece4760/Math/GCC644/fixedPt/FixedPtOps.c http://people.ece.cornell.edu/land/courses/ece4760/Math/GCC644/fixedPt/multASM.S http://people.ece.cornell.edu/land/courses/ece4760/Math/index_GCC_asm.html General hardware Procedures 1. Make sure the evaluation board is connected to power and to the PC as specified in the evaluation board description. Turn on the power supply with the switch on the board. An LED in the middle of the board should cycle from red to yellow to green. For the first part of this lab, there should be jumpers on the PortB to the LED header and on PortD to the switch header. Ask your instructor for help if these are not installed. There should be a two wire connector from pins D.0 and D.1 to the RS232 jumper. 2. Make sure the crystal on the STK500 is actually 16 MHz. 3. There will two serial connections to the STK500. One for programming the Mega644 and one for serial communication between the running program and the PC, which will be running PuTTY. Set up PuTTY for 9600 baud, no parity, 1 stop-bit, no flow-control. In the PuTTY config window, choose serial connection, then click serial in the left hand panel and set the parameters. Connect PuTTY to whatever serial port the USB dongle configures. Use C n r l P n l . S s e . . a d a e oto ae..ytm.Hrwr T b . D v c M n g r B t o . . P r sto find out which serial port is connected to the USB a..eie aae utn.+ot dongle. The STK500 serial port R 2 2 S a ewill be connnected to the dongle. S3 pr 4. Put all files on your Z d i e Do not creat a project on the local drive! Be sure to put all your files rv! there and to back up daily!. 5. There should be a shortcut to AVRstudio on the desktop or start menu. 6. After you define a new project, you can add a C source file and edit it. This first example code is just for testing the hardware setup and to learn how to use AVR studio. Later you will use a different program to measure timer intervals, which is the basis for lab 1. 1. Save this code into your z d i e This program blinks LEDs and responds to buttons. It is rv. organized as three task subroutines. You will use this code to test your hardware setup. 2. In the AVRstudio P o e tmenu choose P o e t W z r . When the dialog box appears, rjc rjc iad choose N w P o e t e rjc. 3. In the new dialog box choose a GCC project, give the project a name, and indicate a source file to use. Choose a location on your Z drive to store the project. 4. In the next dialog box choose to debug with A R s m l t rand choose A m g 6 4 then V iuao Tea4, press F n s iih 5. In the menu item P o e t > C n i u a i n O t o s - G n r l - F e u n yenter rjc- ofgrto pin > eea > rqec 16000000 because the crystal frequency is 16 MHz. 6. Paste this code into the newly created source file. 7. Choose the menu item B i dfrom the B i dmenu. A message window will open to tell you if ul ul there are errors in the code. 8. If the compile is successful, you should be able to download the program to the STK500 board and see some blinking LEDs. To download, select the T o s P o r m A R A t c n e tmenu item which will open a ol>rga V>uoonc dialog box. In the dialog box: 1. In the M i t b set the ISP frequency (S t i g ... dropdown) to 57 Khz, then press an a, etns
people.ece.cornell.edu/land/courses/ece4760/labs/s2012/lab1.html 4/7

1/28/13

ee476 Lab 1

but always be prepared to drop back to the low speed to check the fuze settings in step 3 below. 2. Make sure the device is M g 6 4 ea4. 3. Always check Mega644 fuze settings: 1. Go to the F s stab in the programmer window and set the S T C K E fuse to: ue U_LSL
Etcytloc8 Mz satptm:1kck6 me. x rsa s - H; tru ie 6 l+5 Sc

W i ethen C o e You can try using a higher speed, rt ls.

2. Uncheck the C K I 8box. LDV 3. Uncheck the J A E box and answer YES in the confirming dialog. (If you leave TGN the box checked, PORTC does not work correctly). 4. Click P o r m rga 4. In the P o r m T b F a h b x select the h xfile generated by the compiler. The rga a, ls o, e file will be in the p o e t f l e > e a l f l e . Press the P o r mbutton. If the program does rjc odrdfut odr rga not download to the chip, call a TA. Note that the hex file name is NOT updated for you and will default to the last project compiled on the computer. When the program is running and blinking lights, you should be able to press button 7 to change the blinking rate. Uncomment all the statements necessary to enable serial communication to the PC and make sure it works. Printf statements are a useful way to debug. At this point you can move on to the actual lab exercise below. Timing, eeprom, debounce and LCD: 1. Save TimersGCC644.c, uart.c and uart.h into your directory, add uart.c to the source file list, and build the project. Connect B.3 to D.7 to hook the squarewave output to the comparator input. This program measures the period of a square wave in two diffent ways and demonstrates that polling for a bit is inferior to using the Timer1 capture ISR for measuring time intervals. It is organized as main, one task subroutine and two ISRs. I suggest modifying this code in the assignment below so that the task updates the LCD and another task slowly blinks an LED so that you know your code is running. The example uses timer0 interrupt to maintain the time base for printing data. It uses timer1 capture ISR to measure intervals. You will need to change the ACSR definitions so that AIN1 (B.3) is compared to AIN0 (B.2), rather than the the bandgap reference. 2. Connect the LCD (see below) and run the LCD test program (LCDtestGCC644v2.c, lcd_lib.c, lcd_lib.h). You should see a counter on line 0 and a moving dot on line 1. The LCD library lcd_lib.c and lcd_lib.h are from Scienceprog.com. Be sure to set the crystal frequency in the project options. 3. Remember that a switch which is pushed reads back a logic zero. 4. Timing of all functions in this lab, and every exercise in this course will be handled by interruptdriven counters, not by software wait-loops. This will be enforced because wait-loops are hard to debug and tend to limit multitasking. Specifically, you may not use the delay library routine d l y m . ea_s Using d l y u will be acceptable when you need to produce delays on the order of a few ea_s microseconds. 5. A code showing how to use eeprom. The code records the current system time to an eeprom variable every time you push button D.7. When you press RESET, the last recorded system time is printed to the console. More info at (tutorial1, tutorial2, AVRlib). 6. An example of a debounce state machine for a single STK500 pushbutton (not the keypad) is included here for reference. The Liquid Crystal Display (LCD):
people.ece.cornell.edu/land/courses/ece4760/labs/s2012/lab1.html 5/7

1/28/13

ee476 Lab 1

A 16 character, two line (16x2), LCD display be used as a numerical display. The display we are using has an industry-standard interface. A more detailed data sheet for a similar display shows the command set, but start by reading the demo code, not by reading the data sheet. There are several aspects of the display you should note: Connecting the power backwards may destroy it. Always check the polarity before connecting the display. The 14 pin header is on a corner of the display. LCD pin 1 is closest to the edge of the pcboard. The connection pattern is shown below. Note that pins 7 to 10 on the LCD are not connected and that MCU port pin C3 is not used. LCD pin 1 gnd 2 Vcc 3 contrast ** 4 rs 5 rd 6 en 7 n.c. 8 n.c. 9 n.c. 10 n.c. 11 12 13 14 Mega644 pin gnd on STK500 VTG on STK500 ---------------> port C0 port C1 port C2 ----port C4 port C5 port C6 port C7 Trimpot bottom view: ** contrast is connected to the wiper (connection 2, see below) of a 10K trimpot. Vcc goes to connection 1 and gnd to 3 of the trimpot. Adjust the LCD contrast using a small screwdriver to turn the trimpot. Trimpot schematic:

Trimpot image:

Assignment Timing of all functions in this lab, and every exercise in this course will be handled by interrupt-driven counters, not by software wait-loops. This will be enforced because wait-loops are hard to debug and tend to limit multitasking. Write a C program which will: 1. Wait for the user to push a button. A message on the LCD will tell the user the system is ready. 2. When the button is pushed the program will remove the ready message from the LCD, wait for a random time between about 1.0 and 2.0 seconds,
people.ece.cornell.edu/land/courses/ece4760/labs/s2012/lab1.html 6/7

1/28/13

ee476 Lab 1

then turn on an LED, produce a tone (around 1000 Hz), and start a timer. If the user pushes a button before the LED is turned on, display a "CHEAT" message! 3. When the user presses the same button again the timer will stop, the LED will extinguish, and the tone will stop. The elapsed time since the LED was turned on will be displayed in millseconds on the LCD, along with the fastest time already recorded in eeprom. If the user does not press a button within 1 second, display an appropriate message. 4. When the user presses the same button again the system will go back to the ready state (1) above. If the time just meaured is less than the time in eeprom, the value in eeprom is updated. Initally, the eeprom will be set to 1000 milliseconds. I hope nobody is slower than that. The eeprom value should persist through a power cycle. When you demonstrate the program to a staff member, you should demonstrate all the functions above. Your program should not need to be reset during the demo. Your written lab report should include the sections mentioned in the policy page, and : What is your average reaction time? Is your reaction time faster watching the LED with no sound, or sound with no LED, or with both? A heavily commented listing of your code.
Copyright Cornell University January 3, 2012

people.ece.cornell.edu/land/courses/ece4760/labs/s2012/lab1.html

7/7

You might also like