Knowledge Base
...
Applied PIC Micro Development
PIC Programming

Binary Numbers

6min

Synopsis

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

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 it's used. Although there are other number types such as integer Numbers 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: for 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 values is required, then two or more registers can be joined together.

Registers

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

PORT RegistersWhen configuring 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.

C
๏ปฟ

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 representation of the binary equivalent.

0xFF

This is the maximum value of an 8 Bit register. This is a HEX representation of the binary equivalent.

Summary

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