4030002

Document Information

Document Title Binary Nunbers - Intro to Usage and representation
Document Number & Revision 403 - 0002 - 3.0
Release Date April  4, 2019
Document Status Rev 2: April 5, 2019: Content update
Rev 3: April 12, 2019 Heading changes and additional content

Contact»

Synopsis

Understanding number systems is essential to leveraging the most out of the microcontroller. Since most of the code you create will involve initialising and working with values, understanding the numerical attributes of the device is essential. When initialising values, that may be initialised as variable which is the most common. Alternatively, values may be initialised as constant. Variable values can be worked with and changed.

Note
This topic is introductory, and should be read in conjunction with topics to follow.
 

Introduction

A microcontroller is a binary device, and therefore it works with two values at the base level 1 and 0. The "meaning" of the binary value changes depending on where and how its used. Although there are other number types such as integer

Number Application

1 and 0 form the base 2 numbering system and the highest value which can be held by a single, 8 BIT register is 255 or 11111111. When used in code, ie in the compiler, that number can be represented in a number of ways: example OxFF or 'b11111111. Although, the developer may want to represent a number in any of the following ways, it ultimately comes back to the same when the value is compiled however, it functions in 1's and 0's.
If a number of larger value is required, then two or more registers can be joined together.

Registers

In an electronic context 1 is HIGH and 0 LOW. This can be inverted depending on the application. When configuring a register, a 1 sets the register high and 0 clears the register setting it low. Even though technically 1 is high and 0 is low, ensure that you read the datasheet thuroughly to ensure what the affect of changing the value.

PORT Registers

When configuing the PIC Ports, there are three registers that need to be configured. The direction is controlled by the TRIS registers, 1 = input and 0 is Otuput. However, when setting a pin high, it gets set to 1 and 0 clears the pin.

 

TRISB = 0x00; // All outputs configured for output PORTB = 0xff; // All outputs set high

 

Writing Numbers

When using numbers in code, they must be written in a way that the compiler understands and can use. Some examples are shown below.

Numbers and Representation
1 or 0 'Binary representation of a 1 or zero given a high or low value for positive logic, whereas in negative logic their meaning is inverted.
'b10101010 An 8 bt register can be represented as shown left. Each position however has a set value and designation. This designation is used to correspond to specific attributes of a register
255 This is the maximum value of an 8 Bit register. This is a decimal representatio of the binary equivalent.
0xFF This is the maximum value of an 8 Bit register. This is a HEX representatio of the binary equivalent.

Summary

In the next section, we will look at different number types and how they are used in code.