Professional Documents
Culture Documents
3.0Microcontroller
It’s like a small computer on a single IC. It contains a processor core, ROM, RAM and I/O pins dedicated
to perform various tasks. Microcontrollers are generally used in projects and applications that require
direct control of user. As it has all the components needed in its single chip, it does not need any external
circuits to do its task so microcontrollers are heavily used in embedded systems and major microcontroller
manufacturing companies are making them to be used in embedded market. A microcontroller can be
called the heart of embedded system. Some examples of popular microcontrollers are 8051, AVR,
PIC series of microcontrollers.
From this image of architecture of Microcontroller it can be easily seen that it have registers and ALU
RAM, ROM, Serial Port, input and output device as on chip elements.
3.1Microprocessor
Microprocessor has only a CPU inside them in one or few Integrated Circuits. Like microcontrollers it
does not have RAM, ROM and other peripherals. They are dependent on external circuits of peripherals to
work. But microprocessors are not made for specific task but they are required where tasks are complex
and tricky like development of software’s, games and other applications that require high memory and
where input and output are not defined. It may be called heart of a computer system. Some examples of
microprocessor are Pentium, I3, and I5 etc.
From this image of architecture of Microprocessor it can be easily seen that it have registers and ALU as
processing unit and it does not have RAM, ROM in it.
1
1. Key difference in both of them is presence of external peripheral, where microcontrollers have RAM,
ROM, EEPROM embedded in it while we have to use external circuits in case of microprocessors.
2. As all the peripheral of microcontroller are on single chip it is compact while microprocessor is bulky.
3. Microcontrollers are made by using complementary metal oxide semiconductor technology so they are
far cheaper than microprocessors. In addition the applications made with microcontrollers are cheaper
because they need lesser external components, while the overall cost of systems made with
microprocessors are high because of the high number of external components required for such systems.
4. Processing speed of microcontrollers is about 8 MHz to 50 MHz, but in contrary processing speed of
general microprocessors is above 1 GHz so it works much faster than microcontrollers.
5. Generally microcontrollers have power saving system, like idle mode or power saving mode so overall
it uses less power and also since external components are low overall consumption of power is less. While
in microprocessors generally there is no power saving system and also many external components are used
with it, so its power consumption is high in comparison with microcontrollers.
6. Microcontrollers are compact so it makes them favorable and efficient system for small products and
applications while microprocessors are bulky so they are preferred for larger applications.
7. Tasks performed by microcontrollers are limited and generally less complex. While task performed by
microprocessors are software development, Game development, website, documents making etc. which
are generally more complex so require more memory and speed so that’s why external ROM, RAM are
used with it.
8. Microcontrollers are based on Harvard architecture where program memory and data memory are
separate while microprocessors are based on von Neumann model where program and data are stored in
same memory module.
3.2 CRITERIA FOR SELECTION OF A MICROCONTROLLER IN EMBEDDED SYSTEM
Criteria for selection of microcontroller in any embedded system is as following:
(a) Meeting the computing needs of task at hand efficiently and cost effectively
• Speed of operation
• Packing
• Power consumption
• Amount of RAM and ROM on chip
• No. of I/O pins and timers on chip
• Cost
(b) Availability of software development tools such as compiler, assembler and debugger.
2
It is 8-bit microcontroller, means MC 8051 can Read, Write and Process 8 bit data. This is mostly used
microcontroller in the robotics, home appliances likemp3 player, washing machines, electronic iron and
industries. Mostly used block sin the architecture of 8051 are as follows:
8-bit CPU
16-bit Program Counter
8-bit Processor Status Word (PSW)
8-bit Stack Pointer
Internal RAM of 128bytes
Special Function Registers (SFRs) of 128 bytes
32 I/O pins arranged as four 8-bit ports (P0 - P3)
Two 16-bit timer/counters: T0 and T1
Two external and three internal vectored interrupts
One full duplex serial I/O
8 Kb on chip ROM
3
Pins 1 to 8 − these pins are known
as Port 1. This port doesn’t serve any
other functions. It is internally pulled
up, bi-directional I/O port.
Pins 21 to 28 − these pins are known as Port 2. It serves as I/O port. Higher order address bus signals
are also multiplexed using this port
Pins 18 & 19 − these pins are used for interfacing an external crystal to get the system clock.
Pin 29 − this is PSEN pin which stands for Program Store Enable. It is used to read a signal from the
external program memory.
Pin 30 − this is EA pin which stands for External Access input. It is used to enable/disable the
external memory interfacing.
Pin 31 − this is ALE pin which stands for Address Latch Enable. It is used to demultiplex the
address-data signal of port.
Pins 32 to 39 − these pins are known as Port 0. It serves as I/O port. Lower order address and data
bus signals are multiplexed using this port.
4
8051 microcontrollers have 4 I/O ports each of 8-bit, which can be configured as input or output. Hence,
total 32 input/output pins allow the microcontroller to be connected with the peripheral devices.
o When the external memory is used then the lower address byte (addresses A0A7) is applied
on it, else all bits of this port are configured as input/output.
o When P0 port is configured as an output then other ports consisting of pins with built-in pull-
up resistor connected by its end to 5V power supply, the pins of this port have this resistor left out.
5
As shown in Figure port 0 is also
designated as ADO – AD7, allowing it to
be used for both address and data. The
8051 multiplexes address and data through
port 0 to save pins. ALE indicates if PO
has address or data. When ALE = 0. it
provides data DO – D7. but when ALE = 1
it has address AO -A”. Therefore. ALE is
used for demuliplexing address and data
with the help of a “4LS373 latch. The pins
of PO must be connected externally to a 1
OK-ohm pull-up resistor.
o
The power supply section is the important one for the microcontrollers. It should deliver constant output
regulated power supply for successful working of the microcontroller. A 0-12V/500 mA transformer is
used for this purpose. The primary of this transformer is connected in to main supply through on/off
switch& fuse for protecting from overload and short circuit protection. .
6
The secondary is connected to the diodes to convert 12V AC to 12V DC voltage. And filtered by the
capacitors, which is further regulated to +5v, by using IC 7805.
Arduino Managed High Sensitive LDR based Power Saver for Street Light Control System
The Temperature Humidity Monitoring System of Soil Based on Wireless Sensor Networks using
Arduino
7
RFID based Electronic Passport System for Easy Governance using Arduino
Zigbee based Automatic Meter Reading System
GSM based Electricity Energy Meter Billing with Onsite Display
Android Phone Speech Recognition Sensed Voice Command based Notice Board Display
Parking Availability Indication System
Voice Controlled Home Appliances
Remote Control Home Appliances
PC Mouse operated Electrical Load Control Using VB Application
Solar Highway Lighting System with Auto Turn Off in Daytime
8051 Microcontroller based Wireless Energy Meter
Farmer Friendly Solar Based Electric Fence for Deterring Cattles
Vehicle Movement Sensed Streetlight with Daytime auto off Features
Example:
Mov ax, 1
Mov bx, 2
Add bx
Writing programs can be very time-consuming, as you have to directly manipulate CPU registers and
use complicated interfaces to I/O devices
Code is assembled to make Machine Language (consisting only of 1’s and 0’s) which is the real
language of the CPU
HIGH LEVEL LANGUAGES.
8
Slower, needs more resources but more user friendly (needs less programming time and it is easier to develop a
program).
Now, if high-level languages are similar to English and hence, very easy to understand and write, then why do
we need low-level languages? The answer is simple. High-level languages need to be converted to low-level
and hence, have slower execution and also a larger file size than the low-level counterparts for the same code.
But, in general C is a mid-level language since it contains elements from the English language as well as a
number of machine friendly constructs. Hence, C programming language has both advantages - high
development speed as well as compact size of program.
4.2EMBEDDED C
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.
Each processor is associated with 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.
9
DIFFERENCES BETWEEN C AND EMBEDDED C
C programming is less time consuming, but has larger hex file size
10
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.
A good understanding of C data types for 8051 can help programmers to create smaller hex files
1. Unsigned char
2. Signed char
3. Unsigned int
4. Signed int
5. Sbit (single bit)
6. Bit and sfr
C compilers use the signed char as the default if we do not put the keyword unsigned
comments
preprocessor directives
global variables
11
main() function
local variables
statements
…………..
…………..
fun(1)
local variables
statements
…………..
…………..
The following Figure shows the steps involved in the process of building the C program starting from the
compilation until the loading of the executable image into the memory for program running.
Normally the C’s program building process involves four stages and utilizes different ‘tools’ such as a
preprocessor, compiler, assembler, and linker.
At the end there should be a single executable file. Below are the stages that happen in order regardless of the
operating system/compiler and graphically illustrated in Figure w.1.
1. Preprocessing is the first pass of any C compilation. It processes include-files, conditional
compilation instructions and macros.
2. Compilation is the second pass. It takes the output of the preprocessor, and the source code,
and generates assembler source code.
3. Assembly is the third stage of compilation. It takes the assembly source code and produces an
assembly listing with offsets. The assembler output is stored in an object file.
4. Linking is the final stage of compilation. It takes one or more object files or libraries as input
and combines them to produce a single (usually executable) file. In doing so, it resolves
references to external symbols, assigns final addresses to procedures/functions and variables,
and revises code and data to reflect new addresses (a process called relocation).
12
4.6 Embedded C Programs
1. Write an 8051 C program to toggle all the bits of P1 continuously.
Solution:
//Toggle P1 forever
#include <reg51.h>
void main(void)
{
for (;;)
{
p1=0x55;
p1=0xAA;
13
}
}
2.Write an 8051 C program to toggle bit D0 of the port P1 (P1.0) 50,000 times.
Solution:
#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}
3.Write an 8051 C program to turn bit P1.5 on and off 50,000 times.
Solution:
#include <reg51.h> We can access a single bit of any
sbit MYBIT=0x95;
SFR if we specify the bit address
void main(void)
{
unsigned int z;
for (z=0;z<50000;z++)
{
MYBIT=1;
MYBIT=0;
}
}
4. A door sensor is connected to the P1.1 pin, and a buzzer is connected to P1.7. Write an
8051 C program to monitor the door sensor, and when it opens, sound the buzzer. You
can sound the buzzer by sending a square wave of a few hundred Hz.
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
sbit Dsensor=P1^1;
sbit Buzzer=P1^7;
void main(void)
14
{
Dsensor=1; //make P1.1 an input
while (1)
{
while (Dsensor==1)//while it opens
{
Buzzer=0;
MSDelay(200);
Buzzer=1;
MSDelay(200);
}
}
}
void MSDelay (int k)
{
int j;
int i;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{
}
}
}
5.Write an 8051 C program to toggle all the bits of P0, P1, and P2 Continuously with a 250
ms delay. Use the sfr keyword to declare the port addresses.
Solution:
//Accessing Ports as SFRs using sfr data type
sfr P0=0x80;
sfr P1=0x90;
sfr P2=0xA0;
void MSDelay(unsigned int);
void main(void)
{
15
while (1)
{
P0=0x55;
P1=0x55;
P2=0x55;
MSDelay(250);
P0=0xAA;
P1=0xAA;
P2=0xAA;
MSDelay(250);
}
}
6.Run the following program on your simulator and examine the results.
Solution:
#include <reg51.h>
void main(void)
{
P0=0x35 & 0x0F; //ANDing
P1=0x04 | 0x68; //ORing
P2=0x54 ^ 0x78; //XORing
P0=~0x55; //inversing
P1=0x9A >> 3; //shifting right 3
P2=0x77 >> 4; //shifting right 4
P0=0x6 << 4; //shifting left 4
}
4.7SWITCH AND LED INTERFACING
Push button switch is connected to the first bit of PORT 0 (P0.0) which is configured as an input pin.
Which is connected to a pull up resistor as there is NO INTERNAL PULL UP RESISTORS FOR
PORT P0. Thus P0.0 pin is at Vcc potential when the switch is not pressed. When the switch is pressed
this pin P0.0 will be grounded. The LED is connected to the first bit of PORT 2 (P2.2) and a resistor is
connected in series with it to limit the current.
4.8Relay interfacing
In some electronic applications we need to switch or control high voltages or high currents. In these
cases we may use electromagnetic or solid state relays. For example, it can be used to control home
appliances using low power electronic circuits
An electromagnetic relay is a switch which is used to switch High Voltage or Current using
Low power circuits. It magnetically isolates low power circuits from high power circuits. It is activated
by energizing a electromagnet, coil wounded on a soft iron core A relay should not be directly connected
to a microcontroller, it needs a driving circuit due to the following reasons.
A microcontroller will not able to supply current required for the proper working of a
relay. The maximum current that A89C51 microcontroller can sink is 15mA while a relay needs about
50 – 100mA current.
A relay is activated by energizing its coil. Microcontroller may stop working by the
negative voltages produced in the relay due to its back emf.
Transistor is wired as a switch. Which drives the relay. The transistor will be in OFF state
when the when the pin P2.0 is in LOW state. When 1 is written to P2.0 current will flow to the base of
the transistor and the relay energizes.
#include<reg52.h>
17
void Delay_ms(int);
void main()
{
do
{
relay_pin = 0; //Relay ON
Delay_ms(1000);
relay_pin = 1; //Relay OFF
Delay_ms(1000);
}while(1);
void Delay_ms(int k)
{
int j;
int i;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{
}
}
}
4.9DC MOTOR INTERFACING WITH L293D
The maximum current that can be sourced or sunk from a 8051 microcontroller is 15 mA at 5v.
But a DC Motor need currents very much more than that and it need voltages 6v, 12v, 24v etc, depending
upon the type of motor used. Another problem is that the back emf produced by the motor may affect the
proper functioning of the microcontroller. Due to these reasons we can’t connect a DC Motor directly to a
microcontroller.
To overcome these problems you may use a H-Bridge using transistors. Freewheeling diodes or
Clamp diodes should be used to avoid problems due to back emf. Thus it requires transistors, diodes and
resistors, which may make our circuit bulky and difficult to assembly. To overcome this problem the
L293D driver IC is used. It is a Quadruple Half H-Bridge driver and it solves the problem completely.
You needn’t connect any transistors, resistors or diodes. We can easily control the switching of L293D
using a microcontroller. There are two IC’s in this category L293D and L293. L239D can provide a
maximum current of 600mA from 4.5V to 36V while L293 can provide up to 1A under the same input
conditions. All inputs of these ICs are TTL compatible and clamp diodes is provided with all outputs.
They are used with inductive loads such as relays solenoids, motors etc.
18
The DC Motor is connected to the first pair of drivers and it is enabled by connecting EN1 to
logic HIGH (5V). VSS pin is used to provide logic voltage to L293D. Here 8051 microcontroller, which
works at 5v is used to control L293D, hence the logic voltage is 5. The motor supply is given to Vs pin of
the L293D.
Keil C Program
#include<reg52.h>
#include<stdio.h>
void delay(void);
void main()
{
do
{
motor_pin_1 = 1;
motor_pin_2 = 0; //Rotates Motor Anit ClocK
delay();
motor_pin_1 = 1;
motor_pin_2 = 1; //Stops Motor
delay();
motor_pin_1 = 0;
motor_pin_2 = 1; //Rotates Motor Clockwise
delay();
motor_pin_1 = 0;
motor_pin_2 = 0; //Stops Motor
delay();
}while(1);
}
4.10LCD INTERFACING
Liquid Crystal Display (LCD) is very commonly used electronic display module and having a
wide range of applications such as calculators, laptops, mobile phones etc. 16×2 character lcd display is
very basic module which is commonly used in electronics devices and projects.
LCD can be interfaced with microcontroller in 4 Bit or 8 Bit mode. These differs in how data is
send to LCD. In 8 bit mode to write a character, 8 bit ASCII data is send through the data lines D0 – D7
and data strobe is given through E of the LCD. LCD commands which are also 8 bit are written to LCD in
similar way.
19
But 4 Bit Mode uses only 4 data lines D4 – D7. In this mode 8 bit character ASCII data and
command data are divided into two parts and send sequentially through data lines. The idea of 4 bit
communication is used save pins of microcontroller. 4 bit communication is a bit slower than 8 bit
communication but this speed difference can be neglected since LCDs are slow speed devices. Thus 4 bit
mode data transfer is most commonly used.
Initializing LCD: To initialize LCD to the 8051 the following instruction and commands are to be embed in to
the functions
20
E=1; enable pin should be high
RS=0; Register select should be low
R/W=1; Read/Write pin should be high
#include<reg51.h>
#define LCD P1
sbit RS=P2^4;
sbit RW=P2^6;
Sbit EN=P2^2;
InitLCD();
Void LCDcmd (unsigned char);
Void LCDdisp (unsigned char c)
Void LCDStrdisp (unsigned char *);
Void delay ();
Main ()
{
InitLCD ();
LCDcmd (0x80);
LCDStrdisp ("Welcome");
}
InitLCD ()
{
LCDcmd (0x38);
LCDcmd (0x0E);
LCDcmd (0x01);
LCDcmd (0x06);
LCDcmd (0x80);
}
Void LCDcmd (unsigned char cmd)
{
LCD=cmd;
RS=0;
RW=0;
EN=1;
delay ();
EN=0;
//delay ();
}
Void LCDdisp (unsigned char c)
{
LCD=c;
RS=1;
RW=0;
EN=1;
delay ();
EN=0;
//delay ();
}
Void LCDStrdisp (unsigned char *s)
21
4-BIT LCD DIAGRAM
{
While (*s)
LCDdisp (*s++);
}
Void delay ()
{
Unsigned int i;
For (i=0; i<20000; i++);
}
22
Main Difference between AVR, ARM, 8051 and PIC Microcontrollers
8051 PIC AVR ARM
32-bit mostly
8-bit for standard
Bus width 8/16/32-bit 8/32-bit also available in
core
64-bit
UART, USART,
PIC, UART, USART, LIN, UART, USART, SPI, LIN, I2C, SPI,
UART, CAN, Ethernet, I2C, (special purpose CAN, USB,
Communication
USART,SPI,I2C AVR support CAN, Ethernet, I2S,
Protocols
SPI, I2S USB, Ethernet) DSP, SAI (serial
audio interface),
IrDA
12 Clock/instruction 1 clock/ instruction 1 clock/
Speed 4 Clock/instruction cycle
cycle cycle instruction cycle
Modified
Memory Von Neumann
Harvard architecture Modified Harvard
Architecture architecture
architecture
Apple, Nvidia,
NXP, Atmel, Silicon Qualcomm,
Manufacturer Labs, Dallas, Cyprus, Microchip Average Atmel Samsung
Infineon, etc. Electronics, and
TI etc.
Cost (as
compared to Very Low Average Average Low
features provide)
LPC2148, ARM
Popular PIC18fXX8, PIC16f88X, Atmega8, 16, 32, Cortex-M0 to
AT89C51, P89v51, etc.
Microcontrollers PIC32MXX Arduino Community ARM Cortex-
M7, etc.
23