Basics of Embedded C Programming Introduction PDF
Basics of Embedded C Programming Introduction PDF
Programming
Ease of Implementation
Ease of Maintenance
Readability
Smart Logic Academy 8
Basics of Embedded C Programming
•Machine Code
•Low level language, i.e., assembly
•High level language like C, C++, Java, Ada, etc.
•Application level language like Visual Basic, scripts, Access, etc.
•it is small and reasonably simpler to learn, understand, program and debug.
C++
Java
Assembly
Language Embedded C
Programming
#include<reg51.h>
Address Opcode Operand
int main()
{ 0000 78 01
HERE: MOV R0,#01H
while(1) 0002 79 02
MOV R1,#02H
{ 0004 E8
MOV A,R0
char R0=1,R1=2,A; 0005 29
ADD A,R1
P0=R0+R1; 0006 F5 80
MOV P0,A
} 0008 80 00
SJMP HERE
return 0; Note: Address of P0 = 80h
}
}
Smart Logic Academy 17
Basics of Embedded C Programming
void main(void) /* Main program */
{
}
Smart Logic Academy 18
Basics of Embedded C Programming
stdint.h header
Constant/literal
Integer constants
Character constants
String Constants/Literals
String constants consist of any number of consecutive characters in
enclosed quotation marks (").
String(array) of characters:
char my_string[10] = {'M', 'y', ' ', 'S', 't', 'r', 'i', 'n', 'g', '\0'};
Variable arrays
Program variables
Space for the variable is allocated on the system stack when the
procedure is entered
De-allocated, to be re-used, when the procedure is exited
void delay ( )
{
Int i,j; //automatic variables –visible only within delay( )
void main(void)
{
Int count = 0; //initialize global variable count
while (1)
{
math_op();
count++; //increment global variable count
}
}
&(AND)
|(OR)
^(XOR)
~(Complement)
unsigned int n;
n = 0x64; //HexaDecimal
n = 100; //Decimal
n = 0144 //Octal
Decimal: 100
Binary : 01100100
1 4 4 6 4
Octal Hexadecimal
Shift operators
Bit masking
When we assign value directly to any register. This may change the value
of other bits which might be used to control other hardware feature. To
avoid such scenario best practice is to use bit masking.
Many times we need to read certain Flags in a register that denotes change in
Hardware state.
While ( P0 & (1<<5) ) //wait indefinitely until 5th bit changes from 0 to 1
{ //do something //exit loop
}
1= 00000001
1<<5 = 00100000 P0. 7 P0. 6 P0. 5 P0. 4 P0. 3 P0. 2 P0. 1 P0.0
0 0 Switch 0 0 0 0 0
0
P0= 0x00 = 0000000
P0 & (1<<5) = 00000000 & 00100000 = 00000000
while ( ! (P0 & (1<<5) ) ) //wait indefinitely until 5th bit changes from 0 to 1
{ //do something //exit loop
}
1= 00000001
1<<5 = 00100000 P0. 7 P0. 6 P0. 5 P0. 4 P0. 3 P0. 2 P0. 1 P0.0
0 0 Switch 0 0 0 0 0
P0= 0x20 = 0010000 1
While ( P0 & (1<<5) ) //wait indefinitely until 5th bit changes from 0 to 1
{
//do something //exit loop
}
To monitor for the change in 5th bit from 1 to 0 we just Negate the condition inside
while loop .
while ( ! (P0 & (1<<5) ) ) //wait indefinitely until 12th bit changes from 1 to 0
{ //do something //exit loop
}
Smart Logic Academy 54
Embedded C Programming
Extracting Bits REGHL_16
Now we have asked to extract lower 8-bits and upper 8 bit register into REGH_8
and REGL_8
C control structures
•Conditional execution
•Execute a set of statements if some condition is met
• Select one set of statements to be executed from several options,
depending on one or more conditions
•Iterative execution
•Repeated execution of a set of statements
•A specified number of times, or
•Until some condition is met, or
•While some condition is true
Smart Logic Academy 56
Embedded C Programming
IF-THEN structure
Execute a set of statements if and only if some condition is met
if (a < b)
{
statement s1;
statement s2;
….
}
if (a == 0)
{
statement s1;
statement s2;
}
else
{
statement s3;
statement s4:
} Smart Logic Academy 59
Embedded C Programming
while (a < b)
{
statement s1;
statement s2;
….
}
do
{
statement s1;
statement s2;
….
}
while (a < b);
{
//do inner loop 1000 times
//do “nothing” in inner loop
}
}
Smart Logic Academy 65
Embedded C Programming
C function
Void main()
{
Int a,b,c;
a = 10; b =20;
c=math_func (a,b); Function call
}
&(AND)
|(OR)
^(XOR)
~(Complement)