W1.2 Embedded C
W1.2 Embedded C
Embedded Systems
Embedded C language
https://www.elprocus.com/basics-and-structure-of-embedded-c-program-with-examples-for-beginners/
Frequently used programming conventions
Comments: Operators:
▪ Source code must have additional information ▪ Separated by blank spaces in order to
provided in the form of comments so it can be significantly improve their readability;
clearly interpreted. ▪ A blank space should be used after
▪ Comments are a very important part of commas, semi-colons, or colons.
documentation to ensure it is easy to maintain. ▪ Examples:
▪ Comment for one line: Double diagonal ( // );
BAD: a=(4+c)*2;
▪ Comments for a larger block of code:
Starts with ( /* ) and ends with ( */ ); GOOD: a = (4 + c) * 2;
▪ Example:
BAD: for(i=7;i>=0;i--){}
/*The source code should be written
in such a way that enough GOOD: for(i = 0; i >= 0; i--
information is provided for the ){}
reader to fully understand the
function of the code*/
Pre-processor directives
• The C pre-processor is a program that transform specific parts of the code before
the actual compilation.
• It is also called a Macro Processor as it allows to define short abbreviations for
frequently used code blocks (i.e., macros).
• It uses special directives which begin with the sharp character (#).
• Important directives are
• #include (e.g., #include <stdio.h>)
lets the pre-processor know that you wish to include a file as part of the source code.
• #define (e.g., #define BIT3 0x04)
substitutes the symbolic name by the corresponding expression every time it is found in the
code.
note that no = or ; are used in contrast to regular value assignment: int bit3 = 0x04;
• #pragma
instructs the compiler to use implementation dependent features.
(e.g., where to place interrupt vectors, as we will see learn later)
General C Program Structure
int times_called(void);
void main(void){
static int i;
for (i = 0; i < 10; i++){
• A local variable is known “globally” to its result = increment();
}
function }
• Preserves the variable value after one int increment(void)
{
execution of a function or code block. static int i = 0; //only declared once!
i++;
return i;
}
Data types
A ‘C’ coding standard is a set of rules for source code that is adopted by a team of
programmers working together on a project, such as the design of an embedded
system. Programming teams and companies write down their C coding standards for a
variety of reasons but often bicker internally about which rules to follow.
programming
Prefix Increment ++a Bitwise NOT ~a
Assignment by Addition a += b Bitwise AND a & b
Assignment by Subtraction a -= b Bitwise OR a | b
Assignment by Division a /= b Bitwise XOR a ^ b
Assignment by Multiplication a *= b Bitwise left shift a << b
Assignment by Modulus a %= b Bitwise right shift a >> b
Operator priority
Operator priority (from high to low)
Operator Description
() [] -> . Grouping, scope, member access a ? b : c
Size of type cast (most) unary translates to:
! ~ + - & if (a == TRUE){
operations, …
* / % Multiplication, division, modulo b;
Priority increases
+ - Addition, subtraction }
<< >> Bitwise shift left and right else {
< <= > >= Comparisons: less-than, ... c;
}
== != Comparisons: equal and not equal
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive (normal) OR
&& Logical AND
|| Logical OR
?: Ternary Conditional expression
= += -= *= /= %= &=
Assignment operators
|= ^= <<= >>=
, Concatenation
Bitwise shifts
Left-shift Right-shift
▪ Zeros are shifted in from the right ▪ The sign bit is shifted in on the left, thus
▪ The number of places to shift is given as preserving the sign of the operand
the second argument to the shift ▪ The number of places to shift is given as
operators the second argument to the shift
▪ Example: operators;
x = y << 2; ▪ Example:
Assigns x the result of shifting y to the left by two x = y >> 1;
bits (i.e., acts as a multiplication by 4) Assigns x the result of shifting y to the right by
one bit (i.e., acts as a division by 2)
Pointers
(*) is used to declare a pointer. printf(“Add: %d”, &letter); // output = ‘Add: 200’
printf(“Add: %d”, chptr); // output = ‘Add: 200’
https://www.tutorialspoint.com/cprogramming/index.htm