Requirement
The code that you write for Problem 1 is for a hypothetical microcontroller that is similar to your ChipKit ProMX7. The problem will describe the relevant details of the microcontroller; your task is to write the code that will perform particular tasks. You wont be able to execute the code on your ChipKit ProMX7 to the same effect that is described in the problem, so you only need to submit the Word or PDF document containing your solutions. When doing Problem 1, use the hardware details of the hypothetical ChipKit Pro MX7 given in the problem. Do not use the ones from your actual Chipkit Pro MX7!
Write each of the functions described in Problems 2 - 7. Use the compiler at the web site http://rextester.com to write and test your code. I have provided a skeleton program that you can use as a test-bed for your functions. You should be able to access the skeleton program using the following permalink: http://rextester.com/QNKRAE28683. Insert your functions into this C file accordingly. (We will work on creating package-specific .c and .h files later.)
When testing your code, you may change the values assigned to the strings and integers used as variables, but you must not change any of the function calls or printf statements.
For this assignment, you must submit the following elements:
- A PDF document containing a) the code that solves Problem 1, and b) screenshots of at least three (3) test runs of the program that tests the functions of Problems 2 - 7, and
- A text file containing a full copy of the completed skeleton file, containing all of the functions that you wrote to solve Problems 2 - 7. You should make sure that this code will compile and execute properly when you copy and paste it into an instance of the compiler at Rextester.
Place these two files into a zip file that you call [Last name]_[First name]_HW2.zip. Use your last name and first name to name all of your files. Upload the archive file to Canvas before the submission deadline.
You may use any appropriate reference materials, including material from ECE 2504 and the Internet. However, completed code that you might find on the Internet is not generally an appropriate reference material. You may share references with your classmates, but you may not share code. The work that you submit must be your own.
When doing Problems 2 - 7, you may not call any library functions. When it comes down to it, you are writing library functions!
Problem 1
Imagine a microcontroller like our ChipKit ProMX7 that uses the same GPIO structure and setup, but interfaces a different set of ports to its LEDs and pushbuttons. Pushbuttons BTN1, BTN2, and BTN3 interface with bits 2, 1, and 0 of PORTB, respectively. LEDs LD1, LD2, LD3, and LD4 interface with bits 6, 7, 10, and 11 of PORTD. Remember that the least significant bit of a register is bit position 0.
Using as few lines of code as possible, perform each of the following tasks. For certain tasks, your goal should be to write one line of code. Since this microcontroller uses the same GPIO structure, you may use the same approach for writing to registers and reading from registers that I have described in class and that you will use in Lab 1.
- a. Configure all three pushbuttons as inputs.
- b. Configure all four LEDs as outputs.
- c. Turn on LEDs LD1 and LD2 if BTN3 is pressed, and turn them off otherwise.
- d. Turn on LEDs LD3 and LD4 if BTN1 is pressed or if BTN2 is pressed but not both. LD3 and LD4 should be off if neither of BTN1 and BTN2 is pressed, or if both are.
Problem 2
Write a C function that returns the length of a string.
1 | ////////////////////////////////////////////////////////////////////// |
Problem 3
Write a function that searches for the first occurrence of the character sc in a string. If the character contained in sc is found, the index of the character in str is returned. If the character is not found, -1 is returned.
For example, stringSearch(abcded, d) returns 3, while stringSearch(abcded, g) returns -1.
1 | ////////////////////////////////////////////////////////////////////// |
Problem 4
1 | Write a C function that copies one string to another. |
Problem 5
Write a C function that counts the number of ones in the binary representation of an int.
For example, if number = 0x12345678, the function should return 13, since:
0x12345678 = (00010010001101000101011001111000)2
1 | ////////////////////////////////////////////////////////////////////// |
Problem 6
Write a C function that swaps the positions of the two 16-bit halves of an int. The order of the bits in the two halves should not themselves be changed. This function is functionally the same as circular-shifting the bits of the original number by 16 bit positions.
For example, if number = 0x12345678, the function should return 0x56781234.
1 | ////////////////////////////////////////////////////////////////////// |
Problem 7
Programmers who write embedded-system code must sometimes avoid using floating-point operations. Hardware floating-point units (FPU) tend to be relatively large and expensive, and many microcontrollers dont have them. We could implement floating-point operations in software, but usually we can write code that is much less costly in terms of time and battery life by restricting ourselves to integer operations.
Write a C function that converts an integer representing degrees Celsius to an integer representing degrees Fahrenheit. Your program should round its result to the nearest Fahrenheit degree. You may not use floating-point arithmetic or C math libraries. The rounding problem is trickier than it may seem at first, because different rules are needed for positive and negative results, and because you may only use integer operations.
I recommend that you do some research on rounding techniques, but that you remember that you must incorporate them into an approach that uses only integer operations without also incorporating inaccuracy into your calculation.
For reference, F = (9/5 * C) + 32.
1 | ////////////////////////////////////////////////////////////////////// |