1
LAB 1 Binary Numbers/ Introduction to C
Rajesh Rajamani
ME 4231
Department of Mechanical Engineering
University Of Minnesota
OVERVIEW
What is feedback control?
Binary and Hexadecimal Numbers
Working with the C Language
Tasks in Lab 1
2
FEEDBACK CONTROL
Automatic control: Ensuring that one or more variables of interest are controlled to desired values
Examples Cruise control
Temperature control
Elevator control
The variables of interest are called “outputs”
“Control inputs” are utilized for achieving automatic control
“Disturbance inputs” might act on the system
The power of feedback – Explain
Sensors and actuators
BINARY TO DECIMAL CONVERSION
Binary
Decimal
01 221 x 0 x 01 201 220 x 1 x 10 1
012 2221 x 1 x 0 x 101 5
MSB LSB
3
BINARY NUMBERS
Why are binary numbers needed in programming with data acquisition systems ?
The digital electronics in computers inherently store data as binary numbers
Counters, analog-to-digital-converters, and other systems on a data acquisition card measure electrical signals and convert them to binary numbers
Low cost embedded processors and microcontrollers do not have floating point units
It is computationally efficient to deal entirely with data in binary format
DECIMAL TO BINARY CONVERSION
104 Integer Remainder
104/2 52 0
52/2 26 0
26/2 13 0
13/2 6 1
6/2 3 0
3/2 1 1
1/2 0 1
Convert decimal number 104 to binary
Answer: 1101000104832642221 356 x 1 x 1 x
4
HEXADECIMAL TO DECIMAL CONVERSION
Hexadecimal
Decimal
01 16161 x 0 x 01 16
01 1616 x 1 x 15 1 F 241
012 1616161 x 1 x 11 x 11 B 433
Hexadecimal numbers: Representation in terms of powers of 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 A B C D E F
Decimal
Hexadecimal
DECIMAL TO HEXADECIMAL CONVERSION
104 Integer Remainder
104/16 6 8
6/16 0 6
Convert decimal number 104 to hexadecimal
Answer: 68
10489616166 01 x 8 x
5
HEXADECIMAL TO BINARY
One hexadecimal digit gets replaced by 4 binary digits
Decimal: 104
Hexadecimal: 68
Binary: 0110 1000
6: 0110
8: 1000
ADDITION OF BINARY NUMBERS
0 + 0 = 0
1 + 0 = 1
1 + 1 = 10 i.e. 0 + carry 1
1 + 1 + 1 = 11 i.e. 1 + carry 1
Rules:
6
ADDITION: EXAMPLE
Example
14 + 19 = 33
14: 0 1110
19: 1 0011
10 0001
33 32 2 x x 1121 05
SUBTRACTION OF BINARY NUMBERS
0 - 0 = 0
1 - 0 = 1
1 - 1 = 0
0 - 1 = 10 – 1 + borrow = 1 + borrow
Rules:
7
SUBTRACTION: EXAMPLE
Example
27 - 14 = 13
27: 1 1011
14: 0 1110
0 1101
13 1 4 8 2 x 2 x x 023 1121
SIGNED NUMBERS
A positive number has 0 as the most significant bit
A negative number has 1 as the most significant bit
+ 15: 0 1111
- 15: ?? 1 1111 ????? NO
0 1111
1 1111
10 1110 0
8
SIGNED NUMBERS
Need to use the 2’s complement to represent a negative number
2’s complement
First take 1’s complement
+15: 0 1111
1’s complement: 1 0000
Then add 1
+ 0 0001
1 0001
Hence -15: 1 0001
SIGNED NUMBERS
Check
+15: 0 1111
-15: 1 0001
10 0000
OK !
9
INTERPRETING SIGNED NUMBERS
What is 1 0001 ?
Answer:
From the MSB, it is clear that it is a negative number
Therefore, first subtract 1
1 0001
0 0001
1 0000
Then take 1’s complement 0 1111
Hence the number is -15 !
SIGNED NUMBERS: EXAMPLE
Find – 18435 in hexadecimal
Step 1: First find +18435 in hexadecimal
18435 Integer Remainder
18435/16 1152 3
1152/16 72 0
72/16 4 8
4/16 0 4
Hence the hexadecimal equivalent of + 18435 is
4803
10
SIGNED NUMBERS: EXAMPLE
Step 2: Convert to binary
4803 in binary is 0100 1000 0000 0011
Step 3: Take 1’s complement
1011 0111 1111 1100
SIGNED NUMBERS: EXAMPLE
Step 4: Add 1
1011 0111 1111 1100
1
1011 0111 1111 1101
Step 5: Convert back to hexadecimal
B 7 F D
Final answer: B7FD
11
INTRODUCTION TO C
#include <stdio.h> void main() { float x, y; printf("\nEnter number"); scanf("%f",&x); printf("\n%f\n",x); y = x*x; printf("\nHere is the square of the number:%f\n\n",y); printf("Enter the number whose square you wish to find (q to quit)\n"); while(scanf("%f",&x) == 1) { printf("\nx=%f",x); y = x*x; printf("\ny=%f",y); printf("\nEnter the number whose square you wish to find (q to quit)\n"); } }
Example Program
FOR LOOPS
Syntax
for (k=0; k<5; k++)
{
printf(“%d\n”,k);
}
While loop syntax Covered last week
while( …..)
{
….
}
12
ARRAYS
Example: Declaration of an array
The line
int count[10];
declares “count” to be an integer array of size 10.
In C, all arrays start with slot 0 (rather than slot 1)
Hence,
the first element in the array “count” is called count[0]
The last element in the array “count” is called count[9]
ARRAYS
An array can be declared with no initial values – Then its initial values are unpredictable
If an initializer is given, then array length may be omitted from the square brackets in the declaration statement
Example:
int count[] = {3,18,4,7,6,5,9,21,22,23};
Note that the size of the array count is not explicitly declared.
However, because of the initializer, count will be an array of size 10
13
ARRAYS
The elements of an array can be accessed in 2 ways
Using subscripts
Using pointers
We will use subscripts in this lab.
Pointers will be covered in the next lab.
Example: Write a program to reverse order the elements of an array
Note: Task 2 in lab is to arrange the elements of an array in ascending order
ORDER ARRAY ELEMENTS
Determine th element, =0,1,2, ….,n
Find smallest number among elements , +1, +2,….,n
Place the smallest number in the th element
This can be done using 2 “for loops”
=0,1,2,3,…..,n
=i+1,i+2,i+3,…..,n
=0 =1
=i+1,i+2,….,n
Compare th element with th element and swap if th element is smaller
i i
i i
i
i
j
i
i i i
i
j
j
j
14
TASKS IN LAB
Tasks are described in detail in handout Task 1
“Hello World” example
Task 2 Check addition of hexadecimal numbers from pre-lab
Task 3 Write a program to take in user input temperature and
convert temperature in Fahrenheit to Celsius
Task 4 Sorting elements in an array
Task 5 Introduction to Matlab, plotting
Important: Pre-labs, Post-labs