Download presentation
Presentation is loading. Please wait.
Published byDaniella Eaton Modified over 9 years ago
1
Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers
2
Light-Emitting Diodes (1) Source: Wikipedia (http://en.wikipedia.org/wiki/Light-emitting_diode)
3
Light-Emitting Diodes (2) An LED is a diode Its emitter comprises of a semiconductor die. It has a cathode and an anode. Usually a diode allows an electric current to pass only in one direction. When an LED is connected to a circuit its forward current should not exceed a maximum allowable limit. We can limit this current by using current limiting resistors.
4
Technical Specifications of the LED
5
Forward Current vs Forward Voltage of the LED
6
Methods of interfacing LEDs to a microcontroller Method 1Method 2 What is the value of R?
7
Interfacing push buttons to the microcontroller Switches are used to make, break or change connections in an electrical circuit. Push button switches are usually spring loaded so it returns the moving element to the original position when the pushing force is removed. There two types of push buttons. Normally Open Switch Normally Closed Switch
8
Interfacing Push Buttons “Normally Open“ Push Button “Normally Closed" Push Button
9
Switch Bounce
10
Reset Pin 1 (MCLR) Bootload Pin 33 (B5) LED 2 Pin D1 LED 3 Pin D0
12
Set pin D0 as Output Port (LED) Set Pin B5 as Input Port (switch) Set all pins of PORT D0 as zero Set sw equals to 0010000 in binary
13
PORTB & 0x20 = PORTB AND 0b00100000 When the button is released pin B5 is set to HIGH, PORTB=0b00100000 or 0x20 When the button is pressed pin B5 is set to LOW, PORTB=0b00000000 or 0x00 Port B0x10Result 20 16 00 0 0 000
14
Before the button is pressed pin B5 is HIGH PORTB=0b00100000 or 0x20 Then sw becomes, sw=0x20 & 0x20 = 0x20; Since sw= 0x20, the condition is TRUE so the code will remain in the while loop. When the button is pressed pin B0 is LOW. PORTB=0 or PORTB=0x0 Then sw becomes, sw=0x00 & 0x20 = 0; Since sw=0, the condition is FALSE so the code will exit the while loop. Sets the pin D1 to HIGH When pin D1 is HIGH it lights the LED Sets a delay for button debounce sw = 1 sw = 0 LED is ON LED is OFF LED is ON
15
Now the button is pressed So pin B5 is LOW PORTB=0b00000000 or 0x00 Then sw becomes, sw=0x00 & 0x20 = 0; Since sw= 0, the condition is TRUE so the code will remain in the while loop. When the button is released pin B0 is HIGH. PORTB=0b00100000 or PORTB=0x20 Then sw becomes, sw=0x20 & 0x20 = 0x20; Since sw= 0x20, the condition is FALSE so the code will exit the while loop. Sets a delay for button debounce sw = 0 LED is ON sw = 1
16
LED is ON sw = 1 NOW the button is released pin B5 is HIGH PORTB=0b00100000 or 0x20 Then sw becomes, sw=0x20 & 0x20 = 0x20; Since sw= 0x20, the condition is TRUE so the code will remain in the while loop. When the button is pressed pin B0 is LOW. PORTB=0 or PORTB=0x0 Then sw becomes, sw=0x00 & 0x20 = 0; Since sw=0, the condition is FALSE so the code will exit the while loop. Sets a delay for button debounce Sets the pin D1 to LOW When pin D1 is LOW the LED stops to light LED is OFF sw = 0
17
Sets a delay for button debounce LED is OFF sw = 0 LED is OFF Now the button is pressed So pin B5 is LOW PORTB=0b00000000 or 0x00 Then sw becomes, sw=0x00 & 0x20 = 0; Since sw= 0, the condition is TRUE so the code will remain in the while loop. When the button is released pin B0 is HIGH. PORTB=0b00100000 or PORTB=0x20 Then sw becomes, sw=0x20 & 0x20 = 0x20 ; Since sw= 0x20, the condition is FALSE so the code will exit the while loop. sw = 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.