Switches and LEDs interface to the 8051 microcontroller

10,393 views 17 slides Jan 04, 2016
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

This powerpoint contains a simplified description about the switches and leds used in the 8051 MC, and simple assembly language programs.


Slide Content

Switches and LEDs Interface to the 8051 Microcontroller Prepared by: Afrah Salman Supervised by: Dr. Basim

Switch Basics A switch is an electrical component that can break an electrical circuit, interrupting the current or diverting it from one conductor to another. If we directly connect a switch to one of the microcontroller port pins, the pin floats when the switch is open. Meaning, it is not at a fixed logic level 0v or 5v .

Pin Assignment with 8051

Interfacing Switch Fig. 1 shows how to interface the switch to microcontroller. A simple switch has an open state and closed state. However, a microcontroller needs to see a definite high or low voltage level at a digital input. A switch requires a pull-up or pull-down resistor to produce a definite high or low voltage when it is open or closed. A resistor placed between a digital input and the supply voltage is called a "pull-up" resistor because it normally pulls the pin's voltage up to the supply .

Fig. 1 Interfacing switch to Microcontroller

LED Basics Light Emitting Diodes are the semi conductor light sources. Commonly used LEDs will have a cut-off voltage of 1.7V and current of 10mA. When an LED is applied with its required voltage and current it glows with full intensity. The Light Emitting Diode is similar to the normal PN diode but it emits energy in the form of light. The color of light depends on the band gap of the semiconductor.

Interface to the Microcontroller Now we could go ahead and interface it to the microcontroller, but we would rather interface 8 switches and 8 LED's to two ports as shown in the schematic below.

As we can see the schematic has basic circuits for oscillator, reset and power connections for the microcontroller. A DIP (dual in line package) Switch, array of 8 switches is connected to  PORT3  AND 8 LEDs to PORT2 . Observe the  RR1  component, it is array of 8 resistors in a single pack(SIP). It is as good as connecting 8 pull-up resistors as shown in figure 1. You could also use 8 discrete resistors as well .

Interfacing Switch with 8051 We now want to control the LED by using switches in 8051 Slicker Board. It works by turning ON a LED & then turning it OFF when switch is going to LOW or HIGH. The 8051 Slicker board has eight numbers of point LEDs, connected with I/O Port lines (P0.0 – P0.7) to make port pins high. Eight switches, connected with I/O port lines (P2.0 – P2.7) are used to control eight LEDs .

Circuit Diagram to Interface Switch with 8051

Example1: The following program will make the value of PIN3.1 keeps toggling (ON/OFF) after some delay . o rg 00H ;MAIN PROGRAM Toggle: MOV P3, # 01H ;move 00000001 to PORT3 CALL delay ;execute delay MOV A, P3 ;move PORT3 value to accumulator CPL A ;complement PORT3 value MOV P3, A ;move 11111110 to PORT3 CALL delay ;execute delay SJMP Toggle

;DELAY SUB-ROUTINE delay: MOV R5, # 10 ;load register R5 with 10 third: MOV R6, # 200 ;load register R6 with 200 s econd: MOV R7, # 200 ;load register R7 with 200 DJNZ R7, $ ;decrement R7 till it is zero DJNZ R6, second ;decrement R6 till it is zero DJNZ R5, third ; decrement R5 till it is zero ret ;go back to main program END

Example2: A switch is connected to pin PI .7. Write a program to check the status of SW and perform the following : If SW=0, send letter ‘N’ to P2., If SW=1, send letter ‘Y’ to P2 . o rg 00H ;MAIN PROGRAM SETB P1.7 ;make P1.7 an input AGAIN: JB P1.2, OVER ;jump if P1.7=1 MOV P2, # ’N’ ;SW=0, issue ‘N’ to P2 SJMP AGAIN ;keep monitoring OVER: MOV P2,# ’Y’ ;SW=1, issue ‘Y’ to P2 SJMP AGAIN ;keep monitoring

Example3: A switch is connected to pin PI .7 and an LED to pin P2.7. Write a program to get the status of the switch and send it to the LED. o rg 00H ;MAIN PROGRAM SETB P1.7 ;make P1.7 an input AGAIN: MOV C,P1.0 ;read the SW status into CF MOV P2.7, C ;send the SW status to LED SJMP AGAIN ;keep repeating Note: The instruction “MOV P2.7, P1.0” is wrong since such an instruction doesn’t exist. However, “MOV P2, P1” is a valid instruction.
Tags