SERIAL 8051 PROGRAM and Timer & Counter Program
SERIAL 8051 PROGRAM and Timer & Counter Program
(256-TH1)=28800/9600=3
(256-TH1)=3
Th1=253=FDH
org 0000h
movTMOD,#20H
MOV TH1,#0FDH
MOV SCON,#50H
SETB TR1
L1:
CLR TI
MOV SBUF,#'a'
SELF:
JNB TI,SELF
SJMP L1
END
org 00h
LJMP MAIN
ORG 023H
ISR:
JNB RI,L2
CLR TI
CLR RI
L2:
MOV A,SBUF
MOV SBUF,A
RETI
ORG 200H
MAIN:
MOV TMOD,#20H
MOV TH1,#0FDH
MOV SCON,#50H
MOV IE,#90H
SETB TR1
MOV A,#'A'
MOV SBUF,A
LOOP:
SJMP LOOP
End
Timer programming
The 8051 has two timers/counters, they can be used
either as
Timers to generate a time delay – timer
Event counters to count events happening outside
the microcontroller - counter
Both Timer 0 and Timer 1 are 16 bits wide
To generate a time delay
1. Load the TMOD value register indicating which timer (timer 0 or
timer 1) is to be used and which timer mode (0 or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) with the JNB TFx,target
instruction to see if it is raised Get out of the loop when TF becomes
high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
TIMER program for generating delay
org 00h
MOV TMOD,#01H
HERE:
MOV TL0,#0F2H
MOV TH0,#0FFH
CPL P1.5
ACALL DELAY
SJMP HERE
DELAY:
SETB TR0
AGAIN:
JNB TF0,AGAIN
CLR TR0
CLR TF0
RET
END
TIMER program for generating delay
1. Write a program to generate a Delay
TIMER PROGRAM
org 00h
MOV TMOD,#01H
HERE:
MOV TL0,#0F2H
MOV TH0,#0FFH
CPL P1.5
ACALL DELAY
SJMP HERE
DELAY:
SETB TR0
AGAIN:
JNB TF0,AGAIN
CLR TR0
CLR TF0
RET
END
ORG 00H
MOV TMOD, #10H ; Set Timer 1 in mode 1 (16-bit timer)
MOV TH1, #0FCH ; Load high byte of timer value
MOV TL1, #18H ; Load low byte of timer value
SETB TR1 ; Start Timer 1
SETB P1.5 ; Set P1.5 initially high
MAIN_LOOP:
RESULT :
COUNTER PROGRAM:
ORG 00H
MOV TMOD,#01100000B
MOV TH1,#0
SETB P3.5
AGAIN:
SETB TR1
BACK:
MOV A,TL1
MOV P2,A
JNB TF1,BACK
CLR TR1
CLR TF1
SJMP AGAIN
END