Experiment – 2
Familiarization with Data Types and Data Structures using Arduino UNO
Task – 2.1 Data Types and Data Structures
Sn / Type / Structures / Bits / Decimal Value / Binary Value / Comments1 / void / - / - / - / -
2 / bool / ? / TRUE or FALSE / 0 or 1 / -
3 / unsigned char
byte
uint8_t / 8 / 0 to 255 / 00h to FFh
4 / char
int8_t / 8 / -128 to 127 / 80h to 7Fh
5 / unsigned int
unsigned short
word
uint16_t / 16 / 0 to 65535 / 0000h to FFFFh
6 / int
short / 16 / -32768 to 32767 / 8000h to 7FFFh
7 / unsigned long
unsigned long int
uint32_t / 32 / 00000000h to
FFFFFFFFh
8 / long
long int
int32_t / 32 / -2147483648 to 2147483647 / 80000000h to
7FFFFFFFh
9 / unsigned long long
unsigned long long int
uint64_t / 64
10 / float / 32 / Floating Point binary32 (IEEE-754)
11 / double / 64 / Floating Point
Binary64 (IEEE-754)
12 / enum / User-defined data type
13 / array
14 / pointer
15 / struct
16 / union
17 / class
18 / xxxxU / Unsigned
19 / xxxxL / Long data format
20 / xxxxUL / Unsigned Long
21 / 123 / Integer constant (10 base)
22 / Ob10101010 / Leading 0b (2 base)`
23 / 0173 / Leading 0 (8 base)
24 / 0x7B / Leading 0x (16 base)
Task – 2.2 Integer Data Types
(1) byte x; unsigned char; uint8_t
x is a symbolic name known as variable or identifier.. It refers to the content of a memory space (or a register space). The keyword byte says that the content of the memory location is 8-bit. The keyword bye (unsigned char or uint8_t) also says that the bit pattern of the memory location should be interpreted as: Every bit carries positive positional value. It means that the value of the variable is always positive.
The memory space is a storage location within the microcontroller. The memory location has as an identification code called address. The address is always a positive numerical value that can be read, stored and displayed. The address can be stored in a pointer variable (*p). Now, p points (holds) a memory location starting from which one or more data bytes could be read (written).
Example: byte x = 0xCA;
We have written x = 0xCA. After the compilation of the statement, a bit pattern of 11001010 is stored in the memory location. Let us remember that the memory location always contains bit pattern regardless of the style we adopt to present the value during program creation. The value of the present variable (x) is always positive.
x = 11001010b
= 1x27 + 1x26 + 0x25 + 0x24 + 1x23 + 0x22 + 1x21 + 0x20
= 128 + 64 + 0 + 0 + 8 + 0 + 2 = 0
= 202
(a) lcd.print(x, DEC); //shows: 202 in (decimal format)
(b) lcd.print(x, BIN); //LCD shows: 11001010 (binary format, actual storage)
(c) unsigned int adr = &x; // variable adr holds numerical address of memory loc.
Serial.print(adr, HEX); //Serial Monitor shows: 16-bit numerical value
(d) byte *p; //pointer variable; it will return 1-byte data
p = &x; //pointer variable holds address
byte m = *p; //m holds the value of x
lcd.print(m, DEC); //LCD shows: 202
(2) char y; int8_t
y is a symbolic name known as variable or identifier.. It refers to the content of a memory space (or a register space). The keyword char says that the content of the memory location is 8-bit. The keyword char (int8_t) also says that the bit pattern of the memory location should be interpreted as: Every bit carries positive positional value except the MS-bit which carries negative positional value. It means that the value of the variable is in 2’s Compliment form and varies from some negative value (-128) to some positive value (+127).
The variable (memory location) stores a character value. Character literals are written in single quotes like this: ‘A’ (for multiple characters – the string - let us use double quotes like this: “ABC”. This is actually an array of three characters. Characters are stored as numbers which are their valid ASCII codes. This means that it is possible to do arithmetic on characters. ‘A’ + 1 = 65 + 1 = 66 = ‘B’.
The memory space is a storage location within the microcontroller. The memory location has as an identification code called address. The address is a numerical value that can be read, stored and displayed.
Example: char y = 0xCA;
We have written y = 0xCA. After the compilation of the statement, a bit pattern of 11001010 is stored in the memory location. Let us remember that the memory location always contains bit pattern regardless of the style we adopt to present the value during program creation. The value of the present variable (y) is:
y = 11001010b
= - 1x27 + 1x26 + 0x25 + 0x24 + 1x23 + 0x22 + 1x21 + 0x20
= - 128 + 64 + 0 + 0 + 8 + 0 + 2 = 0
= -54
(a) lcd.print(y, DEC); //shows: -54 in (decimal format)
(b) lcd.print(y, BIN); //LCD shows: sign bit extended 32-bit FFFFFFCA (binary format)
(c) char y = ‘A’;
lcd.print(y, BIN); //LCD shows: 01000001;
(d) char z = 0x41;
lcd.print(z, BIN); //LCD shows: 01000001
(e) int8_t *p; //pointer variable; it will return 1-byte signed data
p = &y; //pointer variable holds address
char m = *p; //m holds the signed value of x
lcd.print(m, DEC); //LCD shows: -54
(3) unsigned int z = 0xABCD; uint16_t z = 0xABCD;
(a) lcd.print(z, DEC); //LCD shows: 43981
(b) unsigned int *p; //pointer variable; it will return 16-bit unsigned value
p = (unsigned int) z; //p holds address starting from which 2-byte unsigned value
unsigned int m = *p; //m holds: ABCD
lcd.print(m, HEX); //LCD shows: ABCD
Serial.print(m, DEC); //Serial Monitor shows: 43981
(4) int z = 0xABCD; int16_t z = 0xABCD;
(a) lcd.print(z, DEC); //LCD shows: -21555
(b) int *p; //pointer variable; it will return 16-bit signed value
p = (int) z; //p holds address starting from which 2-byte signed value
int m = *p; //m holds: sign extended ABCD (FFFFABCD)
lcd.print(m, HEX); //LCD shows: FFFFABCD (sign extended)
Serial.print(m, DEC); //Serial Monitor shows: -21555
(5) unsigned long int z = 0xABCD1234;
(a) Write codes to print the decimal value of z on the LCD.
(b) Write codes to print the HEX value of z in two parts : 1234 on Top Line of LCD and then ABCD on Bottom Line of LCD.
(6) long long z = 0xABCDEF12ABCDEF12;
Int64_t z = 0xABCDEF12ABCDEF12;
(a) Write to show the HEX value of z in 4 blocks of integer values on the LCD Monitor.
(b) Write to show the DEC value of z in 4 blocks of integer values on the Serial Monitor.
Task – 2.3 Fixed Point Data Types
(1) In Fixed Point representation of numbers, we remember (record) the positions of the decimal points. For example: To add the number 23.45 and 75,37, we follow these procedures:
(a) Take 23.45 as 2345 and convert it into binary number ( 0929h).
(b) Take 75.37 as 7537 and convert it into binary number (1D71h)
(c) Add the number of Step-a and Step-b to get: 269Ah
(d) Convert the binary number of Step-c into BCD, and we get: 9882.
(e) Show the result of Step-d on Display Monitor (LCD, Serial, and 7seg).
(f) Place decimal point after two digits from right, and we get: 98.82 (23.45 + 75.37).
(2) Write program codes for the Tasks of Step-1 and show the result on the Top Line of LCD.
(3) Write codes to multiply the decimal numbers of Step-1, and show he result on LCD.
Task – 2.4 Floating Point Data Types
By definition, a Floating Point Number is a number which contains decimal point. 23.45, 87.67, 12.34, and 11.00 are the examples of floating point numbers.
Task – 2.5 32-bit Representation of Floating Point Number
According IEEE-754 standards, a floating point number (say, 0.15625) is represented by a 32-bit value (3E200000h) as per following definition (Fig-2.1). This standard is also known as binary32 format. The value will occupy four consecutive memory locations of storage area.
Figure-2.1: binary32 format for the representation of a floating point number
(1) The Real Value (Floating Point Number) of an unknown 32-bit number as contained in Fig-2.1 is obtained using the following formula.
Real Value = sign (1-bit: b31)*binary fraction bits (23-bit:b22-b0) * 8-bit unsigned integer exponent (e :b30-b23) - 127
(2) In Fig-2.1, we observe that the binary32 format for the number 0.15625 is 3E200000h.
(3) Before we learn the mechanism of obtaining the binary32 formatted value for the given flp (floating point) number, let us execute few instructions on the UNO to see that the given number is correct.
void setup()
{
float x = 0.15625; //the compiler will store 3E200000h in the memory space
unsigned long *p; //pointer variable p will return 3E200000.
p = (unsigned long) &x; //p holds the address
unsigned long m = *p; //m holds the number 3E200000.
lcd.print(m, HEX); //LCD shows: 3E200000
}
(4) Manual procedures to obtain the binary32 formatted value of 3E200000 for 0.15625.
(a) Calculating Binary of 0.15625
0.15625 x 2 = 0.3125
0.3125 x 2 = 0.625
0.625 x 2 = 1.25
0.25 x 2 = 0.50
0.500 x 2 = 1.000
... continue until the residual is exhausted to 0 or 23 fractional bits are accumulated.
(b) (0.15625)10= (0.00101)2 //using the result of Step-4a
===> (0x20+ 0x2-1+ 0x2-2+ 1x2-3+ 0x2-4+ 1x2-5)
===> (0 + 0 + 0 + 0.125 + 0 + 0.03125)
===> 0.15625
(c) (0.00101)2
===> (1.01)2* 2-3
(d) The binary32 format of Fig-2.1 is:
Sign (1-bit: b31) : 0 //this is a positive number
Biased exponent (8-bit: b30 – b23): -3 (from Step-4c) + 127 (fixed bias) = 124 = 7Ch
Binary fraction digits (23-bit:b22-b0) 01000000000000000000000 (from Step-4c
(e) Arranging as nibbles: 0011 1110 0010 0000 0000 0000 0000 0000
(f) Presenting in Hex: 3E200000h
Task – 2.6 Reconstruct the FLP Number 0.15625 from binary32 3E200000h
Task – 2.7 64-bit Representation of Floating Point Number
According IEEE-754 standards, a floating point number (say, 1.23456) is represented by a 64-bit value (0x3FF3C0C1FC8F3238) as per following definition (Fig-2.2). This standard is also known as binary32 format. The 64-bit value will occupy 8 consecutive memory locations of storage area.
Figure-2.2: binary64 format for the representation of a floating point number
(1) Manually compute binary64 formatted number 0x3FF3C0C1FC8F3238 for the flp number 1.23456.
(2) Write program to find and display the flp number with 5-digit accuracy for binary64 formatted 0x3FF3C0C1FC8F3238.
(3) The data type double supported by Arduino DUE which contains 32-bit SAM3X8E Microcontroller. Double is similar to float; but, double is 64-bit binary64 of IEEE-754 standards and float is 32-bit bianry32 of IEEE-754 standard.
Task – 2.8 Array
(1) A block of memory locations which contain ‘identical type’ data items. The number of items (members) determines the size of the array. When an array is declared and defined, the compiler automatically inserts a ‘null character = ‘\0’ = 0x00) at the end of the last item. The whole array has a name; the individual item has also a name being distinguished from others by the index number.
Figure-2.2: An array of cc-codes
(a) Write code to declare and define array of Fig-2.2.
byte lupTable[16] = {
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6d, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71
};
(b) The array name is: lupTable.
(c) The array size is: 16.
(d) The name of the 1st item is: lupTable[0].
(e) Any item of the array can be accessed for read/write operation using the index number.
(f) The index of the array is: 0 to 15.
(g) The keyboard byte says that size of every data item is an unsigned number (00 to 255).
(h) The compiler (not the user) has inserted null-character at the end of the last item.
(i) lcd.print(lupTable[2], DEC); will show 91.
(j) Write program codes to create a 5 members array where each element (member) is an unsigned 16-bit number.
Task – 2.9 Structure
(1) A structure is a conglomerate of various types of data items except a function. It may contain ordinary variable, pointer, array, and even other structure
(2) Example-A of Structure:
struct date { //date of birth
char name[80]; //Rahim, Karim, …
int month;
int day;
int year;
};
(a) struct is a keyword.
(b) day is called tag which says that the members of the structure are of the types as given.
(c) int month, int day, and int year are the members of the structure.
(d) Variable declaration of type date:
struct date birthday[3];
(e) Variable definition/initialization:
struct date birthday[] = {
{“Rahim”, 1, 12, 55},
{“Karim”, 5, 10, 67},
{“Riko”, 4, 26. 75}
};
(f) Print the name Rahim:
lcd.print(birthday[0].name); //LCD shows: Rahim
(3) Example-B of Structure:
struct account { //account details
int acct_no;
char acct_type;
float balance;
struct date lastpayment; //contains structure as member
};
(a) Variable declaration of type account:
struct account customer;
(b) Variable definition/initialization:
struct account customer = {
12345, ‘R’, 586.30,