switches and leds interface to the 8051 microcontroller

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

Upload: faroha-lolya

Post on 13-Apr-2017

369 views

Category:

Education


8 download

TRANSCRIPT

Page 1: Switches and LEDs interface to the 8051 microcontroller

Switches and LEDs Interface to the 8051 Microcontroller

Prepared by: Afrah Salman

Supervised by: Dr. Basim

Page 2: Switches and LEDs interface to the 8051 microcontroller

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.

Page 3: Switches and LEDs interface to the 8051 microcontroller
Page 4: Switches and LEDs interface to the 8051 microcontroller

Pin Assignment with 8051

Page 5: Switches and LEDs interface to the 8051 microcontroller

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.

Page 6: Switches and LEDs interface to the 8051 microcontroller

Fig. 1 Interfacing switch to Microcontroller

Page 7: Switches and LEDs interface to the 8051 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.

Page 8: Switches and LEDs interface to the 8051 microcontroller
Page 9: Switches and LEDs interface to the 8051 microcontroller

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.

Page 10: Switches and LEDs interface to the 8051 microcontroller
Page 11: Switches and LEDs interface to the 8051 microcontroller

• 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 toPORT2.

• 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.

Page 12: Switches and LEDs interface to the 8051 microcontroller

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.

Page 13: Switches and LEDs interface to the 8051 microcontroller

Circuit Diagram to Interface Switch with 8051

Page 14: Switches and LEDs interface to the 8051 microcontroller

Example1: The following program will make the value of PIN3.1 keeps toggling (ON/OFF) after some delay.

org 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

Page 15: Switches and LEDs interface to the 8051 microcontroller

;DELAY SUB-ROUTINEdelay: MOV R5, #10 ;load register R5 with 10third: MOV R6, #200 ;load register R6 with 200second: 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 programEND

Page 16: Switches and LEDs interface to the 8051 microcontroller

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.

org 00H;MAIN PROGRAM SETB P1.7 ;make P1.7 an inputAGAIN: JB P1.2, OVER ;jump if P1.7=1 MOV P2, #’N’ ;SW=0, issue ‘N’ to P2 SJMP AGAIN ;keep monitoringOVER: MOV P2,#’Y’ ;SW=1, issue ‘Y’ to P2 SJMP AGAIN ;keep monitoring

Page 17: Switches and LEDs interface to the 8051 microcontroller

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.

org 00H;MAIN PROGRAM SETB P1.7 ;make P1.7 an inputAGAIN: MOV C,P1.0 ;read the SW status into CF MOV P2.7, C ;send the SW status to LED SJMP AGAIN ;keep repeatingNote: The instruction “MOV P2.7, P1.0” is wrong since such an instruction doesn’t exist. However, “MOV P2, P1” is a valid instruction.