41081 Chapter Notes - Chapter 2: Operand, C Data Types, Embedded System
Chapter 2 – Embedded Programming
C in Embedded Systems
• C and C++ combined represent 81% of embedded programmers and have their
current programs in these languages.
• On a global scale, C and C++ have a combined popularity rating of 13%.
• It was not originally created for embedded systems but rather for mainframe and
desktop computers which tend to manipulate data in files such as integers or
characters. Thus it was useful for embedded systems which manipulate bits and
occasionally other data types.
C Data Types
• Several C datatypes are commonly used to represent integers in embedded system
programs.
• Different data types have different sizes and ranges so it is important to select the
smallest possible data type that can be used based on knowledge of the variable to
be used. This ensures that the limited space available in embedded systems are
conserved.
• Unsigned data types represent positive integers while signed types represent
positive and negative.
• If a variable is used to represent a series of bits rather than a number, an unsigned
data type should be used.
• Embedded systems commonly deal with 1-bit data items however C does not have a
1-bit data type. So, 1-bit items are typically represented using an unsigned char, and
the programmer will only assign the variable a 0 or 1.
• C also has float and double data types for floating point numbers however their use
is avoided as microcontrollers commonly lack the software required to process these
numbers in order to stay small, low cost, and low power.
• When naming variables, it is common practise to use a lower case prefix to indicate
the data type. This helps to esure that larger ostats or ariales aret assiged
to smaller variables.
find more resources at oneclass.com
find more resources at oneclass.com
• The char datatype was given the name as it is commonly used to represent the
integer value of an 8-bit ASCII character. NOTE that char is actually an integer.
• 8 bits is sometimes called a byte.
• The use of type it is aoided due to its upreditaility. This ireases the code
portability which is the ability to recompile code for a different microprocessor
without undesirable changes in the program behaviour.
• The underlying representation of each datatype is binary.
• Siged data types i C use tos opleet represetatio. The iary
representation of a negative uer i tos opleet is otaied y
represetig the uers agitude i iary, opleetig all the its, ad
adding 1.
o Eg. -1
▪ 00000001 (magnitude 1)
▪ 11111110 (complement)
▪ 11111111 (add 1)
• Knowing whether an item is signed or unsigned is important when assigning values
and when determining a variables range.
RIMS implicitly defined I/O variables
• In RIMS, each microcontroller input and output is implicitly defined as a global
variable. It implicitly defines two additional global variables. A represents the 8-bit
input as a decimal number and B, the 8-bit output.
• The RIMS.h file contains these definitions, so a programmer can enable their use
through a #include statement.
• As B is a global variable, a program can write as well as read that variable. However,
variable A is automatically written by the microcontroller and should never be
written by a program, only read in RIMS. Writing to A will cause a runtime error,
causing execution to terminate.
• C variables can be initialised when declared.
o unsigned char i1 = 5;
• The keyword const short for constant, can precede any variable declaration. A
ostat ariales alue aot e haged y later ode, thus helpig to preet
the introduction of future errors. A constant variable must therefore be initialised
when declared.
find more resources at oneclass.com
find more resources at oneclass.com