Microcontroller Programming - WordPress

2y ago
74 Views
8 Downloads
4.76 MB
821 Pages
Last View : 20d ago
Last Download : 3m ago
Upload by : Genevieve Webb
Transcription

MicrocontrollerProgrammingThe Microchip PIC 7189 C000a.indd 110/30/06 3:00:15 PM

7189 C000a.indd 210/30/06 3:00:15 PM

MicrocontrollerProgrammingThe Microchip PIC Julio SanchezMinnesota State University, MankatoMaria P. CantonSouth Central College, North Mankato, MinnesotaBoca Raton London New YorkCRC Press is an imprint of theTaylor & Francis Group, an informa business7189 C000a.indd 310/30/06 3:00:15 PM

CRC PressTaylor & Francis Group6000 Broken Sound Parkway NW, Suite 300Boca Raton, FL 33487‑2742 2007 by Taylor & Francis Group, LLCCRC Press is an imprint of Taylor & Francis Group, an Informa businessNo claim to original U.S. Government worksPrinted in the United States of America on acid‑free paper10 9 8 7 6 5 4 3 2 1International Standard Book Number‑10: 0‑8493‑7189‑9 (Hardcover)International Standard Book Number‑13: 978‑0‑8493‑7189‑9 (Hardcover)This book contains information obtained from authentic and highly regarded sources. Reprinted material is quotedwith permission, and sources are indicated. A wide variety of references are listed. Reasonable efforts have been made topublish reliable data and information, but the author and the publisher cannot assume responsibility for the validity ofall materials or for the consequences of their use.No part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, orother means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any informa‑tion storage or retrieval system, without written permission from the publishers.For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC) 222 Rosewood Drive, Danvers, MA 01923,978‑750‑8400. CCC is a not‑for‑profit organization that provides licenses and registration for a variety of users. For orga‑nizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only foridentification and explanation without intent to infringe.Visit the Taylor & Francis Web site athttp://www.taylorandfrancis.comand the CRC Press Web site athttp://www.crcpress.com7189 C000a.indd 410/30/06 3:00:16 PM

Table of ContentsPrefacexvChapter 1 - Basic Electronics1.01.11.21.31The AtomIsotopes and IonsStatic ElectricityElectrical werOhm's Law1.4 Electrical Circuits1.4.1 Types of Circuits1.5 Circuit rsRevisiting Ohm's LawResistors in Series and ParallelCapacitorsCapacitors in Series and in ParallelInductorsTransformers1.6 Semiconductors1.6.11.6.21.6.31.6.4Integrated CircuitsSemiconductor ElectronicsP-Type and N-Type SiliconThe DiodeChapter 2 - Number Systems2.0 Counting2.0.1 The Tally System2.0.2 Roman Numerals2.1 The Origins of the Decimal System2.1.1 Number Systems for Digital-Electronics2.1.2 Positional Characteristics2.1.3 Radix or Base of a Number System66899101213141515161617171919192020222223v

viMicrocontroller Programming2.2 Types of Numbers2.2.1 Whole Numbers2.2.2 Signed Numbers2.2.3 Rational, Irrational, and Imaginary Numbers2.3 Radix Representations2.3.1 Decimal versus Binary Numbers2.3.2 Hexadecimal and Octal2.4 Number System Conversions23242424252526272.4.1 Binary-to-ASCII-Decimal2.4.2 Binary-to-Hexadecimal Conversion2.4.3 Decimal-to-Binary Conversion282929Chapter 3 - Data Types and Data Storage333.0 Electronic-Digital Machines3.1 Character Representations33333.1.1 ASCII3.1.2 EBCDIC and IBM3.1.3 Unicode3436363.2 Storage and Encoding of Integers3.2.13.2.23.2.33.2.43.2.5Signed and Unsigned RepresentationsWord SizeByte OrderingSign-Magnitude RepresentationRadix Complement Representation3.3 Encoding of Fractional Numbers3.3.13.3.23.3.33.3.43.3.5Fixed-Point RepresentationsFloating-Point RepresentationsStandardized Floating-Point RepresentationsIEEE 754 Single FormatEncoding and Decoding Floating-Point Numbers3.4 Binary-Coded Decimals (BCD)3.4.1 Floating-Point BCDChapter 4 - Digital Logic, Arithmetic, and Conversions4.0 Microcontroller Logic and Arithmetic373738394041444546474850515255554.0.1 CPU Flags4.0.2 Word Size55564.1 Logical gicalLogicalANDORXORNOT4.2 Microcontroller Arithmetic4.2.1 Unsigned and Two’s Complement Arithmetic4.2.2 Operations on Decimal Numbers4.3 Bit Manipulations and Auxiliary Operations4.3.1 Bit Shift and Rotate4.3.2 Comparison Operations4.3.3 Other Support Operations5757575858586062626363

Contentsvii4.4 Unsigned Binary Arithmetic4.4.1 Multi-byte Unsigned Addition4.4.2 Unsigned Multiplication4.4.3 Unsigned Division4.5 Signed Binary Arithmetic4.5.1 Overflow Detection in Signed Arithmetic4.5.2 Sign Extension Operations4.5.3 Multi-byte Signed Operations4.6 Data Format Conversions4.6.14.6.24.6.34.6.44.6.6BCD Digits to ASCII DecimalUnsigned Binary to ASCII Decimal DigitsASCII Decimal String to Unsigned BinaryUnsigned Binary to ASCII Hexadecimal DigitsSigned Numerical ConversionsChapter 5 - Circuits and Logic Gates5.0 Digital Circuits5.1 The Diode Revisited5.1.1 The Light-Emitting Diode (LED)5.2 The 2.1 Bipolar Transistor5.2.2 MOS Transistor81835.3 Logic Gates5.4 Transistor-Transistor nverter GatesThe AND GateThe NAND GateThe OR GateThe NOR GatePositive and Negative LogicThe XOR GateSchmitt Trigger Inverter5.5 Other TTL Logic Families5.6 CMOS Logic GatesChapter 6 - Circuit Components6.0 Power Supplies6.1 Clocked Logic and Flip-flops6.1.16.1.26.1.36.1.46.1.56.1.66.1.7The RS Flip-flopClocked RS Flip-flopThe D Flip-flopThe Edge-triggered D Flip-flopPreset and Clear SignalsD Flip-flop Waveform ActionFlip-flop Applications6.2 Clocks6.2.1 Clock Waveforms6.2.2 The TTL Clock6.2.3 The 555 03104105106

viiiMicrocontroller Programming6.2.4 Microcontroller Clocks6.3 Frequency Dividers and cy DividersThe JK Flip-flop CounterRipple CountersDecoding GatesSynchronous CountersCounter ICsShift Registers6.4 Multiplexers and Demultiplexers6.4.1 Multiplexers6.4.2 Demultiplexers6.4.3 Multiplexer and Demultiplexer ICs6.5 Input Devices6.5.1 Switches6.5.2 Switch Contact Bounce6.5.3 Keypads6.6 Output Devices6.6.1 Seven-segment LED6.6.2 Liquid Crystal Displays6.6.3 LCD TechnologiesChapter 7 - The Microchip PIC7.0 The PICMicro Microcontroller7.0.1 Programming the PICPIC ProgrammersDevelopment Boards7.0.2 Prototyping the PIC Circuit7.1 PIC Architecture7.1.1 Baseline PIC FamilyPIC10 DevicesPIC12 DevicesPIC14 Devices7.1.2 Mid-range PIC FamilyPIC16 Devices7.1.3 High-Performance PIC FamilyPIC18 DevicesChapter 8 - Mid-range PIC Architecture8.0 Processor Architecture and Design8.0.18.0.28.0.38.0.48.0.5Harvard ArchitectureRISC CPU DesignSingle-word InstructionsInstruction FormatMid-Range Device Versions8.1 The Mid-range Core Features8.1.1 Oscillator8.1.2 System Reset8.1.3 Interrupts8.2 Mid-Range CPU and Instruction 38139139139141142142143143144145145145147148149

Contentsix8.2.1 Mid-Range Instruction Set8.2.2 STATUS and OPTION Registers8.3 EEPROM Data Storage8.3.1 EEPROM in Mid-Range PICs8.4 Data Memory Organization8.4.1 The w Register8.4.2 The Data RegistersMemory BanksThe SFRsThe GPRs8.4.3 Indirect Addressing8.5 Mid-range I/O and Peripheral Modules8.5.18.5.28.5.38.5.48.5.58.5.6I/O PortsTimer ModulesCapture-and-Compare ModuleMaster Synchronous Serial Port (MSSP) ModuleUSART ModuleA/D ModuleChapter 9 - PIC Programming: Tools and Techniques9.0 Microchip’s MPLAB9.0.1 Embedded Systems9.1 Integrated Development Environment9.1.19.1.29.1.39.1.4Installing MPLABCreating the ProjectProject Build OptionsBuilding the Project9.2 Simulators and Debuggers9.2.1 MPLAB SIM9.2.2 MPLAB Hardware Debuggers9.2.3 A “Quick-and-Dirty” Debugger9.3 Programmers9.4 Engineering PIC Software9.4.1 Using Program CommentsProgram HeaderCommented BannersCommented Bitmaps9.4.2 Defining Data ElementsThe cblock Directive9.4.3 Banking TechniquesThe banksel DirectiveBank Selection MacrosDeprecated Banking Instructions9.4.4 Processor and Configuration ControlsConfiguration Bits9.4.5 Naming Conventions9.4.6 Errorlevel Directive9.5 Pseudo 186

xMicrocontroller ProgrammingChapter 10 - Programming Essentials: Input and Output10.0 16F84A Programming Template10.1 Introducing the 16F84A10.1.1 Template Circuit for 16F84A10.1.2 Power SuppliesVoltage Regulator10.1.3 Comparisons in PIC ProgrammingThe Infamous PIC Carry Flag10.2 Simple Circuits and Programs10.2.1 A Single LED CircuitLED Flasher Program10.2.2 LED/Pushbutton Circuit10.2.3 Multiple LED Circuit18918919119119119219319419419419619920210.3 Programming the Seven-segment LED10.4 A Demonstration Board20420610.4.1 PCB Images for Demo Board10.4.2 TestDemo1 Program206208Chapter 11 - Interrupts11.0 Interrupts on the 16F8411.0.1 The Interrupt Control Register11.0.2 The OPTION Register11.1 Interrupt Sources11.1.1 Port-B External Interrupt11.1.2 Timer0 Interrupt11.1.3 Port-B Line Change InterruptMultiple External Interrupts11.1.4 EEPROM Data Write Interrupt11.2 Interrupt Handlers11.2.1 Context Saving OperationsSaving w and STATUS Registers11.3 Interrupt Programming11.3.1 Programming the External InterruptRB0 Interrupt InitializationRB0 Interrupt Service Routine11.3.2 Wakeup from SLEEP Using the RB0 InterruptThe SleepDemo Program11.3.3 Port-B Bits 4-7 Status Change InterruptRB4-7 Interrupt InitializationRB4-7 Change Interrupt Service Routine11.4 Sample Programs11.4.1 The RB0Int Program11.4.2 The SleepDemo Program11.4.3 The RB4to7Int ProgramChapter 12 - Timers and 21922022122222322422522722922923223524112.0 The 16F84 Timer0 Module24112.0.1 Timer0 Operation241

ContentsxiTimer0 InterruptTimer0 Prescaler24224212.1 Delays Using Timer024312.1.1 Long Delay LoopsHow Accurate the Delay?The Black-Ammerman Method12.2 Timer0 as a Counter12.3 Timer0 Programming12.3.1 Programming a CounterA Timer/Counter Test CircuitThe Tmr0Counter Program12.3.2 Timer0 as a Simple Delay Timer12.3.3 Measured Time LapseInterrupt-driven Timer12.4 The Watchdog Timer12.4.1 Watchdog Timer Programming12.5 Sample Programs12.5.112.5.212.5.312.5.4The Tmr0Counter programThe Timer0 ProgramThe LapseTimer ProgramThe LapseTmrInt ProgramChapter 13 - LCD Interfacing and Programming13.0 LCD Features and Architecture13.0.1 LCD Functions and ComponentsInternal RegistersBusy FlagAddress CounterDisplay Data RAM (DDRAM)Character Generator ROM (CGROM)Character Generator RAM (CGRAM)Timing Generation CircuitLiquid Crystal Display Driver CircuitCursor/Blink Control Circuit13.0.2 Connectivity and Pin-Out13.1 Interfacing with the HD4478013.1.113.1.213.1.313.1.4Busy Flag or Timed Delay OptionsContrast ControlDisplay BacklightDisplay Memory Mapping13.2 HD44780 Instruction Set13.2.1 Instruction Set OverviewClearing the DisplayReturn homeEntry mode setDisplay and Cursor ON/OFFCursor/display shiftFunction setSet CGRAM addressSet DDRAM addressRead busy flag and Address 8279280281281281283283283284284284284285285285285

xiiMicrocontroller ProgrammingWrite dataRead data13.2.2 A 16F84 8-bit Data Mode Circuit13.3 LCD Programming13.3.1 Defining Constants and VariablesUsing MPLAB Data Directives13.3.2 LCD InitializationFunction Set CommandDisplay OffDisplay and Cursor OnSet Entry ModeCursor and Display ShiftClear Display13.3.3 Auxiliary OperationsTime Delay RoutinePulsing the E LineReading the Busy FlagBit Merging Operations13.3.4 Text Data Storage and DisplayGenerating and Storing a Text StringDisplaying the Text String13.3.5 Data Compression Techniques4-bit Data Transfer ModeMaster/Slave Systems13.4 Sample Programs13.4.1 LCDTest113.4.2 LCDTest2 Program13.4.3 LCDTest3 ProgramChapter 14 - Communications14.0 PIC Communications Overview14.1 Serial Data Transmission14.1.114.1.214.1.314.1.4Asynchronous Serial TransmissionSynchronous Serial TransmissionPIC Serial CommunicationsThe RS-232-C StandardEssential ConceptsThe Serial Bit StreamParity TestingConnectors and WiringThe Null ModemThe Null Modem Cable14.1.5 The EIA-485 StandardEIA-485 in PIC-based Systems14.2 Parallel Data Transmission14.2.1 PIC Parallel Slave Port (PSP)14.3 PIC “Free-style” Serial Programming14.3.1 PIC-to-PIC Serial CommunicationsPIC-to-PIC Serial Communications CircuitsPIC-to-PIC Serial Communications Programs14.3.2 Program Using Shift Register 360

ContentsxiiiThe 74HC165 Parallel-to-Serial Shift Register74HC164 Serial-to-Parallel Shift Register14.4 PIC Protocol-based Serial Programming14.4.1 RS-232-C Communications on the 16F84The RS-232-C Transceiver ICPIC to PC CommunicationsAn RS-232-C TTY BoardA 16F84A UART EmulationAn LCD Scrolling Routine14.4.2 RS-232-C Communications on the 16F87xThe 16F87x USART ModuleThe USART Baud Rate Generator16F87x USART Asynchronous Transmitter16F87x USART Asynchronous ReceiverPIC-to-PC RS-232-C Communications Circuit16F877 PIC Initialization CodeUSART Receive and Transmit RoutinesThe USART Receive Interrupt14.5 Sample 9SerialSnd ProgramSerialRcv ProgramSerial6465 ProgramTTYUsart ProgramSerComLCD ProgramSerIntLCD Program389394400404420438Chapter 15 - Data EEPROM Programming45915.0 PIC Internal EEPROM Memory15.0.1 EEPROM Programming on the 16F84Reading EEPROM Data Memory on the 16F8416F84 EEPROM Data Memory Write16F84 EEPROM Demonstration Program15.0.2 EEPROM Programming on the 16F87xReading EEPROM Data Memory on the 16F87xWriting to EEPROM Data Memory in the 16F87xGFR Access Issue in the 16F87x15.0.3 16F87x EEPROM Circuit and Program15.1 EEPROM Devices and .7The I2C Serial InterfaceI2C CommunicationsEEPROM Communications ConditionsEEPROM Write OperationEEPROM Read OperationI2C EEPROM DevicesPIC Master Synchronous Serial Port (MSSP)MSSP in Master Mode15.1.8 I2C Serial EEPROM Programming on the 16F877IC2 Initialization ProcedureI2C Write Byte ProcedureI2C Read Byte Procedure15.2 Sample 478478479480482486486488490492

xivMicrocontroller Programming15.2.1 EECounter Program15.2.2 Ser2EEP Program15.2.3 I2CEEP ProgramChapter 16 - Analog to Digital and Realtime Clocks16.0 A/D Converters16.0.1 Converter Resolution16.0.2 ADC Implementation16.1 A/D Integrated Circuits16.1.1 ADC0331 Sample Circuit and Program16.2 PIC On-Board A/D Hardware16.2.1 A/D Module on the 16F87xThe ADCON0 RegisterThe ADCON1 RegisterSLEEP Mode Operation16.2.2 A/D Module Sample Circuit and Program16.3 Realtime Clocks16.3.1 The NJU6355 Realtime Clock16.3.2 RTC Demonstration Circuit and ProgramBCD Conversion Procedures16.4 Sample Programs16.4.1 ADF84 Program16.4.2 A2DinLCD Program16.4.3 RTC2LCD 54558558560565568568580595Appendix A - Resistor Color Codes613Appendix B - Building Your Own Circuit Boards615Appendix C - Mid-range Instruction Set621Appendix D - Supplementary Programs659Index795

PrefaceThere are two sides to the computer revolution: one is represented by the PC on yourdesktop and the second one by the device that remote-controls your TV, monitors andoperates your car engine, and allows you to set up your answering machine and yourmicrowave oven. At the core of the PC you find a microprocessor, while at the heart ofa self-contained programmable device (also called an embedded system) is amicrocontroller.Microcontrollers are virtually everywhere in our modern society. They are foundin automobiles, airplanes, toys, kitchen appliances, computers, TVs and VCRs,phones and answering machines, space telescopes, and practically every electronicdigital device that furnishes an independent functionality to its user. In this sense amicrocontroller is a self-contained computer system that includes a processor,memory, and some way of communicating with the outside world, all in a single chipthat can be smaller than a postage stamp.A microcontroller (sometimes called an MCU) is actually a computer on a chip.Essentially it is a control device and its design places emphasis on being self-sufficient and inexpensive. The typical microcontroller contains all the components andfeatures necessary to perform its functions, such as a central processor, input/output facilities, timers, RAM memory for storing program data and executable code,and a clock or oscillator that provides a timing beat. In addition, somemicrocontrollers include a variety of additional modules and circuits. Some common ones are serial and parallel communications, analog-to-digital converters,realtime clocks, and flash memory.Engineers, inventors, experimenters, students, and device designers in generaldeal with microcontrollers on an everyday basis. In fact, interest in microcontrollersis not limited to electrical, electronic, and computer engineers. Mechanical and automotive engineers, among many others, often design devices or components thatcontain microcontrollers. The system that controls the hatch of a ballistic missilesilo and the one that operates the doglike toy that barks and rolls on its back, bothcontain microcontrollers.The Microchip PICMicrocontrollers include an enormous array of models and variations of general- andspecial-purpose devices. Discussing all of them in a single volume would have forced asuperficial scope. Even the products of a single manufacturer can have a mind-bogXV

XVIPrefacegling variety, which sometimes include hundreds of different MCU models in ahalf-dozen families, all with very different applications and features.For this reason we have focused the book on a single type of microcontroller: theMicrochip PIC. Not only are the PIC the most used and best knownmicrocontrollers, they are also the best supported. In fact, PIC system design andprogramming has become a powerful specialization with a large number of professional and amateur specialists. There are hundreds of WEB sites devoted to PIC-related topics. An entire cottage industry of PIC software and hardware has flourishedaround this technology.For practical reasons we have limited the book's scope to 8-bit PICs. In fact, thebook concentrates on a particular type of 8-bit PIC known as the mid-range family.We have chosen this approach partly because of space limitations and partly due tothe fact that 16- and 32-bit microcontrollers (sometimes called external memorymicrocontrollers) are more related to microprocessor technology than to the topicat hand.The Book's DesignThe book is intended as a resource kit for PIC microcontroller programming. But programming microcontrollers is a different paradigm from microprocessor programming. PIC programming requires a set of skills and a knowledge base quite differentfrom the one needed by a computer programmer. The reason is that the designer/programmer is responsible for the entire system. A typical embedded system has no DOS,Windows, or UNIX software to handle the operational and housekeeping chores.Thus, the PIC programmer provides all the functionality needed by the applicationwith very little assistance from other programs. This makes the microcontroller programmer an application developer, a system's programmer, and an input

Chapter 7 - The Microchip PIC 129 7.0 The PICMicro Microcontroller 129 7.0.1 Programming the PIC 130 PIC Programmers 131 Development Boards 131 7.0.2 Prototyping the PIC Circuit 132 7.1 PIC Architecture 134 7.1.1 Baseline PIC Family 134 PIC10 Devices 135 PIC12 Devices 135 PIC14 Devices 138 7.1.2 Mi

Related Documents:

Microcontroller Based Home Automation System Page ix 2.7 MICROCONTROLLER FEATURES' 19 2.8 Microcontroller clock 20 2.9 THE MICROCONTROLLER SYSTEM 20 3.1 SMART HOME CONTROL SYSTEM 23 4.1 AC AND HEATER CONTROL 27 4.2 Temperature meter, day light sensor & water level sensor 28 4.3 REMOTE CONTROL CIRCUIT 29 5.1 Software Development 31 5.2 FINAL .

A microcontroller is an integrated chip that is often part of an embedded system. The microcontroller includes a CPU, RAM, ROM, I/O ports, and timers like a standard computer, but . microcontroller programming .Doing t

Abstract—This paper describes new microcontroller platform based on 32-bit ARM Cortex M microcontroller. This platform was developed at our faculty to replaceolder board with Freescale HCS08 microcontroller for use in lessons of microcontroller programming. Main comp

PIC Microcontroller: PIC microcontrollers is a kind of programmable microcontroller which produced by microchip, it can be programmed by many programming language.[3] Fig ( 11 ): PIC Microcontroller Chip H-Bridge (

microcontroller, this project focus only on the PIC16F877A Microcontroller where it's pins as shown in Fig. 6. Fig. 6 Pin diagram of PIC16F877A microcontroller . 3 Automatic street light control Circuit Design . The inputs in the streets lighting system are LDR and photoelectric sensors, after dusk the light sensor .

Ultra-Low Power Microcontroller OVERVIEW The AX8052F100 is a single chip ultra lowpower microcontroller primarily for use in radio applications. The AX8052F100 contains a high speed microcontroller compatible to the industry standard 8052 instruction set. It contains 64 kBytes of FLASH and 8.25 kBytes of internal SRAM. The AX8052F100 features 3 16 bit general purpose timers with capability .

Pic-Microcontroller Based Fully-Automatic International organization of Scientific Research 14 P a g e Figure 1: System Block Diagram 2.1 PIC16F877A Microcontroller A microcontroller is a microcomputer on a single

AVR vs PIC The two most common microcontroller-brands for hobbyists are probably AVR from Atmel and PIC from Microchip. AVR is the type of microcontroller used on the Arduino. I have used AVR a lot and I think it’s a really good choice of