SlideShare a Scribd company logo
Presented by
-Nandan Desai
Department of Electrical Engineering (FOE)
MEFGI
1

 To let the microcontroller do any task, we need to
program it.
 The 8051 microcontroller can be programmed in 2
ways:
1. Using Assembly Language
2. Using Embedded C language
 We, today, are going to concentrate on programming
of 8051 microcontroller using Embedded C.
 AN OBVIOUS QUESTION :
Programming the 8051 Microcontroller
2

 Advantages of C language:
1. Comparatively, modification is easy.
2. The code developing time is less.
 Disadvantages/Limitations:
1. Memory requirement is more.
2. More execution time is required.
So, the reason is…
3

 For programming the embedded hardware devices,
we need to use Embedded C language instead of our
conventional C language.
 The key differences between conventional C and
Embedded C are:
1. Embedded C has certain predefined variables for
registers, ports etc. which are in 8051 e.g. ACC, P1,
P2, TMOD etc.
2. We can run super loop (infinite loop) in embedded
C language.
Embedded C
4

 We know that the programming in C language is
solely done by dealing with different variables.
 In case of Embedded C, these variables are nothing
else but the memory locations of different memories
of the microcontroller like code memory(ROM), data
memory (RAM), external memory etc.
 To use these memory locations as variables, we need
to use data types.
 There are 7 different data types in embedded C for
8051… Lets go through them
Data types in Embedded C
5

 There are 7 data types in Embedded C for 8051:
1. unsigned char
This data type is used to define an unsigned 8-bit
variable. All 8-bits of this variable are used to specify data.
Hence the range of this data type is (0)10 𝑡𝑜 255 10.
e.g. unsigned char count;
2. signed char
This data type is used to define a signed 8-bit variable.
Here MSB of variable is used to show sign (+/-) while rest 7
bits are used to specify the magnitude of the variable. Hence
the range of this data type is (−128)10 𝑡𝑜 127 10.
e.g. signed char temp;
7 Data types
6

3. unsigned int
This data type is used to define a 16-bit variable.
Hence from this we can comment that this data types
combines any 2 memory locations of the data memory
as one variable. Here all 16 bits are used to specify data.
So the range of this data type is (0)10 𝑡𝑜 (65535)10.
4. signed int
This data type is used to define a signed variable
like signed char but of 16-bit size. Hence its range is
(−32768)10 𝑡𝑜 (32767)10.
continued...
7

5. sfr
This is an 8-bit data type used for defining names of
Special Function Registers (SFR’s) that are located in
RAM memory locations 80 H to FF H only.
e.g. sfr P0 = 0x80;
6. bit
This data type is used to access single bits from the
bit-addressable area of RAM.
e.g. bit MYBIT = 0x32;
continued...
8

7. sbit
The sbit data type is the one which is used to define
or rather access single bits of the bit addressable SFR’s
of 8051 microcontroller.
e.g. sbit En = P2^0;
continued...
9

 The decision control structures are used to decide
whether to execute a particular block of code
depending on the condition specified.
 Following are some decision control structures:
1. if statement
2. if…else statement
3. elseif ladder
Decision control structures
10

if and if...else statement
if statement
if(condition)
{
statement-1;
statement-2;
……..
}
 if...else statement
if(condition)
{
statement-1;
statement-2;
………
}
else
{
statement n;
} 11

 The loop statements are the one which are used
when we want to execute a certain block of code for
more than one times either depending on situation
or by a predefined number of times.
 Embedded C is basically having two loop
statements:
1. for loop
2. while loop
 Let us see on their formats:
Loop statements
12

for loops are used to repeat any particular
piece of code a predefined number of times.
for(initializations ; conditions ; updates)
{
statement-1;
statement-2;
………
}
for loop
13
 while loop also has the provision to repeat a certain
block of code but here the block is repeated
depending on the condition specified.
 The loop keeps on repeating until the condition
becomes false.
 Format of while loop is:
while(condition)
{
statement-1;
statement- 2;
………
}
while loop
14

 break
The break statement, whenever is encountered in the
loop, it forces the control to terminate the loop in which
it is written.
 continue
Whenever this statement is encountered in any loop,
the statements in the loop after it won’t be executed i.e.
will be skipped and again control will be transferred to
check the condition of the loop.
break & continue statements
15

#include <reg51.h> header file
sbit <name>=<bit address>; sfr bit definitions
sfr <name>=<sfr address>; sfr definition
Data-type udf1(data-type var_name); User defined
function
Data-type udf2(data-type var_name);
void main(void) main function
{
statement-1;
statement-2;
…….. ;
Format of any C Program
16
………;
udf1(value);
………; User defined function call
udf2(value);
}
Data-type udf1(data-type var_name)
{ statement-block;
return(ret_value);
}
Similarly 2nd function udf2 is defined…
continue…
17

 Sometimes, there comes a situation in which in a
program a group of statements is used frequently.
 Writing these statements again & again makes our
program clumsy to write as well as it consumes more
memory space.
 To overcome this problem there is a facility in C
language to define a function. In function we can
write the particular group of statements which is
getting repeated continuously.
 Now anytime when we want to use that code group,
we just have to call the function and its done.
Functions
18

1. No arguments, no return values
2. With no arguments and a return value
3. With arguments but no return value
4. With arguments and return value
Types of functions
19

 There are 3 ways to deal with a function:
1. Define first, then use
2. Do prototyping (i.e. Define first, use after main() )
3. Do prototyping in header file
 Let us see all the 3 in brief:
 Define first, then use
In this case, before writing the main function, we
define the user-defined function and then use it in
main() function whenever required.
Using a function
20

 Do prototyping and define after main function
In this case the function name, data type and
argument data type are specified before writing main
function to declare that we’ll later implement this
function.
 Do prototyping in header file
In this case, define the function in a (user defined)
header file and then just include that header file in your
program.
continued…
21
22

More Related Content

PDF
Embedded Systems Training Report
PPTX
Pic microcontroller architecture
PPTX
Msp 430 architecture module 1
PDF
ARM CORTEX M3 PPT
PPTX
Embedded Systems - Training ppt
PDF
Delays in verilog
PDF
Verilog VHDL code Parallel adder
PDF
Embedded C programming based on 8051 microcontroller
Embedded Systems Training Report
Pic microcontroller architecture
Msp 430 architecture module 1
ARM CORTEX M3 PPT
Embedded Systems - Training ppt
Delays in verilog
Verilog VHDL code Parallel adder
Embedded C programming based on 8051 microcontroller

What's hot (20)

PPT
8051 MICROCONTROLLER
PPTX
PPT ON Arduino
PPTX
Embedded system design using arduino
PPTX
PIC-18 Microcontroller
PPTX
Discrete Input module block diagram and wiring in PLC
PPTX
8255 PPI
PPTX
Xilinx 4000 series
PPTX
Microcontroller 8096
PPTX
Subroutine in 8051 microcontroller
PDF
Device drivers and interrupt service mechanism
PDF
Logic Synthesis
PPT
Interrupt programming with 8051 microcontroller
PPTX
Smart street light system
PPT
Master synchronous serial port (mssp)
PPTX
System bus timing 8086
PPT
Memory organization of 8051
PPTX
Trends in Embedded system Design
PPTX
Lcd interfaing using 8051 and assambly language programming
PPTX
Embedded system
PDF
Serial Communication Interfaces
8051 MICROCONTROLLER
PPT ON Arduino
Embedded system design using arduino
PIC-18 Microcontroller
Discrete Input module block diagram and wiring in PLC
8255 PPI
Xilinx 4000 series
Microcontroller 8096
Subroutine in 8051 microcontroller
Device drivers and interrupt service mechanism
Logic Synthesis
Interrupt programming with 8051 microcontroller
Smart street light system
Master synchronous serial port (mssp)
System bus timing 8086
Memory organization of 8051
Trends in Embedded system Design
Lcd interfaing using 8051 and assambly language programming
Embedded system
Serial Communication Interfaces
Ad

Viewers also liked (20)

PDF
Embedded C - Lecture 1
PPT
Embedded c program and programming structure for beginners
PPT
Introduction To Embedded Systems
PPT
Embedded System Basics
PPT
Looping in C
PPT
Embeded system by Mitesh Kumar
PPTX
Embeded system
PPT
Embedded systems
PDF
Selection and Integration of Embedded Display Devices
PPTX
Linux Timer device driver
PDF
RFID embedded - MAGIC-PCB containing MAGICSTRAP
PPTX
Serial Communication & Embedded System Interface
PPTX
Linux watchdog timer
PPTX
Embedded systems
PPTX
Serial peripheral Interface - Embedded System Protocol
PPTX
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
PPT
Programmable Logic Devices Plds
PPT
Risc and cisc eugene clewlow
Embedded C - Lecture 1
Embedded c program and programming structure for beginners
Introduction To Embedded Systems
Embedded System Basics
Looping in C
Embeded system by Mitesh Kumar
Embeded system
Embedded systems
Selection and Integration of Embedded Display Devices
Linux Timer device driver
RFID embedded - MAGIC-PCB containing MAGICSTRAP
Serial Communication & Embedded System Interface
Linux watchdog timer
Embedded systems
Serial peripheral Interface - Embedded System Protocol
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
Programmable Logic Devices Plds
Risc and cisc eugene clewlow
Ad

Similar to Embedded c (20)

PPT
EMBEDDED SYSTEMS 4&5
PPTX
Microcontroller lec 3
PDF
Embedded C - Lecture 2
PPTX
Embedded system (Chapter 5) part 1
PPTX
Embedded system (Chapter )
PPTX
Unit-2.pptx
PDF
Lenguaje de Programación en C Presentacion
PDF
Embedded concepts
PPTX
Arduino Functions
PPTX
Embedded C.pptx
PPT
C language programming
PPT
C Basics
PDF
Embedded_C_1711824726engéiiiring_with_the_best.pdf
PPTX
Introduction to C Unit 1
PPTX
Programming 8051 with C and using Keil uVision5.pptx
PDF
embedded system Chapter 2 diploma msbte.pdf
PPTX
DOC
Exhibit design and programming skills to build and automate business solution...
PDF
C programming session9 -
PDF
8051 assembly programming
EMBEDDED SYSTEMS 4&5
Microcontroller lec 3
Embedded C - Lecture 2
Embedded system (Chapter 5) part 1
Embedded system (Chapter )
Unit-2.pptx
Lenguaje de Programación en C Presentacion
Embedded concepts
Arduino Functions
Embedded C.pptx
C language programming
C Basics
Embedded_C_1711824726engéiiiring_with_the_best.pdf
Introduction to C Unit 1
Programming 8051 with C and using Keil uVision5.pptx
embedded system Chapter 2 diploma msbte.pdf
Exhibit design and programming skills to build and automate business solution...
C programming session9 -
8051 assembly programming

Recently uploaded (20)

PPTX
Welding lecture in detail for understanding
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Digital Logic Computer Design lecture notes
PPTX
Sustainable Sites - Green Building Construction
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
OOP with Java - Java Introduction (Basics)
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Lecture Notes Electrical Wiring System Components
Welding lecture in detail for understanding
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Digital Logic Computer Design lecture notes
Sustainable Sites - Green Building Construction
Arduino robotics embedded978-1-4302-3184-4.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Internet of Things (IOT) - A guide to understanding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Model Code of Practice - Construction Work - 21102022 .pdf
additive manufacturing of ss316l using mig welding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Geodesy 1.pptx...............................................
OOP with Java - Java Introduction (Basics)
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Structs to JSON How Go Powers REST APIs.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Lecture Notes Electrical Wiring System Components

Embedded c

  • 1. Presented by -Nandan Desai Department of Electrical Engineering (FOE) MEFGI 1
  • 2.   To let the microcontroller do any task, we need to program it.  The 8051 microcontroller can be programmed in 2 ways: 1. Using Assembly Language 2. Using Embedded C language  We, today, are going to concentrate on programming of 8051 microcontroller using Embedded C.  AN OBVIOUS QUESTION : Programming the 8051 Microcontroller 2
  • 3.   Advantages of C language: 1. Comparatively, modification is easy. 2. The code developing time is less.  Disadvantages/Limitations: 1. Memory requirement is more. 2. More execution time is required. So, the reason is… 3
  • 4.   For programming the embedded hardware devices, we need to use Embedded C language instead of our conventional C language.  The key differences between conventional C and Embedded C are: 1. Embedded C has certain predefined variables for registers, ports etc. which are in 8051 e.g. ACC, P1, P2, TMOD etc. 2. We can run super loop (infinite loop) in embedded C language. Embedded C 4
  • 5.   We know that the programming in C language is solely done by dealing with different variables.  In case of Embedded C, these variables are nothing else but the memory locations of different memories of the microcontroller like code memory(ROM), data memory (RAM), external memory etc.  To use these memory locations as variables, we need to use data types.  There are 7 different data types in embedded C for 8051… Lets go through them Data types in Embedded C 5
  • 6.   There are 7 data types in Embedded C for 8051: 1. unsigned char This data type is used to define an unsigned 8-bit variable. All 8-bits of this variable are used to specify data. Hence the range of this data type is (0)10 𝑡𝑜 255 10. e.g. unsigned char count; 2. signed char This data type is used to define a signed 8-bit variable. Here MSB of variable is used to show sign (+/-) while rest 7 bits are used to specify the magnitude of the variable. Hence the range of this data type is (−128)10 𝑡𝑜 127 10. e.g. signed char temp; 7 Data types 6
  • 7.  3. unsigned int This data type is used to define a 16-bit variable. Hence from this we can comment that this data types combines any 2 memory locations of the data memory as one variable. Here all 16 bits are used to specify data. So the range of this data type is (0)10 𝑡𝑜 (65535)10. 4. signed int This data type is used to define a signed variable like signed char but of 16-bit size. Hence its range is (−32768)10 𝑡𝑜 (32767)10. continued... 7
  • 8.  5. sfr This is an 8-bit data type used for defining names of Special Function Registers (SFR’s) that are located in RAM memory locations 80 H to FF H only. e.g. sfr P0 = 0x80; 6. bit This data type is used to access single bits from the bit-addressable area of RAM. e.g. bit MYBIT = 0x32; continued... 8
  • 9.  7. sbit The sbit data type is the one which is used to define or rather access single bits of the bit addressable SFR’s of 8051 microcontroller. e.g. sbit En = P2^0; continued... 9
  • 10.   The decision control structures are used to decide whether to execute a particular block of code depending on the condition specified.  Following are some decision control structures: 1. if statement 2. if…else statement 3. elseif ladder Decision control structures 10
  • 11.  if and if...else statement if statement if(condition) { statement-1; statement-2; …….. }  if...else statement if(condition) { statement-1; statement-2; ……… } else { statement n; } 11
  • 12.   The loop statements are the one which are used when we want to execute a certain block of code for more than one times either depending on situation or by a predefined number of times.  Embedded C is basically having two loop statements: 1. for loop 2. while loop  Let us see on their formats: Loop statements 12
  • 13.  for loops are used to repeat any particular piece of code a predefined number of times. for(initializations ; conditions ; updates) { statement-1; statement-2; ……… } for loop 13
  • 14.  while loop also has the provision to repeat a certain block of code but here the block is repeated depending on the condition specified.  The loop keeps on repeating until the condition becomes false.  Format of while loop is: while(condition) { statement-1; statement- 2; ……… } while loop 14
  • 15.   break The break statement, whenever is encountered in the loop, it forces the control to terminate the loop in which it is written.  continue Whenever this statement is encountered in any loop, the statements in the loop after it won’t be executed i.e. will be skipped and again control will be transferred to check the condition of the loop. break & continue statements 15
  • 16.  #include <reg51.h> header file sbit <name>=<bit address>; sfr bit definitions sfr <name>=<sfr address>; sfr definition Data-type udf1(data-type var_name); User defined function Data-type udf2(data-type var_name); void main(void) main function { statement-1; statement-2; …….. ; Format of any C Program 16
  • 17. ………; udf1(value); ………; User defined function call udf2(value); } Data-type udf1(data-type var_name) { statement-block; return(ret_value); } Similarly 2nd function udf2 is defined… continue… 17
  • 18.   Sometimes, there comes a situation in which in a program a group of statements is used frequently.  Writing these statements again & again makes our program clumsy to write as well as it consumes more memory space.  To overcome this problem there is a facility in C language to define a function. In function we can write the particular group of statements which is getting repeated continuously.  Now anytime when we want to use that code group, we just have to call the function and its done. Functions 18
  • 19.  1. No arguments, no return values 2. With no arguments and a return value 3. With arguments but no return value 4. With arguments and return value Types of functions 19
  • 20.   There are 3 ways to deal with a function: 1. Define first, then use 2. Do prototyping (i.e. Define first, use after main() ) 3. Do prototyping in header file  Let us see all the 3 in brief:  Define first, then use In this case, before writing the main function, we define the user-defined function and then use it in main() function whenever required. Using a function 20
  • 21.   Do prototyping and define after main function In this case the function name, data type and argument data type are specified before writing main function to declare that we’ll later implement this function.  Do prototyping in header file In this case, define the function in a (user defined) header file and then just include that header file in your program. continued… 21
  • 22. 22