ELE 575 Chapter 2 Summer 2023-2024.pdf

Full Transcript

7/16/2024 ELE 575: Applications of Microcontrollers PIC Microcontrollers Summer 2023\2024 ELE 575 Dr. Shadi Alboon 1 Overview  What are...

7/16/2024 ELE 575: Applications of Microcontrollers PIC Microcontrollers Summer 2023\2024 ELE 575 Dr. Shadi Alboon 1 Overview  What are PIC’s  Applications  Using a PIC  Architecture  PIC Families  PIC Oscillator  Additional Components ELE 575 Dr. Shadi Alboon 2 1 7/16/2024 What are PIC’s?  Peripheral Interface Controller  Developed by Microchip  Microcontrollers  NOT Microprocessors  Microprocessor system with number of components (EEPROM, RAM, I/O Support) ELE 575 Dr. Shadi Alboon 3 Microchip and PIC MCU  PIC was originally a design of General Instruments (now Motorola Connected Home Solutions)  Microchip (formed in 1989) developed their first 8-bit Harvard architecture MCU – PIC16C5xx  All MCUs are low-cost, self-contained, Harvard arch, RISC, single accumulator, with fixed reset and interrupt vectors ELE 575 Dr. Shadi Alboon 4 2 7/16/2024 Why Use PIC?  Variety of choices (8-bit to 32-bit)  Affordable (8-bit: $0.41, 32-bit: $6.00)  Low Power  Reasonable Size  Convenient Packaging  Through Hole (Dip)  Surface Mount (QFN/SPDIP)  Resources and References ELE 575 Dr. Shadi Alboon 5 Common Features of PIC Architecture Harvard architecture: Separate paths for the instructions and data RISC instruction set: Small number of instructions Flash program ROM: Easily re-programmed Single Working register: Reduces the cumber of complexity of instructions Multiple Interrupt sources: Variety of interrupts are available at one address Hardware timers: 8- and 16-bit timers are available Sleep mode: Power saving Serial in-circuit programming: Common program downloading system ELE 575 Dr. Shadi Alboon 6 3 7/16/2024 Speeds All PICs require oscillators to execute instructions: – Internal* (low speeds, up to 8 MHz) – External (high speeds, up to 40 MHz) Instructions are executed at least at ¼ oscillator speed (4 clocks/instruction) (*Note: not all PICs have internal oscillators) ELE 575 Dr. Shadi Alboon 7 A/D converters and C/C modules All PICs have between 0 and 16 A/D converters with 8/10-bit resolution 8-16 bit Timers/Counters Comparator Modules (0-2) ELE 575 Dr. Shadi Alboon 8 4 7/16/2024 Example: PIC16F877A 5/6 Programming pins 8 A/D channels 2 Oscillator Inputs 2 RS-232 inputs 33 I/O ports ELE 575 Dr. Shadi Alboon 9 Using A PIC  Tools:  MPLab IDE  C and assembly environment  Debugger included  C compiler included  Free  Programmers  Variable cost  Ex. ICDE2, PICKit2, many more…  Software Development Tools  Libraries by Microchip ELE 575 Dr. Shadi Alboon 10 5 7/16/2024 PIC Families  Divided into families  8-bit: 12F, 16F, 18F  16-bit: 24F, ds33F  32-bit: 32F  Minor differences  Power Consumption  Speed  Package size  Memory Capacity ELE 575 Dr. Shadi Alboon 11 PIC instruction set  The PIC instruction set has a small number of simple (RISC) instructions: PIC16 series: 35 instructions coded into 14 bits PIC 18 series: 59 instructions coded into 16 bits PIC 24 series: 71 instructions coded into 24 bits  Most instructions are executed in one instruction cycle which corresponds to 4 clock cycles  Thus a PIC operating at 40 MHz clock frequency will have an instruction rate of 10 MIPS. ELE 575 Dr. Shadi Alboon 12 6 7/16/2024 PIC16F (8-bit) ELE 575 Dr. Shadi Alboon 13 PIC Microcontroller In order to enable the microcontroller to operate properly it is necessary to provide: Power Supply; Reset Signal; and Clock Signal. ELE 575 Dr. Shadi Alboon 14 7 7/16/2024 PIC Microcontroller ELE 575 Dr. Shadi Alboon 15 PIC Microcontroller  POWER SUPPLY PIC16F887 can operate at different supply voltages, A 5V DC is the most suitable. The circuit, shown on the previous page, uses a cheap integrated three-terminal positive regulator LM7805 Provides high-quality voltage stability and quite enough current to enable the microcontroller and peripheral electronics to operate normally (enough here means 1A).  RESET SIGNAL In order that the microcontroller can operate properly, a logic one (VCC) must be applied on the reset pin. (active LOW) However, it is almost always provided because it enables the microcontroller to return safely to normal operating conditions if something goes wrong. By pushing this button, 0V is brought to the pin, the microcontroller is reset and the program execution starts from the beginning. A10K resistor is used to allow 0V to be applied to the MCLR pin, via the push button, without shorting the 5VDC rail to earth. ELE 575 Dr. Shadi Alboon 16 8 7/16/2024 PIC Microcontroller CLOCK SIGNAL Determine the operating speed of the microcontroller Even though the microcontroller has a built-in oscillator,. Depending on elements in use as well as their frequencies, the oscillator can be run in four different modes: LP - Low Power Crystal; XT - Crystal / Resonator; HS - High speed Crystal / Resonator; and RC - Resistor / Capacitor. ELE 575 Dr. Shadi Alboon 17 MCU Elements – Oscillator Some MCUs have on-chip oscillator that supplies synchronized clock pulses Ensures harmonic and synchronous operation of all circuits of the microcontroller Usually configured to use quartz crystal or ceramic resonator for frequency stabilization Instructions are not executed at the rate imposed by the oscillator itself, but several times slower (WHY?) (Because each instruction is executed in several steps) In some MCU, the same number of cycles is needed to execute any instruction, while in others, the execution time is not the same for all instructions ELE 575 Dr. Shadi Alboon 18 9 7/16/2024 PIC Oscillator The function of an oscillator circuit is to provide an accurate and stable periodic clock signal to a microcontroller. The frequency of this clock signal can range from a few kilohertz to tens of megahertz. Determines how quickly the microcontroller executes its instructions. Most microcontrollers include a clock driver circuit which is designed to drive a quartz crystal into oscillation. The clock driver circuitry built into the PICmicro family is very flexible and allows for four different clocking options: Clock signal supplied from another oscillator, R-C clock (based on a resistor-capacitor charging time constant), A ceramic resonator, A crystal oscillator. ELE 575 Dr. Shadi Alboon 19 Clock Oscillator Two types of oscillator circuit in common use in microcontrollers Resistor-Capacitor, Crystal Oscillator Depends on the piezo-electric properties of quartz crystal Any mechanical distortion of the material causes a voltage to be produced across opposite sides of it; similarly, if a voltage is applied to the material, a mechanical distortion results Crystal vibration occurs at a fixed and remarkably stable frequency – a great advantage ELE 575 Dr. Shadi Alboon 20 10 7/16/2024 Clock Oscillator and Instruction Cycle Some popular clock frequencies for PIC16 series For the fastest clock frequency, 20 MHz (the instruction cycle freq. is 5 MHz => 5,000,000 instructions per sec; period of 200 ns) The slightly cheaper version of the controller, the 16F84-04, with maximum clock frequency of 4 MHz (1 us) - Simple timing applications, using software delay loops and the counter/timer A popular clock frequency is 32.768 kHz (122.07 us) - The result is very low power, but strictly no high-speed calculations! - E.g., digital clock, wristwatches (WHY used?) ELE 575 Dr. Shadi Alboon 21 Quartz Crystal When the quartz crystal is used for frequency stabilization, a built-in oscillator operates at a precise frequency which is not affected by changes in temperature and power supply voltage. This frequency is usually labeled on the crystal casing. ELE 575 Dr. Shadi Alboon 22 11 7/16/2024 Ceramic Resonator Ceramic resonator is cheaper, but very similar to quartz by its function and the way of operation. This is why schematics illustrating their connection to the microcontroller are identical. However, the capacitor value is slightly different due to different electric features. Refer to the table below. ELE 575 Dr. Shadi Alboon 23 External Oscillator If it is required to synchronize the operation of several microcontrollers or if for some reason it is not possible to use any of the previous schematics, a clock signal may be generated by an external oscillator. ELE 575 Dr. Shadi Alboon 24 12 7/16/2024 PIC16FXXX Clock Oscillator Four different oscillator modes, allowing implementation of RC, crystal or ceramic oscillators, and external clock source Selected by user  RC – resistor–capacitor An external R-C must be connected to pin 16. This is the lowest cost way of getting an oscillator, but should not be used when any timing accuracy is required.  LP – low power This mode is intended for low-frequency crystal applications (up to 200kHz) and gives the lowest power consumption possible.  XT – crystal This is the standard crystal configuration (1MHz – 4MHz)  HS – high speed This is a higher drive version of the XT configuration (4MHz – 20MHz). It recognizes that higher frequency crystals, and ceramic resonators in general, require a higher drive current. It leads to the highest current consumption of all the oscillator modes. ELE 575 Dr. Shadi Alboon 25 Ports A port is the microcontroller’s interface into the real world. All the data manipulation and operations that are done within the microcontroller ultimately reveals as output signals through the ports. Let us consider an air conditioning system built around a microcontroller. The temperature sensors measure the room temperature and gives it as input to the microcontroller through the ports. The data coming in through the ports will be stored in the microcontroller. The stored data will be compared against a set temperature. If the external temperature reported by the sensor is higher that the threshold, the microcontroller switches on the air conditioning mechanism. This is done by switching on the corresponding port pin. ELE 575 Dr. Shadi Alboon 26 13 7/16/2024 PIC 16F877A Ports Physically, ports are some of the pins that are seen in the IC package. There are 6 ports for PIC 16f877a. They are named as PORTA, PORTB, PORTC, PORTD and PORTE. Ports B, C and D are 8 bit wide (8 pins each). While PORTA is 5bitand PORTE is 3 bit wide. The individual port pins are named 0 through n. for eg. 1st pin of PORTA will be RA0. Note that the port pins can also be individually configured, i.e. any combination of input and output configuration is possible in any of the ports. ELE 575 Dr. Shadi Alboon 27 PIC 16F877A Ports ELE 575 Dr. Shadi Alboon 28 14 7/16/2024 PIC 16F877A Ports When designing a device, select a port through which the microcontroller will communicate to the peripheral environment. If you using only digital inputs/outputs, select any port you want. If you intend using some of the analog inputs, select the appropriate ports supporting such pins configuration (which port ?); Each port pin may be configured as either input or output. If you use switches and push-buttons as input signal source, connect them to Port B pins because they have pull-up resistors. It is usually necessary to react as soon as input pins change their logic state. It is far simpler to connect such inputs to the PORTB pins and enable the interrupt on every voltage change. ELE 575 Dr. Shadi Alboon 29 ADDITIONAL COMPONENTS RELAYS A relay is an electrical switch that opens and closes under the control of another electrical circuit. It is therefore connected to output pins of the microcontroller and used to turn on/off high-power devices such as motors, transformers, heaters, bulbs, etc. These devices are almost always placed away from the boards sensitive components. A relay uses a small amount of current to control a large amount of current flow. ELE 575 Dr. Shadi Alboon 30 15 7/16/2024 ADDITIONAL COMPONENTS RELAYS  Relays are amazingly simple devices. There are four parts in every relay: Electromagnet Armature that can be attracted by the electromagnet Spring Set of electrical contacts ELE 575 Dr. Shadi Alboon 31 ADDITIONAL COMPONENTS RELAYS There are various types of relays, but all of them operate in the same way. When a current flows through the coil, the relay is operated by an electromagnet to open or close one or many sets of contacts. There is no galvanic connection (electrical contact) between input and output circuits. Relays usually demand both higher voltage and current to start operation but there are also miniature ones that can be activated by a low current directly obtained from a microcontroller pin. ELE 575 Dr. Shadi Alboon 32 16 7/16/2024 ADDITIONAL COMPONENTS RELAYS To power a relay or configure it with an electronic circuit, a small output circuit is generally incorporated and is known as the relay driver circuit. As shown in the diagram, the section basically consists of a transistor T1, resistor R1 and a flyback diode D1 connected across the relay coil. Resistor R1 is used to bias the transistor and this biasing voltage is in fact the triggering voltage, which is generally received from a source such as the PIC. ELE 575 Dr. Shadi Alboon 33 ADDITIONAL COMPONENTS RELAYS As soon as the transistor receives the trigger voltage, it instantly conducts and activates the relay. (what is the sate of the transistor ??) This happens because the end of the relay which is connected to the transistor is pulled to the ground potential, so that the entire supply voltage passed through the coil to energize it. When the transistor is switched OFF. The relay coil instantly kicks a dangerous EMF back into the transistor. This may cause permanent damage to the transistor unless and until some precautionary measure is adopted. Diode D1 performs the important function of neutralizing this EMF by short circuiting it and avoiding its passage through the transistor. ELE 575 Dr. Shadi Alboon 34 17 7/16/2024 ADDITIONAL COMPONENTS RELAYS Calculating the value of a resistor R1= ((Ub - 0.8) × Hfe) ÷ Relay Current, Ub = source voltage to R1, 0.8 = minimum transistor biasing voltage (Saturation), Hfe = forward current gain of T1 used (150 nominal) Relay current may be calculated through the following given formula: Relay I = Resistance of the relay coil ÷ Supply Voltage, ELE 575 Dr. Shadi Alboon 35 ADDITIONAL COMPONENTS RELAYS ELE 575 Dr. Shadi Alboon 36 18 7/16/2024 ADDITIONAL COMPONENTS RELAYS In order to prevent the appearance of high voltage self-induction caused by a sudden stop of current flow through the coil, an inverted polarized diode is connected in parallel to the coil. The purpose of this diode is to "cut off" the voltage peak. ELE 575 Dr. Shadi Alboon 37 ADDITIONAL COMPONENTS LED DIODES You probably know all you need to know about LED diodes, but we should also think of the younger generations...How to destroy a LED?! Well...Very simple.  Quick burning Like any other diode, LEDs have two ends an anode and a cathode. Connect it properly to a power supply voltage. The diode will happily emit light. Turn it upside down and apply the same power supply voltage (even for a moment). It will not emit light- NEVER AGAIN! ELE 575 Dr. Shadi Alboon 38 19 7/16/2024 ADDITIONAL COMPONENTS LED DIODES  Slow burning There is a nominal, i.e. maximum current determined for every LED which should not be exceeded. If it happens, the diode will emit more intensive light, but not for a long time!  Something to remember Similar to the previous example, all you need to do is to insert a current limiting resistor. (How to calculate the resistor values range ?!) ELE 575 Dr. Shadi Alboon 39 ADDITIONAL COMPONENTS LED DISPLAY (7-Segemnts) Basically, LED display is nothing more than several LEDs molded in a plastic case. There are many types of displays composed of several dozens of built in diodes which can display different symbols. The most commonly used is so called 7-segment display. It is composed of 8 LEDs- 7 segments are arranged as a rectangle for symbol displaying and there is an additional segment for decimal point (dp) displaying. ELE 575 Dr. Shadi Alboon 40 20 7/16/2024 ADDITIONAL COMPONENTS LED DISPLAY (7-Segemnts) In order to simplify connection, anodes or cathodes of all diodes are connected to the common pin so that there are common anode displays and common cathode displays, respectively. Segments are marked with the letters from a to g, plus dp, as shown in figure below. On connecting, each diode is treated separately, which means that each must have its own current limiting resistor. ELE 575 Dr. Shadi Alboon 41 ADDITIONAL COMPONENTS LED DISPLAY (7-Segemnts) If a microcontroller port is connected to the display in such a way that bit 0 activates segment ‘a’, bit 1 activates segment ‘b’, bit 2 segment ‘c’ etc., the table below shows the mask digits. Digits to Display Display Segments dp a b c d e f g 0 0 1 1 1 1 1 1 0 1 0 0 1 1 0 0 0 0 2 0 1 1 0 1 1 0 1 3 0 1 1 1 1 0 0 1 4 0 0 1 1 0 0 1 1 5 0 1 0 1 1 0 1 1 6 0 1 0 1 1 1 1 1 7 0 1 1 1 0 0 0 0 8 0 1 1 1 1 1 1 1 In the event that common anode displays are used, all ones contained in the previous table should be replaced by zeros and vice versa. ELE 575 Dr. Shadi Alboon 42 21 7/16/2024 ADDITIONAL COMPONENTS OPTO-COUPLERS An opto-coupler is a device commonly used to galvanically separate microcontroller electronics from any potentially dangerous current or voltage in its surroundings. Opto-couplers usually have one, two or four light sources (LED diodes) on their input while on their output, opposite to diodes, there is the same number of elements sensitive to light (phototransistors, photo-thyristors or photo-triacs). The point is that an opto-coupler uses a short optical transmission path to transfer a signal between elements of circuit, while keeping them electrically isolated. This isolation makes sense only if diodes and photo-sensitive elements are separately powered. In this way, the microcontroller and expensive additional electronics are completely protected from high voltage and noises which are the most common cause of destroying, damaging or unstable operation of electronic devices in practice. The most frequently used opto-couplers are those with phototransistors on their outputs. ELE 575 Dr. Shadi Alboon 43 ADDITIONAL COMPONENTS OPTO-COUPLERS The R/C network represented by the broken line in the figure above denotes optional connection which lessens the effects of noises by eliminating very short pulses. ELE 575 Dr. Shadi Alboon 44 22 7/16/2024 ADDITIONAL COMPONENTS LCD DISPLAY This component is specifically manufactured to be used with microcontrollers, which means that it cannot be activated by standard IC circuits. It is used for displaying different messages on a miniature liquid crystal display. The model described here is for its low price and great capabilities most frequently used in practice. It is based on the HD44780 microcontroller (Hitachi) and can display messages in two lines with 16 characters each. It displays all the letters of alphabet, Greek letters, punctuation marks, mathematical symbols etc. In addition, it is possible to display symbols made up by the user. Other useful features include automatic message shift (left and right), cursor appearance, LED backlight etc. ELE 575 Dr. Shadi Alboon 45 ADDITIONAL COMPONENTS LCD DISPLAY  LCD Connecting Depending on how many lines are used for connecting the LCD to the microcontroller, there are 8-bit and 4-bit LCD modes. The appropriate mode is selected at the beginning of the operation in this process called "initialization". 8-bit LCD mode uses outputs D0-D7 to transfer data The main purpose of 4-bit LED mode is to save valuable I/O pins. Only 4 higher bits (D4-D7) are used for communication. Each piece of data is sent to the LCD in two steps- four higher bits are sent first and four lower bits are sent afterwards. ELE 575 Dr. Shadi Alboon 46 23 7/16/2024 ELE 575 Dr. Shadi Alboon 47 ADDITIONAL COMPONENTS LCD DISPLAY ELE 575 Dr. Shadi Alboon 48 24 7/16/2024 ADDITIONAL COMPONENTS LCD DISPLAY ELE 575 Dr. Shadi Alboon 49 ADDITIONAL COMPONENTS KEYPAD  4x4 Matrix Membrane Keypad This 16-button keypad provides a useful human interface component for microcontroller projects. Convenient adhesive backing provides a simple way to mount the keypad in a variety of applications.  Features  Ultra-thin design.  Adhesive backing.  Excellent price/performance ratio.  Easy interface to any microcontroller. ELE 575 Dr. Shadi Alboon 50 25 7/16/2024 ADDITIONAL COMPONENTS KEYPAD  How it Works Matrix keypads use a combination of four rows and four columns to provide button states to the host device, typically a microcontroller. Underneath each key is a pushbutton, with one end connected to one row, and the other end connected to one column. In order for the microcontroller to determine which button is pressed, it first needs to pull each of the four columns (pins 1-4) either low or high one at a time, and then poll the states of the four rows (pins 5-8). Depending on the states of the columns, the microcontroller can tell which button is pressed. ELE 575 Dr. Shadi Alboon 51 Interface the PIC18 with an RS232 connector ELE 575 Dr. Shadi Alboon 52 26 7/16/2024 Introduction Computers transfer data in two ways: Parallel and Serial. Parallel: Eight or more data lines, few feet only, short time Serial: Single data line, long distance The PIC has serial communication capability built into it. ELE 575 Dr. Shadi Alboon 53 Basics of Serial Communication The byte of data must be converted to serial bits using a parallel-in-serial-out shift register Serial versus Parallel Data Transfer ELE 575 Dr. Shadi Alboon 54 27

Use Quizgecko on...
Browser
Browser