8/3/2019 EmbeddedC
1/33
07/09/11 Microcontroller 1
Embedded CEmbedded C
8/3/2019 EmbeddedC
2/33
07/09/11 Microcontroller 2
The development processThe development process
Plan tasks and interactionPlan tasks and interaction
Setup overall project fileSetup overall project file
EditEdit
Project.PRJProject.PRJ
Task1.A51Task1.A51 Task2.CTask2.C
reviserevise
11 22
8/3/2019 EmbeddedC
3/33
07/09/11 Microcontroller 3
The development processThe development process
Task1.A51Task1.A51 Task2.CTask2.C
AssembledAssembledCompiled andCompiled andassembledassembled
Task1.lstTask1.lst
Task2.lstTask2.lst
Link/LocateLink/LocateVarious.libVarious.lib Project.M51Project.M51
33
8/3/2019 EmbeddedC
4/33
07/09/11 Microcontroller 4
The development processThe development process
Link/LocateLink/LocateVarious.libVarious.lib Project.M51Project.M51
Project.hexProject.hexBurn in EPROM orBurn in EPROM ordownloaddownload
reviserevise
8/3/2019 EmbeddedC
5/33
07/09/11 Microcontroller 5
Intel HEX formatIntel HEX format
Example:Example:
:10400000755AFF111675907F11167590BF111675B0:10400000755AFF111675907F11167590BF111675B0
:10401000903F111680EA780379007A00DAFED9FA27:10401000903F111680EA780379007A00DAFED9FA27
:03402000D8F622AD:03402000D8F622AD
:00000001FF:00000001FF
8/3/2019 EmbeddedC
6/33
07/09/11 Microcontroller 6
An ExampleAn Example
#include#include#include #include
void main(void) {void main(void) {
}}
8/3/2019 EmbeddedC
7/33
07/09/11 Microcontroller 7
Development ToolsDevelopment Tools
DebuggingDebugging SimulatorSimulator
becomes less useful when there isbecomes less useful when there is
a lot of time critical hardwarea lot of time critical hardwareDownload to a target with aDownload to a target with amonitormonitor
Disadvantages: serial port is tiedDisadvantages: serial port is tiedup for downloadingup for downloading
need for a softwareneed for a software
8/3/2019 EmbeddedC
8/33
07/09/11 Microcontroller 8
Development ToolsDevelopment Tools
InIn--circuit emulatorcircuit emulator
combines monitor and simulatorcombines monitor and simulator
functionsfunctions
emulator plugs into the socket whereemulator plugs into the socket where
the final processor would residethe final processor would reside
CostlyCostly
8/3/2019 EmbeddedC
9/33
07/09/11 Microcontroller 9
What Language To Use for EmbeddedWhat Language To Use for EmbeddedApplicationsApplications
Assembly Assembly -- Most efficientMost efficient
but difficult to read and maintainbut difficult to read and maintain
CC
C++C++
JavaJava
8/3/2019 EmbeddedC
10/33
07/09/11 Microcontroller 10
Why CWhy C
Can control many machine level functionsCan control many machine level functions
without resorting to assembly languagewithout resorting to assembly language
Application can be written in C more easilyApplication can be written in C more easily
than assembly language because thethan assembly language because the
development software manages the detailsdevelopment software manages the details
because of modularity,reusable codes can bebecause of modularity,reusable codes can bedeveloped and maintaineddeveloped and maintained
8/3/2019 EmbeddedC
11/33
8/3/2019 EmbeddedC
12/33
07/09/11 Microcontroller 12
Memory Areas
Code memory (program memory).
External data memory.
Internal data memory.
8/3/2019 EmbeddedC
13/33
07/09/11 Microcontroller 13
Memory Areas
Internal data memory:>read from or written to.
>faster than any other memory.
>memory specifiers
data
idata
bdata
8/3/2019 EmbeddedC
14/33
07/09/11 Microcontroller 14
Memory TypesMemory Types
8/3/2019 EmbeddedC
15/33
07/09/11 Microcontroller 15
Memory TypesMemory Types
Frequently used variables are put inFrequently used variables are put in
the internal memory in preference tothe internal memory in preference to
the external RAMthe external RAM By including a memory type specifierBy including a memory type specifier
in the variable declaration, you canin the variable declaration, you can
specify where variables are storedspecify where variables are stored
8/3/2019 EmbeddedC
16/33
07/09/11 Microcontroller 16
ExampleExample
char data var1;char data var1;
char code text[] = "ENTER PARAMETER:";char code text[] = "ENTER PARAMETER:";
unsigned long xdata array[100];unsigned long xdata array[100];
float idata x,y,z;float idata x,y,z;unsigned int pdata dimension;unsigned int pdata dimension;
char bdata flags;char bdata flags;
8/3/2019 EmbeddedC
17/33
07/09/11 Microcontroller 17
Memory ModelsMemory Models
SMALL :SMALL : all variables default to the internal dataall variables default to the internal data
memory of the 8051memory of the 8051
same as if t
hey were declared explicitly usingsame as if th
ey were declared explicitly usingthe data memory type specifierthe data memory type specifier
variable access is very efficientvariable access is very efficient
However, all data objects, as well as theHowever, all data objects, as well as the
stack must fit into the internal RAMstack must fit into the internal RAM
8/3/2019 EmbeddedC
18/33
07/09/11 Microcontroller 18
Memory ModelsMemory Models
Compact:Compact: all variables default to one page of externalall variables default to one page of external
data memorydata memory
same as if t
hey were explicitly declared usingsame as if th
ey were explicitly declared usingthe pdata memory type specifierthe pdata memory type specifier
can accommodate a maximum of 256 bytes ofcan accommodate a maximum of 256 bytes of
variables as it is accessed indirectly throughvariables as it is accessed indirectly through
R0 and R1R0 and R1
variable access is not as fast as small modelvariable access is not as fast as small model
8/3/2019 EmbeddedC
19/33
07/09/11 Microcontroller 19
Memory ModelsMemory Models
Large:Large: all variables default to external data memoryall variables default to external data memory
same as if they were explicitly declared usingsame as if they were explicitly declared usingthe xdata memory type specifierthe xdata memory type specifier
Memory access through this data pointer isMemory access through this data pointer is
inefficient, especially for variables with ainefficient, especially for variables with alength of two or more byteslength of two or more bytes
This type of data access generates more codeThis type of data access generates more codethan the small or compact modelsthan the small or compact models
8/3/2019 EmbeddedC
20/33
07/09/11 Microcontroller 20
Key differences between C and Keil CKey differences between C and Keil C
8/3/2019 EmbeddedC
21/33
07/09/11 Microcontroller 21
Key differences between C and Keil CKey differences between C and Keil C
The sbit, sfr, and sfr16 data types areThe sbit, sfr, and sfr16 data types are
included to allow access to the specialincluded to allow access to the special
function registers that are available onfunction registers that are available on
the 8051the 8051 E.g: the declaration:E.g: the declaration: sfr P0 = 0x80sfr P0 = 0x80
declares the variabledeclares the variable P0P0 and assigns it theand assigns it the
special function register address ofspecial function register address of 0x800x80..This is the address of PORT 0 on the 8051.This is the address of PORT 0 on the 8051.
8/3/2019 EmbeddedC
22/33
07/09/11 Microcontroller 22
Default memory typeDefault memory type
If the memory type specifier is omitted in aIf the memory type specifier is omitted in avariable declaration, the default or implicitvariable declaration, the default or implicit
memory type is automatically selectedmemory type is automatically selected
Function arguments and automatic variablesFunction arguments and automatic variables
which cannot be located in registers are alsowhich cannot be located in registers are also
stored in the default memory areastored in the default memory area
The default memory type is determined by theThe default memory type is determined by the
SMALL, COMPACT and LARGE compiler controlSMALL, COMPACT and LARGE compiler controldirectivesdirectives
8/3/2019 EmbeddedC
23/33
07/09/11 Microcontroller 23
An ExampleAn Example 8 switches are connected to P08 switches are connected to P0 8 LEDs are connected to P28 LEDs are connected to P2#include #include void msec (unsigned int);void msec (unsigned int);void main ()void main (){{
unsigned char array[10];unsigned char array[10];unsigned char I;unsigned char I;while (1)while (1)
{{for (i=0; i
8/3/2019 EmbeddedC
24/33
07/09/11 Microcontroller 24
Operators
== equal to comparison= greater than or equal to
!= not equal to
> shift right
++ increment
-- decrement&& Boolean and
|| Boolean or
8/3/2019 EmbeddedC
25/33
07/09/11 Microcontroller 25
Operators+= add value to
-= subtract value to
*= multiply value to
/= divide value to
|= or value to&= and value to
^= exclusive or value to
= shift value right
%= modulo divide value to
-> pointer to a structure
8/3/2019 EmbeddedC
26/33
07/09/11 Microcontroller 26
Bitwise operatorsBitwise operators
Logical operationLogical operation AssemblyAssembly CC
NOT CPL A ~NOT CPL A ~
AND ANL A,# & AND ANL A,# &
OR ORL A,# |OR ORL A,# |
XOR XRL A,# ^XOR XRL A,# ^
8/3/2019 EmbeddedC
27/33
07/09/11 Microcontroller 27
Assignment operatorsAssignment operators
Unique to C is the shorthandUnique to C is the shorthandrepresentation for modifying arepresentation for modifying avariable and assigning it backvariable and assigning it back
portA = portA & 0xf7;portA = portA & 0xf7;
portA &= oxf7;portA &= oxf7;
8/3/2019 EmbeddedC
28/33
07/09/11 Microcontroller 28
Delay by software loopingDelay by software looping
Void msec (unsigned int x)Void msec (unsigned int x){{unsigned char j;unsigned char j;while (xwhile (x---- > 0)> 0)
{{for (j=0; j
8/3/2019 EmbeddedC
29/33
07/09/11 Microcontroller 29
Accessing an array elementAccessing an array element
Unsigned int ary[20];Unsigned int ary[20];unsigned int x;unsigned int x;ary[9] =x;ary[9] =x;
While defining arrays, it has to be madeWhile defining arrays, it has to be madesure that large amounts of memory issure that large amounts of memory isnot tied upnot tied up
8/3/2019 EmbeddedC
30/33
07/09/11 Microcontroller 30
PointersPointers
C51 compiler supports two different typesC51 compiler supports two different types
of pointers:of pointers:
Generic pointersGeneric pointers
Memory specific pointersMemory specific pointers
8/3/2019 EmbeddedC
31/33
07/09/11 Microcontroller 31
Generic PointersGeneric Pointers
char *s;char *s; /* string ptr *//* string ptr */
int *numptr;int *numptr; /* int ptr *//* int ptr */
long *state;long *state; /* long ptr *//* long ptr */
use t
hree bytes,t
he first for t
he memory type,use t
hree bytes,t
he first for t
he memory type,the second for the highthe second for the high--order byte of the offsetorder byte of the offset
and the third for the lowand the third for the low--order byte of theorder byte of theoffsetoffset
Generic pointers may be used to access anyGeneric pointers may be used to access anyvariable regardless of its location in 8051variable regardless of its location in 8051memory space.memory space.
8/3/2019 EmbeddedC
32/33
07/09/11 Microcontroller 32
Memory Specific PointersMemory Specific Pointers
char data *str;char data *str; /* ptr to string in data *//* ptr to string in data */int xdata *numtab;int xdata *numtab; /* ptr to int(s) in xdata *//* ptr to int(s) in xdata */
long code *powtab;long code *powtab; /* ptr to long(s) in code *//* ptr to long(s) in code */
can be stored using only one byte (idata,can be stored using only one byte (idata,
data, bdata, and pdata pointers) ordata, bdata, and pdata pointers) or
two bytes (code and xdata pointerstwo bytes (code and xdata pointers)
8/3/2019 EmbeddedC
33/33
07/09/11 Microcontroller 33
I request Electronics and
communication ENGINEERINGstudents to visit my blog formore
abhishek1ek.blogspot.comawhengineering.blogspot.com
THANK YOU