1 - Micro C BASIC IO - LED AND SWITCH

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 90

INPUT AND OUTPUT INTERFACING

LED AND SWITCH


WHAT WILL BE COVER?

 What is the PIC microcontroller basic circuit?

 What is the function of TRISx and PORTx registers

 What is LED?

 How to control LED using PIC microcontroller?

 What is SWITCH?

 How to control SWITCH using PIC microcontroller?

ZMI 2023 2
PIC MICROCONTROLLER: BASIC CIRCUIT

 To start up the PIC16F877A, there are 7 pins


that should be connected correctly

Pin No Pin Name


1 MCLR ,Master Clear Reset
11,32 VDD, Positive Supply
12,31 VSS, Ground
13 OSC1, Oscillator pin 1
14 OSC2, Oscillator pin 2

ZMI 2023 3
PIC MICROCONTROLLER: BASIC CIRCUIT

 RESET PIN
 Pin 1 is Reset pin ( MCLR - Master Clear Reset at Pin number 1).

 Its active low pin meaning that if PIC read 0V at MCLR pin, PIC
will enter reset mode and reset the program (program will not
executed).
 This state is remaining until PIC read 5V at this pin.

 POWER SUPPLY PIN


 Likes others electronic component, the supply pin is the most
important.
 The ideal voltage for PIC16F877A is 5V (Direct Current).
 4 pins are connected to power supply
 2 pins for 5V (VDD, pin 11 and 32)
 2 pins for ground (VSS, pin 12 and 31)

ZMI 2023 4
PIC MICROCONTROLLER: BASIC CIRCUIT
 OSCILLATOR PIN
There are 2 pins used for oscillator connection.
OSC1 (pin 13)

OSC2 (pin 14)

They can be connected to the crystal oscillator from various frequency.

Pulse generated from the oscillator some time will have the noise.
To reduce the noise, two capacitors in Pico farad value are needed.
The value of capacitor is depending on the speed of selected oscillator

ZMI 2023 5
PIC MICROCONTROLLER: TRIS AND PORT REGISTER

 PIC16F877A has 33-gpio's grouped into five ports namely PORTA-PORTE as shown in the table.

 Though the gpio pins are grouped into 8-bit ports they can still be configured and accessed individually.

 Each Port is associated with 2 registers for direction configuration (Input/Output) (TRIS) and for Read/Write
(PORT).

PORT Direction Register Number of Pins Alternative Function


PORTA TRISA 6 (RA0-RA5) ADC
PORTB TRISB 8 (RB0-RB7) Interrupts
PORTC TRISC 8 (RC0-RC7) UART,I2C,PWM
PORTD TRISD 8 (RD0-RD7) Parallel Slave Port
PORTE TRISE 3 (RE0-RE2) ADC
ZMI 2023 6
PIC MICROCONTROLLER: TRIS AND PORT REGISTER
 TRISx REGISTER: TRI-State Register/ Data Direction Register
 Used to configure the respective PIN/PORT as output/input
 Before reading or writing the data from the ports, their direction
needs to be set.
 Unless the PORT is configured as output, the data from the registers
will not go to controller pins.
 This register is used to configure the PORT pins as Input or Output.
 Writing 1's to TRISx will make the corresponding PORTx pins as
Input.
 Writing 0's to TRISx will make the corresponding PORTx pins as
Output.
 note: Here 'x' could be A,B,C,D,E so on depending on the number of ports
supported by the controller.

ZMI 2023 7
PIC MICROCONTROLLER: TRIS AND PORT REGISTER

 Example:

 Setting the whole PORT:

 Setting individual PIN:

ZMI 2023 8
PIC MICROCONTROLLER: TRIS AND PORT REGISTER

 PORTx REGISTER: Read/Write Register


 This register is used to read/write the data from/to port pins.
 Writing 1's to PORTx will make the corresponding PORTx pins as
HIGH.
 Similarly writing 0's to PORTx will make the corresponding PORTx
pins as LOW.
 Before reading/writing the data, the port pins should be configured
as Input/Output.
 note: Here 'x' could be A,B,C,D,E so on depending on the number of ports
supported by the controller.

ZMI 2023 9
PIC MICROCONTROLLER: TRIS AND PORT REGISTER

 Example:

 Reading the whole PORT:

 Reading individual PIN:

ZMI 2023 10
PIC MICROCONTROLLER: TRIS AND PORT REGISTER

 Example:

 Writing the whole PORT:

 Writing individual PIN:

ZMI 2023 11
I/O INTERFACING: LIGHT EMITTING DIODE (LED)

 An LED is basically a p-n junction diode which emits light energy when adequate voltage is given.
 LED bulbs are available in different colours.
 Earlier LEDs made use of the infra-red wavelength and such LEDs are used even these days in
remote-control circuits.
 LED bulbs making use of the ultra violet wavelength is also available in the market.
 Light emitting diodes (LED) are basic display units in electronics world.
 It is the most commonly used indicator lights in all the applications.
 LEDs are present in many electronic devices including lamps, digital clocks and incandescent
bulbs.
 A very popular application of LED is a seven segment display system.
 The advantages of LEDs over normal incandescent lights include robustness, faster switching,
lower energy consumption and small size.
ZMI 2023 12
I/O INTERFACING: LIGHT EMITTING DIODE (LED)
 The microcontroller pin that connected to LED must be set as an output before can be used to control the LED
 TRISX = 0; // 0 means output, x any port r pin

 An LED can be connected to a microcontroller output pin in two different modes:


Active Low (current-sinking) mode Active High (current-sourcing) mode
 the anode leg of the LED is connected to the VDD supply  the anode leg of the LED is connected to the
(+5V), and the cathode leg is connected to the microcontroller output port through a current-limiting
microcontroller output port through a current-limiting resistor and the cathode leg is connected to the ground.
resistor.
• The LED is turned ON when
• The LED is turned ON when the output of the
the output of the microcontroller is at logic 1,
microcontroller is at logic 0, so that current flows through
so that current flows through the LED.
the LED.

• Vo is the microcontroller output, typically


ZMI 2023 around 4.85V 13
EXERCISE

1. Exercise 1: Turn on LEDs


 Objective: Objective: Learn how to configure and activate multiple LEDs in active high and active low configuration.
There are two exercises: 1.1 is turn ON and OFF multiple LEDs in active high mode and 1.2 is turn ON and OFF multiple
LEDs in active low mode.

ZMI 2023 14
EXERCISE 1.1 – CIRCUIT CONNECTION

ZMI 2023 15
EXERCISE 1.1 - HARDWARE CONFIGURATION IN PICSIMLAB

 Configuration

 8 LEDs – PORTD
 Mode: Active High
 Colour: All Reds

 Task:
 Turn ON 1 LED
 Turn ON 4 LEDs
 Turn ON 8 LEDs
 All process used individual pin
control and PORT control

ZMI 2023 16
EXERCISE 1.1 -
FLOWCHART

ZMI 2023 17
EXERCISE 1.1 - CODE

ZMI 2023 18
EXERCISE 1.1 - CODE

ZMI 2023 19
EXERCISE 1.1 - CODE

ZMI 2023 20
EXERCISE 1.1 - RESULTS
One LED

Four LEDs

Eight LEDs

ZMI 2023 21
EXERCISE 1.2 – CIRCUIT CONNECTION

ZMI 2023 22
EXERCISE 1.2 - HARDWARE CONFIGURATION IN PICSIMLAB

 Configuration

 8 LEDs – PORTD
 Mode: Active LOW
 Colour: All Reds

 Task:
 Turn ON 1 LED
 Turn ON 4 LEDs
 Turn ON 8 LEDs
 All process used individual pin
control and PORT control

ZMI 2023 23
EXERCISE 1.2 -
FLOWCHART

ZMI 2023 24
EXERCISE 1.2 - CODE

ZMI 2023 25
EXERCISE 1.2 - CODE

ZMI 2023 26
EXERCISE 1.2 - CODE

ZMI 2023 27
EXERCISE 1.2 - RESULTS
One LED

Four LEDs

Eight LEDs

ZMI 2023 28
LED CONTROL: BLINKING LEDS
First Method - Direct
 ON – delay – OFF – delay
 Blinking means to turn ON and turn OFF display for a specific time.  ON – delay – OFF – delay
 ON – delay – OFF – delay
 The complete cycle of ONE blink is given as follows:
 ON – delay – OFF – delay

 The process can be repeated as many as you want by repeating the Second Method – Loop
 Begin
sequence.  ctr = 0
 DO 3 Times
 Example, to blink the display 3 times  ON
 Delay
 Can use either direct method of using loop (for or while)
 OFF
 Direct method – simpler but messy when the number of count value is bigger  Delay
 Increment ctr
 Loop method – effective for any number of count value and easy to modify  ENDDO

ZMI 2023 29
EXERCISE

2. Exercise 2: LED Blinking


 Objective: Learn how to blink LEDs for several time. There are two methods used to blink the LEDs, direct method and
looping method.

ZMI 2023 30
EXERCISE 2 – CIRCUIT CONNECTION

ZMI 2023 31
EXERCISE 2 - HARDWARE CONFIGURATION IN PICSIMLAB

 Configuration

 8 LEDs – PORTD
 Mode: Active High
 Colour: 4 Red (RD0 – RD3)
: 4 Blue (RD4 - RD7)

 Task:
 Blink ALL LEDs 5 times using
DIRECT method
 Blink 4 LEDs Alternate 5 times
using FOR loop method
 Blink LEDs Alternately 5 times
using WHILE loop method

ZMI 2023 32
EXERCISE 2 - FLOWCHART

ZMI 2023 33
EXERCISE 2 - FLOWCHART

ZMI 2023 34
EXERCISE 2 - CODE

ZMI 2023 35
EXERCISE 2 - CODE

ZMI 2023 36
EXERCISE 2 - CODE

ZMI 2023 37
EXERCISE 2 - RESULTS
Task One : Blink All LEDs 5 times

ZMI 2023 38
EXERCISE 2 - RESULTS

Task Two: Blink 4 LEDs Alternate 5 times

ZMI 2023 39
EXERCISE 2 - RESULTS

Task Three: Blink LEDs Alternately 5 times

ZMI 2023 40
EXERCISE

3. Exercise 3: LED Running Light


 Objective: Learn how to create running light effect.

ZMI 2023 41
EXERCISE 3 – CIRCUIT CONNECTION

ZMI 2023 42
EXERCISE 3 - HARDWARE CONFIGURATION IN PICSIMLAB

 Configuration
 8 LEDs – PORTD
 Mode: Active High
 Colour: 4 Red (RD0 – RD3)
: 4 Blue (RD4 - RD7)

 Task:
 LED move from RIGHT to LEFT
using DIRECT method
 LED move from LEFT to RIGHT
using shift right (>>) operator and
LOOP method
 LED move from BOTH direction
using shift right (>>) and shift left
(<<) operators and LOOP method
ZMI 2023 43
EXERCISE 3 - FLOWCHART

ZMI 2023 44
EXERCISE 3 - FLOWCHART

ZMI 2023 45
EXERCISE 3 - CODE

ZMI 2023 46
EXERCISE 3 - CODE

ZMI 2023 47
EXERCISE 3 - CODE

ZMI 2023 48
EXERCISE 3 - RESULTS
Task One

ZMI 2023 49
EXERCISE 3 - RESULTS
Task Two

ZMI 2023 50
EXERCISE 3 - RESULTS
Task Three

ZMI 2023 51
I/O INTERFACING: SWITCHES

 In electrical and electronic system, a switch is a device, which


can make or break an electrical circuit or as a controlling device,
which interrupt the flow of current or direct the flow of current in
another direction.
 Almost all the electrical and electronics systems contain at least one
switch, which is used to make the device ON or OFF.
 In addition, a switch is used to control the circuit operation and user
may able to activate or deactivate the whole or certain parts of the
connected circuit.

ZMI 2023 52
I/O INTERFACING: SWITCHES

 Generally, Switches can be categories as:


1. Mechanical Switches
2. Electrical/Electronic Switches

 Both of these types of switches are widely used in Electrical and Electronics systems.

 Type of switch selection depends upon the system in which they are going to be incorporated.

 Switches can also be categories on many different bases.

 Switches can also be categories on the basis of holding the current state.
1. Latch Switch
 The latch switch holds its state whether ON or OFF until the new commands initiated.

2. Momentary Switch
 Momentary switch holds the state only when the specific command is presented only
ZMI 2023 53
I/O INTERFACING: SWITCHES - MECHANICAL

 Mechanical switch is a switch in which two metal plates touch each other to make a physical contact for the current to
flow and separate from each other to interrupt the flow of current.
 There are many types of Mechanical switches and they are also be categories on the basis of power handling capacity.

 Mechanical Switches can be categories on the basis of their operation:

ZMI 2023 54
I/O INTERFACING: SWITCHES – LATCH AND MOMENTARY

 Push Buttons: - Momentary Switch


 This button is used in many electronics circuits and can handle a small amount of current.
 When a user press the button, its metal plate connects with each other, hence the circuit is
completed.
 When the user removes its finger from the button, contact of the pins detached.

 Toggle switch: - Latch Switch


 Toggle switches are actuated by a lever angled in one or more directions.
 This switch is stable in state and remain in that state unless or until lever is pushed in another
direction.
 Most of all household applications have toggle switch and it can fall into any category as
mentioned before in Mechanical Switch e.g. SPST, DPDT etc.
ZMI 2023 55
I/O INTERFACING: SWITCHES – ELECTRICAL

 Electrical switches, which are faster in response than mechanical switches and can be switched automatically by
an electronic circuit like microcontroller or microprocessor. They can also be categories on the basis of current and
voltage rating like mechanical switches.
 There are the most widely used electronic switches
1. Transistor
2. Mosfets
3. Relays
 The question arises here, why we need electronics switch?
 The answer of the question is that sometimes, it is necessary that circuit, which makes decision also turn OFF or ON certain
devices based on the decision.
 If only mechanical switch is used, then there should be one person present there all the time to make the device ON and OFF
after getting indication message from the circuit.
 To eliminate this problem, electronics switches are used then. They are very much fast and accurate as compared to
mechanical switches.
 Electronic switches are small in size and do not make noise while switching operation and they make sure the stability and
reliability of the system
ZMI 2023 56
I/O INTERFACING: SWITCHES – PUSH BUTTON

 A Push Button (Switch) can be connected to a microcontroller input pin in two different modes:

Active Low (Pull-up) mode


• For the pull-up mode, the switch will be in active low
configuration.
• That is, the state of the pin when the push button not
pressed is HIGH, we need to check for a LOW to check the
switch press event.

Active High (Pull-down) mode


• In pull-down mode, the current flows to the current when the
switch is pressed.
• In open state, the pin is connected to the ground via a
resistor.
• In this mode, the output acts as a active high
ZMI 2023 57
I/O INTERFACING: SWITCHES – PUSH BUTTON

 The microcontroller pin that connected to Switch can be define for easier to be identified
and manipulate in the program
 #define switch_name pin_no
switch_name – the name to call the switch. Example: SW1, PB1
pin_no – the microcontroller pin connected to the switch. Example: RB0, RE1

 Example
 #define SW1 RB0
 #define PB1 RE1

 The microcontroller pin that connected to Switch must be set as an input before can be
used to read the switch
 TRISX = 1; // 1 means input, x any port r pin
ZMI 2023 58
EXERCISE

4. Exercise 4: Manipulating Single Switch


 Objective: Objective: Learn how to configure and activate switch (push button) in active high (Pull Down) and active low
(Pull Up) configuration. There are two exercises: 4.1 is manipulating push button in active high mode and 4.2 is
manipulating push button in active low mode.
5. Exercise 5: Manipulating Multiple Switches
 Objective: Learn how to control multiple switch (Push Button)

ZMI 2023 59
EXERCISE 4.1 – CIRCUIT CONNECTION

ZMI 2023 60
EXERCISE 4.1 - HARDWARE CONFIGURATION IN PICSIMLAB

 Configuration

 1 LED – RD0
 Mode: Active High
 Colour: Red

 1 PUSH BUTTON – RB7


 Mode: Active High

 Task:
 When Push Button Pressed, turn
ON the LED
 When Push Button Released, turn
OFF the LED

ZMI 2023 61
EXERCISE 4.1 - FLOWCHART

ZMI 2023 62
EXERCISE 4.1 - CODE

ZMI 2023 63
EXERCISE 4.1 - CODE

ZMI 2023 64
EXERCISE 4.1 - RESULTS

Push Button Pressed  LED ON

Push Button Released  LED OFF

ZMI 2023 65
EXERCISE 4.2 – CIRCUIT CONNECTION

ZMI 2023 66
EXERCISE 4.2 - HARDWARE CONFIGURATION IN PICSIMLAB

 Configuration

 1 LED – RD0
 Mode: Active High
 Colour: Red

 1 PUSH BUTTON – RB7


 Mode: Active Low

 Task:
 When Push Button Pressed, turn
ON the LED
 When Push Button Released, turn
OFF the LED

ZMI 2023 67
EXERCISE 4.2 - FLOWCHART

ZMI 2023 68
EXERCISE 4.2 - CODE

ZMI 2023 69
EXERCISE 4.2 - CODE

ZMI 2023 70
EXERCISE 4.2 - RESULTS

Push Button Pressed  LED ON

Push Button Released  LED OFF

ZMI 2023 71
EXERCISE 5 – CIRCUIT CONNECTION

ZMI 2023 72
EXERCISE 5 - HARDWARE CONFIGURATION IN PICSIMLAB

 Configuration
 8 LEDs – PORTD
 Mode: Active High
 Colour: Red (RD0 – RD3)
: Blue (RD4 – RD7)

 3 PUSH BUTTONs – RB5 – RB7


 Mode: Active Low

 Task:
 When Push Button 1 (RB5) Pressed,
Blink all LEDs 10 times
 When Push Button 2 (RB6) Pressed,
run running light
 When Push Button 3 (RB7) Pressed,
run running light and then blinking

ZMI 2023 73
EXERCISE 5 -
FLOWCHART

ZMI 2023 74
EXERCISE 5 - CODE

ZMI 2023 75
EXERCISE 5 - CODE

ZMI 2023 76
EXERCISE 5 - CODE

ZMI 2023 77
EXERCISE 5 - RESULTS
Task 1: SW1 Pressed  Blink All LEDs 10 times

ZMI 2023 78
EXERCISE 5 - RESULTS
Task 2: SW2 Pressed  Running Light

ZMI 2023 79
EXERCISE 5 - RESULTS
Task 3: SW3 Pressed  Running Light and Blinking: Running

ZMI 2023 80
EXERCISE 5 - RESULTS

Task 3: SW3 Pressed  Running Light and Blinking: Blinking

ZMI 2023 81
I/O INTERFACING: SWITCHES – BOUNCING EFFECT

 What is switch bounce?


 When you push a button, press a push button switch or flip a toggle switch, two metal parts come together.
 For the user, it might seem that the contact is made instantly. That is not quite correct.
 Inside the switch there are moving parts.
 When you push the switch, it initially makes contact with the other metal part, but just in a brief split of a
microsecond.
 Then it makes contact a little longer, and then again a little longer.
 In the end the switch is fully closed. The switch is bouncing between in-contact, and not in-contact.
 "When the switch is closed, the two contacts actually separate and reconnect, typically 10 to 100 times
over a period of about 1ms." ("The Art of electronics", Horowitz & Hill, Second edition, pg 506.)
 Usually, the hardware works faster than the bouncing, which results in that the hardware thinks you are
pressing the switch several times. The hardware is often an integrated circuit.
ZMI 2023 82
I/O INTERFACING: SWITCHES – BOUNCING EFFECT

 The following screenshots illustrates a typical switch bounce, without any sort of bounce control.

ZMI 2023 83
I/O INTERFACING: SWITCHES – BOUNCING EFFECT

Active High (Pull-down) mode Active Low (Pull-up) mode

Bouncing Effect
ZMI 2023 84
I/O INTERFACING: SWITCHES – BOUNCING EFFECT

 We can filter the bounces in two ways: Hardware and Software

 Hardware solution by using SR Latch  Hardware solution by using R-C circuit


 The basic R-C circuit used for debouncing is shown below.
The circuit uses two Resistors, Capacitor, Schmidt trigger
hex inverter (eg : 7414)

ZMI 2023 85
I/O INTERFACING: SWITCHES – BOUNCING EFFECT

 Hardware solution by using Debouncer IC


 They are only a few of them and rarely used.
 MAX6816, MAX 6817, MAX 6818
 MC 14490

ZMI 2023 86
I/O INTERFACING: SWITCHES – BOUNCING EFFECT

 Software solution by introducing delay (not suggested), use a counter method, use shift
register method or any method that able to check the switch stability.
 Software Delay Method (not highly recommended)
 Example, suppose that all bouncing for the relevant switch should cease following a one-millisecond
debounce delay.
 Then, to debounce the switch, one could simply create a software delay of one millisecond.
 After such a delay, the program would then check the appropriate pin level and perform any necessary
function(s).
 Overall, although software delays have the potential to be very precise, they prevent a microprocessor
from executing other instructions, and ultimately cause CPU time to be wasted.
 Software delays are also extremely non-modular, as they cannot easily be used at other processor clock
speeds.
ZMI 2023 87
I/O INTERFACING: SWITCHES – BOUNCING EFFECT

 Counter Method
 This approach uses a counter to time how long the switch signal has been low.
 If the signal has been low continuously for a set amount of time, then it is considered pressed and
stable.
 Example:

ZMI 2023 88
I/O INTERFACING: SWITCHES – BOUNCING EFFECT

 Shift Register Method


 Similar to counter, but uses a shift register instead of a counter.
 The algorithm assumes an unsigned 8bit register value, such as that found it 8-bit microcontrollers.
 Example:

ZMI 2023 89
REFERENCES

I. https://electrosome.com/switch-pic-microcontroller-mplab-xc8/
II. https://electrosome.com/switch-debouncing/
III. https://www.newbiehack.com/ButtonorSwitchDebounceinSoftware.aspx
IV. http://www.labbookpages.co.uk/electronics/debounce.html

V. https://circuitdigest.com/electronic-circuits/what-is-switch-bouncing-and-how-to-prevent-it-using-debounce-circuit
VI. https://web.engr.oregonstate.edu/~traylor/ece473/lectures/debounce.pdf

VII. https://www.thegeekpub.com/246471/debouncing-a-switch-in-hardware-or-
software/#:~:text=Debouncing%20in%20Hardware,operating%20voltage%2C%20current%2C%20etc.

ZMI 2023 113

You might also like