17
Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

Embed Size (px)

Citation preview

Page 1: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

Engineering 1040: Mechanisms & Electric Circuits

Winter 2015

Interfacing Light-Emitting Diodes (LEDs) &

Push Buttons to Microcontrollers

Page 2: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

Light-Emitting Diodes (1)

Source: Wikipedia (http://en.wikipedia.org/wiki/Light-emitting_diode)

Page 3: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

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.

Page 4: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

Technical Specifications of the LED

Page 5: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

Forward Current vs Forward Voltage of the LED

Page 6: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

Methods of interfacing LEDs to a microcontroller

Method 1 Method 2

What is the value of R?

Page 7: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

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 ClosedSwitch

Page 8: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

Interfacing Push Buttons

“Normally Open“ Push Button “Normally Closed" Push Button

Page 9: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

Switch Bounce

Page 10: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

ResetPin 1

(MCLR)

BootloadPin 33 (B5)

LED 2Pin D1

LED 3Pin D0

Page 11: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers
Page 12: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

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

Page 13: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

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 B 0x10 Result

2016 0 0

2016 2016 2016

0 2016 0

0 0 0

Page 14: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

• Before the button is pressed pin B5 is HIGHPORTB=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

Page 15: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

• 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 = 0LED is ON

LED is ON

sw = 1

Page 16: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

LED is ON

sw = 1

• NOW the button is released pin B5 is HIGHPORTB=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 OFFsw = 0

Page 17: Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers

• 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