PROGRAMMING PIC MICROCONTROLLERS IN C:PICC LITE VARIABLE TYPES

PICC LITE VARIABLE TYPES

The PICC Lite compiler supports the following basic data types:

• bit

• unsigned char

• signed char

• unsigned int

• signed int

• long

• unsigned long

• float

• double

Bit

A bit can store a Boolean variable (a 0 or a 1). Bit variables are usually used as flags in programs. In the following example, the variable answer can only take the values 0 or 1:

bit answer;

Unsigned Char

An unsigned char is 8 bits wide and can store a single ASCII character or an integer number in the range 0 to 255 (bit pattern ‘11111111’). In the following example, variables count and initial are declared as unsigned char and count is assigned a decimal value 120, and initial is assigned the ASCII character ‘T’:

Programming PIC Microcontrollers in C-0098Signed Char

A signed char (or simply char) is 8 bits wide and is used to store signed decimal numbers in the range –128 (bit pattern ‘10000000’) to +127 (bit pattern ‘01111111’). In the following example, the variable first is assigned the decimal value −180, and the variable count is

assigned a decimal value 25:

Programming PIC Microcontrollers in C-0099

Unsigned Int

An unsigned int is 16 bits wide and can be used to store decimal numbers in the range 0 to +65 535 (bit pattern ‘1111111111111111’). In the following example, the variable result is

assigned the decimal value 28 512:

Programming PIC Microcontrollers in C-0100

Signed Int

A signed int (or simply int) is 16 bits wide and it is used to store signed numbers in the range

−32 768 (bit pattern ‘1000000000000000’) to +32 767 (bit pattern ‘0111111111111111’). In the following example, the variable count is declared as a signed int and the negative value

− 25 000 is assigned to it:

Programming PIC Microcontrollers in C-0101

Long

A long data type is 32 bits wide and is used to store large signed integer numbers. The range of numbers that can be stored in a long are −2 147 483 648 to +2 147 483 647. In the following example, the variable sum stores the integer value 45 000:

Programming PIC Microcontrollers in C-0102

Unsigned Long

An unsigned long data type is 32 bits wide and is used to store large unsigned integer numbers. Numbers in the range 0 to 4 294 967 295 can be stored in an unsigned long data type. In the following example, the large number 3 200 000 is stored in the variable cnt:

Programming PIC Microcontrollers in C-0103

Float

A float data type is 24 bits wide and is used to store noninteger fractional numbers (i.e. floating- point real numbers). Variables of this type are implemented using the IEEE 754 truncated 24-bit format. In this format a number consists of:

• a 1-bit sign bit;

• an 8-bit exponent which is stored as excess 127 (i.e. an exponent of 0 is stored as 127);

• a 15-bit mantissa. An implied bit to the left of the radix point is assumed, which is always 1, unless the number is zero itself, when the implied bit is 0.

Programming PIC Microcontrollers in C-0104

Double

A double data type is 24 bits or 32 bits wide (selected during the compilation) and is used to store double-precision floating-point numbers. The truncated IEEE 754 24-bit format is used in 24-bit mode. The 32-bit format is based in the IEEE 754 32-bit standard where the number consists of:

• a 1-bit sign bit;

• an 8-bit exponent stored as excess 127;

• a 23-bit mantissa.

In the following example, the variable temp stores the number 12.34567 as a double-precision floating-point number:

Programming PIC Microcontrollers in C-0105

Leave a comment

Your email address will not be published. Required fields are marked *