You are on page 1of 3

Microcontroller Lab (BME 312)

Expt. No. 4

VI Sem. B.Tech. Biomedical Engineering

Bit manipulation, Programming using I/O ports and


Counter programs.

1. Write a program to create a square wave of 50% duty cycle on bit 0 of port1.
ORG 0000h
AJMP START
START:

SETB P1.0
LCALL DELAY
CLR P1.0
LCALL DELAY
SJMP START

DELAY: SETB TR0


; Delay loop using Timer 0
BACK:
JNB TF0, BACK
CLR TR0
CLR TF0
RET
END
2. Write a program to generate a square wave of 66% duty cycle on bit 0 of port 1.
ORG 0000h
AJMP START
START:

SETB P1.0
LCALL DELAY
LCALL DELAY
CLR P1.0
LCALL DELAY
SJMP START

DELAY: SETB TR0


BACK:
JNB TF0, BACK
CLR TR0
CLR TF0
RET
END

; send logic 1 on P1.0


; call delay program
; send logic 0 on p1.0
; repeat the steps.
; start timer 0
; test for timer overflow
; on timer 0 overflow stop
; timer and reset timer flag.
; return to calling programme

3. Assuming that clock pulses fed into pin T1, write a program for counter 1 in mode2 to
count the pulses and display the state of the TL1 count on P2.
ORG 0000h
AJMP START
START:

AGAIN:
BACK:

MOV TMOD, #0110000B


MOV TH1, #00
SETB P3.5
SETB TR1
MOV A, TL1

; Counter in mode 2 (Auto reload mode)


; enable P3.5 (T1) as input

1
Department of Biomedical Engineering, Manipal Institute of Technology, Manipal 576 104

Microcontroller Lab (BME 312)

MOV P2, A
JNB TF1, BACK
CLR TR1
CLR TF1
SJMP AGAIN
END

VI Sem. B.Tech. Biomedical Engineering

; send the count to port 2

4. Implement a 2-digit decimal up counter and display the counts on Port 2 pins.
ORG 0000H
AJMP START
START:
AGAIN:

MOV A, #00
ADD A, #01
DA A
MOV P2, A
ACALL DELAY
SJMP AGAIN

DELAY:
UP1:
UP2:
UP3:

MOV R3, #0Fh


MOV R4, #0FFh
MOV R5, #0FFH
NOP
NOP
DJNZ R5, UP3
DJNZ R4, UP2
DJNZ R3, UP1
RET
END

5. Implement a 16- bit hexadecimal up counter and update the counts in register R0 and R1
register.
ORG 0000H
AJMP START
START:
UP:

MOV R0, #00 ; Low byte of count in R0


MOV R1, #00 ; High byte of count in R4
MOV A, R0
ADD A, #01H
MOV R0, A
MOV P2, A
MOV A, R1
ADDC A, #00h
MOV P3, A
MOV R1, A
CALL DELAY
CALL DELAY
SJMP UP

2
Department of Biomedical Engineering, Manipal Institute of Technology, Manipal 576 104

Microcontroller Lab (BME 312)

DELAY:
UP1:
UP2:
UP3:

VI Sem. B.Tech. Biomedical Engineering

MOV R3, #0Fh


MOV R4, #0FFh
MOV R5, #0FFH
NOP
NOP
DJNZ R5, UP3
DJNZ R4, UP2
DJNZ R3, UP1
RET
END

6. Read an 8-bit number through port 1 and perform logical AND operation with AAH and
send the result to port2.

EXERCISES
1. Implement an 8 bit ring counter.
2. Assuming R1:R0 registers as an 16 bit register rotate the content of this register left
such that bit-7 of R0 becomes bit 0 of R1 and bit-7 of R1 becomes bit-0 of R0.
3. Implement an 8-bit octal up counter.
4. Program Timer 1 to be an event counter in mode 2 and display the decimal count on
P0, P1 and P2 continuously. Set the initial count to 99.

PRACTICE PROGRAMS
1.
2.
3.
4.

Implement a 16 - bit down counter.


Set the carry flag to 0 if the number in accumulator is even, else reset the carry flag.
Implement a 4 digit decimal down counter and display the counts on ports.
Program timer 1 to generate the square wave of 1 KHz. Assume crystal frequency is
11.0592 MHz

3
Department of Biomedical Engineering, Manipal Institute of Technology, Manipal 576 104

You might also like