C代写:ECE2534 GPIO

代写硬件作业,编写程序,控制ProMX7开发版。

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 won’t 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
2
3
4
5
6
7
8
9
10
11
//////////////////////////////////////////////////////////////////////
// Function name: stringLength
//
// Description: Determines the number of characters in a string.
//
// Input parameters:
// *str, a pointer to the string whose length is to be calculated.
//
// Returns:
// The length of the string.
unsigned int stringLength(char *str);

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
2
3
4
5
6
7
8
9
10
11
12
13
14
//////////////////////////////////////////////////////////////////////
// Function name: stringSearch
//
// Description: Finds the location of the first occurrence of a character // within a string
//
// Input parameters:
// *str, a pointer to the source string.
// sc, the character being searched for
//
// Returns:
// The index of the location of the first occurrence of the character,
// if found, or the value -1, if the character is not found
//
int stringSearch(const char *str, const char sc);

Problem 4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Write a C function that copies one string to another.
//////////////////////////////////////////////////////////////////////
// Function name: stringCopy
//
// Description: Copies the contents of a source string to a
// destination string.
//
// Input parameters:
// *source, a pointer to the source string.
// *dest, a pointer to the destination string.
//
// Returns:
// No value is returned.
//
void stringCopy(char *source, char *dest);

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
2
3
4
5
6
7
8
9
10
11
12
13
//////////////////////////////////////////////////////////////////////
// Function name: onesCounter
//
// Description: Returns the number of ones in the binary
// representation of a 32-bit int.
//
// Input parameters:
// number, the integer being checked.
//
// Returns:
// The number of ones in the binary representation of number.
//
Unsigned int onesCounter(unsigned int number);

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//////////////////////////////////////////////////////////////////////
// Function name: swapHalves
//
// Description: Swaps the positions of the lower and upper 16 bits
// of a 32-bit value. DOES NOT switch the order of the
// bits in each half as the result of swapping the two
// halves.
//
// Input parameters:
// val, the value to be shifted.
//
// Returns:
// The swapped value.
//
unsigned int swapHalves(unsigned int val);

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 don’t 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
2
3
4
5
6
7
8
9
10
11
12
//////////////////////////////////////////////////////////////////////
// Function name: ctof
//
// Description: Perform Celsius-to-Fahrenheit conversion, using
// integer operations only.
//
// Input parameters:
// c, the input value measured in degrees Celsius.
//
// Returns:
// The Fahrenheit temperature that corresponds to the input value.
int ctof (int c);