EENG 383 Microcomputer Architecture and Interfacing Fall 2015 Lab 4 – Audible Distance Indicator September 24, 2015 In this lab you will use the HCS12 microcontroller on the SSMI board to sense distance using an infrared (IR) optical distance sensor, and then sound a tone on a speaker to indicate the distance. 1 PrelabQuestions(completepriortocomingtolab) Read this handout before coming to lab and answer the following questions. Find the pin number for signal AN02 on header H1 on the SSMI board. What Port M pin is hardwired to the speaker on the SSMI board? Find the memory address corresponding to register ATDDR0L. Complete the table of 32 musical notes at the end of this handout (see Appendix F in the Huang textbook, or look up online). An Excel spreadsheet might be helpful for this. 2 A/DInput The HCS12 microcontroller has an analog-to-digital (A/D) converter. You input an analog voltage from 0..5V, and the A/D converter digitizes that into a binary number from $00 to $FF, whose value is proportional to the input voltage. You can then simply read out the binary number from the A/D result register (called ATDDR0L). For example, the input value $00 corresponds to an input of 0.0V, the input value $80 corresponds to an input of 2.5V, etc. Later in the course we will study the details of how the analog-to-digital converter works, but for now we will just treat it as a “black box”. In particular, I will just give you the following code to insert at the beginning of your program, which turns on the A/D converter and initializes some parameters: // Initialize A/D converter. ATDCTL2 = 0xc0; // enable ATD and fast flag clear ATDCTL3 = 0x08; // set the ATD for 1 channel conversion ATDCTL4 = 0x85; // set the ATD for 2 MHz,2 sample clks,8 bits ATDCTL5 = 0xa2; // right justified, continuous conversions of AD2 /* A/D results appear in ATDDR0L */ This code needs to be executed just once at the beginning of your program. It causes the A/D converter to continuously digitize the analog voltage that is present on pin AN02 and put the results into register ATDDR0L. To verify that the A/D converter is working, do the following: 1 EENG 383 Microcomputer Architecture and Interfacing Fall 2015 Create a simple C language program that performs the initialization code above, and then goes into an infinite loop. Identify the pins on the 25K potentiometer in your kit using the multimeter. Draw a schematic for a simple circuit that connects the potentiometer to pin AN02 such that it can supply a variable Figure 1. Potentiometer voltage between 0..5V to that pin. Hook up the potentiometer to the SSMI board. Sign-Off 1: Show the instructor your schematic and implementation. Connect the power supply and run your program. Inspect the memory location corresponding to ATDDR0L and verify that the numbers being written to that location vary according to the potentiometer wiper position. 3 Speaker The SSMI board has a little speaker on it, driven by an NPN transistor. The power for the speaker comes from a source called Vmotor. Take a look at the schematic and see where Vmotor comes from. The control input to the speaker is hardwired to one of the Port M pins (which one?). By writing a series of 1s and 0s to that pin, you can create a square wave which will drive the speaker to sound a tone1 at that frequency. Develop the flowchart or pseudocode description for a program that reads the analog voltage from the potentiometer and looks up a musical note from a table of 32 notes. Then drive the speaker at the corresponding frequency. Low notes should correspond to low input voltage, and high notes to high input voltage. Implement this program in C. Sign-Off 2: Demonstrate your program to the instructor. 4 IRDistanceSensor The Sharp GP2D120 Optoelectronic distance sensor has an analog output voltage that depends on the distance to the nearest object. For the theory of operation, see http://acroname.com/robotics/info/articles/sharp/sharp.html. The sensor has 3 pins: Vcc (power), ground, and Vout (the output signal). The connector on this device is designed to plug into connector J8 on the SSMI board. 1 It will sound a little “tinny” because of the square wave. To generate a pure tone you would have to generate a sine wave; but we can’t do that with a single pin digital output. 2 EENG 383 Microcomputer Architecture and Interfacing Fall 2015 The pin corresponding to Vout from the sensor is hardwired to pin AN02 of the A/D converter. Remove the potentiometer and plug the distance sensor into J8. You should be able to run the same program as before to drive the speaker at a frequency that depends on the distance to the object. Sign-Off 3: Demonstrate your program to the instructor. 5 TableInterpolation Instead of limiting ourselves to 32 discrete tones, change your program to output tones that vary continuously with the input signal. The best way to do this is to linearly interpolate between entries in the table. In other words, if the input value from the A/D converter falls between two table entries, you assume that the desired value falls on a straight line between the table entries. See the Wikipedia article http://en.wikipedia.org/wiki/Linear_interpolation for a description of this method. If you want to use floating point arithmetic in your program, you have to tell CodeWarrior to include floating point capability when you create your project. However, floating point arithmetic is slow in our CPU because it has to perform the operations in software rather than hardware. It is better (although not necessary for this lab) to do the table interpolation using fixed point arithmetic. Ask the instructor if you are unsure how to do this. Sign-Off 4: Demonstrate your program to the instructor. Turn in: Your program, along with a corresponding flowchart or pseudocode. A schematic of the circuit. Note that the sensor’s Vcc is powered by Vsharp on the SSMI board. Show how Vsharp is generated and indicate what the nominal voltage should be. An explanation of the circuit and program. A brief description of how the distance sensor works 6 Extracredit Play a song! The best way to do this is to create a table containing the notes of the song. The notes are represented in the form of the period of the note in microseconds. Then create a program that reads each successive note from the table, and plays that note for a short time2. 2 Note: A “rest” can be easily implemented by playing a note with a frequency that is too high to hear. 3 EENG 383 Microcomputer Architecture and Interfacing Fall 2015 You can play any song you want, but I would keep it short. “Mary had a little lamb” is a particularly easy song because it only has a few unique notes. Or, the Huang textbook actually has all the notes for the “Star Spangled Banner” in Example 8.8. Table 1. A table of 32 notes, partially filled in. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Note Frequency (Hz) Period (microsec) C4 261.63 3822 # b C 4/D 4 277.18 3608 D4 293.66 3405 # b D 4/E 4 311.13 3214 E4 329.63 3034 F4 349.23 2863 # b F 4/G 4 369.99 2703 G4 392.00 2551 # b G 4/A 4 415.3 2408 A4 440.00 2273 # b A 4/B 4 466.16 2146 B4 493.88 C5 C#5/Db5 D5 # D 5/Eb5 E5 F5 # F 5/Gb5 G5 # G 5/Ab5 A5 # A 5/Bb5 B5 C6 # C 6/Db6 D6 # D 6/Eb6 E6 F6 # F 6/Gb6 G6 4 EENG 383 Microcomputer Architecture and Interfacing Fall 2015 Lab 4: Audible Distance Indicator Name: ________________________________Name: ________________________________ Task Sign-Off 1 Description Potentiometer Implementation Sign-Off 2 Speaker Implementation Sign-Off 3 Sensor Implementation Initials 7 Rubric Deliverables (including questions, measurements, etc. from Tasks 1, 2 and 3 ) Task 3: IR Distance Sensor Program with flowchart or psuedocode 20 pts Pre-Lab /8 Questions Schematic, Circuit Explanation / 7 Demonstrations Sensor Explanation /3 Vsharp Explanation /2 Composition Sign-Off 1: Potentiometer Implementation 5 pts Total / 50 pts Extra Credit 10 points maximum 5 5 pts /5 20 pts /5 Sign-Off 2: Speaker Implementation /7 Sign-Off 3: Sensor Implementation /8