Professional Documents
Culture Documents
1 - Micro C BASIC IO - LED AND SWITCH
1 - Micro C BASIC IO - LED AND SWITCH
1 - Micro C BASIC IO - LED AND SWITCH
What is LED?
What is SWITCH?
ZMI 2023 2
PIC MICROCONTROLLER: BASIC CIRCUIT
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.
ZMI 2023 4
PIC MICROCONTROLLER: BASIC CIRCUIT
OSCILLATOR PIN
There are 2 pins used for oscillator connection.
OSC1 (pin 13)
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).
ZMI 2023 7
PIC MICROCONTROLLER: TRIS AND PORT REGISTER
Example:
ZMI 2023 8
PIC MICROCONTROLLER: TRIS AND PORT REGISTER
ZMI 2023 9
PIC MICROCONTROLLER: TRIS AND PORT REGISTER
Example:
ZMI 2023 10
PIC MICROCONTROLLER: TRIS AND PORT REGISTER
Example:
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
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
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
ZMI 2023 39
EXERCISE 2 - RESULTS
ZMI 2023 40
EXERCISE
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
ZMI 2023 52
I/O INTERFACING: 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 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.
ZMI 2023 54
I/O INTERFACING: SWITCHES – LATCH AND MOMENTARY
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:
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
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
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
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
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
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)
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
ZMI 2023 81
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
Bouncing Effect
ZMI 2023 84
I/O INTERFACING: SWITCHES – BOUNCING EFFECT
ZMI 2023 85
I/O INTERFACING: SWITCHES – BOUNCING EFFECT
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
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.