ECE2049 HW #1-- C programming and Binary Number Representations (DUE 9/1/2017 At the BEGINNING of class) Your homework should be neat and professional looking. You will loose points if your HW is not properly submitted (by properly I mean legible and stapled with a cover sheet including your ECE box number). Any code must be typed to be graded. Please, no papers with ripped or ruffly edges! Always review lecture notes and reading assignments before attempting the homework Show all of your work. No work = No points! Clearly indicate results. Always write neatly. The grader can not be expected to GUESS what you meant to do! Reading -- Davies Ch 1&2, class notes, see Useful Links Problem #1 (25 pts) a) How many times would each of the following loops actually iterate on the MSP430F5529? Explain your reasoning. (10 pts) a) How many times would each loop actually iterate on the MSP430F5529? Explain your reasoning. (10 pts) unsigned char kk; unsigned int inval, out, mm=1, cnt=65535; kk = 1; while (kk > 0) /* do a few things */ /* */ kk++; >> What is the value of kk in the last 3 loops? while (cnt) // equivalent to while (cnt!= 0) inval = (mm + cnt)/cnt; out = myfunction(inval); mm *= 2; cnt = cnt >> 1; What are the values of cnt, mm and inval at the end of the first loop and at the end of the final loop?
b) Rewrite this code segment using a for loop and a switch case statement instead of the while loop and if statements. Note: The function is NOT one from our demo lab. (5 pts) unsigned char trial = 0, tp_history[100];... while (trial < 100) tp = alphatouchpad(); // returns letter A-E for touch pad tp_history[trial] = tp; if (tp == 'A') led_on(0); else if (tp == 'B') led_on(1); else if (tp == 'C') led_on(2); else if (tp == 'D') led_on(3); else if (tp == 'E') led_on(4); trial++; c) In the real world, the data collected from a sensor like a pressure sensor or a temperature sensor or a microphone are often noisy. That is, a single reading is often not reliable. Often the readings over time will be smoothed or averaged usually using a moving averager. Assume we have an array of raw data points. To form an array of M- point moving averages we start with the M-th point in the array and form the average of the of the previous M points then mover over one point in the raw data and forms the average of those previous M points, etc. If r[n] = raw data m[n]= 1 M n r[i] = moving average i=n M +1 Write a complete C program that does the following. In main(), declare 2 arrays, one for the raw data and one for the moving averages, and fill the raw data array with 200 random integers with values between -500 and 2000 exclusive (Hint: In this case you may use the C library function rand() random number generator to help generate the data). Use a compiler directive to define M as a constant equal to 8. Write code (within main) to form a moving average of the data your raw array storing the the results in the moving average array. How will you handle the first few points in your moving average where you have less than M raw data points to average? To what value will you set the first M-1 elements of your moving average array? There is not necessarily a single best answer here but you must justify your code design choice. Code must be typed. (10 pts)
BONUS: 5 pts Assume M is always defined as a power of 2. Implement your moving averager completely using integer variables and integer math. Why would this be desirable way to implement the code on an MSP430F5529? Problem #2 (15 pts) a) Express the following numbers as 16 bit unsigned integers 2309, 58002, 104. Show all your work. b) Express the following number as 16 bit signed (two's complement) integers -5, 7000, -32677. Show all your work. You are given the following 16-bit numbers 2A0Ah, C036h, and D800h. Each of these values could be interpreted as c. An unsigned number d. A sign-magnitude number e. A two's-complement number. Provide the base 10 equivalent of each value assuming each of these interpretations. Show all your work. Problem #3 (10 pts) a) Express the base 10 integer Y = 19765320 in BCD? b) Assume that the variable X is encoded in BCD. What is the decimal equivalent value of X if X = 0101100000100110b? Problem #4 (10 pts) a) What are the ASCII codes (in hex) for the characters '0', '1', '2', '3'.'9'? b) In lab you will regularly need to display numbers on the LCD screen. Therefore you will need to convert between integer digits and their ASCII representations and the reverse. What C code (variable declarations and 1 line of code) would you use convert a single decimal digit to its ASCII code? c) Assume the integer value D = 7531 has been converted for display into an array of ASCII values, declared as char D_asc[4]; What value (in hex) should stored in each array location so that the number would display properly (i.e. in left to right order) if printed on our Sharp LCD screen? Explain you reasoning. Hint: Check the Lab 0 demo code. D_asc[3] = D_asc[2] = D_asc[1] = D_asc[0] =
Problem #5 (25 pts) Convert the following numbers from decimal to IEEE 32-BIT floating point format. a) -128.75d b) 18.20d c) 9.666 The following numbers are encoded using 32-bit IEEE floating point format. Find the decimal values that they represent. Problem #6 (15 pts) d) C2A80000h e) 60C48000h In a table like the one below, show how the following variables would stored successively in memory by (a) a Little-endian CPU (b) a Big-endian CPU Show the values in hex (not binary) starting at address 02400 h. That is, variable ser_num is stored beginning at address 02400h. Label each address in your table. Remember each address in memory holds 1 byte. Is the MSP430F5529 big or little endian? char str[4] = Tst: ; // array of ASCII text // (No NULL terminator) float ss = -128.75; // IEEE 32-bit floating pt (see 5a) // an unsigned 64-bit integer long long unsigned int ser_num = Ox3EF5CBDF009CB6A4; int jj = -5; // a two's comp 16 bit integer int arr[2] = 32767, 4; // 2 element array of integers long unsigned int trp = 182092; Address Little Endian Big Endian... 02402 h 02401 h 02400 h
ECE2049 Homework #1 Submitted by: ECE Box #: << Be sure to include!! Date: Question 1-- 25 (5 pts) 2 -- 15 3 -- 10 4 -- 10 5 -- 25 6 15 Total: 100 (105) Grade YOU MUST ATTACH THIS COVER SHEET TO YOUR HW!!