Ece3003 Microcontroller and Its Applications Quick References

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

ECE3003 MICROCONTROLLER AND ITS APPLICATIONS

QUICK REFERENCES

Memory Organization
PSW REGISTER

MACHINE CYCLES
A cycle is, in reality, 12 pulses of the crystal

Since we know the crystal is pulsing 11,059,000 times per second and
that one machine cycle is 12 pulses, we can calculate how many
instruction cycles the 8051 can execute per second:

1 Machine cycle = 11,059,000 / 12 = 921,583 and


I machine cycle period = 1/ 921,583=1.085µs

Simple Delay Program using registers only

TMOD REGISTER
 To calculate the values to be loaded into the TL and TH registers, look at the following
example

 Assume XTAL = 11.0592 MHz, we can use the following steps for finding the TH, TL
registers’ values

1. Divide the desired time delay by 1.085 us

2. Perform 65536 – n, where n is the decimal value we got in Step1

3. Convert the result of Step2 to hex, where yyxx is the initial hex value to be loaded into
the timer’s register

4. Set TL = xx and TH = yy To generate a time delay

Steps to program Timer 0 Mode 1 (16-bit timer)

• Assuming XTAL = 11.0592 MHz, write a program to generate a square wave of 50 Hz


frequency on pin P2.3.

Solution:

1. The period of the square wave = 1 / 50 Hz = 20 ms.

2. The high or low portion of the square wave = 10 ms.

3. 10 ms / 1.085 s = 9216

4. 65536 – 9216 = 56320 in decimal = DC00H in hex.

5. TL1 = 00H and TH1 = DCH.

6. CLR P2.3

MOV TMOD,#10H ;timer 1, mode 1

AGAIN: MOV TL1,#00 ;Timer value = DC00H

MOV TH1,#0DCH

SETB TR1 ;start

BACK: JNB TF1,BACK

CLR TR1 ;stop

CPL P2.3

CLR TF1 ;clear timer flag 1

SJMP AGAIN ;reload timer since


Timer 0 Mode-2 (Auto-reload Timer)
SCON is an 8-bit register used to program the start bit, stop
bit, and data bits
EXAMPLE
EXAMPLE
LCD INTERFACE
EXAMPLE

ORG 0000H

MOV A, #38H ; INITIALIZE 2x16 LCD

ACALL COMNWRT ; call command subroutine

ACALL DELAY ; give LCD some time

MOV A, #0EH ; display on, cursor on

ACALL COMNWRT ; call command subroutine

ACALL DELAY ; give LCD some time

MOV A, #01 ; clear LCD

ACALL COMNWRT ; call command subroutine

ACALL DELAY ; give LCD some time


MOV A, #06H ; shift cursor right

ACALL COMNWRT ; call command subroutine

ACALL DELAY ; give LCD some time

MOV A, #84H ; cursor at line 1, pos. 4

ACALL COMNWRT ; call command subroutine

ACALL DELAY ; give LCD some time

MOV A, #’B’ ; display letter SAY ‘ M ‘

ACALL DATAWRT ; call display subroutine

ACALL DELAY ; give LCD some time

MOV A, #’E’ ; display letter SAY ‘ E ’

ACALL DATAWRT ; call display subroutine

AGAIN: SJMP AGAIN ; stay here

COMNWRT: ; send command to LCD

MOV P1, A ; copy reg A to port 1

CLR P2.0 ; RS=0 for command

CLR P2.1 ; R/W=0 for write

SETB P2.2 ; E=1 for high pulse

ACALL DELAY ; give LCD some time

CLR P2.2 ; E=0 for H-to-L pulse

RET

DATAWRT: ; write data to LCD

MOV P1, A ; copy reg A to port 1

SETB P2.0 ; RS=1 for data

CLR P2.1 ; R/W=0 for write

SETB P2.2 ; E=1 for high pulse

ACALL DELAY ; give LCD some time

CLR P2.2 ; E=0 for H-to-L pulse

RET

DELAY: MOV R3, #50 ; 50 or higher for fast CPUs


HERE2: MOV R4, #255 ; R4 = 255

HERE: DJNZ R4, HERE ; stay until R4 becomes 0

DJNZ R3, HERE2 ; stay until R3 becomes 0

RET

END

You might also like