lecture1_new

lecture1_new - LAB 1 Binary Numbers/ Introduction to C...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 Introduction to C Tasks in Lab 1 1 FEEDBACK CONTROL Automatic control: Ensuring that one or more variables of interest are controlled to desired values Examples The variables of interest are called “outputs” “Control inputs” are utilized for achieving automatic control “Disturbance inputs” might act on the system Cruise control Temperature control Elevator control The power of feedback – Explain Sensors and actuators BINARY TO DECIMAL CONVERSION Binary Decimal 10 1 x 21 0 x 2 0 2 01 0 x 21 1 x 20 1 101 1 x 2 2 0 x 21 1 x 20 5 MSB LSB 2 DECIMAL TO BINARY CONVERSION Covert decimal number 104 to binary 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 Answer: 1101000 1 x 2 1 x 25 1 x 23 64 32 8 104 6 HEXADECIMAL TO DECIMAL CONVERSION Hexadecimal numbers: Representation in terms of powers of 16 Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hexadecimal 0123456789 A B C D E F Hexadecimal Decimal 10 1 x 161 0 x 16 0 16 F1 15 x 161 1 x 16 0 241 1B 1 1 x 16 2 11 x 161 1 x 16 0 433 3 DECIMAL TO HEXADECIMAL CONVERSION Convert decimal number 104 to hexadecimal 104 Integer Remainder 104/16 6 8 6/16 0 6 Answer: 68 6 x 161 8 x 16 0 96 8 104 HEXADECIMAL TO BINARY One hexadecimal digit gets replaced by 4 binary digits Decimal: 104 Hexadecimal: 68 6: 0110 8: 1000 Binary: 0110 1000 4 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 ADDITION: EXAMPLE Example 14 + 19 = 33 14: 19: 0 1110 1 0011 10 0001 1 x 25 1 x 20 32 1 33 5 SUBTRACTION OF BINARY NUMBERS Rules: 0-0=0 1-0=1 1-1=0 0 - 1 = 10 – 1 + borrow = 1 + borrow SUBTRACTION: EXAMPLE Example 27 - 14 = 13 27: 14: 1 1011 0 1110 0 1101 1 x 23 1 x 2 2 1 x 20 8 4 1 13 6 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 0 10 1110 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 7 SIGNED NUMBERS Check +15: 0 1111 -15: 1 0001 10 0000 OK ! 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 ! 8 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 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 9 SIGNED NUMBERS: EXAMPLE Step 4: Add 1 1011 0111 1111 1100 1 1011 0111 1111 1101 Step 5: Convert back to hexadecimal B7FD Final answer: B7FD TASKS IN LAB Tasks are described in detail in handout Task 1 Task 2 Write a program to take in user input temperature and convert temperature in Fahrenheit to Celsius Task 4 Check addition of hexadecimal numbers from pre-lab Task 3 “Hello World” example Introduction to Matlab, plotting Important: Pre-labs, Post-labs 10 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"); } 11 ...
View Full Document

Ask a homework question - tutors are online