Embedded Systems Oscillator and I/O Hardware Eng. Anis Nazer First Semester 2016-2017
Oscillator configurations Three possible configurations for Oscillator (a) using a crystal oscillator (b) using an RC circuit (c) using an external clock
Oscillator configurations (a) using a crystal oscillator: OSC1 and OSC2 are used to generate the clock signal
Oscillator configurations (b) using an RC circuit: OSC1 is used to generate the clock signal OSC2 can be used to drive other circuit components with ¼ the frequency of the clock
Oscillator configurations (c) using an external clock
Logic Values In a digital world, two values: Logic 1 Logic 0 In electrical terms: Logic 1: pin connected to VDD, i.e. power supply Logic 0: pin is connected to ground, i.e. 0 Volts
Input If the input is connected to Vdd, the PIC will treat it as a logic 1 If the input is connected to ground, the PIC will treat it as a logic 0 PIC PIC Logic 1 Logic 0
Input electrical characteristics Some definitions: VIH : minimum acceptable voltage when input is High VIL: maximum acceptable voltage when input is Low IIH: current when input is High IIL: current when input is Low Values for such parameters are found in the Data Sheet To be safe current should not exceed IIH or IIL for an input
Input voltage range V DD VIH } Logic 1 VIL 0 Volts } Logic 0
Output If an output is logic 1, it will be internally connected to power supply through a pull-up pull-up can be a resistor or a transistor PIC pull up Logic 1
Output If an output is logic 0, it will be internally connected to ground through a pull-down pull-down can be a resistor or a transistor PIC pull down Logic 0
Output electrical characteristics Some more definitions: VOH: voltage when the output pin is at Logic 1 VOL: voltage when the output pin is at Logic 0 IOH: current when the output pin is at logic 1 IOL: current when the output pin is at logic 0 The load at an output pin should be chosen such that IOL or IOH is not exceeded
Voltage Ranges, Input/Output V DD } Logic 1 V DD VOH } Logic 1 VIH VIL 0 Volts } Logic 0 VOL 0 Volts } Logic 0
LED LED: light emitting diode It has: Anode (positive) Cathode (negative) For the LED to be ON, the anode must have a higher voltage than the cathode by a certain amount VD
Output connecting an LED Two ways to connect an LED Current source: the current flows out from the PIC logic 1 LED is ON logic 0 LED is OFF PIC LED
Output connecting an LED Current sink: the current flows into the PIC logic 0 LED is ON logic 1 LED is OFF PIC LED
PIC with LED What should be the value of R Example: Calculate the value of R if the LED has a threshold voltage VD = 2.0 V and requires 20mA to light. a) The led is to be connected in the current source mode and VOH is 4.5V R = ( VOH VD ) / I = ( 4.5 2.0 ) / 20m = 125 Ohm
PIC with LED Example: Calculate the value of R if the LED has a threshold voltage VD = 2.0 V and requires 20mA to light. b) The led is to be connected in the current sink mode and VOL is 0.2V R = (VDD VD VOL) / I =( 5 2 0.2 ) / 20m= 140 Ohm
PIC output current In the PIC, IOL or I OH should not exceed 25mA for each output pin. What to do if you need more than 25mA? for example, if you connect 3 LEDs to the same output. You can connect a transistor at the output, and draw the current from the power supply
Transistor Many types: Bi-polar: NPN, PNP MOSFET: NMOS, PMOS others types
Bipolar transistor Collector Emitter Base Base Emitter Collector NPN PNP
MOS Drain Source Gate Gate NMOS Source Drain PMOS
Transistor as switch Bipolar: ON: Collector and Emitter are short circuit OFF: Collector and Emitter are open circuit Collector Collector Collector Collector Logic 1 ON Logic 0 OFF Emitter Emitter Emitter Emitter Emitter Emitter Emitter Emitter Logic 1 OFF Logic 0 ON Collector Collector Collector Collector
Transistor as switch MOS: ON: Source and Drain are short circuit OFF: Source and Drain are open circuit Drain Drain Drain Drain Logic 1 ON Logic 0 OFF Source Source Source Source Source Source Source Source Logic 1 OFF Logic 0 ON Drain Drain Drain Drain
Connecting an LED VDD PIC LED PIC VDD LED
Connecting an LED The transistor acts as an electrically controlled switch The output of the PIC controls the switch PIC
Connecting a switch to an Input Two modes to connect a switch/push button: switch gives a Logic 1 when pushed switch gives a Logic 0 when pushed In both cases, in all switch states, the input should be connected to Vdd or ground The input pin should never be left floating PIC PIC
Switch debounce All switches/buttons suffer from a bounce problem The switch bounces between states before resting in the final position. Bounce period is usually less than 10ms
Switch debounce Since the microcontroller is fast, it will interpret the oscillations as multiple transitions multiple presses on the switch How to deal with this? Hardware: use a capacitor to filter out the high frequency oscillations Software: use a small delay after detecting the first transition
PORTA Hardware RA0, RA1, RA2, RA3 If pin is output: TRIS = 0 0 NMOS on PMOS off 1 NMOS off PMOS on If pin is input, TRIS = 1 NMOS and PMOS are OFF logic value at pin is transferred to the data bus inside the PIC PIC PORTA TRISA PORTA TRISA GND To data bus
PORTA Hardware RA4 Is an open drain output Can serve as a clock for TMR0 When used as an input, RA4 has a Schmitt trigger buffer PIC PORTA TRISA GND To data bus or TMR0 clock
Open drain output No internal pull up You have to provide an external pull up resistor or transistor Why? you can connect the output to a power supply different than VDD, for example to drive a motor. PIC RA4 V+
Schmitt trigger buffer Used to clean the input signal The buffer has two thresholds high threshold low threshold output is low if input is lower than the low threshold output is high if input is higher than the high threshold
Schmitt trigger Input output characteristics V out High output Low output Low threshold High threshold V in
Example What is the output of a buffer that has a single threshold Voltage threshold time
Example What is the output of a buffer that has a single threshold Voltage threshold time
Example What is the output of a Schmitt trigger buffer Voltage high threshold low threshold time
Example What is the output of a Schmitt trigger buffer Voltage high threshold low threshold time
PORTB Hardware RB0 serves also as an interrupt source through a Schmitt trigger buffer RB4 to RB7, will generate an interrupt if a change occurs at any of the inputs change is detected using an XOR gate XOR gate compares the previous value and the current value, if different the XOR output is logic 1
PORTB Hardware If PORTB is configured as input. All pins of PORTB have an internal pull-up The internal pull-up can be enabled through the OPTION_REG register
PORTB Hardware In this case you can connect a switch to the input without the need for an external resistor Assume RB4 is configured as an input, and the weak pull-up is enabled, a switch can be connected as shown PIC RB4
PORTB Hardware RB0 serves also as an interrupt source through a Schmitt trigger buffer RB4 to RB7, will generate an interrupt if the value changes at any of the inputs change is detected using an XOR gate XOR gate compares the previous value of the pin and the current value, if different the XOR output is logic 1