lab 3: time and temperature display - wpi · lab 3: time and temperature display in this lab, you...

12
ECE2049 – Embedded Computing in Engineering Design Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature and displays the time to the LCD. You will measure temperature using the ADC12’s internal temperature sensor once per second, using a timer. The main purpose of this assignment is to gain some experience with the MSP430’s analog-to-digital converter. In addition, you will make use of some elements from previous labs and homework assignments like timers, decimal-to-ASCII conversion, and textual display of results. Lab Assignment Digital time and temperature displays are a common sight. In this lab, you will make a mini-time and temperature display using the MSP430. First, you will implement a UTC-style clock with one second resolution. The temperature sensor you will use is the MSP430’s internal temperature sensor, which is connected to analog input channel 10 (ADC12INCH_10). You will also use the “scroll wheel” potentiometer on the lab board to provide additional functionality. Pre-lab Assignment Before coming to lab, you should do the following: 1. READ THE ENTIRE LAB ASSIGNMENT! Write down any questions you may have to ask in lab. 2. If you have not done so already, you should do Problem 3 from HW7, which involves configuring the scroll wheel.

Upload: others

Post on 09-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049 – Embedded Computing in Engineering Design

Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature and displays the time to the LCD. You will measure temperature using the ADC12’s internal temperature sensor once per second, using a timer. The main purpose of this assignment is to gain some experience with the MSP430’s analog-to-digital converter. In addition, you will make use of some elements from previous labs and homework assignments like timers, decimal-to-ASCII conversion, and textual display of results.

Lab Assignment Digital time and temperature displays are a common sight. In this lab, you will make a mini-time and temperature display using the MSP430. First, you will implement a UTC-style clock with one second resolution. The temperature sensor you will use is the MSP430’s internal temperature sensor, which is connected to analog input channel 10 (ADC12INCH_10). You will also use the “scroll wheel” potentiometer on the lab board to provide additional functionality.

Pre-lab Assignment Before coming to lab, you should do the following:

1.   READ THE ENTIRE LAB ASSIGNMENT! Write down any questions you may have to ask in lab.

2.   If you have not done so already, you should do Problem 3 from HW7, which involves configuring the scroll wheel.

Page 2: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 2 of 12

Lab Requirements To implement your time and temperature display, you are required to complete each of the following tasks. As in the previous labs, you do not need to complete the tasks in the order listed. Be sure to answer all of the highlighted questions fully in your lab report. As always, if you have conceptual questions on how to approach any of the requirements or about specific C programming or software design concepts, please feel free to as us—we are happy to help! System Requirements

1.   Startup: No welcome screen this time—just get right to business: as soon as the program is started, your system should display the date (month and day), the time, and the current temperature.

2.   Periodic measurements: Your system should take temperature measurements once per second. To do this, you should configure TimerA2 to measure 1 second intervals. To keep track of the time, implement a UTC-style clock count capable of holding 1 year of time (in seconds). What data type did you use to store your time count?

3.   Initial time value: Since it would be difficult to pre-program your MSP430 with the current time, initialize your UTC count to the last 7 digits of your student ID number (or some similar 7 digit number).

4.   Time display: To display the time, you will need to write code to convert the UTC count into a number of months, days, hours, etc. Manually convert that number to a date and time to confirm your time conversion code is correct. Include an example of this conversion in your report.

5.   Your system will measure temperature using the MSP430’s internal temperature sensor. You should select your reference voltage so provide the best resolution for this sensor. What is the resolution of your temperature readings in volts/bit and °𝐶 per bit? You can find a complete example (adc12_tempSensor.c) for how to configure the ADC12 to read from the temperature sensor on the labs page of the course website and in the lecture notes, which takes readings using single-channel, single-conversion mode. As a test, you can run this example as a separate CCS project by using the same procedure for loading blink.c from Lab 0. You are welcome to use parts of this example in your code—however, keep in mind that you may want to configure the ADC differently to support the other parts of the lab (for example, you may wish to do multi-channel conversions or use interrupts).

(continued on the next page)

Page 3: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 3 of 12

6.   Display format: Your LCD should continually display the following items, formatted as shown below:

Name Format Example Date MMM DD Feb 22 Time HH:MM:SS 10:20:49 Temp (°𝐶)

DDD.F C 24.7 C

Temp (°𝐹)

DDD.F C 72.3 F

Your display should show all of these values at once and should refresh once per second. To display these values, you must write your own functions to build strings from your converted values.

For more information on this, see the “Important Considerations” section of this document. In addition, see the example "String Building Examples" on the Labs page of the course website for some code examples on building strings, including an approach for displaying the months.

7.   To ensure that you only take measurements and update your display at most once per second, you should use the timer to limit how often you take measurements from the ADC and update the display. Thus, it is NOT sufficient to write code like this:

while(1) { tempC = getTemperature(); displayTime(timer); displayTemperature(tempC); // . . . }

In this example, a new measurement is taken as soon as the display update is finished, which could be much faster than one second. Instead, you can keep track of the timestamp when the display was last updated, and only update when more than one second has elapsed.

8.   BONUS (5 pts): Use ADC12 interrupts instead of busy-bit polling to signal completion of your ADC conversions. To ensure that your system takes temperature measurements at a precise rate of once per second, your ADC should be triggered from your timer ISR.

9.   Data Logging: To provide a log of the temperature data you have measured, store the last two minutes of temperature data and the timestamp at which each measurement was taken. You can do this using two arrays (e.g. one for timestamps, one for temperature values). Since our system should run for more than two minutes, you should use “circular indexing” (ie. To store inew data into the array at index idx, compute idx = (idx + 1) % 120) get a new index that "wraps around" the circular buffer. This is more efficient than shifting data around in the array to only keep the last two minutes’ worth of data. See the example "Data Logger Example" on the labs page for more information on this. Explain how you used circular indexing and why it’s efficient in your report.

10.  Serial Console: Your lab template includes some new functions for writing data to a serial connection that is part of the debugger on the Launchpad boards. You can connect a terminal application to this connection to see the data written by the MSP430 on your computer. Configure your system to write the latest 30 seconds of temperature log data to the serial console when a button is pressed.

Page 4: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 4 of 12

You can do this by writing out each log entry as the time and temperature separated by a comma. For this part, you can print the time just as a count of seconds (at least the last 4 digits from your timer count). For example, your console output could look like this:

3512,23.50 3513,23.55 3514,24.60 . . .

For information how to connect the serial console, see “Using the Serial Console” below. Note that you will need keep track of how many entries you have written to your log somehow—that way, if you press the button before 30 seconds have elapsed, you do not print garbage data.

To demonstrate using your log data, copy 30 seconds’ worth of data from the terminal and graph the temperature values in Excel or Matlab. You can cause a measurable temperature change by pressing your finger on the chip for a few seconds (watch the temperature go up on the display), and then removing your finger to let it cool down. Try this experiment and include the raw data and a graph in your report. If the data looks very noisy, this is okay—the output from the internal temperature sensor seems to fluctuate considerably.

For more information on how this part could work, see the example "Data Logger Example" on the labs page of the course website—you don't need to copy this example exactly, but it should familiarize you with the concepts involved.

11.  Scroll Wheel: Configure the ADC to read values from the scroll wheel (using your work from HW 7 as a guide). Remember that you are also using the ADC12 to take readings from the temperature sensor. How will you use the ADC to take readings from two inputs? There are many valid ways to do this. Explain how you configured the ADC for both sensors and how you set the reference voltages for both inputs.

12.  Using the Scroll Wheel: We didn’t hear enough of the buzzer in lab 2, so we will use it again in this lab to give us something to do with the scroll wheel J. When the button S1 on the lab board is pressed, the buzzer should sound—the pitch of the buzzer should be controlled by the scroll wheel. That is, when the wheel is turned in one direction, the pitch of the buzzer should go up. When the wheel it turned in the other direction, the pitch should go down. Pressing button S2 on the lab board should turn the buzzer off. For this part, you can read from the scroll wheel at a rate faster than once per second if you wish. It is up to you to decide how you will use the output value from the scroll wheel to control the buzzer’s pitch--explain how you do this in your report.

13.  EXPERIMENTAL BONUS (Up to 25 pts): Want to try some never-before-tested lab components? If so, read the section below for a description of some extra bonus features you can complete!

14.  Write a high quality lab report using the instructions below. Be sure to answer all of the questions in this section!

Page 5: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 5 of 12

Experimental Bonus: Using power modes

Do you want more extra credit? Want to try some never-before-tested lab components? If so, you can complete the extra bonus challenges described below. These brand-new components are highly experimental and not well tested—if they don't work, that's fine—please just share your experiences with the course staff and document it in your report. Your work is much appreciated, and will be very helpful in developing new lab materials for future versions of the course!

1.   EXPERIMENTAL BONUS Part 1 (10 pts): Once you have the rest of the lab working and are using ADC interrupts triggered by the timer, you can measure the amount of time it takes for your main function to perform one full of conversions and display updates (that is all of the code that runs in main() once per second).

To do this, configure an LED (for example, P1.0 on the Launchpad) as an output. When your conversion and update process starts, turn on the LED; when it finishes, turn it off. Instead of watching the LED, disconnect the LED's jumper (don't lose it!), connect an oscilloscope to the LED pin and use it to measure the amount of time the LED is on. Feel free to ask the course staff for help on this. Record the average over a few samples and discuss it in your report. Does your update function meet your requirement to update the screen every second?

2.   EXPERIMENTAL BONUS Part 2 (15 pts): If your update process does not complete in one second, this is expected. The graphics library is very slow and does not make this an easy task. Try to optimize your code slightly to reduce the amount of time to perform conversions and build strings. Some suggestions:

a.   Ensure you are only calling Graphics_flushBuffer() once. In addition, instead of clearing the display, use OPAQUE_TEXT instead of TRANSPARENT_TEXT when drawing strings to ensure you are only updating the pixels you need.

b.   The only time you should need floating point math is when converting the temperature.

c.   Avoid performing operations on longs unless necessary (when converting the UTC time), as operations on 32-bit datatypes must be emulated in software.

Do not go crazy with the optimizations! (Unless you have a lot of time and want to have some fun…). Before optimizing beyond (a) and (b), please consult with the instructor about your results so far.

If your update process does complete within one second, refactor your code so that your conversion and update process takes place in your timer ISR. Then, configure your program to enter a low power mode (Which one should you choose?) when not running an interrupt—see the lecture notes for details on this. Measure your update routine's execution time similar to part 1: what proportion of time does your program spend in active mode? How much power does this save in terms of average current consumption?

Page 6: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 6 of 12

Important Considerations and Coding Standards All of the previous software design guidelines from the previous labs still apply to this one. In particular, you should remember the following requirements:

•   Try to use sane control flow •   Always keep your ISRs short •   Avoid using floating point math when possible

As always, if you have questions on these guidelines, or need help structuring your code, feel free to ask us for help! In addition to the typical coding standards, this lab has two unique software design considerations involving number conversions and string building, which are described below.

Number Conversions

When converting the time and date, you can sometimes encounter some fun issues relating to how the MSP430 compiler handles constant data types. If you type a line like this:

long x = 24 * 3600;

You will see a warning reading “Integer operation is out of range.” This is because the compiler will treat the constants 24 and 3600 as ints before multiplying them, meaning the multiplication will overflow! This is silly, but it makes sense: operations on longs are emulated in software, so the compiler wants to avoid using them as much as possible. To make the compiler actually treat the constant values as longs, you can either use integer suffixes to tell the compiler that the constants are of type long, like this:

long x = 24L * 3600L;

… or you can cast any of them:

long x = ((long)24) * 3600;

Using either of these options will have the same effect. When you perform any conversions, it is a good idea to check the results of the conversion in the debugger at each step to make sure your math is correct. This can make it a lot easier to debug issues caused by intermediate computations. The moral of this story is to always pay attention to your compiler warnings, which will point out if your code is affected by this issue. The compiler is your friend—it’s trying to help you out, so listen to it! Building ASCII Strings

A decent portion of this lab involves formatting data to display it for the user. You can use a similar approach to displaying the player's score back in lab 2: to display an integer value, you must convert it into a character array to display on the screen with the necessary formatting. To do this, you will need to get each digit from the value and format it as an ASCII character in the correct position. For example, to display the time, you might write a function similar to the example below:

Page 7: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 7 of 12

void display_time(int hour, int min, int x, int y) // Display time as hh:mm { unsigned char str[6]; // Declare a buffer of 5 chars, which we will fill str[0] = ??? // Get the first digit of the hour str[1] = ??? // Get the second digit of the hour str[2] = ':' // Add a ":" str[3] = ??? // First digit of minute str[4] = ??? // Second digit of minute str[5] = '\0'; Graphics_drawStringCentered(&g_sContext, str, AUTO_STRING_LENGTH, x, y, OPAQUE_TEXT); }

To fill in the remaining parts, ask yourself the following:

1.   Given an integer (say, 5678), how can you (in code) find the digit in the "ones" place? You can do this using a single math expression.

2.   How can you convert, for example, the integer 7 into the ASCII character '7'? Again, you can do this in a single expression. (Hint: ASCII table.)

For an example of how to do this—and a method to handle displaying the month, see the code example "String Building Examples" on the Labs page of the course website with the resources for this lab. You are not permitted to use library functions like sprintf() to do this. Why? To put it simply, sprintf() is big and slow—it uses a lot of code memory, is very slow, and can easily overflow the stack.

(continued on the next page)

Page 8: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 8 of 12

Using the Serial Console Using the serial console requires a bit of configuration on your computer. Please follow the instructions for your operating system. If you are using CCS using via the virtual machine, please follow the Linux instructions to connect to the console from inside the VM.

If you encounter any issues with this process, please do not hesitate to contact the course staff. These instructions are new, so any feedback is greatly appreciated!

From Windows

1.   First, we need an application that can connect to the serial port on your computer. On Windows, a widely-used application for this is PuTTY. You can download putty using the following link: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

You do not need to download the installer for PuTTY, just download the application under the “alternative binary files” section here:

Figure 1: PuTTY download link

2.   Connect your MSP430 board to your computer and open the Device Manager from the Windows start menu (you can find it easily by searching). On the lab computers, you will see a warning indicating that you are not an administrator of the computer—this is normal. Once the device manager opens, expand the “Ports” section and you should see a port labeled “MSP Application UART1”. This is the serial port created by our MSP430!

Figure 2: Device manager port list

3.   Take note of the COM port number listed next to the device name (in this figure, it is COM184). Now, open PuTTY and configure it as follows:

•   Select Serial for the connection type and enter the COM port number you found in the previous step in the “serial line” field.

•   Ensure the “Speed” field is set to 9600, which should be the default. •   In addition, click on the “Serial” category in the sidebar on the left and make sure your

settings look like Figure 3.

Page 9: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 9 of 12

Figure 3: PuTTY serial settings

4.   When you are sure your settings are correct, click Open. You should be presented with a blank terminal window. Test the console by loading the lab 3 template onto your board. You should see “Hello world!” printed to the console when the program starts and should see input when pressing keys on the keypad. Take a few minutes to examine the code in this template for examples on how to use the functions for the serial terminal.

On Linux

1.   Ensure the lab board is connected to your computer.

2.   Open the terminal application using the “Ubuntu” menu (top left icon in the taskbar). You can find it easily by searching for “terminal” and selecting the application as shown below:

Figure 4: Opening the terminal

(Instructions continue on the next page)

Page 10: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 10 of 12

3.   In the terminal, enter the following command: “ls /dev/ttyACM*” (without the quotes). This

command shows all available console devices of the type provided by the lab board. You should see two devices in the list (/dev/ttyACM0 and /dev/ttyACM1), similar to Figure 5.

Figure 5: Serial TTY device list

4.   Both console devices listed are from our lab board: one is for the debugger and the other is for the serial console. Unfortunately, there is currently no easy and way to identify which one is which without some trial and error: you should connect to one device and see if it works—if it doesn’t, you can try the other one. I recommend trying /dev/ttyACM1 first. We will connect to the terminal devices using an application called screen. Connect to one of the devices by entering the command “screen /dev/ttyACM1”. Once you press enter, the content of your terminal will be replaced with a blank screen while the program waits for input. To test the console, load the lab 3 template from the course website onto your board. When it starts running, you should see “Hello world!” printed to the console and you should see output when pressing keys on the keypad.

5.   If you did not see any output after running the lab 3 template, you need to go back and try connecting to the other device. To exit screen, press Control-A, release it, then press k, then press y. Your terminal should return to normal. Now repeat step 4, but using the name of the other device (e.g. run “screen /dev/ttyACM0”).

On Max OS X

To use the serial console on OS X, you can follow the same instructions as for the Linux version, except with a few minor differences, as outlined below:

1.   When opening a terminal, use the "Terminal" app found in the Applications menu. 2.   To connect to a serial port, your device will have the name /dev/ttyUSB.xxxxxx (where xxxxxx is

some random string of characters and numbers) instead of /dev/ttyACM1. If you encounter issues with these instructions or have suggestions on how they can be improved, please

do not hesitate to share your thoughts with the course staff!

Page 11: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 11 of 12

Writing your Report Your lab should be written in a professional style. It should be an electronically-prepared technical document like what you would submit to a fellow engineer or your boss. Only one report is required per lab team. The report should include:

•   Introduction (1-2 paragraphs max): Succinctly state the objectives of the lab and give an overview of what you accomplished.

•   Discussion and results: Discuss what you did in each part of the lab and how you solved any problems. Describe what you did and be sure to thoroughly answer and explain the questions asked in the lab assignment. In general, this section should be as long as necessary to say what you need—no padding or fluff!

•   Summary and Conclusion (1-2 paragraphs max): Summarize what you accomplished in the lab and what you learned. This should be a “bookend” to the introduction.

•   Appendices: You should not need any in this lab. DO NOT PASTE YOUR CODE INTO THE END OF THE LAB REPORT! Instead, your code will be submitted as an archive file alongside your report, which is a lot cleaner!

Lab reports are important. In industry, the FIRST view of YOUR work by anybody other than your

immediate supervisor will see will probably be in WRITING!

Learning to be an effective communicator of technical information is probably THE MOST IMPORTANT

job skill you can have.

Submitting your Work When you are done with your report, you will submit it and your code on Canvas for grading. In order to receive a grade, you must submit both your code and your report online. Only one member of your team needs to submit files for your lab. In addition, you must turn in your signoff sheet to the course staff—usually, you will do this when receiving your last signoff. If not, you can turn it in by placing it in the box in the ECE office, or handing it to a member of the course staff. To submit your code for grading, you will need to create a zip file of your CCS project so that the course staff can build it. You can also use this method to create a complete backup copy of your project (perhaps to send to your partner or save for later). Unfortunately, the only reliable method for doing this is from inside CCS using the instructions below—do NOT attempt to just create a zip file of your code. To export your code:

1.   Inside CCS, right click on your project and select "Rename..." 2.   If you are submitting your project, enter a name in the following format:

ece2049e18_lab3_username1_username2, where username1 and username2 are the user names of you and your partner. (NOTE: Failure to follow this step will result in points deducted from your lab grade! If you don’t do it, it makes a lot of extra work for the graders!)

3.   Click OK and wait for CCS to rename your project. 4.   Right click on your project again and select "Export...” then select “General” and "Archive file" from

the list and click Next.

Page 12: Lab 3: Time and Temperature Display - WPI · Lab 3: Time and Temperature Display In this lab, you will use an MSP430 and several peripherals to implement a system that measures temperature

ECE2049: Lab 3

ECE2049-E18 Page 12 of 12

5.   In the next window, you should see the project you want to export selected in the left pane and all of the files in your project selected in the right pane. Select all. You should not need to change which files are selected.

6.   Click the "Browse" button, find a location to save the archive (like your R drive) and type in a file name using the EXACT SAME NAME used in Step (2).

7.   Click "Finish". CCS should now create a zip file in the directory you specified. 8.   Go to the Assignments page on the class Canvas website. Click on the assignment for Lab 0 and attach

the archive file of your project that you just created and your report. When you are ready, hit the Submit button. Only one code and report submission is required per team.