LAB 1 Binary Numbers/ Introduction to C Rajesh Rajamani ME 431 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 1
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 1 0 10 1 x 0 x 01 1 0 0 x 1x 1 101 1 0 1 x 0 x 1x 5 MSB LSB
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 Convert decimal number 104 to binary 104 Integer Remainder 104/ 5 0 5/ 6 0 6/ 13 0 13/ 6 1 6/ 3 0 3/ 1 1 1/ 0 1 Answer: 1101000 1x 6 1x 5 1x 3 64 3 8 104 3
HEXADECIMAL TO DECIMAL CONVERSION Hexadecimal numbers: Representation in terms of powers of 16 0 1 3 4 5 6 7 8 9 10 11 1 13 14 15 Decimal 0 1 3 4 5 6 7 8 9 A B C D E F Hexadecimal Hexadecimal Decimal 10 1 x 1 0 16 16 16 F1 1 0 41 1 1 15 x 16 1x 16 B 1 0 433 1 x 16 11x 16 1x 16 DECIMAL TO HEXADECIMAL CONVERSION Convert decimal number 104 to hexadecimal 104 Integer Remainder 104/16 6 8 6/16 0 6 Answer: 68 1 6 x 16 8 x 16 0 96 8 104 4
HEXADECIMAL TO BINARY One hexadecimal digit gets replaced by 4 binary digits Decimal: 104 Hexadecimal: 68 6: 0110 8: 1000 Binary: 0110 1000 ADDITION OF BINARY NUMBERS Rules: 0 + 0 = 0 1 + 0 = 1 1 + 1 = 10 i.e. 0 + carry 1 1 + 1 + 1 = 11 i.e. 1 + carry 1 5
ADDITION: EXAMPLE Example 14 + 19 = 33 14: 0 1110 19: 1 0011 10 0001 1x 5 0 1x 3 1 33 SUBTRACTION OF BINARY NUMBERS Rules: 0-0 = 0 1-0 = 1 1-1 = 0 0-1 = 10 1 + borrow = 1 + borrow 6
SUBTRACTION: EXAMPLE Example 7-14 = 13 7: 1 1011 14: 0 1110 0 1101 1x 3 1x 1x 0 8 4 1 13 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 7
SIGNED NUMBERS Need to use the s complement to represent a negative number s complement First take 1 s complement +15: 0 1111 1 s complement: 1 0000 Then add 1 + 0 0001 Hence -15: 1 0001 1 0001 SIGNED NUMBERS Check +15: 0 1111-15: 1 0001 10 0000 OK! 8
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 115 3 115/16 7 0 7/16 4 8 4/16 0 4 Hence the hexadecimal equivalent of + 18435 is 4803 9
SIGNED NUMBERS: EXAMPLE Step : 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 10
INTRODUCTION TO C Example Program #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"); } FOR LOOPS Syntax for (k=0; k<5; k++) { printf( %d\n,k); } While loop syntax Covered last week while(..) {. } 11
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,1,,3}; 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 1
ARRAYS The elements of an array can be accessed in 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 in lab is to arrange the elements of an array in ascending order ORDER ARRAY ELEMENTS Determine i th element, i =0,1,,.,n =0 =1 i i Find smallest number among elements i, i +1, i+,.,n Place the smallest number in the i th element This can be done using for loops i =0,1,,3,..,n j =i+1,i+,i+3,..,n i j =i+1,i+,.,n Compare i th element with j th element and swap if j th element is smaller 13
TASKS IN LAB Tasks are described in detail in handout Task 1 Hello World example Task 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 14