Friday, February 29, 2008

Introduction of Microntroller

Microcontroller design is highly user friendly. Only requirement is to raise questions in our mind! Of course, because it is logical subject.

To understand the all actions, we should follow instructions based approach. Instructions will generate required signals and configurations of hardware with its main function. For example, MOVX instruction transfers data with external RAM and also sends logic high or low on RD/WR pins. In this sense it’s a user friendly to deign engineer, because instructions will do required interfacing activations.


So, during study of 8051 keep track with instructions. Don’t try to understand hardware working and configuration alone but realize every working step with help of instructions.

What should learn first, Hardware or software? Without knowledge of hardware its not possible to write fully 8051 utilized program. But to understand hardware functions, we need to learn instruction set and also to write programs which helpful to understand hardware functions. Keeping in mind to make easy learning by this approach, We should learn instruction set first.

8051 uC has following salient features:

Four Bidirectional I/O Parralel Ports
One Full Duplex serial Port
Two 16-Bit Programmable Timer/Counters
Bit and Byte Addressing
128 Bytes RAM including 128 bit addressable Space
4kB Flash Memory
DC to 33MHz Operation
Power down and Idle mode for power saving
Five Programmable Interrupt Sources

AT89C51 microcontrooler datasheet from ATMEL and DS89c450 microcontroller datasheet from Dallas Semiconductor was taken as a prime reference throughout the discussion of the blog.

Functions of Timer

The 8051 microcontroller incorporates two general purpose programmable 16-bit timers/counters. they are termed as Timer 0 and Timer 1.

We can configure timers in four operating modes by program. Also, we can initialize timer, start timer or stop timer as per our requirement by program. All this features places timers in a category of programmable timers.
All three Major Functions of timers are explained here.

(1) Time Delay and Measuring Time

Timer can be used to generate required time delay between two events. It can be also
used to measure time duration between to events. These functions of a timer is purely
described as a “Timer”.

(2) Counting the External Event

Timer can be used to count any external events. When timer is used to count external
event, it is known as “Counter”.

(3) Baud Rate Generation

Serial communication requires defined baud rate. Timer 1 is used for baud rate
generation. Timer 0 is not intended for this function.

Thursday, February 28, 2008

Timer SFRs

Following six SFRs plays important role for timer functions. And that’s why they are known as timer SFRs.

(1) TL0
(2) TH0
(3) TL1
(4) TH1
(5) TCON
(6) TMOD



(1) TL0- Timer 0 LSB register

Address: 8Ah
Reset value: 00000000b
Operation: Read and Write
Addressability: Byte. It can be address as TL0 or 8Ah in byte addressing



Bit Format:

7--------6------5-------4-------3-------2 -------1-------0

TL0.7 --TL0.6--TL0.5--TL0.4--TL0.3--TL0.2--TL0.1--TL0.0


(2) TH0- Timer 0 MSB register

Address: 8Ch
Reset value: 00000000b
Operation: Read and Write
Addressability: Byte. It can be address as TH0 or 8Ch in byte addressing


Bit Format:
7--------6------5-------4-------3-------2 -------1-------0

TH0.7--TH0.6--TH0.5--TH0.4--TH0.3--TH0.2--TH0.1--TH0.0


(3) TL1- Timer 1 LSB register


Address: 8Bh
Reset value: 00000000b
Operation: Read and Write
Addressability: Byte. It can be address as TL1 or 8Bh in byte addressing

Bit Format:
7--------6------5-------4-------3-------2 -------1-------0
TL1.7 --TL1.6--TL1.5--TL1.4--TL1.3--TL1.2--TL1.1--TL1.0


(4) TH1- Timer 1 MSB register


Address: 8Dh
Reset value: 00000000b
Operation: Read and Write
Addressability: Byte. It can be address as TH1 or 8Dh in byte addressing

Bit Format:
7--------6------5-------4-------3-------2 -------1-------0

TH1.7 --TH1.6--TH1.5--TH1.4 --TH1.3 --TH1.2 --TH1.1--TH1.1

(5) TCON : Timer/Counter Register

Address: 88h
Reset value: 00000000b
Operation: Read and Write
Addressability: Byte and Bit. It can be address as TCON.0, TCON.1, …, TCON.7 or 88.0h, 88.1h, …, 88.7 or by name of each bit (e.g. IT0) in bit addressing and TCON or 88h in byte addressing.

Bit Format:
7--------6------5-------4-------3-------2 -------1-------0

TF1----TR1----TF0----TR0----IE1-----IT1----IE0-----IT0

IT0
Bit 0
Interrupt 0 Type Select.

This bit selects whether the INT0 pin will detect edge orlevel triggered interrupts.
0 = INT0 is level triggered.
1 = INT0 is edge triggered


IE0
Bit 1
Interrupt 0 Edge Detect.

This bit is set when an edge/level of the type defined
by IT0 is detected. If IT0=1, this bit will remain set until cleared in software or the start of the External Interrupt 0 service routine.If IT0=0, this bit will inversely reflect the state of the INT0 pin.

IT1
Bit 2
Interrupt 1 Type Select.

This bit selects whether the INT1 pin will detect edge or level triggered interrupts.
0 = INT1 is level triggered.
1 = INT1 is edge triggered.

IE1
Bit 3
Interrupt 1 Edge Detect.

This bit is set when an edge/level of the type defined
by IT1 is detected. If IT10=1, this bit will remain set until cleared in software or the start of the External Interrupt 1 service routine.If IT1=0, this bit will inversely reflect the state of the INT1 pin.

TR0
Bit 4
Timer 0 Run Control.

This bit enables/disables the operation of Timer 0.
0 = Timer 0 is halted.
1 = Timer 0 is enabled

TF0
Bit 5
Timer 0 Overflow Flag.

This bit indicates when Timer 0 overflows its maximum count as defined by the current mode. This bit can be cleared by software and is automatically cleared when the CPU vectors to the Timer 0 interrupt service routine or by software.
0 = No Timer 0 overflow has been detected.
1 = Timer 0 has overflowed its maximum count.

TR1
Bit 6
Timer 1 Run Control.

This bit enables/disables the operation of Timer 1.
0 = Timer 1 is halted.
1 = Timer 1 is enabled.

TF1
Bit 7
Timer 1 Overflow Flag.

This bit indicates when Timer 1 overflows its maximum count as defined by the current mode. This bit can be cleared by software and is automatically cleared when the CPU vectors to the Timer1 interrupt service routine.
0 = No Timer 1 overflow has been detected.
1 = Timer 1 has overflowed its maximum count.



(6) TMOD : Timer Mode Control Register

Address: 89h
Reset value: 00000000b
Operation: Read and Write
Addressability: Byte. It can be address as TMOD or 89h in byte addressing.

Bit Format:
7--------6------5-------4-------3-------2 -------1-------0
GATE--C/ T---T1M1---T1M0--GATE---C/ T---T0M1---T0M0

T0M0
Bit 0
Timer 0 Mode Select.

T0M1
Bit 1
Timer 0 Mode Select.

C/T0
Bit 2
Timer 0 Counter/Timer Select.
0 = Timer 0 is incremented by internal clocks.
1 = Timer 0 is incremented by pulses on T0 when TR0 (TCON.4) is 1

GATE0
Bit 3
Timer 0 Gate Control.
This bit enable/disables the ability of Timer 0 to
increment.
0 = Timer 0 will clock when TR0=1, regardless of the state of INT0
1 = Timer 0 will clock only when TR0=1 and INT01

T1M1
Bit 4
Timer 1 Mode Select.

T1M1
Bit 5
Timer 1 Mode Select.

Bit 6
Timer 1 Counter/Timer Select.
0 = Timer 1 is incremented by internal clocks.
1 = Timer 1 is incremented by pulses on T1 when TR1 (TCON.6) is 1

GATE1
Bit 7
Timer 1 Gate Control.
This bit enable/disables the ability of Timer 1 to increment.
0 = Timer 1 will clock when TR1=1, regardless of the state of INT1
1 = Timer 1 will clock only when TR1=1 and INT1=1

Timer Modes of operation

Timer 0 Modes of operation

Timer 0 has four operating modes. Operating modes can be selected by configuring T0M0 and T0M1 bit of TMOD register as shown in table.

T0M1---T0M0 ---Mode---- Description
0 -------0-------- Mode 0-- 8 bits
0 -------1 --------Mode 1 ---16 bits
1 -------0-------- Mode 2--- 8 bits with auto-reload
1------- 1-------- Mode 3----Split into two 8 bit timer/counters.


Timer 0 will start under either of following two conditions:

(1) If GATE0=0, Then TR0=1
(2) If GATE0=1, Then TR0=1 and INT0 Pin=1

From above discussion it is obvious that if only software control to run the timer 0 is required than clear GATE0 bit. In this case only software instruction, SETB TR0 will start the timer 0. If software and hardware both control is required to run the timer then set GATE0 bit. In this case only SETB TR0 software instruction will not run the timer 0 but INT0 pin status also must be 1 by hardware to run the timer 0.

Starting the timer will increments TH0-TL0 register every machine cycle if C/T0=0. If C/T0=1, then high to low transition on pin T0 (P3.4) will increments timer register. Timer will starts to increment from initialize value. When timer attains maximum value, then in next count it rolls to all zero. When timer rolls over from all one to all zero, timer 0 flag bit, TF0 will set and timer 0 interrupt request is generated.

If C/T0=1, then timer 0 counts external events occurring on pin T0. So, this is known as a counter operation of timer. The external input is sampled every machine cycle. When the sample is high in one cycle and low in the next one, the counter is incremented. Since it takes 2 machine cycles to recognize a high to low transition, the maximum count rate is oscillator frequency/24. There are no restrictions on the duty cycle of the external input signal, but to ensure that a given level is sampled at least once before it changes, it should be high or low at least one machine cycle.


MODE 0

Clearing TMOD register bits T0M1 and T0M0 will select this operating mode. Instruction MOV TMOD, #xxxxxx00b will do this selection.

Mode 0 configures Timer 0 as a 13-bit Timer/Counter. TL0 uses only bit 0 to bit 4 and bit 5 to bit 7 are ignored. TL0 register bit 0 to bit 4 serve as the 5 LSBs of the 13-bit timer. TH0 provides the 8 MSBs of the 13-bit timer. As shown in Figure, 8-bit TH0 and 5-bit TL0 are connected in cascade to form 13-bit operation.

TH0.7 TH0.6 TH0.5 TH0.4 TH0.3 TH0.2 TH0.1 TH0.0 TL0.4 TL0.3 TL0.2 TL0.1 TL0.0

Starting the timer will increments 13-bit TH0-TL0 register. Timer will starts to increment from initialize value. When timer attains maximum value 1111 1111 11111b (1FFFH), then in next count it rolls to all zero. TF0 will set and timer 0 interrupt request is generated.

This sequence can be explained more clearly as following:

TH0 TL0
0000 0000 00000 ; It’s a initial value of timer register. We can initialize in the range of 0000 0000 00000 to 1111 1111 11111 by MOV instruction.
0000 0000 00001 ; First increment
…………………
…………………
0000 0000 11111 ; TL0(5-bit) atains its high value
0000 0001 00000 ; TL0(5-bit) overflows and increments TH0 registers.
0000 0001 00001
…………………
…………………
1111 1111 11111 ; Higher Value
0000 0000 00000 ; Timer 0 is overflowed. TF0 sets and Interrupt request is generated.

0000 0000 00001 ; timer will continue to increment.

MODE 1

Clearing TMOD register bit T0M1 and setting T0M0 will select this operating mode.

Mode 1 configures Timer 0 as a 16-bit Timer/Counter. As shown in Figure, 8-bit TH0 and 8-bit TL0 are connected in cascade to form 16-bit operation.


Starting the timer will increment 16-bit TH0-TL0 register. Timer will starts to increment from initialize value. When timer attains maximum value 1111 1111 1111 1111b (FFFFH), then in next count it rolls to all zero. TF0 will set and timer 0 interrupt request is generated.

This sequence can be explained more clearly as following:

TH0 TL0
0000 0000 0000 0000 ; It’s a initial value of timer register. We can initialize in the range
of 0000 0000 00000 to 1111 1111 11111 by MOV instruction.
0000 0000 0000 0001 ; First increment
…………………
…………………
0000 0000 1111 1111 ; TL0 atains its high value
0000 0001 0000 0000 ; TL0 overflows and increments TH0 registers.
0000 0001 0000 0001
…………………
…………………
1111 1111 1111 1111 ; Higher Value
0000 0000 0000 0000 ; Timer 0 is overflowed. TF0 sets and Interrupt request is
generated.
0000 0000 0000 0001 ; timer will continue to increment

MODE 2

Setting TMOD register bit T0M1 and clearing bit T0M0 will select this operating mode.

Mode 2 configures Timer 0 as a 8-bit auto reload Timer/Counter. TH0 holds the 8-bit reset value. TL0 is used to form 8-bit timer operation.

Starting the timer will increment 8-bit TL0 register. Timer will starts to increment from initialize value. When TL0 attains maximum value 1111 1111b (FFH), then in next count it loaded with contents of TH0 register. TF0 will set and timer 0 interrupt request is generated.

This sequence can be explained more clearly as following:

TL0
0101 0101 ; It’s a initial value loaded from TH0( Suppose[TH0]=55h) register. We can
initialize in the range of 0000 0000 to 1111 1111b by MOV instruction.
0101 0110 ; First increment
…………
…………
1111 1111 ; Higher Value
0101 0101 ; Timer 0 is overflowed and loaded with TH0 contents. TF0 sets and
Interrupt request is generated.
0101 0110 ; timer will continue to increment

MODE 3

Setting TMOD register bits T0M1 and T0M0 will select this operating mode.

Mode 3 configures Timer 0 as two 8-bit separate Timers. Register TL0 and TH0 both works as a two independent timers.

TL0 uses the timer 0 control bits C/T0 in the TMOD register and TR0 and TF0 in the TCON register. TH0 uses the timer 1 control bits TR1 and TF1 in the TCON register. TH0 can not perform counter operation.

In this mode TH0 uses control bits of timer 1, TR1 and TF1. So, without TR1 bit, timer 1 can start in free running by forcing it in mode 0, 1 or 2 and stop by forcing it in mode 3. Without TF1, interrupt facility is not available but timer 1 can be used as a baud rate generator.


Timer 1 Modes of operation

Timer 1 have also four operating modes. Operating modes can be selected by configuring T1M0 and T1M1 bit of TMOD register as shown in table.


T0M1 T0M0 Mode Description 0 0 Mode 0 8 bits 0 1 Mode 1 16 bits 1 0 Mode 2 8 bits with auto-reload 1 1 Mode 3 Split into two 8 bit timer/counters.

T1M1---T1M0 ---Mode---- Description
0 -------0-------- Mode 0-- 8 bits
0 -------1 --------Mode 1 ---16 bits
1 -------0-------- Mode 2--- 8 bits with auto-reload
1------- 1-------- Mode 3----Timer 1 is halted, but holds its cou
nt


Mode 0, mode 1 and Mode 2

These modes are identical to timer 0 mode 0, mode 1 and mode 2 respectively. In timer 1, bits T1M0, T1M1 C/T1, GATE1, TR1, TF1 and pin INT1 is utilizing instead of timer 0 bits T0M0, T0M1 C/T0, GATE0, TR0, TF0 and pin INT0 respectively.

Mode 3

In mode 3, Timer 1 is halted, but holds its count whatsoever. Without using TR1 bit, we can stop timer 1 by forcing it in this mode.

Timer Programming

PROGRAM NO.01

USING SOFTWARE POLLED TIMER APPROACH WRITE A PROGRAM WHICH BLINKS LED CONNECED AT BIT 0 OF PORT 1. LED SHOULD REMAIN ON FOR 500MS AND OFF FOR 500MS. USE TIMER 0 TO GENERATE BASIC 1MS DELAY.


ORG 0000H

MOV TMOD,#01H ; 65536-1000=64536(FC18H)
MOV TL0, #18H ;ASSUME 12MHZ CRYSTAL FREQUENCY
MOV TH0, #0FCH
SETB TR0

MOV DPTR,#000CH ;200H=512D
MAIN: JNB TF0, MAIN
MOV TL0, #18H
MOV TH0, #0FCH
CLR TF0

INC DPTR
MOV A, DPH
CJNE A, #02H, MAIN
CPL P1.0
MOV DPTR, #000CH
AJMP MAIN

END



PROGRAM NO.02

WRIE AN ALP IN 8051 MICROCONTROLLER TO GENERATE 5MS DELAY USING FOLLOWIN METHODS: (1) REGISTER PAIR (2) TIMER-1 IN MODE-1.




(1) USING TIMERIN MODE-1


ORG 0000H

MAIN:
MOV TMOD,#10H
ACALL DELAY

PASS: AJMP PASS

DELAY: MOV TL1, #78H ;5MS DELAY
MOV TH1, #0ECH ;TAKE F=12MHZ, 65536-5000=60536 (EC78H)
SETB TR1
L1: JNB TF1, L1
CLR TR1
RET


(2) USING REGISTER PAIR

ORG 0000H

MAIN:
MOV TMOD,#10H
ACALL DELAY

PASS: AJMP PASS

DELAY: ;MC =3+3R1+2R1R2
MOV R1, #0C8H ; R1=200, R2=11
L2: MOV R2, #0BH
L1: DJNZ R2, L1
DJNZ R1, L2
RET

END

PROGRAM NO.03

WRITE A PROGRAM TO USE TIMER 1 TO INTERRUPT THE PROGRAM AFTER A DELAY OF 2 MS. (CRYSTAL FREQUENCY = 10 MHZ).


ORG 0000H
AJMP MAIN ; 2000/1.2=1667,65536-1667=63869 (F97DH)

ORG 001BH
MOV A, #7EH
ADD A,TL1
MOV TL1,A ;MOV TL1,#7DH
MOV TH1, #0F9H
RETI

MAIN: MOV IE, #88H
MOV TMOD,#10H
MOV TL1, #7DH
MOV TH1, #0F9H
SETB TR1

TIMEPASS: LJMP TIMEPASS

END

PROGRAM NO.04

WRITE AN ASEMBLY LANGUAGE PROGRAM TO MEASURE AN UNKNOWN FREQUENCY CONNECTED ON PIN P3.4(T0). USE TIMER 0 IN COUNTER MODE.



ORG 0000H

MAIN: MOV TMOD,#05H ; TIMER-0 IN MODE-1 AS COUNTER
MOV TL0, #00H
MOV TH0, #00H
SETB TR0

ACALL DELAY ;5+3R3+3R3R4+2R3R4R5
CLR TR0
MOV R0,TL0
MOV R1,TH0

AJMP MAIN

DELAY: MOV R3, #32H
LOOP3: MOV R4, #64H
LOOP2: MOV R5, #64H
LOOP1: DJNZ R5, LOOP1
DJNZ R4, LOOP2
DJNZ R3, LOOP3
RET

END



PROGRAM NO.05

WRITE A PROGRAM WHICH CONTINUOUSLY READS FROM P0 AND SEND IT TO P1, WHILE SIMULTANEOUSLY CREATING SQUAREWAVE OF 5 KHZ ON PIN 2.1. USE TIMER 0 INTERRUPT SERVICE ROUTINE. INTIALIZE TIMER 0 IN MODE-2.



ORG 0000H
AJMP MAIN ;ASSUME 12MHZ CRYSTAL FREQUENCY
;1/5KHZ=0.2MS
; 256-100 =156 (9CH)

ORG 000BH
CPL P2.1
RETI

MAIN: MOV IE, #82H
MOV TMOD,#02H
MOV TH0, #9CH
SETB TR0

SEND: MOV P0, #0FFH
MOV A, P0
MOV P1, A
AJMP SEND

END

PROGRAM NO.06

WRITE AN ALP USING INTERRUPTS TO DO FOLLOWING TASK,(1) MAKE TIMER TO GENERATE A SQUAREWAVE OF 4KHZ FREQUNCY ON P2.0, (2) ASSUME THAT INT1 PIN IS CONNECTED TO A SWITCH THAT IS NORMALLY HIGH, WHWNEVER IT GOES LOW, IT SHOULD READ PORT P1, COMPLEMENT THE DATA AND SEND IT TO PORT P2.

ORG 0000H
AJMP MAIN

ORG 000BH
CPL P2.0
RETI

ORG 0013H
AJMP IN_EXT

MAIN: MOV IE, #86H
MOV IP, #04H
MOV TMOD,#0AH
MOV TH0, #83H ;ASSUME 12MHZ CRYSTAL FREQUENCY
SETB TR0 ;1/4KHZ=0.25MS, 125US HALF CYCLE PERID
; 256-125 =131 (83H)
PASS: AJMP PASS
IN_EXT: MOV P1, #0FFH
MOV A, P1
CPL A
MOV P2, A
RETI
END

16 x 2 LCD interfacing with uC

16x2 LCD Display is interfaced with port P1 and Port P2 of 8051 microcontroller. Assemebly language program code is wriiten to display "Fine" on First line Fifth column of LCD display with using and Without using Busy Flag status.

Circuit Diagram:



Assemebly language program Without using Busy Flag status:-

org 0000h
mov a, #38h
acall config
acall delay
mov a, #0eh
acall config
acall delay
mov a, #01h
acall config
acall delay
mov a, #06h
acall config

acall delay
mov a, #84h
acall config
acall delay
mov a, #'F'
acall display
acall delay
mov a, #'i'
acall display
acall delay
mov a, #'n'
acall display
acall delay
mov a, #'e'
acall display
acall delay

config:
mov p2, a
clr p1.2 ;rs
clr p1.1 ;r/w
setb p1.0 ;e
acall delay
clr p1.0 ;e
ret
display:
mov p2, a
setb p1.2
clr p1.1 ;r/w
setb p1.0 ;e
acall delay
clr p1.0 ;e
ret
delay:
mov r2, #01h
here3: mov r3, #00h
here2: mov r4, #00h
here: djnz r4, here
djnz r3, here2
djnz r2, here3
ret
end

Assemebly language program Without using Busy Flag status:-

org 0000h

acall delay
mov a, #38h
acall config

mov a, #0eh
acall config
mov a, #01h
acall config
mov a, #06h
acall config
mov a, #84h
acall config

mov a, #F
acall display
mov a, #'i'
acall display
mov a, #'n'
acall display
mov a, #'e'
acall display

main:
ajmp main
config:
acall ready
mov p2, a
clr p1.2 ;rs
clr p1.1 ;r/w
setb p1.0 ;e
nop
nop
nop
nop
clr p1.0 ;e
ret
display:
acall ready
mov p2, a
setb p1.2 ;rs
clr p1.1 ;r/w
setb p1.0 ;e
nop
nop
nop
nop
clr p1.0 ;e
ret
ready:
setb p1.7
clr p1.2 ;rs
setb p1.1 ;r/w
repeat: clr p1.0 ;e
nop
nop
nop
setb p1.0 ;e
jb p1.7, repeat
ret
delay:
mov r2, #2ah
here3: mov r3, #00h
here2: mov r4, #00h
here: djnz r4, here
djnz r3, here2
djnz r2, here3
ret
end

4x4 keypad ineterfacing with 8051 uC

Hexkey pad (4x4) is interfaced with port P2 of 8051 microcontroller. A common cathode seven segment LED display is connected to port P1. Assemebly language program code is wriiten as given below to display pressed key no. on seven segment dispaly.


Circuit Diagram:




Assembly Language Program:

ORG 0000H
ROW:MOV p2, #0FH ;C+4R = R1+4R0
JB p2.0, NEXT
MOV R0,#00H
AJMP COLUMN
NEXT:
JB p2.1, NEXT1
MOV R0,#01H
AJMP COLUMN
NEXT1:
JB p2.2, NEXT2
MOV R0,#02H
AJMP COLUMN
NEXT2:
JB p2.3, ROW
MOV R0,#03H
COLUMN:
MOV p2, #0F0H
JB p2.4, NEXT3
MOV R1,#00H
AJMP DISPLAY
NEXT3:
JB p2.5, NEXT4
MOV R1,#01H
AJMP DISPLAY
NEXT4:
JB p2.6, NEXT5
MOV R1,#02H
AJMP DISPLAY
NEXT5:
JB p2.7, ROW
MOV R1,#03H
DISPLAY:
MOV A, R0
RL A
RL A
ADD A, R1
ACALL CONVERT
MOV P1, A
AJMP ROW
CONVERT:
INC A
MOVC A, @A+PC
RET
DB 00111111B
DB 00000110B
DB 01011011B
DB 01001111B
DB 01100110B
DB 01101101B
DB 01111101B
DB 00000111B
DB 01111111B
DB 01101111B
DB 01110111B
DB 01111100B
DB 00111001B
DB 01011110B
DB 01111001B
DB 01110001B
END

Good Programs

WRITE A PROGRAM TO PUT THE NUMBER 8DH IN RAM LOCATIONS 30H TO 34H WITH THREE DIFFERENT METHOD.

; FIRST METHOD

ORG 0000H

MOV 30H, #8DH
MOV 31H, #8DH
MOV 32H, #8DH
MOV 33H, #8DH
MOV 34H, #8DH

END

; SECOND METHOD

ORG 0000H
MOV A, #8DH
MOV 30H, A
MOV 31H, A
MOV 32H, A
MOV 33H, A
MOV 34H, A

END

; THIRD METHOD


ORG 0000H
MOV SP, #30H
MOV 30H, #8DH
PUSH 30H
PUSH 30H
PUSH 30H
PUSH 30H
END
WRITE A PROGRAM WHICH DOUBLE THE NUMBER IN R2 AND PUT THE RESULT IN REGISTER R3 (LSB) AND R4 (MSB).

ORG 0000H

MOV R2, #04H ; STORE SAMPLE DATA IN R2
MOV A, R2
MOV B, #02H
MUL AB
MOV R3,A
MOV R4,B

END


DIVIDE THE DATA IN RAM LOCATION 1FH BY THE NUMBER 09H AND PLACE THE QUOTIENT IN R4 AND REMAINDER IN R5.


ORG 0000H
MOV 1FH, #04H ; STORE SAMPLE DATA IN 1FH
MOV A, 1FH
MOV B, #09H
DIV AB
MOV R4,A
MOV R5, B
END


WRITE A PROGRAM WHICH READS UNSIGNED NUMBER FROM PORT 2 AND MULTIPLIES IT WITH THE NUMBER IN R3 AND PUT THE RESULT IN EXTERNAL RAM LOCATION 10H(MSB) AND 11H(LSB).

ORG 0000H

MOV P2, #0FFH
MOV A, P2
MOV B, R3
MUL AB

MOV DPTR, #0011H
MOVX @DPTR, A

MOV DPTR, #0010H
MOV A, B
MOVX @DPTR, A

END


WRITE A PROGRAM THAT ADD THE UNSIGNED NUMBERS FOUND IN INTERNAL RAM LOCATION 25H, 26H AND 27H TOGETHER AND PUT THE RESULT IN RAM LOCATIONS 31H(LSB) AND 30(MSB).

ORG 0000H

MOV 25H, #0F0H
MOV 26H, #0FH
MOV 27H, #01H

MOV 30H, #00H
MOV 31H, #00H

MOV A, 25H
ADD A, 26H
JNC NEXT
INC 30H
NEXT:
ADD A, 27H
MOV 31H,A
JNC STOP
INC 30H

STOP:
AJMP STOP



WRITE A PROGRAM TO GET THE CONTENTS OF THE DPTR TO PC


ORG 0000H
MOV DPTR, #1234H ;STORE SAMPLE DATA IN DPTR
PUSH DPL
PUSH DPH
RET

END



WRITE A PROGRAM TO PUT A RANDOM NUMBER AT 3CH AND INCREMENT IT UNTILL IT EQUALS 2AH.

ORG 0000H

MOV 3CH, #04H ;STORE SAMPLE DATA IN 3CH

MAIN: INC 3CH
MOV A, 3CH
CJNE A, #2AH, MAIN

END



WRITE A PROGRAM THAT SWAP THE NIBBLES OF R0 AND R1 SO THAT THE LOW NIBBLES OF R0 SWAPS WITH THE HIGH NIBBLES OF R1 AND HIGH NIBBLE OF R0 SWAPS WITH THE LOW NIBBLE OF R1.


ORG 0000H

MOV R0, #12H ; STORE SAMPLE DATA IN R0
MOV R1, #34H ; STORE SAMPLE DATA IN R1

MOV 30H, R0
MOV 31H, R1

MOV A,30H
SWAP A
MOV R1, A

MOV A, 31H
SWAP A
MOV R0, A

END



WRITE A PROGRAM OF 8051 TO MAKE OR OPERATION OF TWO BYTES FROM ARRRAYS STARTING FROM EXTERNAL RAM LOCATION 4000H TO 4009H AND 5000H TO 5009H. STORE RESULT AT EXTERNAL RAM LOCATION 6000H TO 6009H. MAKE OR OPRATION WITHOUT ORL INSTRUCTION.

ORG 0000H
MOV R0, #00H
MAIN:
MOV DPL, R0
MOV DPH, #40H
MOVX A, @DPTR
CPL A
MOV 40H, A

MOV DPH, #50H
MOVX A, @DPTR
CPL A

ANL A, 40H
CPL A
MOV DPH, #60H
MOVX @DPTR, A

INC R0
CJNE R0, #0AH, MAIN

END


CONVERT A SERIES OF PACKED BCD NUMBERS LOCATED IN ROM LOCATION STARTING FROM 0300H TO 0304H INTO ASCII CODE. STORE RESULT AT INTERNAL RAM LOCATION STARTING FROM 40H.

ORG 0000H
MOV DPTR, #0300H
MOV R1,#40H

MAIN: MOV A, #00H
MOVC A, @A+DPTR
MOV R0, A
ANL A, #0FH
ADD A,#30H
MOV @R1,A

MOV A,R0
SWAP A
ANL A,#0FH
ADD A, #30H
INC R1
MOV @R1, A

INC R1
INC DPTR

CJNE R1, #4AH, MAIN

PASS: AJMP PASS

END


MAKE PROGRAM WHERE LOOPS RUN SO LED L1 IS "ON", WHEN SWITCH S1 IS PRESS AND LED L2 IS "ON" WHEN SWITCH S2 IS PRESS.

ORG 0000H

SETB P3.0
SETB P3.1

START: MOV C, P3.0
MOV P2.0, C
MOV C, P3.1
MOV P2.1, C
AJMP START

END



INTERFACE ONE COMMON CATHODE TYPE SEVEN SEGMENT DISPLAY WITH PORT 1 AND DRAW THE NECESSARY HARDWARE DIAGRAM. THEN WRITE A PROGRAM THAT DISPLAYS PATTERN 'E' FOR 1 SECOND AND OFF FOR 1 SECOND ALSO, CONTINUOUSLY.

ORG 0000H

MAIN:

MOV P2, #01111001b
ACALL DELAY

MOV P2, #00000000b
ACALL DELAY

AJMP MAIN


DELAY: MOV R1, #07H ; 1 Second delay loop
LOOP3: MOV R2, #00H
LOOP2: MOV R3, #00H
LOOP1: DJNZ R3, LOOP1
DJNZ R2, LOOP2
DJNZ R1, LOOP3
RET

END



USING SOFTWARE POLLED TIMER APPROACH WRITE A PROGRAM WHICH BLINKS LED CONNECED AT BIT 0 OF PORT 1. LED SHOULD REMAIN ON FOR 500MS AND OFF FOR 500MS. USE TIMER 0 TO GENERATE BASIC 1MS DELAY.


ORG 0000H


MOV TMOD,#01H ; 65536-1000=64536(FC18H)
MOV TL0, #18H ;ASSUME 12MHZ CRYSTAL FR. MOV TH0, #0FCH
SETB TR0

MOV DPTR,#000CH ;200H=512D

MAIN:
JNB TF0, MAIN
MOV TL0, #18H
MOV TH0, #0FCH
CLR TF0

INC DPTR
MOV A, DPH
CJNE A, #02H, MAIN
CPL P1.0
MOV DPTR, #000CH
AJMP MAIN

END


WRITE A PROGRAM USING INTERRUPTS TO GENERATE A SQUARE WAVE OF 2KHZ ON PIN P1.5. USE TIMER 0 AND MODE 1.

;ASSUME 12MHZ CRYSTAL FREQUENCY
;1/2KHZ=0.5MS
; 65536-250 =65286 (FF06H)

ORG 0000H
MOV TMOD,#01H
SETB TR0

START: MOV TL0, #06H
MOV TH0, #0FFH

HERE: JNB TF0, HERE
CPL P1.5
AJMP START

END


ORG 0000H

MAIN:
MOV TMOD,#10H
ACALL DELAY

PASS: AJMP PASS

DELAY:
;MC =3+3R1+2R1R2
MOV R1, #0C8H ;R1=200, R2=11
L2: MOV R2, #0BH
L1: DJNZ R2, L1
DJNZ R1, L2
RET
END

WRITE A PROGRAM TO USE TIMER 1 TO INTERRUPT THE PROGRAM AFTER A DELAY OF 2 MS. (CRYSTAL FREQUENCY = 10 MHZ).

ORG 0000H
AJMP MAIN ; 2000/1.2=1667,65536-1667=63869 (F97DH)

ORG 001BH
MOV TL1,#7DH
MOV TH1, #0F9H
RETI

MAIN: MOV IE, #88H
MOV TMOD,#10H
MOV TL1, #7DH
MOV TH1, #0F9H
SETB TR1

TIMEPASS: LJMP TIMEPASS

END


WRITE AN ASEMBLY LANGUAGE PROGRAM TO MEASURE AN UNKNOWN FREQUENCY CONNECTED ON PIN P3.4(T0). USE TIMER 0 IN COUNTER MODE.

ORG 0000H


MAIN: MOV TMOD,#05H ; TIMER-0 IN MODE-1 AS COUNTER
MOV TL0, #00H
MOV TH0, #00H
SETB TR0

ACALL DELAY ;5+3R3+3R3R4+2R3R4R5
CLR TR0
MOV R0,TL0
MOV R1,TH0

AJMP MAIN

DELAY: MOV R3, #08H
LOOP3: MOV R4, #00H
LOOP2: MOV R5, #004H
LOOP1: DJNZ R5, LOOP1
DJNZ R4, LOOP2
DJNZ R3, LOOP3
RET

END



WRITE A PROGRAM WHICH CONTINUOUSLY READS FROM P0 AND SEND IT TO P1, WHILE SIMULTANEOUSLY CREATING SQUAREWAVE OF 5 KHZ ON PIN 2.1. USE TIMER 0 INTERRUPT SERVICE ROUTINE. INTIALIZE TIMER 0 IN MODE-2.


ORG 0000H
AJMP MAIN ;ASSUME 12MHZ CRYSTAL FR.
;1/5KHZ=0.2MS
; 256-100 =156 (9CH)

ORG 000BH
CPL P2.1
RETI

MAIN: MOV IE, #82H
MOV TMOD,#02H
MOV TH0, #9CH
SETB TR0

SEND: MOV P0, #0FFH
MOV A, P0
MOV P1, A
AJMP SEND

END



WRITE AN ALP USING INTERRUPTS TO DO FOLLOWING TASK,(1) MAKE TIMER TO GENERATE A SQUAREWAVE OF 4KHZ FREQUNCY ON P2.0, (2) ASSUME THAT INT1 PIN IS CONNECTED TO A SWITCH THAT IS NORMALLY HIGH, WHWNEVER IT GOES LOW, IT SHOULD READ PORT P1, COMPLEMENT THE DATA AND SEND IT TO PORT P2.

ORG 0000H
AJMP MAIN

ORG 000BH
CPL P2.0
RETI

ORG 0013H
AJMP IN_EXT

MAIN:
MOV IE, #86H
MOV IP, #04H

MOV TMOD,#0AH
MOV TH0, #83H
SETB TR0

;ASSUME 12MHZ CRYSTAL FREQUENCY
;1/4KHZ=0.25MS, 125US FOR HALF PERID
; 256-125 =131 (83H)


PASS: AJMP PASS


IN_EXT: MOV P1, #0FFH
MOV A, P1
CPL A
MOV P2, A
RETI
END
MENTION THE STEPS IN PROGRAMMING THE 8051 TO TRANSFER CHARACTER BYTES SERIALLY BY METHOD OF POLLING. THEN WRITE A PROGRAM TO TRANSMITT "NGU" SERIALLY AT 2400 BAUDRATE. USE TIMER 1 TO SET BAUD RATE.

ORG 0000H

MOV TMOD,#20H
MOV SCON,#50H
MOV TH1,#0F4H
SETB TR1

MOV A, #'N'
ACALL SEND_PC
MOV A, #'G'
ACALL SEND_PC
MOV A, #'U'
ACALL SEND_PC

PASS: AJMP PASS

SEND_PC: MOV SBUF, A
HERE: JNB TI, HERE
CLR TI
RET

END



WRITE A PROGRAM WITH TWO ISRS FOR INT0 AND INT1 RESPECTIVELY SATISFYING FOLLOWING CONDITIONS. WHEN THERE IS NO INTERRUPT, MAINLINE PROGRAM SHOULD BLINK GREEN LED THROUGH PORT PIN P1.0 FOR 1 SECOND ON AND 1 SCOND OFF CONTINUOUSLY. WHEN EDGE TRIGGER INTERRUPT WILL COME AT INT0, GLOW RED LED THROUGH PORT PIN P1.1 FOR 1 SECOND, THEN TURN IT OFF AND RETURN TO MAINLINE. WHEN EDGE TRIGGER INTERRUPT WILL COME AT INT1, GLOW YELLOW LED THORUGH PORT PIN P1.2 FOR 1 SECOND, THEN TURN IT OFF AND RETURN TO MAINLINE PROGRAM. DRAW NECESSARY HARDWARE.


ORG 0000H
AJMP MAIN

GREEN EQU P1.0
RED EQU P1.1
YELLOW EQU P1.2

ORG 0003H
AJMP ISR_IN0

ORG 0013H
AJMP ISR_IN1


MAIN: MOV IE,#85H
MOV TCON,#05H
MOV P1, #00H

LOOP: SETB GREEN
ACALL DELAY
CLR GREEN
ACALL DELAY
AJMP LOOP

ISR_IN0:
SETB RED
ACALL DELAY
CLR RED
RETI

ISR_IN1:
SETB YELLOW
ACALL DELAY
CLR YELLOW
RETI


DELAY: ; 1 SECOND DELAY
MOV R1, #08H ; 5+3R1+3R1R2+2R1R2R3
LOOP3: MOV R2, #00H
LOOP2: MOV R3, #00H
LOOP1: DJNZ R3, LOOP1
DJNZ R2, LOOP2
DJNZ R1, LOOP3
RET


END



EXPLAIN WITH THE HELP OF DIAGRAM THE NECESSARY CIRCUIT TO INTERFACE 4X4 KEYPAD WITH 8051. DISCUSS THE LOGICAL STEPS TO DETECT THE KEY PRESSED AND ENCODE IT. WRITE AN ALPTO DISPLAY THE DETECTED KEY ON ONE 7-SEGMENT DISPLAY.

ORG 0000H



FIND_ROW:

MOV P3, #0FH

MOV R0,#00H
JNB P3.0, FIND_COLUMN
MOV R0,#01H
JNB P3.1, FIND_COLUMN
MOV R0,#02H
JNB P3.2, FIND_COLUMN
MOV R0,#03H
JNB P3.3, FIND_COLUMN
AJMP FIND_ROW



FIND_COLUMN:
MOV P3, #0F0H

MOV R1,#00H
JNB P3.4, CALULATE_KEY
MOV R1,#01H
JNB P3.5, CALULATE_KEY
MOV R1,#02H
JNB P3.6, CALULATE_KEY
MOV R1,#03H
JNB P3.7, CALULATE_KEY
AJMP FIND_ROW


CALULATE_KEY:

MOV A, R0
RL A
RL A
ADD A, R1
ACALL CONVERT
MOV P2, A
AJMP FIND_ROW

CONVERT:
INC A
MOVC A, @A+PC
RET

DB 00111111B
DB 00000110B
DB 01011011B
DB 01001111B
DB 01100110B
DB 01101101B
DB 01111101B
DB 00000111B
DB 01111111B
DB 01101111B
DB 01110111B
DB 01111100B
DB 00111001B
DB 01011110B
DB 01111001B
DB 01110001B

END


INTERFACE LCD AND WRITE A PROGRAM TO DIPLAY “Hello India”.

ORG 0000H

MOV A, #3CH
ACALL COMNWRT

MOV A, #01H
ACALL COMNWRT

MOV A, #0EH
ACALL COMNWRT



MOV A, #06H
ACALL COMNWRT

MOV A, #84H
ACALL COMNWRT





MOV A, #'H'
ACALL DATAWRT


MOV A, #'E'
ACALL DATAWRT

MOV A, #'L'
ACALL DATAWRT

MOV A, #'L'
ACALL DATAWRT

MOV A, #'O'
ACALL DATAWRT

MOV A, #0C4H
ACALL COMNWRT


MOV A, #'I'
ACALL DATAWRT

MOV A, #'N'
ACALL DATAWRT

MOV A, #'D'
ACALL DATAWRT

MOV A, #'I'
ACALL DATAWRT

MOV A, #'A'
ACALL DATAWRT


MAIN:AJMP MAIN

COMNWRT:
ACALL READY
MOV P2, A
CLR P1.5 ;RS
CLR P1.6 ;R/W
SETB P1.7 ;E
NOP
NOP
NOP
NOP
CLR P1.7 ;E
RET

DATAWRT:
ACALL READY
MOV P2, A
SETB P1.5 ;RS
CLR P1.6 ;R/W
SETB P1.7 ;E
NOP
NOP
NOP
NOP
CLR P1.7 ;E
RET

READY:
SETB P2.7
CLR P1.5 ;RS
SETB P1.6 ;R/W
REPEAT: CLR P1.7 ;E
NOP
NOP
NOP
NOP
SETB P1.7 ;E
JB P2.7, REPEAT
CLR P1.7 ;E
RET

END