Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Embedded

Systems -- Lab 5
20th September, 2019
The objective of this lab is to get familiar with a number of constructs in C programming that
will be useful eventually in embedded systems programming.

Section A
a. Create a function to print out the content of any one dimensional array of integers
b. Create a function to reverse the contents of any array of characters. The size of the array
is not known before hand. It could be any size. This function does NOT print out the
characters. The function does not “return” the array, but allows the array to be changed,
by passing the array as a reference. Hint: Arrays are passed by reference by default to a
function.
c. Create a function called delay1unit() that counts up to a million and does nothing in the
mean time. It just wastes time for a fraction of a second. Hint: you could use a for loop.
d. Create a function whose prototype is void delay(int t) where t is the number of time units
to delay. It calls the delay1unit() t times. This is how the delay(milli_secs) in Arduino has
been implemented.

Section B
The objective of this section is to get familiar with bitwise operations, and the application is to
manipulate bits within a number. By convention, bit positions are zero based. Hence bit zero is
the least significant bit.
The decimal number 40 can be expressed as 0b0010 1000 (as a binary number in C code).
Assign this number to a variable named data. Use a short int as the type.
Hint: the following are equivalent
data=0b00101000;
data=0x28;
data=050;
data=40;
a. Do the following in one function in your code: eg function named
void data40opeartions()
i. Create a fixed mask for position 3 and another for position 4. (hint: create a macro)
ii. Create a mask that can be used for any position
iii. Create a macro for shifts for any position
iv. Create macros for each position (ie POS0, POS1….POS16)
v. Using what has been created up to this point, print out the bit at position 3
vi. Print out the bit at POS4
vii. Set the bit at POS1 to 1 and then print out the new value of data. What do you expect?
viii. Using a loop, >> bits in data and print the current value is POS0 each time. This should
be the value of data printed in reverse. (print 16 bits only)
ix. Using an appropriate loop and >>, reprint the bits, with leftmost bit first. Create a
function call void binary_format(…..) to do this. Extra credit: create a macro to print the
binary format. Do not make use of web resources!

b. Do the following in another function in your code: eg function named void changebits()
Initialize data to 40 again as before within this function.

1
Ask the user for a bit positon which should be set. Within the function, set the bit and print out
the new value of data. Eg if positon zero is to be set, the new value will become 41 (verify this
and others. Make use of a macro to set the bits within the function.
c. Create another function to toggle a bit obtained from user input. Make use of a macro to
toggle the appropriate bits within the function.
d. Create another function to clear a bit obtained from user input. Make use of a macro to
clear the appropriate bits in the function

Use the following function to ask for 4 bit positions


void read4positions(int *c0, int *c1, int *c2, int *c3){
char C[10]; //just to avoid stack smashing
scanf("%s", C);
//printf("data is %s\n", C);
*c0=C[0]-'0';
*c1=C[1]-'0';
*c2=C[2]-'0';
*c3=C[3]-'0';
}
Call the function above as follows:
int d0, d1, d2, d3;
read4positions(&d0, &d1, &d2, &d3);
printf("%d %d %d %d", d0, d1, d2, d3);
//checking values read.

e. Create a new function that calls the read4positions(….) function. This function can
update 4 different bit positons. The function accepts an additional parameter that
indicates what operation is to be performed for all four positions.
• Define macros for the four operations: clear, set, toggle and read. Hint: define as integer
values.
• Using a switch-case statement and the help of macros, if the user selects the clear
operation, clear the bits in the four positions indicated.
• Do same for the three other operations. Eg if the operation is to set, and the positons are
1, 3, 9, 0, set the bits in those four positons to 1.
• If a read operation is specified, print the corresponding bit values, but also call the void
binary_format(…..) function to print out the entire number. If none of these four
operations is selected, print out the current value of ‘data’.

Section C
The objective of this section is to become familiar with how to organize code in multiple files
and the use of preprocessor directives for conditional compilation

i. Move all the function prototypes you have created into fxnprot.h header file. Make sure
to inport appropriate libraries needed in this header file too.
ii. Move all macros and masks into masks.h header file.
iii. Include masks.h in the fxnprot.h file, also include both these files in your main c file.
iv. Use conditional compilation preprocessor directives to avoid duplicate file definitions
during the compilation of your entire project.

You might also like