42
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

15EE51 - Microcontrollers Laboratory

Embed Size (px)

Citation preview

Page 1: 15EE51 - Microcontrollers Laboratory

MICROCONTROLLERS LABORATORY (15EE51) REPORT

JABEZ WINSTON C 15MU01

Microcontroller Laboratory report submitted in partial fulfilment of the requirements for the degreeof

MASTER OF ENGINEERINGBranch: ELECTRICAL AND ELECTRONICS ENGINEERING

EMBEDDED AND REAL TIME SYSTEMSOf Anna University, Chennai.

December 2015

DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERINGPSG COLLEGE OF TECHNOLOGY

(Autonomous Institution)

COIMBATORE – 641 004

Page 2: 15EE51 - Microcontrollers Laboratory

TABLE OF CONTENTS

Ex.No Date Title of the ExperimentPage

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

Page 3: 15EE51 - Microcontrollers Laboratory

TABLE OF FIGURESFig.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

Page 4: 15EE51 - Microcontrollers Laboratory

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

Page 5: 15EE51 - Microcontrollers Laboratory

15EE51 – Microcontrollers Laboratory Page 1

1. INTRODUCTION TO KEIL µVISION IDE

Keil µVision IDE is a window-based software development platform for 8051 and ARMmicrocontrollers that combines a robust and modern editor with a project manager and make facilitytool. 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. TheStatus 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 windowlayout is saved for each project automatically and restored the next time the project is used. You canrestore 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 foranalyzing 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

Page 6: 15EE51 - Microcontrollers Laboratory

Introduction to Keil µVision IDE

15EE51 – Microcontrollers Laboratory Page 2

Fig

1.1:

Kei

lIDE

show

ing

the

vario

us W

indo

wsa

vaila

ble

Page 7: 15EE51 - Microcontrollers Laboratory

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

Page 8: 15EE51 - Microcontrollers Laboratory

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

Page 9: 15EE51 - Microcontrollers Laboratory

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, PerformanceAnalyzer,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, PerformanceAnalyzer,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, PerformanceAnalyzer,etc.,Register and Memory status can be viewed during execution.

Page 10: 15EE51 - Microcontrollers Laboratory

Introduction to Keil µVision IDE

15EE51 – Microcontrollers Laboratory Page 6

Logic Analyzer

To perform Logic Analysis,Click View Analysis WindowLogic 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

Page 11: 15EE51 - Microcontrollers Laboratory

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 WindowPerformanceAnalyzer

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 parametersare 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 WindowPerformanceAnalyzer

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 parametersare 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 WindowPerformanceAnalyzer

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 parametersare shown to pave way for optimization

Fig. 1.12: Performance Analyzer

Page 12: 15EE51 - Microcontrollers Laboratory

Introduction to Keil µVision IDE

15EE51 – Microcontrollers Laboratory Page 8

Register Window

Memory Window

To access Memory Window,Click ViewMemory WindowMemory 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 ViewRegister Window .

Register window displays the contents ofAccumulator A ,register B,registers from R0to R7,Program Status Word(PSW),DataPointer(DPTR),Stack Pointer(SP),Programcounter(PC),etc.,This makes programdebugging 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 ViewMemory WindowMemory 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 ViewRegister Window .

Register window displays the contents ofAccumulator A ,register B,registers from R0to R7,Program Status Word(PSW),DataPointer(DPTR),Stack Pointer(SP),Programcounter(PC),etc.,This makes programdebugging 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 ViewMemory WindowMemory 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 ViewRegister Window .

Register window displays the contents ofAccumulator A ,register B,registers from R0to R7,Program Status Word(PSW),DataPointer(DPTR),Stack Pointer(SP),Programcounter(PC),etc.,This makes programdebugging easier

Fig. 1.13 : Register Window

Fig. 1.15 : Memory Window showing Data Memory

Page 13: 15EE51 - Microcontrollers Laboratory

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 andregisters at that point.

To insert a breakpoint place on a particular line or instruction and press F9.Pressing F9 again willremove 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 andregisters at that point.

To insert a breakpoint place on a particular line or instruction and press F9.Pressing F9 again willremove 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 andregisters at that point.

To insert a breakpoint place on a particular line or instruction and press F9.Pressing F9 again willremove the breakpoint.

Multiple breakpoints can be inserted and program will stop at breakpoints when run.

Page 14: 15EE51 - Microcontrollers Laboratory

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 HEXfile ,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

Page 15: 15EE51 - Microcontrollers Laboratory

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. Start2. 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. Start2. 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. Start2. Load 0xFF to Port 1.3. Call Delay routine.4. Load 0x00 to Port 0.5. Call Delay routine.6. Goto Start

Page 16: 15EE51 - Microcontrollers Laboratory

Introduction to Keil µVision IDE

15EE51 – Microcontrollers Laboratory Page 12

PROGRAM:

ORG 0x0000

START:MOV P1,#0xFFACALL DELAYMOV P1,#0x00ACALL DELAY

AJMP START

DELAY:MOV R0,#0xFFL2:MOV R1,#0xFFL1:DJNZ R1,L1DJNZ R0,L2RET

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 Keil\C51\BIN 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

Page 17: 15EE51 - Microcontrollers Laboratory

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 blinkingLED 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 blinkingLED 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 blinkingLED was executed.

Page 18: 15EE51 - Microcontrollers Laboratory

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.5ACALL DELAY1CLR P1.5ACALL DELAY2SJMP L1

DELAY1:MOV TH0,#0x00 ;8 bit TH0 valueMOV TL0,#0x00 ;5 bit TL0 valueSETB TR0 ;Start timer

L2: JNB TF0,L2 ;Poll TF0 flagCLR TR0CLR TF0RET

DELAY2:MOV TH0,#0x80 ;8 bit TH0 valueMOV TL0,#0x00 ;5 bit TL0 valueSETB TR0 ;Start timer

L3: JNB TF0,L3 ;Poll TF0 flagCLR TR0CLR TF0RET

OUTPUT:

Figure 2.1 Waveform at P1.5

Ex No.2

10-10-2015

Page 19: 15EE51 - Microcontrollers Laboratory

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.2ACALL DELAYCLR P1.2ACALL DELAYSJMP LOOP

DELAY:SETB TR1 ;Start timer 1L1:JNB TF1,L1 ; Poll TF1 flag

CLR TF1CLR TR1RET

OUTPUT:

Figure 2.2 Timer 1 registers

Page 20: 15EE51 - Microcontrollers Laboratory

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 modeMOV TH1,#0x00 ; Autoreload valueSETB P3.5

AGAIN:SETB TR1BACK:MOV A,TL1

MOV P2,AJNB TF1,BACKCLR TR1CLR TF1SJMP AGAIN

OUTPUT:

Figure 2.4

Page 21: 15EE51 - Microcontrollers Laboratory

ASSEMBLY LANGUAGE PROGRAMMING IN 8051

15EE51 –Microcontrollers Laboratory Page 17

B.Serial CommunicationAIM:

To turn on and turn off LEDs connected at PORT 1 by using serial communication. If ‘y’ is sent LEDs are turned onand if ‘n’ is sent LEDs are turned off.Acknowledgment messages are also end.

PROGRAM:ORG 0x00MOV TMOD,#0x20 ; Timer 1, 8 bit autoreloadMOV SCON,#0x50 ; 8 bit,1 stop bit,Receive enabledMOV TH1,#-3 ; 9600 baud rate for 11.0592 MHzSETB TR1

WAIT:JNB RI,WAIT ;Poll RI flag bitMOV R0,SBUFCLR RI

CJNE R0,#'y',C1 ;If ‘y’ turn on LEDMOV P1,#0x00SJMP DISPLAY_ON

C1:CJNE R0,#'n',C2 ;If ‘n’ turn off LEDMOV P1,#0xFFSJMP DISPLAY_OFF

C2:SJMP WAIT

STRING1:DB "LEDs ON",10,0STRING2:DB "LEDs OFF",10,0

DISPLAY_ON:MOV DPTR,#STRING1SJMP SEND

DISPLAY_OFF:MOV DPTR,#STRING2SJMP SEND

SEND:CLR AMOVC A,@A+DPTRJZ WAITMOV SBUF,A ;Place character to be transmitted in SBUFWT:JNB TI,WT ;Poll TI flag bitCLR TIINC DPTRSJMP SEND

Page 22: 15EE51 - Microcontrollers Laboratory

ASSEMBLY LANGUAGE PROGRAMMING IN 8051

15EE51 –Microcontrollers Laboratory Page 18

OUTPUT:

Figure 2.5 (a) Serial Communication

Figure 2.5 (b) Serial Communication

Page 23: 15EE51 - Microcontrollers Laboratory

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 INTERRUPTLJMP MAIN

ORG 0x0003 ;EXTERNAL INTERRUPT 0CPL P1.0RETI

ORG 0x000B ;TIMER 0 OVERFLOW INTERRUPTCPL P1.1RETI

ORG 0x0023 ;SERIAL COMMUNICATION INTERRUPTLJMP SERIAL_ISR

ORG 0x0030MAIN:MOV SCON,#0x50MOV IE,#0x93 ;ENABLE EXT ,TIMER 0 , SERIAL INTERRUPTMOV TMOD,#0x22MOV TH0,#0x00MOV TH1,#-3SETB TR0SETB TR1SETB IT0 ;EDGE TRIGERRED EXT. INTERRUPTHERE:SJMP HERE

SERIAL_ISR:CLR RIMOV R0,SBUF

CJNE R0,#'y',C1MOV P2,#0x00

C1:CJNE R0,#'n',C2MOV P2,#0xFF

C2:RETI

Page 24: 15EE51 - Microcontrollers Laboratory

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.1and 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

Page 25: 15EE51 - Microcontrollers Laboratory

ASSEMBLY LANGUAGE PROGRAMMING IN 8051

15EE51 –Microcontrollers Laboratory Page 21RESULT:

Thus programs for Timers/Counters, Serial Communication and Interrupts were written in assembly andsimulated using Keil µVision IDE.

Page 26: 15EE51 - Microcontrollers Laboratory

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.5delay1();P1&=~(1<<5); //Clear bit P1.5delay2();}

}

void delay1(){

TH0=0x00;TL0=0x00;TR0=1; //Start Timer 0while(!TF0); //Poll TF0 flagTR0=0;TF0=0;

}

void delay2(){

TH0=0x80;TL0=0x00;TR0=1; //Start Timer 0while(!TF0); //Poll TF0 flagTR0=0;TF0=0;

}

C LANGUAGE PROGRAMMING IN 8051Ex No.3

12-11-2015

Page 27: 15EE51 - Microcontrollers Laboratory

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 MHzTMOD=0x20;while(1){P1|=(1<<2); //Set bit P1.2delay();P1&=~(1<<2); //Clear bit P1.3delay();}

}

void delay(){

TR1=1;while(!TF1); //Poll TF1 flagTF1=0;TR1=0;

}

Page 28: 15EE51 - Microcontrollers Laboratory

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 1TH1=0x00;T0=1;while(1){

TR1=1;while(!TF1)

P2=TL1;TR1=0;TF1=0;

}}

Page 29: 15EE51 - Microcontrollers Laboratory

C PROGRAMMING IN 8051

15EE51 – Microcontrollers Laboratory Page 25

OUTPUT:

Figure 3.3 Counter output

Page 30: 15EE51 - Microcontrollers Laboratory

C PROGRAMMING IN 8051

15EE51 – Microcontrollers Laboratory Page 26

B. Serial CommunicationAIM:

To turn on and turn off LEDs connected at PORT 1 by using serial communication. If ‘y’ is sent LEDs are turned onand 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 ON\n");break;

case 'n':P1=0xFF;Serial_Str_Transmit("LEDs OFF\n");break;

}}

}

void Serial_Init(){

TMOD=0x20;SCON=0x50;TH1=-3; // 9600 baud rate for 11.0592 MHzTR1=1;

}

char Serial_Char_Receive(){

while(!RI); //Poll RI flagRI=0; //Clear RI flagreturn(SBUF);

}

Page 31: 15EE51 - Microcontrollers Laboratory

C PROGRAMMING IN 8051

15EE51 – Microcontrollers Laboratory Page 27

void Serial_Str_Transmit(char *p){

while(*p!='\0'){

SBUF=*p;while(!TI); //Poll TI flagTI=0;p++;

}}OUTPUT:

Figure 3.4 Serial Communication

Page 32: 15EE51 - Microcontrollers Laboratory

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);}

Page 33: 15EE51 - Microcontrollers Laboratory

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.1and 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 8051microcontroller were written and simulated using Keil µVision IDE.

Page 34: 15EE51 - Microcontrollers Laboratory

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 ispressed.

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 onlyGREEN 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 8051Ex No : 4

16-11-15

Page 35: 15EE51 - Microcontrollers Laboratory

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 ispressed.

#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

Page 36: 15EE51 - Microcontrollers Laboratory

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);

Page 37: 15EE51 - Microcontrollers Laboratory

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;

}

Page 38: 15EE51 - Microcontrollers Laboratory

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;

}}

Page 39: 15EE51 - Microcontrollers Laboratory

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"};

Page 40: 15EE51 - Microcontrollers Laboratory

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 delaylcd_write_cmd(0x38); // 8BIT, 2 LINE , 5x8 DOT Format fontdelay(5); // Small delaylcd_write_cmd(0x38);lcd_write_cmd(0x06);lcd_write_cmd(0x0c);lcd_write_cmd(0x0E);lcd_clear();

Page 41: 15EE51 - Microcontrollers Laboratory

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 CommandP0 = cmd;LCD_EN = 1; // Pulse LCD_ENLCD_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 DATAP0 = val;LCD_EN = 1; // Pulse LCD_ENLCD_EN = 0;LCD_WR = 1;

}

Page 42: 15EE51 - Microcontrollers Laboratory

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.