0% found this document useful (0 votes)
116 views41 pages

Unit-III Ies Part2

Embedded C programming is used to program microcontrollers in embedded systems. It allows the microcontroller to perform specific tasks like controlling appliances, medical equipment, vehicles, and more. The C language is commonly used for embedded systems because it is portable, scalable, and easier to understand than assembly language. Embedded C programming involves defining functions and statements to control the microcontroller hardware via I/O ports, timers, and interrupts. Common microcontrollers like the 8051 have features like RAM, ROM, I/O pins and timers that embedded C code can interface with to perform dedicated functions.

Uploaded by

harini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views41 pages

Unit-III Ies Part2

Embedded C programming is used to program microcontrollers in embedded systems. It allows the microcontroller to perform specific tasks like controlling appliances, medical equipment, vehicles, and more. The C language is commonly used for embedded systems because it is portable, scalable, and easier to understand than assembly language. Embedded C programming involves defining functions and statements to control the microcontroller hardware via I/O ports, timers, and interrupts. Common microcontrollers like the 8051 have features like RAM, ROM, I/O pins and timers that embedded C code can interface with to perform dedicated functions.

Uploaded by

harini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Basics of Embedded C Program

and Programming
What Are Embedded Systems?
“Special purpose system which are either used as standalone or part of a
big system”

An Embedded System is one that has computer hardware with software


embedded in it as one of its important components.

An embedded product uses a microprocessor (or microcontroller) to do


one task and one task only.

A printer is an example of embedded system since the processor inside


it
performs only one task; namely, getting the data and printing it
WHERE A R E THEY PRESENT
 Household appliances
?
 Integrated systems in aircrafts
and missiles

 Ce l l ul a r telephones

 E l e ct r i c and Electronic Motor


controllers

 E n g i n e controllers
in automobiles

 Calculators

 Medical
equipment’s
The necessary tools for a
microprocessor/microcontroller
 CPU: Central Processing Unti
 I/O: Input /Output
 Bus: Address bus & Data bus
 Memory: RAM & ROM
 Timer
 Interrupt
Introduction to Microprocessors
“A Microprocessor is a general purpose chip used to create multifunction
device ”

 N o RAM, ROM, I/O ports on CPU chip itself

 C P U is stand-alone, Must add RAM, ROM, I/O ports, and timers


externally to make them functional

 D e s i g n e r can decide on the amount of ROM, RAM and I/O


ports

 Ex a m p l e : Intel’s x86, Motorola’s 680x0


Introduction to Microcontrollers
“A Microcontroller is a computer-on-a-chip optimized to control Electronic
devices”

 C P U + I/O + Timer(s) [+ ROM] [+ RAM]  A l l on single chip

 L i m i te d RAM space, ROM space and I/O pins

 L o w chip-count to implement a small system

 Low - co st at large quantities

 Development tools readily available at reasonable cost


Microprocessor System Contrasted With Microcontroller System
Microprocessor vs Microcontroller

Microcontroller Microprocessor
• CPU, RAM, ROM, I/O and • CPU is stand-alone,
timer are all on a single chip RAM, ROM, I/O, timer are
separate
• Fix amount of on-chip ROM,

RAM, I/O ports
Designer can decide the amount
• For applications in which cost, of ROM, RAM and I/O ports
power and space are critical •
Expansive
• Not Expansive •
Versatility
• Single-purpose •
General-
Criteria for Choosing a Microcontroller
Following must be kept in mind while choosing a

 Speed
microcontroller
 Packaging
 Power consumption
 The amount of RAM and ROM on chip
 The number of I/O pins and the timer on chip
 How easy to upgrade to higher performance or lower power-
consumption versions
 Cost per unit
Microcontroller Architectures
Memory
Address Bus 0
Program
CPU Data Bus + Data Von Neumann
Architecture
2n

Memory
Address Bus 0
Program
CPU
Fetch Bus Harvard
Address Bus 0
Architecture
Data Bus Data
Features Of 8051
The Intel 8051 is used in embedded systems has

 8-bit CPU

 4k bytes ROM for the program

 128 BYTES of RAM for variables

 32 I/O lines ( 4 PORTS WITH 8 EACH )

 2 timers

 6 interrupt sources

 Low cost per chip


8051 PIN description
P1.0 1
40 Vcc
P1.1 2
39 P0.0 (AD0)
3 P0.1 (AD1)
38
P1.2
P1.3 4
37 P0.2 (AD2)
5 P0.3 (AD3)
36
P1.4
P1.5 6
35 P0.4 (AD4)
P1.6 7 P0.5 (AD5)
34
8 8051 33 P0.6 (AD6)
P1.7 9
32 P0.7 (AD7)
10 31 EA/VPP
RST
(TXD) P3.1 11
30 ALE/PROG
(INT0) P3.0
(RXD) P3.2 12
29 PSEN
(INT1) 13 28 P2.7 (A15)
P3.3 14 27 P2.6
(T0) P3.4 (A14)
15 26
(T1) P3.5 16 25 P2.5
(WR) P3.6 (A13)
17 24 P2.4
(RD) P3.7 (A12)
18 23
XTAL2
XTAL1 19 22 P2.3
(A11)
GND 20 21
P2.2
(A10)
I/0 Ports:-
8051 microcontroller has four I/O ports P0, P1, P2 and P3 each use 8 pins making
them 8 bit ports. All the ports can be used as either input or output

•Port 0: Port 0 occupies a total of 8 pins (pins 32-39) .It can be used for input or
output. To use the pins of port 0 as both input and output ports, each pin must be
connected externally to a 10K ohm pull-up resistor.

•Port 1: Port 1 occupies a total of 8 pins (pins 1-8). It can be used as input or
output. In contrast to port 0, this port does not need any pull-up resistors since it
already has pull-up resistors internally.

•Port 2 : Port 2 occupies a total of 8 pins (pins 21- 28). It can be used as input or
output. Just like P1, P2 does not need any pull-up resistors since it already has
pull-up resistors internally.

•Port 3 : Port 3 occupies a total of 8 pins (pins 10 -17). It can be used as input or
output. P3 does not need any pull-up resistors, the same as P1 and P2 did not.
Port 3 has the additional function of providing some extremely important signals
such as interrupts, etc.
IMPORTANT
PINS
 VCC (Pin-40): Vcc provides supply voltage to the chip. The
voltage source is +5V.
 GND (Pin-20): It is ground pin.
 RST (Pin-9): It is reset pin. Upon applying high pulse to thsi pin
the micro controller will reset and terminate all activities.
 XTAL1 & XTAL2 (Pin-18,19): These 2 pins provide external
clock using a quartz crystal oscillator Crystal inputs for
internal oscillator.
 PSEN (Pin-29): Program Store Enable, the read signal
for external program memory.
IMPORTANT PINS

 A L E (Pin-30): Address Latch Enable, to latch address outputs


at Port0 and Port2, used for both data and address transmission.

 E A (Pin-31):External Access pin should be connected to Vcc in


order to fetch code from internal program memory and to fetch code
from external program memory EA is connected to Ground.

 Interrupts (Pin-12,13): An interrupt is an event that disturbs the


microcontroller to inform it that a device needs its service.

 Timers/Counters (Pin-14,15): They are used for internal or


external events.
TIMER/COUNTERS IN 8051

TI MER 8051 is of two types


 Timer T0 16-bit
 Timer T1 16-bit

Features
• Both Timer T0 and T1 are 16 bits wide.
• When used as a “timer” the register is programmed to count
internal clock pulses
• When used as a “counter” the register is programmed to
count external clock pulses
INTERRUPT
Interrupt is an event that causes program to change its
normal execution and branch up to specific subroutine
putting normal program on hold.

 Whenever any device needs its service, the device notifies


the microcontroller by sending it an interrupt signal.

 Upon receiving an interrupt signal, the microcontroller


interrupts whatever it is doing and serves the device.
Embedded C Programming

Embedded C Programming is the soul of the processor functioning inside each


and

every embedded system we come across in our daily life, such as mobile

phone, washing machine, and digital camera.

1
8
 Each processor is associated with an embedded software.

 The first and foremost thing is the embedded software that

decides functioning of the embedded system.

 Embedded C language is most frequently used to program

the microcontroller.

1
9
 Earlier, many embedded applications were developed using assembly level

programming. However, they did not provide portability.

 This disadvantage was overcome by the advent of various high level

languages like C, Pascal, and COBOL. However, it was the C language that

got extensive acceptance for embedded systems, and it continues to do so.

 The C code written is more reliable, scalable, and portable; and in fact,

much easier to understand.

2
0
About C
Language
•C language was developed by Dennis Ritchie in 1969.

•It is a collection of one or more functions, and every function is a collection

of statements performing a specific task.

•C language is a middle-level language as it supports high-level

applications and low-level applications.

•Before going into the details of embedded C programming, we should


know
RAM Memory organisation

5
 Salient features of the language

 C language is a software designed with different keywords, data types,


variables, constants, etc.

 Embedded C is a generic term given to a programming language written in


C, which is associated with a particular hardware architecture.

 Embedded C is an extension to the C language with some additional


header files.
These header files may change from controller to controller.

 The microcontroller 8051 #include<reg51.h> is used.

 The embedded system designers must know about the hardware architecture to write programs.
These programs play prominent role in monitoring and controlling external devices. They also
directly operate and use the internal architecture of the microcontroller, such as interrupt
handling, timers, serial communication and other available features.
The basic additional features of the embedded
software
Data types

The data type refers to an extensive system for declaring variables


of different types like integer, character, float, etc.

The embedded C software uses four data types that are used to store
data in the memory.

The ‘char’ is used to store any single character; ‘int’ is used to store
integer value, and ‘float’ is used to store any precision floating point
value.

The size and range of different data types on a 32-bit machine is given in
the following table. The size and range may vary on machines with
Keyword
s
There are certain words that are reserved for doing specific tasks. These

words are known as keywords.

They are standard and predefined in the Embedded C.

Keywords are always written in lowercase. These keywords must be

defined before writing the main program.

The basic keywords of an embedded software are given below:


 sbit: This data type is used in case of accessing a single bit
of SFR register.
 Syntax: sbit variable name = SFR bit ;
 Ex: sbit a=P2^1;
 Explanation: If we assign p2.1 as ‘a’ variable, then we can
use ‘a’ instead of p2.1 anywhere in the program, which
reduces the complexity of the program.
 Bit: This data type is used for accessing the bit
addressable memory of RAM (20h-2fh).
 Syntax: bit variable name;
 Ex: bit c;
 Explanation: It is a bit sequence setting in a small data
area that is used by a program to remember something.
 SFR: This data type is used for accessing a SFR register by another name.
All
the SFR registers must be declared with capital
letters.
 Syntax: SFR variable name = SFR address of SFR
register;
 Ex: SFR
port0=0x80;
 Explanation: If we assign 0x80 as ‘port0’, then we can use 0x80 instead
of
port0 anywhere in the program, which reduces the complexity of
the

program.
SFR Register:

The SFR stands for ‘Special Function Register’.


Microcontroller 8051 has 256 bytes of RAM memory.

This RAM is divided into two parts: the first part of 128 bytes

is used for data storage, and the other of 128 bytes is used
for SFR registers.

All peripheral devices like I/O ports, timers and counters

are stored in the SFR register, and each element has a


unique address.
 The Structure of an Embedded C
Program
 comments
 preprocessor directives
 global variables
 main() function
 {
 local variables
 statements
 …………..

 …………..

 }
 fun(1)
 {
 local variables
 statements
 …………..
Comments:

In embedded C programming language, we can place

comments in our code which helps the reader to


understand the code easily.
C=a+b; /* add two variables whose value is stored in another variable
C*/

Preprocessor directives:

All the functions of the embedded C software are included

in the preprocessor library like “#includes<reg51.h>,


#defines”.
These functions are executed at the time of running
Global variable

A global variable is a variable that is declared before


the main function, and can be accessed on any
function in the program.
Local
variable
A local variable is a variable declared within a
function, and it is valid only to be used within that
function.
Main () function
The execution of a program starts with the main
function. Every program uses only one main () function.

Advantages of embedded C program

Its takes less time to develop application program.

 It reduces complexity of the program.

 It is easy to verify and understand.

 It is portable in nature from one controller to another.


 Examples of a few Embedded C Programs

 The following are a few simple Embedded C

programs used for microcontroller-based projects.

You might also like