8051 Data Types: Data Types Size in Bits Data Range/Usage
8051 Data Types: Data Types Size in Bits Data Range/Usage
• unsigned char
• signed char
• unsigned int
• signed int
• sbit (single bit)
• bit and sfr
unsigned char:
The character data type is the most common choice. 8051 is an 8-bit microcontroller and
unsigned char is also an 8-bit data type in the range of 0 – 255 (00 – FFH). C compilers use the
signed char as the default data types if we do not put the keyword unsigned char.
Example:
void main(void)
unsigned char i;
for (i=0;i<=255;i++)
P1=i;
}
Signed char :
The signed char is an 8-bit data type. signed char use the MSB D7 to represent – or +. signed
char give us values from –128 to +127.
We always use unsigned char in program until and unless we don’t need to represent signed
numbers for example Temperature.
Example :
//Singed numbers
void main(void)
{
char anynum[ ]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1=anynum[z];
}
Unsigned int :
Example :
#include <reg51.h>
sbit MY_BIT=P1^0;
void main(void)
{ unsigned int z;
for (z=0;z<=50000;z++)
{ MY_BIT=0;
MY_BIT=1;
}
}
Signed int:
Example :
void main(void)
signed int z;
for (z=0;z<=8;z++)
P1=mynum[z];
The bit data type allows access to single bits of bit-addressable memory spaces 20 –2FH
To access the byte-size SFR registers, we use the sfr data type.
Example:
sbit test=P1^0;
sfr PORT=0x80;
void main(void)
{
unsigned char i;
for (i=0;i<=255;i++)
{
PORT=i;
test=~test;
}
}