SlideShare a Scribd company logo
MICROCONTROLLERS LABORATORY (15EE51) REPORT
JABEZ WINSTON C 15MU01
Microcontroller Laboratory report submitted in partial fulfilment of the requirements for the degree
of
MASTER OF ENGINEERING
Branch: ELECTRICAL AND ELECTRONICS ENGINEERING
EMBEDDED AND REAL TIME SYSTEMS
Of Anna University, Chennai.
December 2015
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING
PSG COLLEGE OF TECHNOLOGY
(Autonomous Institution)
COIMBATORE – 641 004
TABLE OF CONTENTS
Ex.No Date Title of the Experiment
Page
No.
Signature of Staff
1 7-9-2015
INTRODUCTION TO KEIL µVISION IDE 1
Debug Windows and Dialogs 1
Working with Keil µVision IDE 3
Sample program for blinking LEDs 11
2 10-10-2015
ASSEMBLY LANGAGE PROGRAMMING IN 8051 14
Timers/Counters 14
Serial Communication 17
Interrupts 19
3 12-11-2015
C LANGUAGE PROGRAMMING IN 8051 22
Timers/Counters 22
Serial Communication 26
Interrupts 28
4 16-11-2015
INTERFACED HARDWARE WITH 8051 30
LED interfacing with Zkit-51 30
Buzzer interfacing with Zkit-51 31
LCD interfacing with Zkit-51 32
Matrix keypad interfacing with Zkit-51 35
TABLE OF FIGURES
Fig.No. Title of the Figure Page No.
1.1 Keil IDE showing the various Windows available 2
1.2 Creation of new project 3
1.3 ‘Create New Project’ Window 3
1.4 ‘Select Device for Target’ Window 4
1.5 Drop down Menu in Project Window 4
1.6 ‘Add New Item ‘ Dialog Box 5
1.7 Keil IDE with sample program 5
1.8 ‘View’ Drop down menu in Keil IDE 6
1.9 Logic Analyzer Setup 6
1.10 Logic Analyzer Window 7
1.11 Setup Performance Analyzer 7
1.12 Performance Analyzer Window 7
1.13 Register Window 8
1.14 Memory Window showing Code Memory 8
1.15 Memory Window showing Data Memory 8
1.16 Disassembly Window 9
1.17 Serial Window 9
1.18 Program having breakpoints at Line 4,11,13 10
1.19 ‘Flash’ Drop Down Menu in Keil IDE 10
1.20 ‘Options for Target’ Window 10
1.21 HEX file in Objects Folder 11
1.22 Contents of HEX file 11
1.23 LED Window 12
1.24 LED Setup 13
1.25 LED Window showing LEDs 13
2.1 Waveform at P1.5 14
2.2 Timer 1 registers 15
2.3 Waveform at P1.2 16
2.4 Counter output 16
2.5 Serial Communication 18
2.6 Windows showing Interrupts and Serial Communication 20
3.1 Waveform at P1.5 22
3.2 Timer registers 23
3.3 Counter output 25
3.4 Serial Communication 27
3.5 External ,Timer and Serial Communication Interrupt 29
4.1 RED LED glowing when KEY1 is pressed 31
4.2 GREEN LED glowing when KEY2 is pressed 31
4.3 Two strings printed on LCD 35
4.4 Matrix keypad and 16 x 2 LCD display 38
15EE51 – Microcontrollers Laboratory Page 1
1.INTRODUCTION TO KEIL µVISION IDE
Keil µVision IDE is a window-based software development platform for 8051 and ARM
microcontrollers that combines a robust and modern editor with a project manager and make facility
tool. It integrates all the tools needed to develop embedded applications including a C/C++ compiler,
macro assembler, linker/locator, and a HEX file generator.
µVision GUI
The µVision GUI provides menus for selecting commands and toolbars with command buttons. The
Status Bar, at the bottom of the window, displays information and messages about the current
µVision command. Windows can be relocated and docked to another physical screen. The window
layout is saved for each project automatically and restored the next time the project is used. You can
restore the default layout using the menu Window – Reset View to Defaults.
µVision has two operating modes, the Build Mode for creating applications and the Debug Mode for
analyzing applications, which offers additional Windows and Dialogs.
Debug Windows and Dialogs
µVision provides many debugging windows and dialogs. Some of them are
 Breakpoints- Define stop conditions for program execution.
 Code Coverage- Examine statistics about code execution, including branch testing.
 Command Window- Enter and view executed commands.
 Disassembly Window- Test programs at the level of assembly instructions.
 Logic Analyzer - Investigate value changes of peripherals, registers, and variables on a time
graph.
 Memory Map- Evaluate memory areas and their access rights.
 Memory Window - Analyze and modify memory content.
 Performance Analyzer - Evaluate time and call statistics on module or function level.
 Registers Window- view and modify register content.
 Serial Window is a communication interface between the application and the PC.
 Status Bar - View debugging status information.
 Symbols Window- Find debug symbol information used in program.
 System Viewer Find peripheral register information and change property values at runtime.
 Toolbox Use and define configurable buttons for executing debugging commands
interactively.
Ex. No. 1
07-09-2015
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 2
Fig1.1:KeilIDEshowingthevariousWindowsavailable
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 3
Working with Keil µVision IDE
Getting started…
1.OpenKeil µVision ,Click Project New µVision Project.
2.Type a name and save the project file
Fig.1.3
Fig.1.2:Creation of new project
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 4
3.Select Device for target as AT89S51as shown in Fig. 1.4
Fig.1.4
4.Right click Source Group 1 and select Add new Item to Source Group 1
Fig.1.5
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 5
Choose file type as ASM ,name the file and click Add
Fig. 1.6
5.Type the code ,press F7/Bulid icon and Ctrl+F5 / Debug icon
Fig.1. 7
In Debug mode various simulations can be done with variety of tools like Logic Analyzer, Performance
Analyzer,etc.,Register and Memory status can be viewed during execution.
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 5
Choose file type as ASM ,name the file and click Add
Fig. 1.6
5.Type the code ,press F7/Bulid icon and Ctrl+F5 / Debug icon
Fig.1. 7
In Debug mode various simulations can be done with variety of tools like Logic Analyzer, Performance
Analyzer,etc.,Register and Memory status can be viewed during execution.
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 5
Choose file type as ASM ,name the file and click Add
Fig. 1.6
5.Type the code ,press F7/Bulid icon and Ctrl+F5 / Debug icon
Fig.1. 7
In Debug mode various simulations can be done with variety of tools like Logic Analyzer, Performance
Analyzer,etc.,Register and Memory status can be viewed during execution.
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 6
Logic Analyzer
 To perform Logic Analysis,Click View  Analysis WindowLogic Analyzer as shown in Fig 1.8
Fig. 1.8
The Logic Analyzer Window opens up.Click Setup and configure as shown below to observe waveform at P1.0
Fig. 9: Logic Analyzer Setup
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 7
Output waveform will be displayed as
Fig. 1.10: Logic Analyzer
Performance Analyzer
 To perform analysis of various parts of the program,Click View  Analysis Window Performance
Analyzer
 Click Setup and configure as follows
Fig. 1.11: Setup Performance Analyzer
 Output of performance is shown. Average execution time of a function and various other parameters
are shown to pave way for optimization
Fig. 1.12: Performance Analyzer
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 7
Output waveform will be displayed as
Fig. 1.10: Logic Analyzer
Performance Analyzer
 To perform analysis of various parts of the program,Click View  Analysis Window Performance
Analyzer
 Click Setup and configure as follows
Fig. 1.11: Setup Performance Analyzer
 Output of performance is shown. Average execution time of a function and various other parameters
are shown to pave way for optimization
Fig. 1.12: Performance Analyzer
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 7
Output waveform will be displayed as
Fig. 1.10: Logic Analyzer
Performance Analyzer
 To perform analysis of various parts of the program,Click View  Analysis Window Performance
Analyzer
 Click Setup and configure as follows
Fig. 1.11: Setup Performance Analyzer
 Output of performance is shown. Average execution time of a function and various other parameters
are shown to pave way for optimization
Fig. 1.12: Performance Analyzer
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 8
Register Window
Memory Window
 To access Memory Window,Click View  Memory Window  Memory 1
 We can view both code memory as well as data memory.
 Code memory contains the opcodes.Data memory contains the contents of RAM.
 To access code memory or data memory,use C: or D: followed by address.
Eg: C:0x00 D:0x00
Fig. 1.14 : Memory Window showing Code Memory
 To access Register Window ,Click View 
Register Window .
 Register window displays the contents of
Accumulator A ,register B,registers from R0
to R7,Program Status Word(PSW),Data
Pointer(DPTR),Stack Pointer(SP),Program
counter(PC),etc.,This makes program
debugging easier
Fig. 1.13 : Register Window
Fig. 1.15 : Memory Window showing Data Memory
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 8
Register Window
Memory Window
 To access Memory Window,Click View  Memory Window  Memory 1
 We can view both code memory as well as data memory.
 Code memory contains the opcodes.Data memory contains the contents of RAM.
 To access code memory or data memory,use C: or D: followed by address.
Eg: C:0x00 D:0x00
Fig. 1.14 : Memory Window showing Code Memory
 To access Register Window ,Click View 
Register Window .
 Register window displays the contents of
Accumulator A ,register B,registers from R0
to R7,Program Status Word(PSW),Data
Pointer(DPTR),Stack Pointer(SP),Program
counter(PC),etc.,This makes program
debugging easier
Fig. 1.13 : Register Window
Fig. 1.15 : Memory Window showing Data Memory
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 8
Register Window
Memory Window
 To access Memory Window,Click View  Memory Window  Memory 1
 We can view both code memory as well as data memory.
 Code memory contains the opcodes.Data memory contains the contents of RAM.
 To access code memory or data memory,use C: or D: followed by address.
Eg: C:0x00 D:0x00
Fig. 1.14 : Memory Window showing Code Memory
 To access Register Window ,Click View 
Register Window .
 Register window displays the contents of
Accumulator A ,register B,registers from R0
to R7,Program Status Word(PSW),Data
Pointer(DPTR),Stack Pointer(SP),Program
counter(PC),etc.,This makes program
debugging easier
Fig. 1.13 : Register Window
Fig. 1.15 : Memory Window showing Data Memory
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 9
Disassembly Window
 Click View  Disassembly Window.
 Disassembly Window shows the opcodes for each instruction.
Fig. 1.16: Disassembly Window
Serial Window
 Click View  Serial Windows  UART #1
 This is useful for simulating Serial communication before downloading it to microcontroller.
Fig. 1.17 : Serial Window
Breakpoints
 Program can be made to stop at a particular instruction so as to know the status of memory and
registers at that point.
 To insert a breakpoint place on a particular line or instruction and press F9.Pressing F9 again will
remove the breakpoint.
 Multiple breakpoints can be inserted and program will stop at breakpoints when run.
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 9
Disassembly Window
 Click View  Disassembly Window.
 Disassembly Window shows the opcodes for each instruction.
Fig. 1.16: Disassembly Window
Serial Window
 Click View  Serial Windows  UART #1
 This is useful for simulating Serial communication before downloading it to microcontroller.
Fig. 1.17 : Serial Window
Breakpoints
 Program can be made to stop at a particular instruction so as to know the status of memory and
registers at that point.
 To insert a breakpoint place on a particular line or instruction and press F9.Pressing F9 again will
remove the breakpoint.
 Multiple breakpoints can be inserted and program will stop at breakpoints when run.
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 9
Disassembly Window
 Click View  Disassembly Window.
 Disassembly Window shows the opcodes for each instruction.
Fig. 1.16: Disassembly Window
Serial Window
 Click View  Serial Windows  UART #1
 This is useful for simulating Serial communication before downloading it to microcontroller.
Fig. 1.17 : Serial Window
Breakpoints
 Program can be made to stop at a particular instruction so as to know the status of memory and
registers at that point.
 To insert a breakpoint place on a particular line or instruction and press F9.Pressing F9 again will
remove the breakpoint.
 Multiple breakpoints can be inserted and program will stop at breakpoints when run.
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 10
Fig. 1.18 Program having breakpoints at Line 4,11 and 13
Generating HEX file
To download the compiled program into the 8051 ,HEX file needs to be generated.To generate HEX
file ,click Flash  Configure Flash Tools
Fig. 1.19
A dialog box opens up.Click the Output tab and check in Create HEX fileoption.ClickOK.
Fig.1.20
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 11
Press F7/Bulid icon . Generated HEX file can be found in the Objects folder
Fig. 1.21 : HEX file in Objects folder
Fig.1. 22:Contents of the HEX file
Writing a sample program for blinking 8 LEDs connected to PORT 1 of 8051 periodically in Keil µVision IDE….
Additional software required: DLL files for LED simulation ( LED_CONTROL.DLL and LED_DATABASE.CDB )
ALGORITHM:
1. Start
2. Load 0xFF to Port 1.
3. Call Delay routine.
4. Load 0x00 to Port 0.
5. Call Delay routine.
6. Goto Start
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 11
Press F7/Bulid icon . Generated HEX file can be found in the Objects folder
Fig. 1.21 : HEX file in Objects folder
Fig.1. 22:Contents of the HEX file
Writing a sample program for blinking 8 LEDs connected to PORT 1 of 8051 periodically in Keil µVision IDE….
Additional software required: DLL files for LED simulation ( LED_CONTROL.DLL and LED_DATABASE.CDB )
ALGORITHM:
1. Start
2. Load 0xFF to Port 1.
3. Call Delay routine.
4. Load 0x00 to Port 0.
5. Call Delay routine.
6. Goto Start
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 11
Press F7/Bulid icon . Generated HEX file can be found in the Objects folder
Fig. 1.21 : HEX file in Objects folder
Fig.1. 22:Contents of the HEX file
Writing a sample program for blinking 8 LEDs connected to PORT 1 of 8051 periodically in Keil µVision IDE….
Additional software required: DLL files for LED simulation ( LED_CONTROL.DLL and LED_DATABASE.CDB )
ALGORITHM:
1. Start
2. Load 0xFF to Port 1.
3. Call Delay routine.
4. Load 0x00 to Port 0.
5. Call Delay routine.
6. Goto Start
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 12
PROGRAM:
ORG 0x0000
START:MOV P1,#0xFF
ACALL DELAY
MOV P1,#0x00
ACALL DELAY
AJMP START
DELAY:MOV R0,#0xFF
L2:MOV R1,#0xFF
L1:DJNZ R1,L1
DJNZ R0,L2
RET
SIMULATION:
 Create a new project , add a new assembly file, type the above code and save it.
 To simulate the blink LED program, we have to download DLL for LED control from Keil website.
 Copy the LED_CONTROL.DLL and LED_DATABASE.CDB file to KeilC51BIN directory.
 Add this line to [C51] section of Tools.ini in Keil directory.
AGSI2=LED_CONTROL.DLL ("LED simulation")
 Restart Keil µVision IDE.
 With the project open, press Ctrl+F5 to Enter debug mode.
 Click Peripherals  LED. The LED Window opens up.
 Right Click and select add LED.New LED window opens up as shown in Fig. 1.23
Fig. 1.23
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 13
 Setup the pins for LED as shown in Fig 1.24
Fig. 1.24
 Similarly do the same for P1.1 ,P1.2,….P1.7
Fig. 1.25
 Press F5 to run the program.
RESULT:
Thus the development, debugging features of Keil µVision IDE were studied and a sample program for blinking
LED was executed.
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 13
 Setup the pins for LED as shown in Fig 1.24
Fig. 1.24
 Similarly do the same for P1.1 ,P1.2,….P1.7
Fig. 1.25
 Press F5 to run the program.
RESULT:
Thus the development, debugging features of Keil µVision IDE were studied and a sample program for blinking
LED was executed.
Introduction to Keil µVision IDE
15EE51 – Microcontrollers Laboratory Page 13
 Setup the pins for LED as shown in Fig 1.24
Fig. 1.24
 Similarly do the same for P1.1 ,P1.2,….P1.7
Fig. 1.25
 Press F5 to run the program.
RESULT:
Thus the development, debugging features of Keil µVision IDE were studied and a sample program for blinking
LED was executed.
15EE51 – Microcontrollers Laboratory Page 14
ASSEMBLY LANGUAGE PROGRAMMING IN 8051
A.Timers /Counters
AIM:
i. To operate the timer0 in mode 0 and generate square wave of 66 % duty cycle.
ii. To operate the timer1 in mode 2 and generate a delay of 100 µs.
iii. Count clock pulses on P3.4 (T0 pin) and put it on Port 2
PROGRAM:
i. For generating square wave of 66 % duty cyle using timer 0 in mode 0
L1:
SETB P1.5
ACALL DELAY1
CLR P1.5
ACALL DELAY2
SJMP L1
DELAY1:
MOV TH0,#0x00 ;8 bit TH0 value
MOV TL0,#0x00 ;5 bit TL0 value
SETB TR0 ;Start timer
L2: JNB TF0,L2 ;Poll TF0 flag
CLR TR0
CLR TF0
RET
DELAY2:
MOV TH0,#0x80 ;8 bit TH0 value
MOV TL0,#0x00 ;5 bit TL0 value
SETB TR0 ;Start timer
L3: JNB TF0,L3 ;Poll TF0 flag
CLR TR0
CLR TF0
RET
OUTPUT:
Figure 2.1 Waveform at P1.5
Ex No.2
10-10-2015
ASSEMBLY LANGUAGE PROGRAMMING IN 8051
15EE51 –Microcontrollers Laboratory Page 15
ii. To operate the timer1 in mode 2 and generate a delay of 100 µs.
PROGRAM:
MOV TMOD,#0x20 ;Timer 1 in 8 bit autoreload mode (mode 2)
MOV TH1,#-92 ;100 µs /1.085 µs = 92 , 11.0592MHz ==> 1.085 µs
LOOP:SETB P1.2
ACALL DELAY
CLR P1.2
ACALL DELAY
SJMP LOOP
DELAY:SETB TR1 ;Start timer 1
L1:JNB TF1,L1 ; Poll TF1 flag
CLR TF1
CLR TR1
RET
OUTPUT:
Figure 2.2 Timer 1 registers
ASSEMBLY LANGUAGE PROGRAMMING IN 8051
15EE51 –Microcontrollers Laboratory Page 16
Figure 2.3 Waveform at P1.2
iii. Count clock pulses on P3.5 (T0 pin) and put it on Port 2
PROGRAM:
MOV TMOD,#0x50 ; Counter 1 ,8 bit mode
MOV TH1,#0x00 ; Autoreload value
SETB P3.5
AGAIN:SETB TR1
BACK:MOV A,TL1
MOV P2,A
JNB TF1,BACK
CLR TR1
CLR TF1
SJMP AGAIN
OUTPUT:
Figure 2.4
ASSEMBLY LANGUAGE PROGRAMMING IN 8051
15EE51 –Microcontrollers Laboratory Page 17
B.Serial Communication
AIM:
To turn on and turn off LEDs connected at PORT 1 by using serial communication. If ‘y’ is sent LEDs are turned on
and if ‘n’ is sent LEDs are turned off.Acknowledgment messages are also end.
PROGRAM:
ORG 0x00
MOV TMOD,#0x20 ; Timer 1, 8 bit autoreload
MOV SCON,#0x50 ; 8 bit,1 stop bit,Receive enabled
MOV TH1,#-3 ; 9600 baud rate for 11.0592 MHz
SETB TR1
WAIT:JNB RI,WAIT ;Poll RI flag bit
MOV R0,SBUF
CLR RI
CJNE R0,#'y',C1 ;If ‘y’ turn on LED
MOV P1,#0x00
SJMP DISPLAY_ON
C1:CJNE R0,#'n',C2 ;If ‘n’ turn off LED
MOV P1,#0xFF
SJMP DISPLAY_OFF
C2:SJMP WAIT
STRING1:DB "LEDs ON",10,0
STRING2:DB "LEDs OFF",10,0
DISPLAY_ON:MOV DPTR,#STRING1
SJMP SEND
DISPLAY_OFF:MOV DPTR,#STRING2
SJMP SEND
SEND:CLR A
MOVC A,@A+DPTR
JZ WAIT
MOV SBUF,A ;Place character to be transmitted in SBUF
WT:JNB TI,WT ;Poll TI flag bit
CLR TI
INC DPTR
SJMP SEND
ASSEMBLY LANGUAGE PROGRAMMING IN 8051
15EE51 –Microcontrollers Laboratory Page 18
OUTPUT:
Figure 2.5 (a) Serial Communication
Figure 2.5 (b) Serial Communication
ASSEMBLY LANGUAGE PROGRAMMING IN 8051
15EE51 –Microcontrollers Laboratory Page 19
C.Interrupts
AIM:
To write a program for demonstrating external interrupt (edge triggered at P3.2) , timer overflow interrupt(Timer 0)
and serial communication interrupt.
PROGRAM:
ORG 0x0000 ;RESET INTERRUPT
LJMP MAIN
ORG 0x0003 ;EXTERNAL INTERRUPT 0
CPL P1.0
RETI
ORG 0x000B ;TIMER 0 OVERFLOW INTERRUPT
CPL P1.1
RETI
ORG 0x0023 ;SERIAL COMMUNICATION INTERRUPT
LJMP SERIAL_ISR
ORG 0x0030
MAIN:
MOV SCON,#0x50
MOV IE,#0x93 ;ENABLE EXT ,TIMER 0 , SERIAL INTERRUPT
MOV TMOD,#0x22
MOV TH0,#0x00
MOV TH1,#-3
SETB TR0
SETB TR1
SETB IT0 ;EDGE TRIGERRED EXT. INTERRUPT
HERE:SJMP HERE
SERIAL_ISR:
CLR RI
MOV R0,SBUF
CJNE R0,#'y',C1
MOV P2,#0x00
C1:CJNE R0,#'n',C2
MOV P2,#0xFF
C2:RETI
ASSEMBLY LANGUAGE PROGRAMMING IN 8051
15EE51 –Microcontrollers Laboratory Page 20
External interrupt toggles the LED connected with P1.0 , timer overflow interrupt will generate square wave P1.1
and serial interrupt for turning ON and OFF LEDs at Port 2
OUTPUT:
Figure 2.6(a) Windows showing Interrupts,Serial Communication
Figure 2.6(b) Windows showing Interrupts,Serial Communication
ASSEMBLY LANGUAGE PROGRAMMING IN 8051
15EE51 –Microcontrollers Laboratory Page 21
RESULT:
Thus programs for Timers/Counters, Serial Communication and Interrupts were written in assembly and
simulated using Keil µVision IDE.
15EE51 – Microcontrollers Laboratory Page 22
A. Timers /Counters
AIM:
i. To operate the timer0 in mode 0 and generate square wave of 66 % duty cycle.
ii. To operate the timer1 in mode 2 and generate a delay of 100 µs.
iii. Count clock pulses on P3.4 (T0 pin) and put it on Port 2
PROGRAM:
i. For generating square wave of 66 % duty cycle using timer 0 in mode 0
C CODE:
#include<reg51.h>
void delay1();
void delay2();
void main()
{
while(1)
{
P1|=(1<<5); //Set bit P1.5
delay1();
P1&=~(1<<5); //Clear bit P1.5
delay2();
}
}
void delay1()
{
TH0=0x00;
TL0=0x00;
TR0=1; //Start Timer 0
while(!TF0); //Poll TF0 flag
TR0=0;
TF0=0;
}
void delay2()
{
TH0=0x80;
TL0=0x00;
TR0=1; //Start Timer 0
while(!TF0); //Poll TF0 flag
TR0=0;
TF0=0;
}
C LANGUAGE PROGRAMMING IN 8051Ex No.3
12-11-2015
C PROGRAMMING IN 8051
15EE51 – Microcontrollers Laboratory Page 23
OUTPUT:
Figure 3.1 Waveform at P1.5
ii. To operate the timer1 in mode 2 and generate a delay of 100 µs.
C CODE:
#include<reg51.h>
void delay();
void main()
{
TH1=-92; // 100 µs/1.085 µs = 92 for 11.592 MHz
TMOD=0x20;
while(1)
{
P1|=(1<<2); //Set bit P1.2
delay();
P1&=~(1<<2); //Clear bit P1.3
delay();
}
}
void delay()
{
TR1=1;
while(!TF1); //Poll TF1 flag
TF1=0;
TR1=0;
}
C PROGRAMMING IN 8051
15EE51 – Microcontrollers Laboratory Page 24
OUTPUT:
Figure 3.2 (a) Timer registers
Figure 3.2 (b) Timer registers
iii. Count clock pulses on P3.5 (T0 pin) and put it on Port 2
C CODE:
#include<reg51.h>
void main()
{
TMOD=0x50; //Counter 1 ,Mode 1
TH1=0x00;
T0=1;
while(1)
{
TR1=1;
while(!TF1)
P2=TL1;
TR1=0;
TF1=0;
}
}
C PROGRAMMING IN 8051
15EE51 – Microcontrollers Laboratory Page 25
OUTPUT:
Figure 3.3 Counter output
C PROGRAMMING IN 8051
15EE51 – Microcontrollers Laboratory Page 26
B. Serial Communication
AIM:
To turn on and turn off LEDs connected at PORT 1 by using serial communication. If ‘y’ is sent LEDs are turned on
and if ‘n’ is sent LEDs are turned off.Acknowledgment messages ”LEDs ON” and “LEDs OFF” are also sent.
C CODE:
#include<reg51.h>
void Serial_Init();
void Serial_Str_Transmit(char *);
char Serial_Char_Receive();
void main()
{
char ch;
Serial_Init();
while(1)
{
ch=Serial_Char_Receive();
switch(ch)
{
case 'y':
P1=0x00;
Serial_Str_Transmit("LEDs ONn");
break;
case 'n':
P1=0xFF;
Serial_Str_Transmit("LEDs OFFn");
break;
}
}
}
void Serial_Init()
{
TMOD=0x20;
SCON=0x50;
TH1=-3; // 9600 baud rate for 11.0592 MHz
TR1=1;
}
char Serial_Char_Receive()
{
while(!RI); //Poll RI flag
RI=0; //Clear RI flag
return(SBUF);
}
C PROGRAMMING IN 8051
15EE51 – Microcontrollers Laboratory Page 27
void Serial_Str_Transmit(char *p)
{
while(*p!='0')
{
SBUF=*p;
while(!TI); //Poll TI flag
TI=0;
p++;
}
}
OUTPUT:
Figure 3.4 Serial Communication
C PROGRAMMING IN 8051
15EE51 – Microcontrollers Laboratory Page 28
C. Interrupts
AIM:
To write a program for demonstrating external interrupt (edge triggered at P3.2) , timer overflow interrupt(Timer 0)
and serial communication interrupt.
C CODE:
#include<reg51.h>
void Serial_Init();
void Interrupt_Init();
void Timer0_Init();
void main()
{
Serial_Init();
Interrupt_Init();
Timer0_Init();
while(1); // Wait forever
}
void Serial_Init()
{
TMOD=0x20;
SCON=0x50;
TH1=-3;
TR1=1;
}
void Interrupt_Init()
{
IE=0x93;
IT0=1;
}
void Timer0_Init()
{
TMOD|=0x02;
TR0=1;
}
void ext_int0(void) interrupt 0 //Ext.Interrupt 0
{
P1^=(1<<0);
}
void timer_int0(void) interrupt 1 //Timer interrupt 0
{
P1^=(1<<1);
}
C PROGRAMMING IN 8051
15EE51 – Microcontrollers Laboratory Page 29
void serial_int(void) interrupt 4 //Serial Interrupt
{
RI=0;
if(SBUF=='y')
P2=0x00;
if(SBUF=='n')
P2=0xFF;
}
External interrupt toggles the LED connected with P1.0 , timer overflow interrupt will generate square wave P1.1
and serial interrupt for turning ON and OFF LEDs at Port 2
OUTPUT:
Figure 3.5 External ,Timer and Serial Communication interrupt
RESULT:
Thus assembly and C programs for Timers/Counters, Serial Communication and Interrupts in 8051
microcontroller were written and simulated using Keil µVision IDE.
15EE51 – Microcontrollers Laboratory Page 30
AIM:
i. To turn on only RED LED (P1.4) when key 1(P2.0) is pressed and turn only GREEN LED(P1.5)
when key 2(P2.1) is pressed
ii. To produce a single beep sound when key 1 is pressed and a double beep sound when key 2 is
pressed.
iii. To interface HD44780 compatible 16 × 2 LCD display in 8 bit mode.
iv. To interface a 4 × 4 Matrix keypad.
PROGRAM:
i. To write a program to turn on only RED LED (P1.4) when key 1(P2.0) is pressed and turn only
GREEN LED(P1.5) when key 2(P2.1) is pressed.
#include<reg51.h>
sbit RED =P1^4;
sbit GREEN =P1^5;
sbit KEY1=P2^0;
sbit KEY2=P2^1;
void main()
{
while(1)
{
if(!KEY1)
{
RED=0;
GREEN=1;
}
if(!KEY2)
{
RED=1;
GREEN=0;
}
}
4. INTERFACING HARDWARE WITH 8051
Ex No : 4
16-11-15
INTERFACING HARDWARE WITH 8051
15EE51 – Microcontrollers Laboratory Page 31
OUTPUT:
ii. To produce a beep sound when key 1 is pressed and a beep sound twice when key 2 is
pressed.
#include <reg51.h>
void beep();
void delay_ms(int d);
sbit BUZZER = P3^4;
void main()
{
while(1)
{
if(!KEY1)
{
beep();
}
if(!KEY2)
{
beep();
delay_ms(200);
beep();
}
}
Fig 4.1 RED LED glowing when Key1 is pressed Fig 4.2 GREEN LED glowing when Key2 is pressed
INTERFACING HARDWARE WITH 8051
15EE51 – Microcontrollers Laboratory Page 32
void beep()
{
unsigned char i = 25;
while (i--)
{
BUZZER = !BUZZER;
delay(5);
}
}
void delay_ms(int d)
{
int i;
TCON=0x01;
for(i=0;i<d;i++)
{
TH0=0x01;
TL0=0x40;
TR0=1;
while(!TF0);
TR0=0;
}
}
iii. To interface HD44780 compatible 16 × 2 LCD display in 8 bit mode.
#include<reg51.h>
sbit RS =P2^4;
sbit RW=P2^5;
sbit EN=P2^6;
#define CLEAR 0x01
void LCD_Init();
void LCD_SetCursor(char,char);
void LCD_Cmd(char);
void LCD_Data(char);
void LCD_PrintChar(char);
void LCD_PrintStr(char*);
void delay_ms(int);
INTERFACING HARDWARE WITH 8051
15EE51 – Microcontrollers Laboratory Page 33
void main()
{
RW=0;
LCD_Init();
LCD_PrintStr("Jabez Winston");
delay_ms(1000);
LCD_SetCursor(1,0);
LCD_PrintChar("Jagdeesh");
while(1);
}
void LCD_Init()
{
delay_ms(100);
LCD_Cmd(0x38);
delay_ms(10);
LCD_Cmd(0x0C);
LCD_Cmd(0x06);
LCD_Cmd(CLEAR);
}
void LCD_SetCursor(char r,char c)
{
switch(r)
{
case 0: LCD_Cmd(0x80+c);
break;
case 1: LCD_Cmd(0xC0+c);
break;
}
}
void LCD_Cmd(char cmd)
{
EN=0;
RS=0;
P1=cmd;
EN=1;
delay_ms(10);
EN=0;
}
INTERFACING HARDWARE WITH 8051
15EE51 – Microcontrollers Laboratory Page 34
void LCD_Data(char ch)
{
EN=0;
RS=1;
P1=ch;
EN=1;
delay_ms(10);
EN=0;
}
void LCD_PrintChar(char ch)
{
LCD_Cmd(ch);
}
void LCD_PrintStr(char *s)
{
while(*s)
LCD_PrintChar(*s++);
}
void delay_ms(int d)
{
int i;
TCON=0x01;
for(i=0;i<d;i++)
{
TH0=0x01;
TL0=0x40;
TR0=1;
while(!TF0);
TR0=0;
}
}
INTERFACING HARDWARE WITH 8051
15EE51 – Microcontrollers Laboratory Page 35
OUTPUT:
Figure 4.3 Two strings printed on LCD
iv. To interface a 4 × 4 Matrix keypad.
#include<reg51.h>
unsigned char get_key();
void lcd_init();
void lcd_printc(char);
void lcd_prints(char);
void lcd_clear();
void lcd_write_cmd(unsigned char );
void lcd_write_data(unsigned char );
void delay(int);
sbit LCD_RS = P2^4;
sbit LCD_WR = P2^5;
sbit LCD_EN = P2^6;
int x;
char a[4][4]={"123E",
"456U",
"789D",
"*0#e"};
INTERFACING HARDWARE WITH 8051
15EE51 – Microcontrollers Laboratory Page 36
void main()
{
lcd_init();
while(1)
{
while(get_key()==-1);
x=get_key();
lcd_printc(x);
delay(100);
}
}
unsigned char get_key()
{
unsigned char i,j,k;
for(i=0;i<4;i++)
{
P1=~(1<<i);
delay(20);
for(j=4;j<=7;j++)
{
if(!((P1)&(1<<j)))
{
k = a[i][j-4];
return k;
}
}
}
return -1;
}
void lcd_init()
{
delay(15); // LCD Power on delay
lcd_write_cmd(0x38); // 8BIT, 2 LINE , 5x8 DOT Format font
delay(5); // Small delay
lcd_write_cmd(0x38);
lcd_write_cmd(0x06);
lcd_write_cmd(0x0c);
lcd_write_cmd(0x0E);
lcd_clear();
INTERFACING HARDWARE WITH 8051
15EE51 – Microcontrollers Laboratory Page 37
}
void lcd_printc(char a_char)
{
lcd_write_data(a_char);
}
void lcd_prints(char *string)
{
while (*string)
lcd_printc(*string++);
}
void lcd_clear()
{
lcd_write_cmd(0x01);
}
void lcd_write_cmd(unsigned char cmd)
{
delay(10);
LCD_WR = 0;
LCD_EN = 0;
LCD_RS = 0; // Reset LCD_RS for Command
P0 = cmd;
LCD_EN = 1; // Pulse LCD_EN
LCD_EN = 0;
LCD_WR = 1;
}
void lcd_write_data(unsigned char val)
{
delay(10);
LCD_WR = 0;
LCD_EN = 0;
LCD_RS = 1; // SET LCD_RS for DATA
P0 = val;
LCD_EN = 1; // Pulse LCD_EN
LCD_EN = 0;
LCD_WR = 1;
}
INTERFACING HARDWARE WITH 8051
15EE51 – Microcontrollers Laboratory Page 38
void delay(unsigned int dval)
{
TMOD &= 0xF0;
TMOD |= 0x01;
while (dval) {
TR0 = 0;
TF0 = 0;
TH0 = 0xFA;
TL0 = 0x00;
TR0 = 1;
while (TF0 == 0);
dval--;
}
TR0 = 0;
}
Fig 4.4 Matrix keypad and 16 x 2 LCD display
RESULT:
Thus LED,buzzer,matrix keypad and LCD were interfaced with 8051 starter kit.

More Related Content

Viewers also liked

Wireless electronic notice board using rf
Wireless electronic notice board using rfWireless electronic notice board using rf
Wireless electronic notice board using rf
Jagadeesh Yadav
 
Electrical engineering labs modernization
Electrical engineering labs modernizationElectrical engineering labs modernization
Electrical engineering labs modernization
Jabez Winston
 
MPLABX with proteus
MPLABX with proteusMPLABX with proteus
MPLABX with proteus
Jabez Winston
 
lcd
lcdlcd
Advance of usb
Advance of usb Advance of usb
Advance of usb
PREMAL GAJJAR
 
Protection and Switchgear-EE2402 notes Unit-2
Protection and Switchgear-EE2402 notes Unit-2Protection and Switchgear-EE2402 notes Unit-2
Protection and Switchgear-EE2402 notes Unit-2
Jabez Winston
 
Labview -USB
Labview -USB Labview -USB
Labview -USB
Jabez Winston
 
CAN in linux
CAN in linuxCAN in linux
CAN in linux
Jabez Winston
 
Fault avoidance and fault tolerance
Fault avoidance and fault toleranceFault avoidance and fault tolerance
Fault avoidance and fault tolerance
Jabez Winston
 
Local Interconnect Network
Local Interconnect NetworkLocal Interconnect Network
Local Interconnect Network
Jabez Winston
 
VLSI Design
VLSI DesignVLSI Design
VLSI Design
Jabez Winston
 

Viewers also liked (11)

Wireless electronic notice board using rf
Wireless electronic notice board using rfWireless electronic notice board using rf
Wireless electronic notice board using rf
 
Electrical engineering labs modernization
Electrical engineering labs modernizationElectrical engineering labs modernization
Electrical engineering labs modernization
 
MPLABX with proteus
MPLABX with proteusMPLABX with proteus
MPLABX with proteus
 
lcd
lcdlcd
lcd
 
Advance of usb
Advance of usb Advance of usb
Advance of usb
 
Protection and Switchgear-EE2402 notes Unit-2
Protection and Switchgear-EE2402 notes Unit-2Protection and Switchgear-EE2402 notes Unit-2
Protection and Switchgear-EE2402 notes Unit-2
 
Labview -USB
Labview -USB Labview -USB
Labview -USB
 
CAN in linux
CAN in linuxCAN in linux
CAN in linux
 
Fault avoidance and fault tolerance
Fault avoidance and fault toleranceFault avoidance and fault tolerance
Fault avoidance and fault tolerance
 
Local Interconnect Network
Local Interconnect NetworkLocal Interconnect Network
Local Interconnect Network
 
VLSI Design
VLSI DesignVLSI Design
VLSI Design
 

Similar to 15EE51 - Microcontrollers Laboratory

Embedded c lab and keil c manual
Embedded  c  lab  and keil c  manualEmbedded  c  lab  and keil c  manual
Embedded c lab and keil c manual
Hari K
 
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Parth Gajjar
 
Larson and toubro
Larson and toubroLarson and toubro
Larson and toubro
anoopc1998
 
CHAPTER 1
CHAPTER 1CHAPTER 1
CHAPTER 1
mohd_mizan
 
Design the implementation of CDEx Robust DC Motor.
Design the implementation of CDEx Robust DC Motor.Design the implementation of CDEx Robust DC Motor.
Design the implementation of CDEx Robust DC Motor.
Ankita Tiwari
 
Unit III ARM Interface and ARM Programming
Unit III ARM Interface and ARM Programming Unit III ARM Interface and ARM Programming
Unit III ARM Interface and ARM Programming
Dr. Pankaj Zope
 
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
felicidaddinwoodie
 
Module 3.8 application testing.ppt
Module 3.8 application testing.pptModule 3.8 application testing.ppt
Module 3.8 application testing.ppt
ssuserd973fe
 
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docxCASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
keturahhazelhurst
 
IBM iSeries Terminal Based Performance Testing with Rational Performance Tester
IBM iSeries Terminal Based Performance Testing with Rational Performance TesterIBM iSeries Terminal Based Performance Testing with Rational Performance Tester
IBM iSeries Terminal Based Performance Testing with Rational Performance Tester
Winton Winton
 
Info plc net_dcs800_controlbuilder_basic_exercises
Info plc net_dcs800_controlbuilder_basic_exercisesInfo plc net_dcs800_controlbuilder_basic_exercises
Info plc net_dcs800_controlbuilder_basic_exercises
Mahmoud Hassan
 
Avr generator
Avr generatorAvr generator
Avr generatorHarie Tr
 
Micrcontroller iv sem lab manual
Micrcontroller iv sem lab manualMicrcontroller iv sem lab manual
Micrcontroller iv sem lab manual
RohiniHM2
 
Debugging programs with Keil uVision
Debugging programs with Keil uVisionDebugging programs with Keil uVision
Debugging programs with Keil uVision
SaravananVijayakumar4
 
Billing and Invoice Management System
Billing and Invoice Management SystemBilling and Invoice Management System
Billing and Invoice Management System
Bhairesh M
 
Tutorial Penggunaan CodeVision AVR dengan Bahasa C
Tutorial Penggunaan CodeVision AVR dengan Bahasa CTutorial Penggunaan CodeVision AVR dengan Bahasa C
Tutorial Penggunaan CodeVision AVR dengan Bahasa C
Muhammad Kennedy Ginting
 
Innoslate 4.5 and Sopatra
Innoslate 4.5 and SopatraInnoslate 4.5 and Sopatra
Innoslate 4.5 and Sopatra
Elizabeth Steiner
 
Cvavr getting started with atmel studio
Cvavr getting started with atmel studioCvavr getting started with atmel studio
Cvavr getting started with atmel studio
Bayu Agung
 
SW_Cost_Estimation.ppt
SW_Cost_Estimation.pptSW_Cost_Estimation.ppt
SW_Cost_Estimation.ppt
PerumalPitchandi
 
H63ECH_1B_023799_OsamaAzim
H63ECH_1B_023799_OsamaAzimH63ECH_1B_023799_OsamaAzim
H63ECH_1B_023799_OsamaAzimOsama Azim
 

Similar to 15EE51 - Microcontrollers Laboratory (20)

Embedded c lab and keil c manual
Embedded  c  lab  and keil c  manualEmbedded  c  lab  and keil c  manual
Embedded c lab and keil c manual
 
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
 
Larson and toubro
Larson and toubroLarson and toubro
Larson and toubro
 
CHAPTER 1
CHAPTER 1CHAPTER 1
CHAPTER 1
 
Design the implementation of CDEx Robust DC Motor.
Design the implementation of CDEx Robust DC Motor.Design the implementation of CDEx Robust DC Motor.
Design the implementation of CDEx Robust DC Motor.
 
Unit III ARM Interface and ARM Programming
Unit III ARM Interface and ARM Programming Unit III ARM Interface and ARM Programming
Unit III ARM Interface and ARM Programming
 
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
 
Module 3.8 application testing.ppt
Module 3.8 application testing.pptModule 3.8 application testing.ppt
Module 3.8 application testing.ppt
 
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docxCASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
 
IBM iSeries Terminal Based Performance Testing with Rational Performance Tester
IBM iSeries Terminal Based Performance Testing with Rational Performance TesterIBM iSeries Terminal Based Performance Testing with Rational Performance Tester
IBM iSeries Terminal Based Performance Testing with Rational Performance Tester
 
Info plc net_dcs800_controlbuilder_basic_exercises
Info plc net_dcs800_controlbuilder_basic_exercisesInfo plc net_dcs800_controlbuilder_basic_exercises
Info plc net_dcs800_controlbuilder_basic_exercises
 
Avr generator
Avr generatorAvr generator
Avr generator
 
Micrcontroller iv sem lab manual
Micrcontroller iv sem lab manualMicrcontroller iv sem lab manual
Micrcontroller iv sem lab manual
 
Debugging programs with Keil uVision
Debugging programs with Keil uVisionDebugging programs with Keil uVision
Debugging programs with Keil uVision
 
Billing and Invoice Management System
Billing and Invoice Management SystemBilling and Invoice Management System
Billing and Invoice Management System
 
Tutorial Penggunaan CodeVision AVR dengan Bahasa C
Tutorial Penggunaan CodeVision AVR dengan Bahasa CTutorial Penggunaan CodeVision AVR dengan Bahasa C
Tutorial Penggunaan CodeVision AVR dengan Bahasa C
 
Innoslate 4.5 and Sopatra
Innoslate 4.5 and SopatraInnoslate 4.5 and Sopatra
Innoslate 4.5 and Sopatra
 
Cvavr getting started with atmel studio
Cvavr getting started with atmel studioCvavr getting started with atmel studio
Cvavr getting started with atmel studio
 
SW_Cost_Estimation.ppt
SW_Cost_Estimation.pptSW_Cost_Estimation.ppt
SW_Cost_Estimation.ppt
 
H63ECH_1B_023799_OsamaAzim
H63ECH_1B_023799_OsamaAzimH63ECH_1B_023799_OsamaAzim
H63ECH_1B_023799_OsamaAzim
 

Recently uploaded

Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 

Recently uploaded (20)

Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 

15EE51 - Microcontrollers Laboratory

  • 1. MICROCONTROLLERS LABORATORY (15EE51) REPORT JABEZ WINSTON C 15MU01 Microcontroller Laboratory report submitted in partial fulfilment of the requirements for the degree of MASTER OF ENGINEERING Branch: ELECTRICAL AND ELECTRONICS ENGINEERING EMBEDDED AND REAL TIME SYSTEMS Of Anna University, Chennai. December 2015 DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING PSG COLLEGE OF TECHNOLOGY (Autonomous Institution) COIMBATORE – 641 004
  • 2. TABLE OF CONTENTS Ex.No Date Title of the Experiment Page No. Signature of Staff 1 7-9-2015 INTRODUCTION TO KEIL µVISION IDE 1 Debug Windows and Dialogs 1 Working with Keil µVision IDE 3 Sample program for blinking LEDs 11 2 10-10-2015 ASSEMBLY LANGAGE PROGRAMMING IN 8051 14 Timers/Counters 14 Serial Communication 17 Interrupts 19 3 12-11-2015 C LANGUAGE PROGRAMMING IN 8051 22 Timers/Counters 22 Serial Communication 26 Interrupts 28 4 16-11-2015 INTERFACED HARDWARE WITH 8051 30 LED interfacing with Zkit-51 30 Buzzer interfacing with Zkit-51 31 LCD interfacing with Zkit-51 32 Matrix keypad interfacing with Zkit-51 35
  • 3. TABLE OF FIGURES Fig.No. Title of the Figure Page No. 1.1 Keil IDE showing the various Windows available 2 1.2 Creation of new project 3 1.3 ‘Create New Project’ Window 3 1.4 ‘Select Device for Target’ Window 4 1.5 Drop down Menu in Project Window 4 1.6 ‘Add New Item ‘ Dialog Box 5 1.7 Keil IDE with sample program 5 1.8 ‘View’ Drop down menu in Keil IDE 6 1.9 Logic Analyzer Setup 6 1.10 Logic Analyzer Window 7 1.11 Setup Performance Analyzer 7 1.12 Performance Analyzer Window 7 1.13 Register Window 8 1.14 Memory Window showing Code Memory 8 1.15 Memory Window showing Data Memory 8 1.16 Disassembly Window 9 1.17 Serial Window 9 1.18 Program having breakpoints at Line 4,11,13 10 1.19 ‘Flash’ Drop Down Menu in Keil IDE 10 1.20 ‘Options for Target’ Window 10 1.21 HEX file in Objects Folder 11 1.22 Contents of HEX file 11 1.23 LED Window 12 1.24 LED Setup 13 1.25 LED Window showing LEDs 13 2.1 Waveform at P1.5 14 2.2 Timer 1 registers 15 2.3 Waveform at P1.2 16 2.4 Counter output 16 2.5 Serial Communication 18
  • 4. 2.6 Windows showing Interrupts and Serial Communication 20 3.1 Waveform at P1.5 22 3.2 Timer registers 23 3.3 Counter output 25 3.4 Serial Communication 27 3.5 External ,Timer and Serial Communication Interrupt 29 4.1 RED LED glowing when KEY1 is pressed 31 4.2 GREEN LED glowing when KEY2 is pressed 31 4.3 Two strings printed on LCD 35 4.4 Matrix keypad and 16 x 2 LCD display 38
  • 5. 15EE51 – Microcontrollers Laboratory Page 1 1.INTRODUCTION TO KEIL µVISION IDE Keil µVision IDE is a window-based software development platform for 8051 and ARM microcontrollers that combines a robust and modern editor with a project manager and make facility tool. It integrates all the tools needed to develop embedded applications including a C/C++ compiler, macro assembler, linker/locator, and a HEX file generator. µVision GUI The µVision GUI provides menus for selecting commands and toolbars with command buttons. The Status Bar, at the bottom of the window, displays information and messages about the current µVision command. Windows can be relocated and docked to another physical screen. The window layout is saved for each project automatically and restored the next time the project is used. You can restore the default layout using the menu Window – Reset View to Defaults. µVision has two operating modes, the Build Mode for creating applications and the Debug Mode for analyzing applications, which offers additional Windows and Dialogs. Debug Windows and Dialogs µVision provides many debugging windows and dialogs. Some of them are  Breakpoints- Define stop conditions for program execution.  Code Coverage- Examine statistics about code execution, including branch testing.  Command Window- Enter and view executed commands.  Disassembly Window- Test programs at the level of assembly instructions.  Logic Analyzer - Investigate value changes of peripherals, registers, and variables on a time graph.  Memory Map- Evaluate memory areas and their access rights.  Memory Window - Analyze and modify memory content.  Performance Analyzer - Evaluate time and call statistics on module or function level.  Registers Window- view and modify register content.  Serial Window is a communication interface between the application and the PC.  Status Bar - View debugging status information.  Symbols Window- Find debug symbol information used in program.  System Viewer Find peripheral register information and change property values at runtime.  Toolbox Use and define configurable buttons for executing debugging commands interactively. Ex. No. 1 07-09-2015
  • 6. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 2 Fig1.1:KeilIDEshowingthevariousWindowsavailable
  • 7. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 3 Working with Keil µVision IDE Getting started… 1.OpenKeil µVision ,Click Project New µVision Project. 2.Type a name and save the project file Fig.1.3 Fig.1.2:Creation of new project
  • 8. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 4 3.Select Device for target as AT89S51as shown in Fig. 1.4 Fig.1.4 4.Right click Source Group 1 and select Add new Item to Source Group 1 Fig.1.5
  • 9. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 5 Choose file type as ASM ,name the file and click Add Fig. 1.6 5.Type the code ,press F7/Bulid icon and Ctrl+F5 / Debug icon Fig.1. 7 In Debug mode various simulations can be done with variety of tools like Logic Analyzer, Performance Analyzer,etc.,Register and Memory status can be viewed during execution. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 5 Choose file type as ASM ,name the file and click Add Fig. 1.6 5.Type the code ,press F7/Bulid icon and Ctrl+F5 / Debug icon Fig.1. 7 In Debug mode various simulations can be done with variety of tools like Logic Analyzer, Performance Analyzer,etc.,Register and Memory status can be viewed during execution. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 5 Choose file type as ASM ,name the file and click Add Fig. 1.6 5.Type the code ,press F7/Bulid icon and Ctrl+F5 / Debug icon Fig.1. 7 In Debug mode various simulations can be done with variety of tools like Logic Analyzer, Performance Analyzer,etc.,Register and Memory status can be viewed during execution.
  • 10. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 6 Logic Analyzer  To perform Logic Analysis,Click View  Analysis WindowLogic Analyzer as shown in Fig 1.8 Fig. 1.8 The Logic Analyzer Window opens up.Click Setup and configure as shown below to observe waveform at P1.0 Fig. 9: Logic Analyzer Setup
  • 11. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 7 Output waveform will be displayed as Fig. 1.10: Logic Analyzer Performance Analyzer  To perform analysis of various parts of the program,Click View  Analysis Window Performance Analyzer  Click Setup and configure as follows Fig. 1.11: Setup Performance Analyzer  Output of performance is shown. Average execution time of a function and various other parameters are shown to pave way for optimization Fig. 1.12: Performance Analyzer Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 7 Output waveform will be displayed as Fig. 1.10: Logic Analyzer Performance Analyzer  To perform analysis of various parts of the program,Click View  Analysis Window Performance Analyzer  Click Setup and configure as follows Fig. 1.11: Setup Performance Analyzer  Output of performance is shown. Average execution time of a function and various other parameters are shown to pave way for optimization Fig. 1.12: Performance Analyzer Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 7 Output waveform will be displayed as Fig. 1.10: Logic Analyzer Performance Analyzer  To perform analysis of various parts of the program,Click View  Analysis Window Performance Analyzer  Click Setup and configure as follows Fig. 1.11: Setup Performance Analyzer  Output of performance is shown. Average execution time of a function and various other parameters are shown to pave way for optimization Fig. 1.12: Performance Analyzer
  • 12. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 8 Register Window Memory Window  To access Memory Window,Click View  Memory Window  Memory 1  We can view both code memory as well as data memory.  Code memory contains the opcodes.Data memory contains the contents of RAM.  To access code memory or data memory,use C: or D: followed by address. Eg: C:0x00 D:0x00 Fig. 1.14 : Memory Window showing Code Memory  To access Register Window ,Click View  Register Window .  Register window displays the contents of Accumulator A ,register B,registers from R0 to R7,Program Status Word(PSW),Data Pointer(DPTR),Stack Pointer(SP),Program counter(PC),etc.,This makes program debugging easier Fig. 1.13 : Register Window Fig. 1.15 : Memory Window showing Data Memory Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 8 Register Window Memory Window  To access Memory Window,Click View  Memory Window  Memory 1  We can view both code memory as well as data memory.  Code memory contains the opcodes.Data memory contains the contents of RAM.  To access code memory or data memory,use C: or D: followed by address. Eg: C:0x00 D:0x00 Fig. 1.14 : Memory Window showing Code Memory  To access Register Window ,Click View  Register Window .  Register window displays the contents of Accumulator A ,register B,registers from R0 to R7,Program Status Word(PSW),Data Pointer(DPTR),Stack Pointer(SP),Program counter(PC),etc.,This makes program debugging easier Fig. 1.13 : Register Window Fig. 1.15 : Memory Window showing Data Memory Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 8 Register Window Memory Window  To access Memory Window,Click View  Memory Window  Memory 1  We can view both code memory as well as data memory.  Code memory contains the opcodes.Data memory contains the contents of RAM.  To access code memory or data memory,use C: or D: followed by address. Eg: C:0x00 D:0x00 Fig. 1.14 : Memory Window showing Code Memory  To access Register Window ,Click View  Register Window .  Register window displays the contents of Accumulator A ,register B,registers from R0 to R7,Program Status Word(PSW),Data Pointer(DPTR),Stack Pointer(SP),Program counter(PC),etc.,This makes program debugging easier Fig. 1.13 : Register Window Fig. 1.15 : Memory Window showing Data Memory
  • 13. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 9 Disassembly Window  Click View  Disassembly Window.  Disassembly Window shows the opcodes for each instruction. Fig. 1.16: Disassembly Window Serial Window  Click View  Serial Windows  UART #1  This is useful for simulating Serial communication before downloading it to microcontroller. Fig. 1.17 : Serial Window Breakpoints  Program can be made to stop at a particular instruction so as to know the status of memory and registers at that point.  To insert a breakpoint place on a particular line or instruction and press F9.Pressing F9 again will remove the breakpoint.  Multiple breakpoints can be inserted and program will stop at breakpoints when run. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 9 Disassembly Window  Click View  Disassembly Window.  Disassembly Window shows the opcodes for each instruction. Fig. 1.16: Disassembly Window Serial Window  Click View  Serial Windows  UART #1  This is useful for simulating Serial communication before downloading it to microcontroller. Fig. 1.17 : Serial Window Breakpoints  Program can be made to stop at a particular instruction so as to know the status of memory and registers at that point.  To insert a breakpoint place on a particular line or instruction and press F9.Pressing F9 again will remove the breakpoint.  Multiple breakpoints can be inserted and program will stop at breakpoints when run. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 9 Disassembly Window  Click View  Disassembly Window.  Disassembly Window shows the opcodes for each instruction. Fig. 1.16: Disassembly Window Serial Window  Click View  Serial Windows  UART #1  This is useful for simulating Serial communication before downloading it to microcontroller. Fig. 1.17 : Serial Window Breakpoints  Program can be made to stop at a particular instruction so as to know the status of memory and registers at that point.  To insert a breakpoint place on a particular line or instruction and press F9.Pressing F9 again will remove the breakpoint.  Multiple breakpoints can be inserted and program will stop at breakpoints when run.
  • 14. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 10 Fig. 1.18 Program having breakpoints at Line 4,11 and 13 Generating HEX file To download the compiled program into the 8051 ,HEX file needs to be generated.To generate HEX file ,click Flash  Configure Flash Tools Fig. 1.19 A dialog box opens up.Click the Output tab and check in Create HEX fileoption.ClickOK. Fig.1.20
  • 15. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 11 Press F7/Bulid icon . Generated HEX file can be found in the Objects folder Fig. 1.21 : HEX file in Objects folder Fig.1. 22:Contents of the HEX file Writing a sample program for blinking 8 LEDs connected to PORT 1 of 8051 periodically in Keil µVision IDE…. Additional software required: DLL files for LED simulation ( LED_CONTROL.DLL and LED_DATABASE.CDB ) ALGORITHM: 1. Start 2. Load 0xFF to Port 1. 3. Call Delay routine. 4. Load 0x00 to Port 0. 5. Call Delay routine. 6. Goto Start Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 11 Press F7/Bulid icon . Generated HEX file can be found in the Objects folder Fig. 1.21 : HEX file in Objects folder Fig.1. 22:Contents of the HEX file Writing a sample program for blinking 8 LEDs connected to PORT 1 of 8051 periodically in Keil µVision IDE…. Additional software required: DLL files for LED simulation ( LED_CONTROL.DLL and LED_DATABASE.CDB ) ALGORITHM: 1. Start 2. Load 0xFF to Port 1. 3. Call Delay routine. 4. Load 0x00 to Port 0. 5. Call Delay routine. 6. Goto Start Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 11 Press F7/Bulid icon . Generated HEX file can be found in the Objects folder Fig. 1.21 : HEX file in Objects folder Fig.1. 22:Contents of the HEX file Writing a sample program for blinking 8 LEDs connected to PORT 1 of 8051 periodically in Keil µVision IDE…. Additional software required: DLL files for LED simulation ( LED_CONTROL.DLL and LED_DATABASE.CDB ) ALGORITHM: 1. Start 2. Load 0xFF to Port 1. 3. Call Delay routine. 4. Load 0x00 to Port 0. 5. Call Delay routine. 6. Goto Start
  • 16. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 12 PROGRAM: ORG 0x0000 START:MOV P1,#0xFF ACALL DELAY MOV P1,#0x00 ACALL DELAY AJMP START DELAY:MOV R0,#0xFF L2:MOV R1,#0xFF L1:DJNZ R1,L1 DJNZ R0,L2 RET SIMULATION:  Create a new project , add a new assembly file, type the above code and save it.  To simulate the blink LED program, we have to download DLL for LED control from Keil website.  Copy the LED_CONTROL.DLL and LED_DATABASE.CDB file to KeilC51BIN directory.  Add this line to [C51] section of Tools.ini in Keil directory. AGSI2=LED_CONTROL.DLL ("LED simulation")  Restart Keil µVision IDE.  With the project open, press Ctrl+F5 to Enter debug mode.  Click Peripherals  LED. The LED Window opens up.  Right Click and select add LED.New LED window opens up as shown in Fig. 1.23 Fig. 1.23
  • 17. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 13  Setup the pins for LED as shown in Fig 1.24 Fig. 1.24  Similarly do the same for P1.1 ,P1.2,….P1.7 Fig. 1.25  Press F5 to run the program. RESULT: Thus the development, debugging features of Keil µVision IDE were studied and a sample program for blinking LED was executed. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 13  Setup the pins for LED as shown in Fig 1.24 Fig. 1.24  Similarly do the same for P1.1 ,P1.2,….P1.7 Fig. 1.25  Press F5 to run the program. RESULT: Thus the development, debugging features of Keil µVision IDE were studied and a sample program for blinking LED was executed. Introduction to Keil µVision IDE 15EE51 – Microcontrollers Laboratory Page 13  Setup the pins for LED as shown in Fig 1.24 Fig. 1.24  Similarly do the same for P1.1 ,P1.2,….P1.7 Fig. 1.25  Press F5 to run the program. RESULT: Thus the development, debugging features of Keil µVision IDE were studied and a sample program for blinking LED was executed.
  • 18. 15EE51 – Microcontrollers Laboratory Page 14 ASSEMBLY LANGUAGE PROGRAMMING IN 8051 A.Timers /Counters AIM: i. To operate the timer0 in mode 0 and generate square wave of 66 % duty cycle. ii. To operate the timer1 in mode 2 and generate a delay of 100 µs. iii. Count clock pulses on P3.4 (T0 pin) and put it on Port 2 PROGRAM: i. For generating square wave of 66 % duty cyle using timer 0 in mode 0 L1: SETB P1.5 ACALL DELAY1 CLR P1.5 ACALL DELAY2 SJMP L1 DELAY1: MOV TH0,#0x00 ;8 bit TH0 value MOV TL0,#0x00 ;5 bit TL0 value SETB TR0 ;Start timer L2: JNB TF0,L2 ;Poll TF0 flag CLR TR0 CLR TF0 RET DELAY2: MOV TH0,#0x80 ;8 bit TH0 value MOV TL0,#0x00 ;5 bit TL0 value SETB TR0 ;Start timer L3: JNB TF0,L3 ;Poll TF0 flag CLR TR0 CLR TF0 RET OUTPUT: Figure 2.1 Waveform at P1.5 Ex No.2 10-10-2015
  • 19. ASSEMBLY LANGUAGE PROGRAMMING IN 8051 15EE51 –Microcontrollers Laboratory Page 15 ii. To operate the timer1 in mode 2 and generate a delay of 100 µs. PROGRAM: MOV TMOD,#0x20 ;Timer 1 in 8 bit autoreload mode (mode 2) MOV TH1,#-92 ;100 µs /1.085 µs = 92 , 11.0592MHz ==> 1.085 µs LOOP:SETB P1.2 ACALL DELAY CLR P1.2 ACALL DELAY SJMP LOOP DELAY:SETB TR1 ;Start timer 1 L1:JNB TF1,L1 ; Poll TF1 flag CLR TF1 CLR TR1 RET OUTPUT: Figure 2.2 Timer 1 registers
  • 20. ASSEMBLY LANGUAGE PROGRAMMING IN 8051 15EE51 –Microcontrollers Laboratory Page 16 Figure 2.3 Waveform at P1.2 iii. Count clock pulses on P3.5 (T0 pin) and put it on Port 2 PROGRAM: MOV TMOD,#0x50 ; Counter 1 ,8 bit mode MOV TH1,#0x00 ; Autoreload value SETB P3.5 AGAIN:SETB TR1 BACK:MOV A,TL1 MOV P2,A JNB TF1,BACK CLR TR1 CLR TF1 SJMP AGAIN OUTPUT: Figure 2.4
  • 21. ASSEMBLY LANGUAGE PROGRAMMING IN 8051 15EE51 –Microcontrollers Laboratory Page 17 B.Serial Communication AIM: To turn on and turn off LEDs connected at PORT 1 by using serial communication. If ‘y’ is sent LEDs are turned on and if ‘n’ is sent LEDs are turned off.Acknowledgment messages are also end. PROGRAM: ORG 0x00 MOV TMOD,#0x20 ; Timer 1, 8 bit autoreload MOV SCON,#0x50 ; 8 bit,1 stop bit,Receive enabled MOV TH1,#-3 ; 9600 baud rate for 11.0592 MHz SETB TR1 WAIT:JNB RI,WAIT ;Poll RI flag bit MOV R0,SBUF CLR RI CJNE R0,#'y',C1 ;If ‘y’ turn on LED MOV P1,#0x00 SJMP DISPLAY_ON C1:CJNE R0,#'n',C2 ;If ‘n’ turn off LED MOV P1,#0xFF SJMP DISPLAY_OFF C2:SJMP WAIT STRING1:DB "LEDs ON",10,0 STRING2:DB "LEDs OFF",10,0 DISPLAY_ON:MOV DPTR,#STRING1 SJMP SEND DISPLAY_OFF:MOV DPTR,#STRING2 SJMP SEND SEND:CLR A MOVC A,@A+DPTR JZ WAIT MOV SBUF,A ;Place character to be transmitted in SBUF WT:JNB TI,WT ;Poll TI flag bit CLR TI INC DPTR SJMP SEND
  • 22. ASSEMBLY LANGUAGE PROGRAMMING IN 8051 15EE51 –Microcontrollers Laboratory Page 18 OUTPUT: Figure 2.5 (a) Serial Communication Figure 2.5 (b) Serial Communication
  • 23. ASSEMBLY LANGUAGE PROGRAMMING IN 8051 15EE51 –Microcontrollers Laboratory Page 19 C.Interrupts AIM: To write a program for demonstrating external interrupt (edge triggered at P3.2) , timer overflow interrupt(Timer 0) and serial communication interrupt. PROGRAM: ORG 0x0000 ;RESET INTERRUPT LJMP MAIN ORG 0x0003 ;EXTERNAL INTERRUPT 0 CPL P1.0 RETI ORG 0x000B ;TIMER 0 OVERFLOW INTERRUPT CPL P1.1 RETI ORG 0x0023 ;SERIAL COMMUNICATION INTERRUPT LJMP SERIAL_ISR ORG 0x0030 MAIN: MOV SCON,#0x50 MOV IE,#0x93 ;ENABLE EXT ,TIMER 0 , SERIAL INTERRUPT MOV TMOD,#0x22 MOV TH0,#0x00 MOV TH1,#-3 SETB TR0 SETB TR1 SETB IT0 ;EDGE TRIGERRED EXT. INTERRUPT HERE:SJMP HERE SERIAL_ISR: CLR RI MOV R0,SBUF CJNE R0,#'y',C1 MOV P2,#0x00 C1:CJNE R0,#'n',C2 MOV P2,#0xFF C2:RETI
  • 24. ASSEMBLY LANGUAGE PROGRAMMING IN 8051 15EE51 –Microcontrollers Laboratory Page 20 External interrupt toggles the LED connected with P1.0 , timer overflow interrupt will generate square wave P1.1 and serial interrupt for turning ON and OFF LEDs at Port 2 OUTPUT: Figure 2.6(a) Windows showing Interrupts,Serial Communication Figure 2.6(b) Windows showing Interrupts,Serial Communication
  • 25. ASSEMBLY LANGUAGE PROGRAMMING IN 8051 15EE51 –Microcontrollers Laboratory Page 21 RESULT: Thus programs for Timers/Counters, Serial Communication and Interrupts were written in assembly and simulated using Keil µVision IDE.
  • 26. 15EE51 – Microcontrollers Laboratory Page 22 A. Timers /Counters AIM: i. To operate the timer0 in mode 0 and generate square wave of 66 % duty cycle. ii. To operate the timer1 in mode 2 and generate a delay of 100 µs. iii. Count clock pulses on P3.4 (T0 pin) and put it on Port 2 PROGRAM: i. For generating square wave of 66 % duty cycle using timer 0 in mode 0 C CODE: #include<reg51.h> void delay1(); void delay2(); void main() { while(1) { P1|=(1<<5); //Set bit P1.5 delay1(); P1&=~(1<<5); //Clear bit P1.5 delay2(); } } void delay1() { TH0=0x00; TL0=0x00; TR0=1; //Start Timer 0 while(!TF0); //Poll TF0 flag TR0=0; TF0=0; } void delay2() { TH0=0x80; TL0=0x00; TR0=1; //Start Timer 0 while(!TF0); //Poll TF0 flag TR0=0; TF0=0; } C LANGUAGE PROGRAMMING IN 8051Ex No.3 12-11-2015
  • 27. C PROGRAMMING IN 8051 15EE51 – Microcontrollers Laboratory Page 23 OUTPUT: Figure 3.1 Waveform at P1.5 ii. To operate the timer1 in mode 2 and generate a delay of 100 µs. C CODE: #include<reg51.h> void delay(); void main() { TH1=-92; // 100 µs/1.085 µs = 92 for 11.592 MHz TMOD=0x20; while(1) { P1|=(1<<2); //Set bit P1.2 delay(); P1&=~(1<<2); //Clear bit P1.3 delay(); } } void delay() { TR1=1; while(!TF1); //Poll TF1 flag TF1=0; TR1=0; }
  • 28. C PROGRAMMING IN 8051 15EE51 – Microcontrollers Laboratory Page 24 OUTPUT: Figure 3.2 (a) Timer registers Figure 3.2 (b) Timer registers iii. Count clock pulses on P3.5 (T0 pin) and put it on Port 2 C CODE: #include<reg51.h> void main() { TMOD=0x50; //Counter 1 ,Mode 1 TH1=0x00; T0=1; while(1) { TR1=1; while(!TF1) P2=TL1; TR1=0; TF1=0; } }
  • 29. C PROGRAMMING IN 8051 15EE51 – Microcontrollers Laboratory Page 25 OUTPUT: Figure 3.3 Counter output
  • 30. C PROGRAMMING IN 8051 15EE51 – Microcontrollers Laboratory Page 26 B. Serial Communication AIM: To turn on and turn off LEDs connected at PORT 1 by using serial communication. If ‘y’ is sent LEDs are turned on and if ‘n’ is sent LEDs are turned off.Acknowledgment messages ”LEDs ON” and “LEDs OFF” are also sent. C CODE: #include<reg51.h> void Serial_Init(); void Serial_Str_Transmit(char *); char Serial_Char_Receive(); void main() { char ch; Serial_Init(); while(1) { ch=Serial_Char_Receive(); switch(ch) { case 'y': P1=0x00; Serial_Str_Transmit("LEDs ONn"); break; case 'n': P1=0xFF; Serial_Str_Transmit("LEDs OFFn"); break; } } } void Serial_Init() { TMOD=0x20; SCON=0x50; TH1=-3; // 9600 baud rate for 11.0592 MHz TR1=1; } char Serial_Char_Receive() { while(!RI); //Poll RI flag RI=0; //Clear RI flag return(SBUF); }
  • 31. C PROGRAMMING IN 8051 15EE51 – Microcontrollers Laboratory Page 27 void Serial_Str_Transmit(char *p) { while(*p!='0') { SBUF=*p; while(!TI); //Poll TI flag TI=0; p++; } } OUTPUT: Figure 3.4 Serial Communication
  • 32. C PROGRAMMING IN 8051 15EE51 – Microcontrollers Laboratory Page 28 C. Interrupts AIM: To write a program for demonstrating external interrupt (edge triggered at P3.2) , timer overflow interrupt(Timer 0) and serial communication interrupt. C CODE: #include<reg51.h> void Serial_Init(); void Interrupt_Init(); void Timer0_Init(); void main() { Serial_Init(); Interrupt_Init(); Timer0_Init(); while(1); // Wait forever } void Serial_Init() { TMOD=0x20; SCON=0x50; TH1=-3; TR1=1; } void Interrupt_Init() { IE=0x93; IT0=1; } void Timer0_Init() { TMOD|=0x02; TR0=1; } void ext_int0(void) interrupt 0 //Ext.Interrupt 0 { P1^=(1<<0); } void timer_int0(void) interrupt 1 //Timer interrupt 0 { P1^=(1<<1); }
  • 33. C PROGRAMMING IN 8051 15EE51 – Microcontrollers Laboratory Page 29 void serial_int(void) interrupt 4 //Serial Interrupt { RI=0; if(SBUF=='y') P2=0x00; if(SBUF=='n') P2=0xFF; } External interrupt toggles the LED connected with P1.0 , timer overflow interrupt will generate square wave P1.1 and serial interrupt for turning ON and OFF LEDs at Port 2 OUTPUT: Figure 3.5 External ,Timer and Serial Communication interrupt RESULT: Thus assembly and C programs for Timers/Counters, Serial Communication and Interrupts in 8051 microcontroller were written and simulated using Keil µVision IDE.
  • 34. 15EE51 – Microcontrollers Laboratory Page 30 AIM: i. To turn on only RED LED (P1.4) when key 1(P2.0) is pressed and turn only GREEN LED(P1.5) when key 2(P2.1) is pressed ii. To produce a single beep sound when key 1 is pressed and a double beep sound when key 2 is pressed. iii. To interface HD44780 compatible 16 × 2 LCD display in 8 bit mode. iv. To interface a 4 × 4 Matrix keypad. PROGRAM: i. To write a program to turn on only RED LED (P1.4) when key 1(P2.0) is pressed and turn only GREEN LED(P1.5) when key 2(P2.1) is pressed. #include<reg51.h> sbit RED =P1^4; sbit GREEN =P1^5; sbit KEY1=P2^0; sbit KEY2=P2^1; void main() { while(1) { if(!KEY1) { RED=0; GREEN=1; } if(!KEY2) { RED=1; GREEN=0; } } 4. INTERFACING HARDWARE WITH 8051 Ex No : 4 16-11-15
  • 35. INTERFACING HARDWARE WITH 8051 15EE51 – Microcontrollers Laboratory Page 31 OUTPUT: ii. To produce a beep sound when key 1 is pressed and a beep sound twice when key 2 is pressed. #include <reg51.h> void beep(); void delay_ms(int d); sbit BUZZER = P3^4; void main() { while(1) { if(!KEY1) { beep(); } if(!KEY2) { beep(); delay_ms(200); beep(); } } Fig 4.1 RED LED glowing when Key1 is pressed Fig 4.2 GREEN LED glowing when Key2 is pressed
  • 36. INTERFACING HARDWARE WITH 8051 15EE51 – Microcontrollers Laboratory Page 32 void beep() { unsigned char i = 25; while (i--) { BUZZER = !BUZZER; delay(5); } } void delay_ms(int d) { int i; TCON=0x01; for(i=0;i<d;i++) { TH0=0x01; TL0=0x40; TR0=1; while(!TF0); TR0=0; } } iii. To interface HD44780 compatible 16 × 2 LCD display in 8 bit mode. #include<reg51.h> sbit RS =P2^4; sbit RW=P2^5; sbit EN=P2^6; #define CLEAR 0x01 void LCD_Init(); void LCD_SetCursor(char,char); void LCD_Cmd(char); void LCD_Data(char); void LCD_PrintChar(char); void LCD_PrintStr(char*); void delay_ms(int);
  • 37. INTERFACING HARDWARE WITH 8051 15EE51 – Microcontrollers Laboratory Page 33 void main() { RW=0; LCD_Init(); LCD_PrintStr("Jabez Winston"); delay_ms(1000); LCD_SetCursor(1,0); LCD_PrintChar("Jagdeesh"); while(1); } void LCD_Init() { delay_ms(100); LCD_Cmd(0x38); delay_ms(10); LCD_Cmd(0x0C); LCD_Cmd(0x06); LCD_Cmd(CLEAR); } void LCD_SetCursor(char r,char c) { switch(r) { case 0: LCD_Cmd(0x80+c); break; case 1: LCD_Cmd(0xC0+c); break; } } void LCD_Cmd(char cmd) { EN=0; RS=0; P1=cmd; EN=1; delay_ms(10); EN=0; }
  • 38. INTERFACING HARDWARE WITH 8051 15EE51 – Microcontrollers Laboratory Page 34 void LCD_Data(char ch) { EN=0; RS=1; P1=ch; EN=1; delay_ms(10); EN=0; } void LCD_PrintChar(char ch) { LCD_Cmd(ch); } void LCD_PrintStr(char *s) { while(*s) LCD_PrintChar(*s++); } void delay_ms(int d) { int i; TCON=0x01; for(i=0;i<d;i++) { TH0=0x01; TL0=0x40; TR0=1; while(!TF0); TR0=0; } }
  • 39. INTERFACING HARDWARE WITH 8051 15EE51 – Microcontrollers Laboratory Page 35 OUTPUT: Figure 4.3 Two strings printed on LCD iv. To interface a 4 × 4 Matrix keypad. #include<reg51.h> unsigned char get_key(); void lcd_init(); void lcd_printc(char); void lcd_prints(char); void lcd_clear(); void lcd_write_cmd(unsigned char ); void lcd_write_data(unsigned char ); void delay(int); sbit LCD_RS = P2^4; sbit LCD_WR = P2^5; sbit LCD_EN = P2^6; int x; char a[4][4]={"123E", "456U", "789D", "*0#e"};
  • 40. INTERFACING HARDWARE WITH 8051 15EE51 – Microcontrollers Laboratory Page 36 void main() { lcd_init(); while(1) { while(get_key()==-1); x=get_key(); lcd_printc(x); delay(100); } } unsigned char get_key() { unsigned char i,j,k; for(i=0;i<4;i++) { P1=~(1<<i); delay(20); for(j=4;j<=7;j++) { if(!((P1)&(1<<j))) { k = a[i][j-4]; return k; } } } return -1; } void lcd_init() { delay(15); // LCD Power on delay lcd_write_cmd(0x38); // 8BIT, 2 LINE , 5x8 DOT Format font delay(5); // Small delay lcd_write_cmd(0x38); lcd_write_cmd(0x06); lcd_write_cmd(0x0c); lcd_write_cmd(0x0E); lcd_clear();
  • 41. INTERFACING HARDWARE WITH 8051 15EE51 – Microcontrollers Laboratory Page 37 } void lcd_printc(char a_char) { lcd_write_data(a_char); } void lcd_prints(char *string) { while (*string) lcd_printc(*string++); } void lcd_clear() { lcd_write_cmd(0x01); } void lcd_write_cmd(unsigned char cmd) { delay(10); LCD_WR = 0; LCD_EN = 0; LCD_RS = 0; // Reset LCD_RS for Command P0 = cmd; LCD_EN = 1; // Pulse LCD_EN LCD_EN = 0; LCD_WR = 1; } void lcd_write_data(unsigned char val) { delay(10); LCD_WR = 0; LCD_EN = 0; LCD_RS = 1; // SET LCD_RS for DATA P0 = val; LCD_EN = 1; // Pulse LCD_EN LCD_EN = 0; LCD_WR = 1; }
  • 42. INTERFACING HARDWARE WITH 8051 15EE51 – Microcontrollers Laboratory Page 38 void delay(unsigned int dval) { TMOD &= 0xF0; TMOD |= 0x01; while (dval) { TR0 = 0; TF0 = 0; TH0 = 0xFA; TL0 = 0x00; TR0 = 1; while (TF0 == 0); dval--; } TR0 = 0; } Fig 4.4 Matrix keypad and 16 x 2 LCD display RESULT: Thus LED,buzzer,matrix keypad and LCD were interfaced with 8051 starter kit.