Introduction to Embedded C Programming

To start with the programming of any of the advanced microcontrollers, the basic of the C programming needs to be understood thoroughly.

A compiler is system software that converts a high-level language program (human readable format) into object code (machine readable format). It produces software that is fast but to change the software we need to edit the source code and recompile.

C code (z = x+y;) → Assembly code (ADD R2,R1,R0)  → Machine code (0xEB010200)

An assembler is system software that converts an assembly language program (human readable format) into object code (machine readable format).

Assembly code (ADD R2,R1,R0)  → Machine code (0xEB010200)

An interpreter executes directly the high level language. It is interactive but runs slower than compiled code. Many languages can be compiled or interpreted. This means the user typed software to the computer, and the interpreter executed the commands as they were typed. The command window in Keil µVision while running the debugger.

For more information on this interpreter, run Keil uVision and execute Help->uVisionHelp. Next, you need to click the Contents tab, open the uVisionIDEusersGuide, and then click DebugCommands. It will show you a list of debugger commands you can type into the command window.

A linker builds software system by connecting (linking) software components. In Keil µVision, the build command (Project->BuildTarget) performs both the actions of a compilation and a linking. These components that are linked together are

startup.s
uart.c
main.c

A loader will place the object code in memory. In an embedded system, the loader will program object code into Flash ROM. In Keil µVision, the download command (Flash->Download) performs a load operation.

A debugger is a set of hardware and software tools that are used to verify whether the system is operating correctly. The two important aspects of a good debugger are control and observability.

Punctuation marks (semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses) are very important in C. It is one of the most frequent sources of errors for both the beginners and experienced programmers.

Table: Special characters can be punctuation marks

Punctuation

Meaning

;

End of statement

:

Defines a label

,

Separates elements of a list

( )

Start and end of a parameter list

{ }

Start and stop of a compound statement

[ ]

Start and stop of a array index

” “

Start and stop of a string

‘ ‘

Start and stop of a character constant

There are four sections of a C program as shown below.

  • The first section is the documentation section, which includes the purpose of the software, the authors, the date, and any copyright information. When the software involves external hardware,  the information about how the external hardware connection is done could be included.
  • The second section is the preprocessor directives. The preprocessor directive #include is used to connect this software with other modules. Diamond braces < >  are used to include system libraries, like the standard I/O, and quotes ” “  are used to link up with other user code within the project. In this case, the uart module is software written to perform I/O with the universal asynchronous receiver/transmitter (uart).
  • The third section is global declarations section. This section will include global variables and function prototypes for functions defined in this module. The last section will be the functions themselves. The terms subroutine, procedure, function, and program are used interchangeably. Every software system in C has exactly one main program, which define where it begins execution.

There are two types of comments.

  • The first type explains how to use the software. These comments are usually placed at the top of the file, within the header file, or at the start of a function. The reader of these comments will be writing software that uses or calls these routines.
  • The second type of comments assists a future programmer in changing, debugging or extending these routines. These comments are usually placed within the body of the functions. The comments on the right of each line are examples of the second type.

Preprocessor directives begin with # in the first column. The compiler passes through the program handling the preprocessor directives and are processed first.

//**** 0. Documentation Section
 //  This program calculates the area of square shaped rooms
 //  Author: Ankit Modi
 //  Date: 28/06/2018
 //
 // 1. Pre-processor Directives Section
 #include   // Diamond braces for sys lib: Standard I/O
 #include "uart.h"   // Quotes for user lib: UART lib
 
 // 2. Global Declarations section
 // 3. Subroutines Section
 // MAIN: Mandatory routine for a C program to be executable 
 int main(void) {
   UART_Init();  // call subroutine to initialize the uart 
   printf("This program calculates areas of square-shaped rooms\n");
}

Variables and Expressions

Variables are used to hold information. In C, we define a variable by specifying the name of the variable and the type.

Table: Data Types in C

Data type Precision Range
unsigned char 8-bit unsigned 0 to +255
signed char 8-bit signed -128 to +127
unsigned int compiler-dependent
int compiler-dependent
unsigned short 16-bit unsigned 0 to +65535
short 16-bit signed -32768 to +32767
unsigned long unsigned 32-bit 0 to 4294967295L
long signed 32-bit -2147483648L to 2147483647L
float 32-bit float ±10-38 to ±10+38
double 64-bit float ±10-308 to ±10+308

Variables declared outside of a function are properly called external variables because they are defined outside of any function.

  • When variable are defined outside function, these are called global variables.
  • The variables that are defined in another files, which are linked with the program file, are called externals.

The external variables (global as well as external) are used for information sharing between different files or functions.

Variables defined inside a function are called Local variables. These are very important in C programming. They contain temporary information that is accessible only within the function block or subordinate function block. In C, local variable must be declared immediately after a brace { that begins a compound statement. Local variables are created dynamically when their function block is entered, and they cease to exist when program leaves the function block.

Operation Meaning
= Assignment statement
? Selection
< Less than
> Greater than
! Logical not (true to false, false to true)
~ 1’s complement
+ Addition
Subtraction
* Multiply or pointer reference
/ Divide
% Modulo, division remainder
| Logical or
& Logical and, or address of
^ Logical exclusive or
. Used to access parts of a structure
== Equal to comparison
<= Less than or equal to
>= Greater than or equal to
!= Not equal to
<< Shift left
>> Shift right
++ Increment
Decrement
&& Boolean and
|| Boolean or
+= Add value to
-= Subtract value to
*= Multiply value to
/= Divide value to
|= Or value to
&= And value to
^= Exclusive or value to
<<= Shift value left
>>= Shift value right
%= Modulo divide value to
-> Pointer to a structure

To rectify the confusion about precedence, add parentheses () to clarify the expression.

Precedence

Operators

Associativity

Highest () []. ->  ++(postfix)  (postfix) Left to right

++(prefix)  (prefix)   !  ~ sizeof(type) +(unary)  (unary)  &(address)  *(dereference)

Right to left
*   /   % Left to right
+   – Left to right
<<   >> Left to right
<    <=   >   >= Left to right
==  != Left to right
& Left to right
^ Left to right
| Left to right
&& Left to right
|| Left to right
? : Right to left
=   +=   -=  *=  /=  %=  <<=  >>=  |=  &=  ^= Right to left
Lowest , Left to right
Advertisements
%d bloggers like this: