Interfacing Devices To 8051 - Online Article

LED Interfacing

Making a LED Blink

The first step is to build a simple circuit. At this point you should be familiar with the parts used. This design is intended for use with an Atmel 89s51 but also possible for others family 8051.

Step 1: Build the circuit as shown in figure as you seen on figure P0.0 is connected to LED's cathode. We have to make the LED blink.

Step 2: In this step, we must type the assembly program to make the LED blink.

org 0h
start:
	Clr P0.0	;send '0' to P0.0
	call delay	;call delay time
	Setb P0.0	;send '1' to P0.0
	call delay	;call delay time
	sjmp start	;loop forever to start
	;=============================================
	;subroutine delay created to rise delay time
	;=============================================
	delay: mov R1,#255 
	del1: mov R2,#255
	del2: 	djnz R2,del2
			djnz R1,del1
	ret
end

Step 3: Save the assembly program above, and name it with LED1.asm (for example).

Step 4: Download the hex file ( LED1.hex ) into the microcontroller by using Microcontroller ATMEL ISP software, see the instruction. After download this hex file you'll see the action of the LED.

Image1

Diagram Schematic LED Blink

Image2

Diagram Schematic Input Data

Push Button Interfacing

We will command the LED to run right or left by switching the button, for example run the LED to the right when pushing switch on P2.0 and run the LED to the left when pushing switch on P2.1.

Step 1: Build the circuit as shown. As we see. P2.0 through P2.7 is connected to switch push button each and LED's Cathode is connected to P0.0 trough P0.7. All we want to make a LED start running to the left or right, by pushing the button P2.0 or P2.1.

Step 2: In this step, we type the assembly program to make our LED on when we push the switch.

org 0h
CekP20:
	JB P2.0,CekP21	;checking P2.0 while get push
	call RLeft		;if so, call Rotate Left subroutine
	sjmp CekP20		;jump forever to CekP20
CekP21:
	JB P2.1,CekP20	;checking P2.1 while get push
	call RRight		;if so, call Rotate Right
	sjmp CekP20		;jump forever to CekP2.0
;===============================================
;this subroutine is used to move LED to the left.
;================================================
RLeft:	mov A,#11111110b	;send data to Acc
RLeft1: mov P0,A			;send data to P0
	call delay				;call delay time
	JB P2.0,RLeft2			;checking P2.0 while get push
	sjmp EndRLeft			;if so, finish Rotate Left
RLeft2:RL A
		sjmp RLeft1
EndRLeft:ret;
;=================================================
;this subroutine is used to move LED to the right.
;=================================================
RRight: mov A,#01111111b
RRight1:mov P0,A
		call delay
		JB P2.0,RRight2
		sjmp EndRRight
RRight2:RL A
		sjmp RRight1
EndRRight:
		ret
;=============================================
;subroutine delay created to rise delay time
;=============================================
delay: mov R1,#255
del1:  mov R2,#255
del2:  djnz R2, del2
		djnz R1,del1
		ret
end

Step 3: Save the assembly program above, and name it with SW2.asm (for example) Compile the program.

Step 4: Download hex file (SW2.hex) into the microcontroller by using Microcontroller ATMEL ISP software. After download this hex file see the action of the LED.

Power Supply

Image3

The above shown hardware circuitry is used to produce a constant 5 volt supply To power the microcontroller during LED n LCD interfacing.the circuit uses 4 pn diodes (4007) along with LM7805 to produce a 5 v power supply.

Interfacing the Keyboard to 8051 microcontroller

The key board here we are interfacing is a matrix keyboard. This key board is designed with a particular rows and columns. These rows and columns are connected to the microcontroller through its ports of the micro controller 8051. We normally use 8*8 matrix key board. So only two ports of 8051 can be easily connected to the rows and columns of the key board.

When ever a key is pressed, a row and a column gets shorted through that pressed key and all the other keys are left open. When a key is pressed only a bit in the port goes high. Which indicates microcontroller that the key is pressed. By this high on the bit key in the corresponding column is identified.

Once we are sure that one of key in the key board is pressed next our aim is to identify that key. To do this we firstly check for particular row and then we check the corresponding column the key board.

To check the row of the pressed key in the keyboard, one of the row is made high by making one of bit in the output port of 8051 high . This is done until the row is found out. Once we get the row next out job is to find out the column of the pressed key. The column is detected by contents in the input ports with the help of a counter. The content of the input port is rotated with carry until the carry bit is set.

The contents of the counter is then compared and displayed in the display. This display is designed using a seven segment display and a BCD to seven segment decoder IC 7447.

The BCD equivalent number of counter is sent through output part of 8051 displays the number of pressed key.

Image4

Circuit diagram of Interfacing Keuboard to 8051

The programming algorithm, program and the circuit diagram is as follows. Here program is explained with comments.

Image5

Circuit diagram of Interfacing Keuboard to 8051

Keyboard is organized in a matrix of rows and columns as shown in the figure. The microcontroller accesses both rows and columns through the port.

  1. The 8051 has 4 I/O ports P0 to P3 each with 8 I/O pins, P0.0 to P0.7,P1.0 to P1.7, P2.0 to P2.7, P3.0 to P3.7. The one of the port P1 (it understood that P1 means P1.0 to P1.7) as an I/P port for microcontroller 8051, port P0 as an O/P port of microcontroller 8051 and port P2 is used for displaying the number of pressed key.
  2. Make all rows of port P0 high so that it gives high signal when key is pressed.
  3. See if any key is pressed by scanning the port P1 by checking all columns for non zero condition.
  4. If any key is pressed, to identify which key is pressed make one row high at a time.
  5. Initiate a counter to hold the count so that each key is counted.
  6. Check port P1 for nonzero condition. If any nonzero number is there in [accumulator], start column scanning by following step 9.
  7. Otherwise make next row high in port P1.
  8. Add a count of 08h to the counter to move to the next row by repeating steps from step 6.
  9. If any key pressed is found, the [accumulator] content is rotated right through the carry until carry bit sets, while doing this increment the count in the counter till carry is found.
  10. Move the content in the counter to display in data field or to memory location.
  11. To repeat the procedures go to step 2.

Program to interface matrix keyboard to microcontroller 8051

Start of main program

To check that whether any key is pressed:

 start:
		mov a,#00h
		mov p1,a	;making all rows of port p1 zero
		mov a,#0fh
		mov p1,a	;making all rows of port p1 high
press:
		mov a,p2
		jz press	;check until any key is pressed

After making sure that any key is pressed:

	mov a,#01h		;make one row high at a time
	mov r4,a
	mov r3,#00h	;initiating counter
next:
	mov a,r4
	mov p1,a	;making one row high at a time
	mov a,p2	;taking input from port A
	jnz colscan	;after getting the row jump to check
	column
	mov a,r4
	rl a		;rotate left to check next row
	mov r4,a
	mov a,r3
	add a,#08h	;increment counter by 08 count
	mov r3,a
	sjmp next	;jump to check next row 

After identifying the row to check the colomn following steps are followed:

colscan:
		mov r5,#00h
in:
		rrc a	;rotate right with carry until get the carry
		jc out	;jump on getting carry
		inc r3	;increment one count
		jmp in
out:
		mov a,r3
		da a	;decimal adjust the contents of counter
		before display
		mov p2,a
		jmp start	;repeat for check next key.

Image6

Crystal oscillator circuitary to trigger the TIMER

Image7

Interfacing of microcontroller with LCD

LCD PIN description

Image8

Microcontroller PIN description

Image9

About the Author:

No further information.




Comments

inbha rias on 2011-01-06 11:48:38 wrote,

very nice

Shailesh Patel on 2011-02-28 07:00:11 wrote,

fi9...

arun kumar on 2011-06-28 11:46:25 wrote,

please sent c code of this program

Raveen Jinka on 2013-07-04 07:13:50 wrote,

good