Professional Documents
Culture Documents
INTRODUCTION
1.1 INTRODUCTION TO EMBEDDED SYSTEMS
An embedded system is a computer system with a dedicated function within a larger mechanical
or electrical system, often with real-time computing constraints.It is embedded as part of a
complete device often including hardware and mechanical parts. Embedded systems control
many devices in common use today. Ninety-eight percent of all microprocessors are
manufactured as components of embedded systems.
Examples of properties of typically embedded computers when compared with general-purpose
counterparts are low power consumption, small size, rugged operating ranges, and low per-unit
cost. This comes at the price of limited processing resources, which make them significantly
more difficult to program and to interact with. However, by building intelligence mechanisms on
top of the hardware, taking advantage of possible existing sensors and the existence of a network
of embedded units, one can both optimally manage available resources at the unit and network
levels as well as provide augmented functions, well beyond those available.For example,
intelligent techniques can be designed to manage power consumption of embedded systems.
Modern embedded systems are often based on microcontrollers (i.e. CPU's with integrated
memory or peripheral interfaces),but ordinary microprocessors (using external chips for memory
and peripheral interface circuits) are also common, especially in more-complex systems. In either
case, the processor(s) used may be types ranging from general purpose to those specialised in
certain class of computations, or even custom designed for the application at hand. A common
standard class of dedicated processors is the digital signal processor (DSP).
Embedded systems are commonly found in consumer, cooking, industrial, automotive, medical,
commercial and military applications.
Telecommunications systems employ numerous embedded systems from telephone switches for
the network to cell phones at the end user. Computer networking uses
dedicated routers and network bridges to route data.
Consumer electronics include MP3 players, mobile phones, videogame consoles, digital
cameras, GPS receivers, and printers. Household appliances, such as microwave ovens, washing
machines and dishwashers, include embedded systems to provide flexibility, efficiency and
features. Advanced HVAC systems use networked thermostats to more accurately and efficiently
control temperature that can change by time of day and season. Home automation uses wired-
and wireless-networking that can be used to control lights, climate, security, audio/visual,
surveillance, etc., all of which use embedded devices for sensing and controlling.
1.2 INTRODUCTION TO MICRO-CONTROLLER
A microcontroller (or MCU for microcontroller unit) is a small computer on a single integrated
circuit. In modern terminology, it is a system on a chip or SoC. A microcontroller contains one or
more CPUs (processor cores) along with memory and programmable input/output peripherals.
Program memory in the form of Ferroelectric RAM, NOR flash or OTP ROM is also often
included on chip, as well as a small amount of RAM. Microcontrollers are designed for
embedded applications, in contrast to the microprocessors used in personal computers or other
general purpose applications consisting of various discrete chips.
Microcontrollers are used in automatically controlled products and devices, such as automobile
engine control systems, implantable medical devices, remote controls, office machines,
appliances, power tools, toys and other embedded systems. By reducing the size and cost
compared to a design that uses a separate microprocessor, memory, and input/output devices,
microcontrollers make it economical to digitally control even more devices and processes. Mixed
signal microcontrollers are common, integrating analog components needed to control non-
digital electronic systems.
Some microcontrollers may use four-bit words and operate at frequencies as low as 4 kHz, for
low power consumption (single-digit milliwatts or microwatts). They will generally have the
ability to retain functionality while waiting for an event such as a button press or other interrupt;
power consumption while sleeping (CPU clock and most peripherals off) may be just nanowatts,
making many of them well suited for long lasting battery applications. Other microcontrollers
may serve performance-critical roles, where they may need to act more like a digital signal
processor (DSP), with higher clock speeds and power consumption.
1.2.1 TYPES OF MICROCONTROLLERS
As of 2008, there are several dozen microcontroller architectures and vendors including:
The 8051 requires an external oscillator circuit. The oscillator circuit usually runs around
12MHz, although the 8051 (depending on which specific model) is capable of running at a
maximum of 40MHz. Each machine cycle in the 8051 is 12 clock cycles, giving an effective
cycle rate at 1MHz (for a 12MHz clock) to 3.33MHz (for the maximum 40MHz clock). The
oscillator circuit generates the clock pulses so that all internal operations are synchronized.
One machine cycle has 6 states. One state is 2 T-states. Therefore one machine cycle is 12 T-
states. Time to execute an instruction is found by multiplying C by 12 and dividing product by
Crystal frequency.
Click on START button and select Programs / SPJ Systems SC51 / SIDE51. The screen
should look something like this:
Now select Project/Open Project from the menu. Select the path where you installed SC51
and select the EXAMPLES folder. You will see a list of project files (extention
.P51). From the list, select hello.p51. Click Ok. Doing that will open the project HELLO.P51.
This will also automatically open the file HELLO.C in the editor. This is simple program
which outputs the character string Hello World !\n on the standard output device (i.e. the on-
chip serial port of 8051). If the HELLO.C file is not automatically opened, you can select
File/Open from the menu and then double-click Hello.C from the list of files.
2.3 Compiling the program :
To compile the program, select Compile/Build or Compile/Re-build All from the menu.
Doing this may invoke one or more these applications: C compiler, Assembler, Linker. If there
are no errors, then Intel HEX format file (.HEX) and optionally .BIN file will be produced.
The error and warning messages produced by compiler, assembler, linker will be displayed in
the error window. If there are any errors, you may correct them and repeat the process until all
errors are gone.
To run (i.e. debug) the program, select Tools/Simulator from the menu. Doing this will
invoke the Simulator program. The screen should look something like this:
Please note that the program window is already open. In our example, the program simply
sends a message Hello World !\n to the serial port. In order to debug the program, we must
monitor the serial port output. It can be monitored in the Serial Port Window which is
already visible. To view / hide the Serial Port Window, select View/Serial window from the
menu.
Similarly, you can show or hide other windows visible as per your need. To monitor
memory locations in the external RAM, select View / External RAM watch. To monitor
memory locations in the internal RAM, select View / Internal RAM watch. To monitor
the status of connected peripheral devices, select View / I/O watch from the menu. To
monitor the internal registers / SFRs of 8051, select View / SFR watch from the menu. To
monitor C program variables, select View / C variable watch from the menu. Once you
have made the required windows visible, you can run the program either in continuous
mode or in single step mode. To single step through your program, select Run / Single
step from the menu or press F7. Doing this will execute on C language statement at a
time. On the contrary, if you select Run/Run from the menu (or press Ctrl.F7), the
program will run in continuous mode.
To stop the program execution, select Run / Terminate program, or press Ctrl.F2.
Set the necessary options in all the 3 tabs and when done, press Ok button. This will save the
changed settings in the project file. To discard the changes made, press Cancel button.
To send data to serial port, first initialize serial port. To initialize serial port go to
Visual Code Generator menu. It will display 3 options (Initialize TMOD/TCON, Initialize
Serial Port and Interrupt Settings). Select Initialize Serial Port.
Initialize Serial Port window will be displayed. Do proper settings to initialize serial port.
1. Serial Port Mode: 1 : 8-Bit UART (Baud Rate: Variable)
2. Enable REN (Receiver Enable)
3. For Baud Rate Use: TIMER 1
4. Desired Baud Rate: 9600
5. Crystal Frequency: 11.0592 (Depending on Hardware)
6. Click on Generate Code
Click on Copy C Code, corresponding code will be copied. Last Line is Please also
initialize TMOD / TCON. We have to initialize TMOD / TCON because we have used
TIMER 1 for Baud Rate Generation. Click on Ok. Paste the code in the program.
Now we are ready to simulate this program. To do so, simply select Simulator from the
Tools menu.
Simulator for 8051 is an independent program and it will be launched by this action and the
current project (PROG1) will be automatically opened. The Simulator will automatically
display 5 windows (clockwise, from left-top corner): Program window, Serial window,
Internal RAM watch window, SFR watch window, and Symbols watch window. As the
names indicate, these windows display the corresponding information. So, the same program
that you typed (PROG1.C) will be visible in the Program window.
In Simulator you can Debug the program. Different options available in Run menu to debug
the program are:
1. Insert Breakpoint
2. Single Step
3. Step Over
4. Run
5. Terminate Program
6. CPU Reset
To single step through the program, you may select Single Step from the Run menu.
Pressing F7 key is also equivalent to selecting Single Step from the Run menu.
When you do so, the program window will indicate that one C statement has been
executed and the next statement will be highlighted. You may repeat the same to single
step through the entire program.
While single stepping through the program, the contents of general purpose registers (R0-R7) as
well SFRs will be displayed in the SFR Watch Window. The register /SFR contents can be seen
changing as effect of each executed instruction.
It is also possible to modify register / SFR contents by double clicking on its name. (Contents of
PC cannot be modified). Eg. To modify contents of Port 1 double click on P1 in SFR Watch
Window. Edit SFR P1 window will be displayed. You can Enter Hex Value or change Binary
Representation by clicking on the required bit.
Similarly, it is possible to observe values of variables. To do so, right click in the Variables
Watch Window and select Add from the pop-up menu. Select the desired variable(s) to
watch and click OK button. This will display the names and values of the selected variables in
the variables watch window. It is also possible to modify a variable value by double-clicking on
its name.
The Serial I/O window displays the activity related to 8051 serial port. i.e. any character
transmitted to the serial port is displayed in this window. Similarly, it is possible to simulate
receiving characters. To do so, simply click inside the Serial window and type any character(s)
that you wish to simulate as received.
Now Single Step or Run the program from Run menu. We can see WELCOME message on
Serial I/O Window.
When you are through with debugging, select Exit from File menu to close the Simulator. Then
(if necessary) you may edit the program, build it again and start the simulator to debug again.
Please note, only one instance of Simulator program should be started at any time. It is
recommended to close the Simulator while making any changes to the program.
CHAPTER 3
DOWNLOADING AND RUNNING USER PROGRAMS
Microcontroller P89V51RD2 includes on-chip flash for storing user program and non-volatile
data. This flash is In-System-Programmable (ISP). Therefore it is possible to download user
program into on-chip flash, through serial port connected to PC.
This section describes how to use the software Flash Magic to download program into
P89V51RD2.
5. Do proper settings in Flash Magic (COM Port: Suppose COM1, Baud Rate:
4800, Device: 89V51RD2, Interface: None (ISP), Enable Erase blocks used by Hex
File).
6. Click on Verify After Programming. This option will verify that the program loaded in
the flash is same as the hex file selected. Thus if any corruption of data has
occurred then it can be detected.
7. IMPORTANT:
a. DO NOT SELECT: Set Security Bit 1. This will block the further erase and write
cycles on the flash of P89V51RD2.
b. DO NOT SELECT: Fill unused Flash, this will consume lot of time in programming.
c. DO NOT SELECT: Gen Block checksums.
d. DO NOT SELECT: Prog Clocks Bit
8. Click on Browse button.
9. Use the browse option to go to the required directory.
10. Click on the hex file you want to download to the target hardware.
11. Click on open button. Once you do this the file will be selected in the Flash Magic.
12. Click on the Start button. Following message will be displayed:
13. At this point you should power ON the board. If the board is already powered ON,
then you should press the Reset Switch (RST KEY) on the board.
14. After the above, the above message will dis-appear and the programming will start.
15. Once the file is loaded it will be verified.
16. Finally you will get the message on the bottom of the Flash Magic Screen as Finished.
17. To Run the program, RESET the board or Power OFF and Power ON the board again.
CHAPTER 4
EXPERIMENTS
4.1 Experiment 1:To develop and execute program to generate delay and use it in
LED blinking.
Aim: Write a C language program to perform LED blinking after some delay.
Equipments: SC51, PC, TR-51, LED Interface Module.
Source code:
#include<reg51.h>
#define led P2
sbit s = P1^0;
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void main()
{
s=0;
while(1)
{
if(s==1)
{
P2=17;
delay(100);
P2=0;
delay(100);
}
else
P2=0;
}
}
Connections:
Connect FRC cable between FRC 2 connector on TR-51 board and FRC 2 connector on LED
Interface Module.
Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3
To download and run this program: Refer Chapter 3
Output:
You can see output on LED display.
4.2 Experiment 2: To develop and execute the program for 7 segments LED
display to the Microcontroller 8051.
Source Code:
#include<reg51.h>
#define value P1
void delay(unsigned int);
void main()
{
while(1)
{
value=0x3f;
delay(90);
value=0x06;
delay(90);
value=0x5b;
delay(90);
value=0x4f;
delay(90);
value=0x66;
delay(90);
value=0x6d;
delay(90);
value=0x7d;
delay(90);
value=0x07;
delay(90);
value=0x7f;
delay(90);
value=0x6f;
delay(90);
}
}
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
{
for(j=0;j<1275;j++)
{}
}
}
Connections:
Connect FRC cable between FRC 2 connector on TR-51 board and FRC 2 connector on 7
Segment LED Interface Module.
Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3
To download and run this program: Refer Chapter 4
Output:
You can see characters 1,2,3,4,5,6,7,8 and 9 on single 7 segment LED display.
4.3 Experiment 3: To develop and execute the program to generate square wave on port lines of
port 1 of the microcontroller 8051
Aim: Write a C language program for Microcontroller 8051 to generate square wave of
frequency 1 KHz on all port lines of Port 1.Use hardware delay technique.
Equipments: SC51, PC, TR-51, SDIO8.
Source Code:
#include <Intel\8051.h>
#include <standard.h>
unsigned char g_byData = 0; /* Declare Variable */ void main ()
{
/* Following while loop is an infinite loop.
The action which you want to perform continuously, should be written in this loop. In
this program we are generating square wave on all port pins of Port 1, continuously.
The frequency of the Square Wave is approximately 1KHz */
while(1)
{
P1 = g_byData; /* Make all the port pins of Port 1 low */
delay_ms(500); /* This is a library Function. To see help of this, move
the cursor on the function and press F1 and keep it low
for approx 500*1000 machine cycles. If target
hardware freq is 12 MHz, this will generate
approximately 500 milli seconds delay */
P1 = ~g_byData; /* Make all the port pins of Port 1 high */
delay_ms(500); /* Keep them low for approx. 500ms */
}
}
Connections: Connect one channel of Oscilloscope to any pin of Port 1.
Procedure: To Edit / Compile/ Generate Hex file: Refer Section 2.3
To download and run this program: Refer Chapter 3
Output: You can see the square wave of frequency 1 KHz on oscilloscope.
4.4 Experiment 4: To develop and execute the program to interface 16X2 LCD display to
the Microcontroller 8051
Aim: Write a C language program for Microcontroller 8051 to display
message WELCOME on 16 X 2 LCD display.
Source code:
#include <Intel\8051.h>
#include <standard.h>
BIT RS P3.2 // Register Select. 0 = Cmd , 1 = data
BIT RW P3.1 // Read/Write, 0 = write, 1 = read
BIT EN P3.0 // LCD Enable
#define DATAPORT P2 // Port P2 connected to data lines of the LCD
unsigned char g_byData = 0xff ; //
Variable for data void lcd_cmd (unsigned
char p_byCmd)
{
// sends command to LCD
RS = 0; // select command register
RW = 0; // Make Read write in low
DATAPORT = p_byCmd ;
// Generate a pulse on EN Pin
EN = 1;
asm
nop
asm
nop
EN = 0;
}
void lcd_init ()
{
/* this function is written for 16 char X 2 line LCD row == 0 means first row row == 1
second row */ unsigned char count, ch ;
if(p_byRow) // if row is same as saying if row is non zero, in our
// case its row = 1
lcd_cmd(0xc0); // display on second line
else
lcd_cmd(0x80); //display on first line
delay(250) ;
for(count=0;count<16;count++)
{
if(ch = p_byDataArr[count]) // equate p_byDataArr element to ch and
check if
{ // its value is non zero
lcd_dat(ch); // If not non zero send data to the lcd
delay(250) ;
}
else {
while(count < 16)
{
lcd_dat(' ') ; // insert spaces until end of line
delay(250) ;
count ++ ;
}
}
}
}
void main (){
{
display_row(0,"WELCOME");
}
Connections:
Connect FRC cable between FRC 2 connector on TR-51 board and FRC 2 connector on
LCD Interface Board.
Output:
WELCOME will be displayed on LCD
4.5 Experiment 5: To develop and execute the program to interface stepper
motor to the microcontroller 8051
Aim: Write a C language program for Microcontroller 8051 to
rotate stepper motor continuously in clockwise direction. Motor is
interfaced through Port 0 of Microcontroller 8051.
Source Code:
#include <Intel\8051.h>
#include <standard.h>
P0 = PHASEC;
delay_ms(120) ;
P0 = PHASEB;
delay_ms(120) ;
P0=PHASEA;
delay_ms(120);
}
}
Connections:
Connect FRC cable between FRC 1 connector on TR-51 board and FRC 1 connector on
Stepper Motor Driver Circuit.
Connect FRC cable between FRC 2 connector on TR-51 board and FRC 2 connector on
Stepper Motor Driver Circuit.
Connect Stepper Motor to SM-CON connector on Stepper Motor Driver Circuit.
If stepper motor requires +5V or +12V external power supply then connect it to +5V or
+12V supply respectively (depending on colour combination of connector pins), present on
main power supply provided along with TR-51 board.
Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3
Output:
Stepper motor will rotate in clockwise direction.
4.6 Experiment 6: To develop and execute the program to interface ADC 0809 to the
microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to read the digital output of the
ADC and output it on port 1 and on serial port.
Source Code:
#include <Intel\8051.h>
#include <standard.h>
/*
8 LEDs are connected to port P1.
So data read from ADC should be sent to P1.
ADC channel selection pins = P2.2, P2.1, P2.0
ADC start of conversion pin = P0.1
ADC ALE pin = P0.2
ADC end of conversion pin = P0.4
ADC output enable pin = P0.0
ADC data =P3
BIT oe P0.0
unsigned char adc_data;
void initser()
{
TH1 = 0xfd ;
TL1 = 0xfd ;
PCON &= 0x7f ;
SCON = 0x50 ;
TMOD = 0x20 ;
TCON = 0x40 ;
}
void main ()
{
initser();
P2 = 0x02; /* select channel IN2 */
ale = 1; /* ale pin = high */
delay_ms(60);
/*
Following while loop is an infinite loop
So ADC data of channel IN2 will be continuously read and sent to P1 and serial
port. Since it is connected to a POTentiometer, output will change when POT is
moved.
*/
while(1)
{
/*
}
wait until conversion completes. */
oe = 1;
/* output enable pin = high */
asm nop
delay_ms(60
);
adc_data = P3; /* conversion is complete. Now read ADC data. */
Connections:
Connect FRC cable between FRC 2 connector on TR-51 board and FRC 2 connector on
ADC Interface Board.
Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3
Output:
Digital value of corresponding analog input can be seen on LEDs (on SDIO8 board)
connected to Port 1 of 8051 microcontroller. If you vary the analog input by varying pot
then corresponding digital value can be seen on LEDs.