SERIAL COMMUNICATION AND TIMER PROGRAMMING
Baud rate calculations:
oscillator frequency
Baud frequency = 2SMOD x
(256 –TH1) 12x32
11.0592
9600 = 20 x
(256 –TH1) 12x32
(256-TH1)=28800/9600=3
(256-TH1)=3
Th1=253=FDH
11.0952/12x32 mhz =28,800hz
SERIAL COMMUNICATION TRANSMIT A CHARACTER with baud rate of
9600
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
Write a program for 8051 to transmit data snist
ORG 00H
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON,#50H
SETB TR1
UP: MOV A, #'S'
ACALL SEND
MOV A, #'N'
ACALL SEND
MOV A, #'I'
ACALL SEND
MOV A, #'S'
ACALL SEND
MOV A, #'T'
ACALL SEND
SJMP UP
SEND : MOV SBUF, A
HERE: JNB TI, HERE
CLR TI
RET
END
SERIAL COMMUNICATION TRANSMIT&RECEIVE A CHARACTER
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
2. To create a square wave with 50% duty cycle on the p1.5
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:
JNB TF1, MAIN_LOOP ; Wait for Timer 1 overflow
CLR TF1 ; Clear Timer 1 overflow flag
CPL P1.5 ; Toggle P1.5
SJMP MAIN_LOOP ; Repeat the loop
END
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