Kiko said:
Gramo thank you.
Let me ask something, as I did not have any class about programming pics I can't understand when you say 1 byte, 2 bytes, etc... I know 1 Byte is 8 bits but I can't make a relationship between a real number or word as bit/byte. For example: Let's say I have a number (1345) or a word (test). How many bits or bytes does it have?
I know it's a basic of programming and perhaps is a stupid question.
Thank you again
Not at all, it’s a problem that’s common with micro controllers and programming (for both assembler and higher languages)
Put simply,
1 Byte =
8 Bits
Lowest value of a byte = 00000000 (8 zero's)
Highest value of a byte = 11111111 (8 ones)
Remember that each place value of a byte is worth a value.
So;
00000001 = 1
00000010 = 2
00000100 = 4
00001000 = 8
Notice each place is worth twice the value before, and that bytes are
right justified
If multiple bits are high, you just add each place value, eg;
00001011
Means 8 + 2 + 1 = 11
I'm leading this to explaining what the maximum value for a byte is, and that is 11111111 (8 one's) =
255. Now that's not a very big number...
You can join two bytes together, and use all 16 bits as one register. Now our largest value is 1111111111111111 (16 ones) =
65535. Now that's looking better! Go ahead and try that in your calculator.
Strings... Text or Strings are a rather inefficient way of working with things, but many devices require you interface with them with strings. An example of this is the humble 16 * 2 LCD.
An
**broken link removed** (put your mouse over any letter to see its ASCII value with that link) contains a
chart in which any value from 0 to 255 means a letter. With this in mind,
each letter/character will require
1 Byte. Most (all) compilers require you to terminate your string/text with a
null (ASCII 0)
You needn't worry though!!
Higher languages are structured to do all of this "on the fly". This knowledge just helps explain why when writing/reading to the EEPROM (internal/external), that some variables take more Bytes (memory) than others
Here are some examples;
**broken link removed**
Back to your original question in the previous post;
The number
1345 could be stored in a
Word (16 Bits = 0 to 65535)
The String
text would require
5 Bytes, one for each letter followed by a
null