Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Homework 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Ver

Embedded Systems
Homework #2
Instructions – General
• There are 5 questions on this assignment. Answer all for full credit. Questions or their subparts
are not necessarily equally weighted.
• You may work in groups of two but you must formulate your own responses, answers or solutions
for each question or problem. Formulating one answer or solution and copying from each other is
not permissible.
• Provide rationale, explanation, trace of solution or reasoning for your answers where applicable.
Simply and magically providing responses or answers for those questions or problems that require
reasoning may not receive any credit.
• Submission must be in hardcopy and delivered by the student in person. Use blank and letter size
typing (printer) paper and in portrait layout.
• You may use handwriting or word processor or combination for your submission. However, it must
be organized, legible and presentable.
• Write your full name, rocket ID and your group member’s name on the top right of the first page of
your submission. For multiple sheets, staple them at the top left.
---------------------------------------------------------------------------------
----------------- Instructions – Specific to this assignment
• You must use Keil/uVision to develop code for embedded applications. Please note that a Keil C
project template for programming questions in this assignment is provided as a single compressed
folder which is named as “KeilProjectTemplateForHw2” and posted with the course Blackboard
site.
• For programming questions, you will need to verify the correctness of your code using the
Keil/uVision debugger. Accordingly, you are expected to capture snapshots (as screen captures)
of relevant debug windows to demonstrate the correctness of your code during execution.
Annotate your screen captures to explain as needed, which must be included in your submission.
• Capture and include in your submission the compiler/linker output window that shows that your
code compiles and links without error or warnings for each programming question.
• Develop a flowchart and pseudocode for each programming question (except question 1) and
include as part of your submission.
• Provide the source code in your submission with proper indentation, label use, commenting and
layout for each programming question.  For programming tasks that is not fully functional,
provide a detailed explanation for what is functional.

Questions

1. Implement and test the following functionalities through C code fragments.


a) Write a C code fragment to check a bit value. Declare three character type variables ch1, ch2
and mask with initial values of 0x34 and 0x20 for ch1 and mask, respectively. C code should set
character type variable ch2 to 1 if the bit 5 in mask is 1, otherwise 0. Note that bits in a byte are
numbered from 0 (lsb) to 7 (msb).
b) Write a C code fragment to set a bit value. Declare two character type variables ch1 and mask
with initial value of 0x34 for ch1. C code should set bit 7 in variable ch1 to 1 using mask
without changing any other bit values in ch1.
c) Write a C code fragment to clear a bit value. Declare two character type variables ch1 and mask
with initial value of 0x34 for ch1. C code should clear bit 3 in variable ch1 to 0 using mask
without changing any other bit values in ch1.
d) Write a C code fragment to toggle a bit value. Declare two character type variables ch1 and
mask with initial value of 0x34 for ch1. C code should toggle bit 4 in variable ch1 using mask
without changing any other bit values in ch1.

2. Write a C program for an embedded application (which must have a dead/forever loop) which
computes factorial of number 5 using a “for loop” structure. Your program should use a single
main() function to implement the functionality, and stay in the dead/forever loop once computation is
complete:

3. Write a C program for an embedded application (using a dead/forever loop) which determines the
maximum valued number in an array of numbers. Declare a global array with 10 elements of integer
type, name it maxValueArray, and initialize the array to following values: -1, 5, 3, 8, -10, 23, 6, 5, 2,
10. Declare a global integer type variable “arraySize” and initialize it to 10. Implement the
functionality within the main() function. Once the task of determining the maximum number is
completed, the main() should stay in a forever loop.

4. Develop the following template into an embedded C program.

/* Global variables here – avoid declaring too many globals unnecessarily as it is not
considered a good programming practice. */
int creditRatingAlice = 750;

// 0 represents “defaulted” and 1 represents “paid” for the payment history


int monthlyPaymentHistoryAlice[24] = {1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1};

void main (void) {


// main(), while looping endlessly, sets a global flag creditStatus to the value
returned by the function // rewardsOrAlarm().

/* Function called when Alice makes a monthly payment or defaults on the monthly
payment.
* Returns nothing, updates global variable
creditRatingAlice. */ void
updateCreditRatingAlice (int opcode, /* 0 if
default, 1 if paid */) {

Page 2 of 4
// each monthly payment increments rating by 10 points all the way up to 800

// every default decrements rating by 10 points down to 700.


}

/* Function that raises an alarm when Alice's rating is 700 for 2


consecutive months, * or rewards her if she has maintained a rating
of 800 for 2 consecutive months.
* Reads montlyPaymentHistoryAlice for
input. * Returns 0 if alarm, 1 if reward. */

int rewardsOrAlarm () {
// Read next value in monthlyPaymentHistoryAlice. Note that this function needs
to keep track of the // last array element accessed so that when it reaches the
last it can rewind to the first in a round // robin fashion to read a value from the
monthlyPaymentHistoryAlice array forever.
// Pass the read value in monthlyPaymentHistoryAlice to function
updateCreditRatingAlice(int x).
// Return reward/alarm value.
}

5. One approach to functional debugging is to collect input and output data for the software for typical
input values. In particular we define a set of input values, run the software, and record the output data
generated by the software. In this exercise we will test the software using input values from 0 to N-1,
where N is 25. Consider the function sum(n)to test. The claim is that this function calculates the
sum of the numbers N down to 1. For example sum(5)is 5+4+3+2+1 which equals 15.
Furthermore, we will also test the hypothesis that the sum of N numbers can be expressed as the
simple calculation of (N(N+1))/2.

The test program will evaluate the function sum(n)and store the result in the array at sumBuf[n].
We need to define a second function fun(n)that simply calculates (N(N+1))/2. We have another
array funBuf that we will use to store the first 25 calculations of this second function. We will check
in the main program for equivalence of the two functions by comparing elements of sumBuf to the
elements of funBuf. The main function should set a flag value based on the outcome of this
comparison.

In a top-down approach, we place the main program on the top and the functions after. In this example,
we will configure the system as bottom up by placing the functions first and the main program at the
end. Notice that bottom up organization does not need prototypes for the functions.

Following C program template with code fragments are given to help you get started:

#define TRUE 1

Page 3 of 4
#define FALSE 0
#define N 25 // array size

unsigned long sum(unsigned long n){


unsigned long partial, count;

} unsigned long
fun(unsigned long n){

int main(void)
{ unsigned long
num;
unsigned long sumBuf[N], funBuf[N]; // arrays to store
computed values int correctFlag;

Write the C code with following functions: sum(), fun() and main() in a bottom-up layout in a
single source code file as an embedded application. Note that function definitions precede the main
function in a bottom-up layout and therefore not requiring function prototype declarations as given in
the above template.

As a secondary method of verification for equivalence of two computations, use Keil/uVision to verify
that sum(n) and fun(n) functions compute the same values by comparing values stored in the
sumBuf and funBuf arrays using appropriate windows in the debugger.

Page 4 of 4

You might also like