






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: Mechatronic Systems; Subject: Mechanical Engineering; University: Rose-Hulman Institute of Technology; Term: Unknown 1989;
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!
ME430 Mechantronic Systems Variable Types
Everything stored in a computer is stored in some sort of binary format. I don’t care what it, pro- grams, images, data, doesn’t matter. It must get stored as a much of 1’s and 0’s. The primary building blocks of storage are:
bit: A single storage location, an individual switch which can have a value of 0 or 1.
nibble (less commonly used): A set of four bits. A nibble can represent values from 0000 to 1111 in binary or 0 to F in hexadecimal
byte: A set of eight bits: A byte can represent values from 0000 0000 to 1111 1111 in binary or 00 to FF in hexadecimal
When working with PICs there are really just two primary variable types, integers and floating point numbers. There are lots of other variable related things like arrays, pointers, structs, strings (made from character arrays), etc. Lets start with a look at integers and floating point numbers.
Integers Using the C-18 compiler there are integers of 4 different lengths, 8-bit, 16-bit, 24-bit, and 32-bit. In general you want to save memory space so you should get into the practice of using the small- est size integer that fulfills your needs. For example don’t use 16-bit integers when an 8-bit inte- ger will do just fine.
The advantages of the larger integers are that you can fit bigger numbers into them. If an integer
variable is 8-bits then it can hold 2 8 (256) different numbers. If the 8-bit number is unsigned you can hold numbers from 0 to 255. If the 8-bit number is signed you can hold numbers from -128 to
The names of the bits are: 8-bit char 16-bit int 24-bit short-long (rarely used) 32-bit long
Floating Point In addition to integer variable storage there is also floating point variable storage. Integers are all well and good but as soon as you need a fraction you need a new variable type. If you try to store 1.5 into an interger storage location you will be very dissappointed.
Using the C-18 compiler floating point numbers are 32 bits long. The 32 bits are broken up into 3 different sections, the sign, the mantissa, and the exponent. This format is very similar
to scientific notation. -1.234 * 10^4. In scientific notation the sign is (-) which in binary would be shown as a 1 (1 = negative, 0 = positive). The 1.234 is the mantissa portion and 4 is the exponent.
The primary difference between scientific notation and floating point variable storage is that in floating point variable storage everything needs to be stored in binary. So the exponent needs to be stored in binary (that’s easy) and the mantissa needs to be stored in binary (a little bit
tricky). Also instead of using *10exponent^ floating point variable storage uses 2 so it’s *2exponent.
Here is a blurb from the C-18 Getting Started document (it uses the word significand or fraction instead of the more common work mantissa, they all mean the same thing):
So the mantissa determines the resolution and the exponent is largely responsible for the magnitude. The exponent is called a bias exponent because the value stored needs to have 127 subtracted off before using the value. Since the exponent is biased you don’t need to worry about
2’s complement since you don’t need negative numbers (if 127 is subtracted off). So if you
wanted 2^4 you’d store E = 131.
For the mantissa, there is assumed to be a 1.xxxxx format used. So the exponent is selected such that the manissa is between 1 and 2 (works well when using binary form of scientific notation). Since it’s always 1.xxx you don’t need to store the 1. d 1 then is worth 0.5 if it’s a 1, d 2 is worth
0.25, d 3 is worth 0.125,... It’s a fractional form of binary where each place is worth 2-1^ , 2-2^ , 2 -3,
... So the general form of a floating point variable looks like:
Your turn: What would the 32 bits be when storing: float x = -10;
The down side of floating point variable storage is that it isn’t exact. You simply have 23 bits of resolution in the mantissa. To see this example let’s look at: float x = 0.1;
Sign = 0 Exponent = need 0.0625 = 2-4. So exponent = 123 = 0b 0111 1011 Mantissa = 100 1100 1100 1100 1100 1101
In order to be an exact value the mantissa should repeat 1100 forever. That doesn’t happen. It simply is forced to round at the end. So if you stored the value: float x = 0.100000005; It’s store as: Sign = 0 Exponent = need 0.0625 = 2-4. So exponent = 123 = 0b 0111 1011 Mantissa = 100 1100 1100 1100 1100 1101
Therefore if you tried to compare is 0.1 < 0.100000005. It’d say NO. They are equal. To get around this problem your PC uses 64 bits of resolution. A 64 bit floating point number is called a
Click here to go to the solution
double. (The PIC doesn’t use double just float). Using 64 bits doesn’t fix the problem it just makes it not occur until you have more zeros in there.
So there are a lot of advantages but there are a few things tricky about floating point numbers. A few other disadvantages are that printf doesn’t support floats, and you need to be careful about adding a float to an int. Also if you have a float adding to an int you can’t store it into an int variable.
Arrays In addition to integers and floating point numbers, you’ll probably also need to be able to use arrays. Arrays are lists that have an index. They can be very handy to go through lots of data.
Let’s see an example of using an array:
int i[] = { 1, 2, 3, 4, 5};
int y[5]; y[0] = 10; y[1] = 20; y[2] = 30; y[3] = 40; y[4] = 50;
The top example shows an initialized array. You can use the { } syntax and don’t need to state the
size with that format. The second example should an uninitialized array. With that format you
need to specify the size then load the entries individually. Be careful with arrays because you can
only access the entries you’ve previously created. If you tried to say “y[5] = 60” above you
would be unhappy with the result. You said the array would have 5 spots (“int y[5]”). “y[5]” is
the 6th spot. That doesn’t fly and people make that mistake all the time.
A very common type of array is an array of char’s. This special type of array is usually called a
string. Let’s look at ways to use strings.
Structures Structures are special combinations of variables that you create. If you are really into object oriented programming you might choose to use structures a lot, but to be honest you could probably make it through your C programming career without knowing anything about structs. Here is some example code for creating a struct.
struct { int x; char y[4]; } s1 = { 0x5A, “abc” };
struct { int x[5]; int y; } s2 = { { 10, 22, 30, 40, 50 }, 0xA5 };
In the first example you made a variable of type s1. So it’s not an int or char or float, it’s an s variable type. An s1 variable you decided needs an int and an array of 4 chars. The second example creates an s2 variable with an array of ints and a seperate int.
There are a lot of cool things you can do with structs and there is a lot more I’d need to tell you about how to access the fields ect., but let’s let you look that up somewhere else if you want to learn to use structs. I just wanted you to be aware that they exist.
Pointers A pointer is a variable that stores where a variable is stored in memory. A pointer is always an Integer type and is either 16 or 24 bits when using the C-18 compiler. For this reason when you use a pointer you don’t need to say the variable type. You only need to tell the compiler what type of data the pointer points to. A pointer can point to data in your RAM space, where you usually store your variables, or to ROM space, where you programming code lives.
Let’s look at some code using pointers. ram char * ram_ptr; rom char * near_rom_ptr;
char ram_array[] = "this is RAM"; rom char rom_array[] = "this is ROM";
void main (void) { ram_ptr = &ram_array[0]; near_rom_ptr = &rom_array[0]; }
So in this chunk of code we have 2 pointers. Each pointer points to a char. The first pointer, points to a char in RAM. The second points to a char in ROM. In this example the pointer points to the first char in an array of chars. This is pretty common since pointer syntax works a lot like array syntax. So for example ram_ptr[2] points to the char data ‘i’. The two major new things to pointers are the * and & symbols. * is for derefferencing. That means “what is the value at this pointer location”. The & is the opposite. That means “what is the address this value is stored at”.
Again in order to use pointers effectively you’ll need to look them up. It takes some practice getting good with pointers. The code above also introduces the idea of ROM and RAM space. There is a lot to talk about there. For now I just wanted you to be aware that there is this thing called a pointer and it points to data.