371 18 6MB
English Pages 560 Year 2008
Advanced PIC Microcontroller Projects in C
This page intentionally left blank
Advanced PIC Microcontroller Projects in C From USB to RTOS with the PIC18F Series
Dogan Ibrahim
Newnes is an imprint of Elsevier 30 Corporate Drive, Suite 400, Burlington, MA 01803, USA Linacre House, Jordan Hill, Oxford OX2 8DP, UK Copyright # 2008, Elsevier Ltd. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Permissions may be sought directly from Elsevier s Science & Technology Rights Department in Oxford, UK: phone: (þ44) 1865 843830, fax: (þ44) 1865 853333, E-mail: [email protected]. You may also complete your request online via the Elsevier homepage (http://elsevier.com), by selecting “Support & Contact” then “Copyright and Permission” and then “Obtaining Permissions.” Recognizing the importance of preserving what has been written, Elsevier prints its books on acid-free paper whenever possible. Library of Congress Cataloging-in-Publication Data Ibrahim, Dogan. Advanced PIC microcontroller projects in C: from USB to RTOS with the PIC18F series/Dogan Ibrahim p. cm. Includes bibliographical references and index. ISBN-13: 978-0-7506-8611-2 (pbk. : alk. paper) 1. Programmable controllers. 2. C (Computer program language) I. Title. TJ223.P76I268 2008 629.80 95––dc22 2007050550 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. ISBN: 978-0-7506-8611-2 For information on all Newnes publications visit our Web site at www.books.elsevier.com Printed in the United States of America 08 09 10 11 12 13 9 8 7 6 5
4 3 2
1
Contents
Preface............................................................................................. xiii Acknowledgments ................................................................................ xv Chapter 1: Microcomputer Systems.......................................................... 1 1.1 Introduction..................................................................................................1 1.2 Microcontroller Systems ...............................................................................1 1.2.1 RAM .................................................................................................5 1.2.2 ROM .................................................................................................5 1.2.3 PROM ...............................................................................................5 1.2.4 EPROM.............................................................................................6 1.2.5 EEPROM ..........................................................................................6 1.2.6 Flash EEPROM .................................................................................6 1.3 Microcontroller Features...............................................................................6 1.3.1 Supply Voltage ..................................................................................7 1.3.2 The Clock..........................................................................................7 1.3.3 Timers ...............................................................................................7 1.3.4 Watchdog ..........................................................................................8 1.3.5 Reset Input ........................................................................................8 1.3.6 Interrupts ...........................................................................................8 1.3.7 Brown-out Detector ...........................................................................9 1.3.8 Analog-to-Digital Converter ...............................................................9 1.3.9 Serial Input-Output ............................................................................9 1.3.10 EEPROM Data Memory ..................................................................10 1.3.11 LCD Drivers....................................................................................10 1.3.12 Analog Comparator..........................................................................10 1.3.13 Real-time Clock...............................................................................11 1.3.14 Sleep Mode .....................................................................................11 1.3.15 Power-on Reset................................................................................11
www.newnespress.com
Contents
vi
1.4 1.5
1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 1.21 1.22
1.23 1.24 1.25
1.3.16 Low-Power Operation ....................................................................11 1.3.17 Current Sink/Source Capability ......................................................11 1.3.18 USB Interface ................................................................................12 1.3.19 Motor Control Interface .................................................................12 1.3.20 CAN Interface ...............................................................................12 1.3.21 Ethernet Interface...........................................................................12 1.3.22 ZigBee Interface ............................................................................12 Microcontroller Architectures.................................................................... 12 1.4.1 RISC and CISC ...............................................................................13 Number Systems....................................................................................... 13 1.5.1 Decimal Number System .................................................................14 1.5.2 Binary Number System ....................................................................14 1.5.3 Octal Number System ......................................................................15 1.5.4 Hexadecimal Number System ..........................................................15 Converting Binary Numbers into Decimal................................................. 16 Converting Decimal Numbers into Binary................................................. 16 Converting Binary Numbers into Hexadecimal.......................................... 18 Converting Hexadecimal Numbers into Binary.......................................... 20 Converting Hexadecimal Numbers into Decimal ....................................... 21 Converting Decimal Numbers into Hexadecimal ....................................... 22 Converting Octal Numbers into Decimal................................................... 23 Converting Decimal Numbers into Octal................................................... 23 Converting Octal Numbers into Binary ..................................................... 24 Converting Binary Numbers into Octal ..................................................... 26 Negative Numbers .................................................................................... 26 Adding Binary Numbers ........................................................................... 27 Subtracting Binary Numbers ..................................................................... 29 Multiplication of Binary Numbers............................................................. 29 Division of Binary Numbers ..................................................................... 31 Floating Point Numbers ............................................................................ 31 Converting a Floating Point Number into Decimal .................................... 33 1.22.1 Normalizing Floating Point Numbers .............................................34 1.22.2 Converting a Decimal Number into Floating Point .........................34 1.22.3 Multiplication and Division of Floating Point Numbers ..................36 1.22.4 Addition and Subtraction of Floating Point Numbers ......................37 BCD Numbers .......................................................................................... 38 Summary.................................................................................................. 40 Exercises .................................................................................................. 40
Chapter 2: PIC18F Microcontroller Series .............................................. 43 2.1 PIC18FXX2 Architecture.......................................................................... 46 2.1.1 Program Memory Organization ........................................................50
www.newnespress.com
Contents
vii
2.1.2 Data Memory Organization ..............................................................51 2.1.3 The Configuration Registers.............................................................52 2.1.4 The Power Supply ...........................................................................57 2.1.5 The Reset ........................................................................................57 2.1.6 The Clock Sources...........................................................................60 2.1.7 Watchdog Timer ..............................................................................67 2.1.8 Parallel I/O Ports .............................................................................68 2.1.9 Timers .............................................................................................74 2.1.10 Capture/Compare/PWM Modules (CCP) ..........................................84 2.1.11 Analog-to-Digital Converter (A/D) Module ......................................93 2.1.12 Interrupts ....................................................................................... 101 2.2 Summary.................................................................................................. 115 2.3 Exercises .................................................................................................. 115
Chapter 3: C Programming Language....................................................119 3.1 Structure of a mikroC Program................................................................. 120 3.1.1 Comments ..................................................................................... 121 3.1.2 Beginning and Ending of a Program .............................................. 121 3.1.3 Terminating Program Statements.................................................... 121 3.1.4 White Spaces ................................................................................. 122 3.1.5 Case Sensitivity ............................................................................. 122 3.1.6 Variable Names ............................................................................. 123 3.1.7 Variable Types .............................................................................. 123 3.1.8 Constants ....................................................................................... 126 3.1.9 Escape Sequences .......................................................................... 128 3.1.10 Static Variables.............................................................................. 129 3.1.11 External Variables ......................................................................... 129 3.1.12 Volatile Variables .......................................................................... 130 3.1.13 Enumerated Variables .................................................................... 130 3.1.14 Arrays ........................................................................................... 131 3.1.15 Pointers ......................................................................................... 133 3.1.16 Structures ...................................................................................... 135 3.1.17 Unions........................................................................................... 138 3.1.18 Operators in C ............................................................................... 139 3.1.19 Modifying the Flow of Control ...................................................... 148 3.1.20 Mixing mikroC with Assembly Language Statements ..................... 159 3.2 PIC Microcontroller Input-Output Port Programming ................................ 160 3.3 Programming Examples ............................................................................ 161 3.4 Summary.................................................................................................. 165 3.5 Exercises .................................................................................................. 165
www.newnespress.com
viii
Contents
Chapter 4: Functions and Libraries in mikroC.........................................169 4.1 mikroC Functions ..................................................................................... 169 4.1.1 Function Prototypes ......................................................................... 173 4.1.2 Passing Arrays to Functions............................................................. 177 4.1.3 Passing Variables by Reference to Functions.................................... 180 4.1.4 Variable Number of Arguments ....................................................... 181 4.1.5 Function Reentrancy ........................................................................ 184 4.1.6 Static Function Variables ................................................................. 184 4.2 mikroC Built-in Functions ........................................................................ 184 4.3 mikroC Library Functions......................................................................... 188 4.3.1 EEPROM Library ............................................................................ 189 4.3.2 LCD Library.................................................................................... 192 4.3.3 Software UART Library .................................................................. 199 4.3.4 Hardware USART Library ............................................................... 204 4.3.5 Sound Library.................................................................................. 206 4.3.6 ANSI C Library............................................................................... 208 4.3.7 Miscellaneous Library...................................................................... 212 4.4 Summary.................................................................................................. 218 4.5 Exercises .................................................................................................. 219
Chapter 5: PIC18 Development Tools ...................................................221 5.1 Software Development Tools .................................................................... 222 5.1.1 Text Editors..................................................................................... 222 5.1.2 Assemblers and Compilers............................................................... 222 5.1.3 Simulators ....................................................................................... 223 5.1.4 High-Level Language Simulators ..................................................... 224 5.1.5 Integrated Development Environments (IDEs).................................. 224 5.2 Hardware Development Tools................................................................... 224 5.2.1 Development Boards........................................................................ 225 5.2.2 Device Programmers........................................................................ 239 5.2.3 In-Circuit Debuggers ....................................................................... 242 5.2.4 In-Circuit Emulators ........................................................................ 245 5.2.5 Breadboards..................................................................................... 248 5.3 mikroC Integrated Development Environment (IDE) ................................. 251 5.3.1 mikroC IDE Screen ......................................................................... 251 5.3.2 Creating and Compiling a New File................................................. 258 5.3.3 Using the Simulator ......................................................................... 265 5.3.4 Using the mikroICD In-Circuit Debugger......................................... 272 5.3.5 Using a Development Board ............................................................ 277 5.4 Summary.................................................................................................. 285 5.5 Exercises .................................................................................................. 285
www.newnespress.com
Contents
ix
Chapter 6: Simple PIC18 Projects ........................................................287 6.1 Program Description Language (PDL) ...................................................... 288 6.1.1 START-END .................................................................................. 288 6.1.2 Sequencing...................................................................................... 288 6.1.3 IF-THEN-ELSE-ENDIF .................................................................. 288 6.1.4 DO-ENDDO ................................................................................... 289 6.1.5 REPEAT-UNTIL............................................................................. 290 Project 6.1—Chasing LEDs ............................................................................ 290 Project 6.2—LED Dice ................................................................................... 295 Project 6.3—Two-Dice Project........................................................................ 301 Project 6.4—Two-Dice Project Using Fewer I/O Pins ..................................... 303 Project 6.5—7-Segment LED Counter............................................................. 313 Project 6.6—Two-Digit Multiplexed 7-Segment LED...................................... 319 Project 6.7—Two-Digit Multiplexed 7-Segment LED Counter with Timer Interrupt...................................................................................... 326 Project 6.8—Voltmeter with LCD Display ...................................................... 334 Project 6.9—Calculator with Keypad and LCD ............................................... 341 Project 6.10—Serial Communication–Based Calculator ................................... 352
Chapter 7: Advanced PIC18 Projects—SD Card Projects .........................371 7.1 The SD Card ............................................................................................ 371 7.1.1 The SPI Bus.................................................................................... 373 7.1.2 Operation of the SD Card in SPI Mode ........................................... 377 7.2 mikroC Language SD Card Library Functions .......................................... 384 Project 7.1—Read CID Register and Display on a PC Screen ......................... 385 Project 7.2—Read/Write to SD Card Sectors................................................... 392 Project 7.3—Using the Card Filing System ..................................................... 392 Project 7.4—Temperature Logger ................................................................... 397
Chapter 8: Advanced PIC18 Projects—USB Bus Projects .........................409 8.1 Speed Identification on the Bus ................................................................ 413 8.2 USB States ............................................................................................... 413 8.3 USB Bus Communication......................................................................... 414 8.3.1 Packets............................................................................................ 414 8.3.2 Data Flow Types............................................................................. 416 8.3.3 Enumeration.................................................................................... 417 8.4 Descriptors ............................................................................................... 418 8.4.1 Device Descriptors .......................................................................... 418 8.4.2 Configuration Descriptors................................................................ 421 8.4.3 Interface Descriptors ....................................................................... 423 8.4.4 HID Descriptors .............................................................................. 425 8.4.5 Endpoint Descriptors ....................................................................... 426
www.newnespress.com
x
Contents 8.5 PIC18 Microcontroller USB Bus Interface ................................................ 427 8.6 mikroC Language USB Bus Library Functions ......................................... 429 Project 8.1—USB-Based Microcontroller Output Port ..................................... 430 Project 8.2—USB-Based Microcontroller Input/Output .................................... 456 Project 8.3—USB-Based Ambient Pressure Display on the PC ........................ 464
Chapter 9: Advanced PIC18 Projects—CAN Bus Projects ........................475 9.1 Data Frame............................................................................................. 481 9.1.1 Start of Frame (SOF) .................................................................... 482 9.1.2 Arbitration Field............................................................................ 482 9.1.3 Control Field................................................................................. 484 9.1.4 Data Field ..................................................................................... 484 9.1.5 CRC Field..................................................................................... 484 9.1.6 ACK Field .................................................................................... 485 9.2 Remote Frame ........................................................................................ 485 9.3 Error Frame............................................................................................ 485 9.4 Overload Frame...................................................................................... 485 9.5 Bit Stuffing ............................................................................................ 486 9.6 Types of Errors ...................................................................................... 486 9.7 Nominal Bit Timing ............................................................................... 486 9.8 PIC Microcontroller CAN Interface ........................................................ 489 9.9 PIC18F258 Microcontroller..................................................................... 491 9.9.1 Configuration Mode ...................................................................... 493 9.9.2 Disable Mode................................................................................ 493 9.9.3 Normal Operation Mode................................................................ 493 9.9.4 Listen-only Mode .......................................................................... 493 9.9.5 Loop-Back Mode .......................................................................... 494 9.9.6 Error Recognition Mode................................................................ 494 9.9.7 CAN Message Transmission.......................................................... 494 9.9.8 CAN Message Reception............................................................... 494 9.9.9 Calculating the Timing Parameters ................................................ 496 9.10 mikroC CAN Functions .......................................................................... 498 9.10.1 CANSetOperationMode ............................................................... 499 9.10.2 CANGetOperationMode .............................................................. 500 9.10.3 CANInitialize .............................................................................. 500 9.10.4 CANSetBaudRate ........................................................................ 501 9.10.5 CANSetMask .............................................................................. 501 9.10.6 CANSetFilter .............................................................................. 502 9.10.7 CANRead.................................................................................... 502 9.10.8 CANWrite................................................................................... 503 9.11 CAN Bus Programming .......................................................................... 504 Project 9.1—Temperature Sensor CAN Bus Project ........................................ 504
www.newnespress.com
Contents
xi
Chapter 10: Multi-Tasking and Real-Time Operating Systems....................515 10.1 State Machines ....................................................................................... 516 10.2 The Real-Time Operating System (RTOS) .............................................. 518 10.2.1 The Scheduler.............................................................................. 518 10.3 RTOS Services ....................................................................................... 521 10.4 Synchronization and Messaging Tools .................................................... 521 10.5 CCS PIC C Compiler RTOS................................................................... 522 10.5.1 Preparing for RTOS .................................................................... 523 10.5.2 Declaring a Task ......................................................................... 524 Project 10.1—LEDs ........................................................................................ 524 Project 10.2—Random Number Generator....................................................... 528 Project 10.3—Voltmeter with RS232 Serial Output ......................................... 532
Index...............................................................................................541
www.newnespress.com
This page intentionally left blank
Preface
A microcontroller is a microprocessor system which contains data and program memory, serial and parallel I/O, timers, and external and internal interrupts—all integrated into a single chip that can be purchased for as little as two dollars. About 40 percent of all microcontroller applications are found in office equipment, such as PCs, laser printers, fax machines, and intelligent telephones. About one third of all microcontrollers are found in consumer electronic goods. Products like CD players, hi-fi equipment, video games, washing machines, and cookers fall into this category. The communications market, the automotive market, and the military share the rest of the applications. This book is written for advanced students, for practicing engineers, and for hobbyists who want to learn more about the programming and applications of PIC18F-series microcontrollers. The book assumes the reader has taken a course on digital logic design and been exposed to writing programs using at least one high-level programming language. Knowledge of the C programming language will be useful, and familiarity with at least one member of the PIC16F series of microcontrollers will be an advantage. Knowledge of assembly language programming is not required since all the projects in the book are based on the C language. Chapter 1 presents the basic features of microcontrollers, discusses the important topic of numbering systems, and describes how to convert between number bases. Chapter 2 reviews the PIC18F series of microcontrollers and describes various features of these microcontrollers in detail. Chapter 3 provides a short tutorial on the C language and then examines the features of the mikroC compiler.
www.newnespress.com
xiv
Preface
Chapter 4 covers advanced features of the mikroC language. Topics such as built-in functions and libraries are discussed in this chapter with examples. Chapter 5 explores the various software and hardware development tools for the PIC18F series of microcontrollers. Various commercially available development kits as well as development tools such as simulators, emulators, and in-circuit debuggers are described with examples. Chapter 6 provides some simple projects using the PIC18F series of microcontrollers and the mikroC compiler. All the projects are based on the PIC18F452 microcontroller, and all of them have been tested. This chapter should be useful for those who are new to PIC microcontrollers as well as for those who want to extend their knowledge of programming PIC18F microcontrollers using the mikroC language. Chapter 7 covers the use of SD memory cards in PIC18F microcontroller projects. The theory of these cards is given with real working examples. Chapter 8 reviews the popular USB bus, discussing the basic theory of this bus system with real working projects that illustrate how to design PIC18F-based projects communicating with a PC over the USB bus. The CAN bus is currently used in many automotive applications. Chapter 9 presents a brief theory of this bus and also discusses the design of PIC18F microcontrollerbased projects with CAN bus interface. Chapter 10 is about real-time operating systems (RTOS) and multi-tasking. The basic theory of RTOS systems is described and simple multi-tasking applications are given. The CD-ROM that accompanies this book contains all the program source files and HEX files for the projects described in the book. In addition, a 2K size limited version of the mikroC compiler is included on the CD-ROM. Dogan Ibrahim London, 2007
www.newnespress.com
Acknowledgments
The following material is reproduced in this book with the kind permission of the respective copyright holders and may not be reprinted, or reproduced in any other way, without their prior consent. Figures 2.1–2.10, 2.22–2.36, 2.37, 2.38, 2.41–2.55, 5.2–5.4, 5.17, 5.20, 8.8, and 9.13, and Table 2.2 are taken from Microchip Technology Inc. data sheets PIC18FXX2 (DS39564C) and PIC18F2455/2550/4455/4550 (DS39632D). Figure 5.5 is taken from the web site of BAJI Labs. Figures 5.6–5.8 are taken from the web site of Shuan Shizu Ent. Co., Ltd. Figures 5.9, 5.13, 5.18 are taken from the web site of Custom Computer Services Inc. Figures 5.10, 5.19, and 6.43 are taken from the web site of mikroElektronika Ltd. Figure 5.11 is taken from the web site of Futurlec. Figure 5.21 is taken from the web site of Smart Communications Ltd. Figure 5.22 is taken from the web site of RF Solutions. Figure 5.23 is taken from the web site of Phyton. Figures 5.1 and 5.14 are taken from the web site of microEngineering Labs Inc. Figure 5.16 is taken from the web site of Kanda Systems. Thanks is due to mikroElektronika Ltd. for their technical support and for permission to include a limited size mikroC compiler on the CD-ROM that accompanies this book. PICW, PICSTARTW, and MPLABW are all registered trademarks of Microchip Technology Inc.
www.newnespress.com
CHAPTER 1
Microcomputer Systems 1.1 Introduction The term microcomputer is used to describe a system that includes at minimum a microprocessor, program memory, data memory, and an input-output (I/O) device. Some microcomputer systems include additional components such as timers, counters, and analog-to-digital converters. Thus, a microcomputer system can be anything from a large computer having hard disks, floppy disks, and printers to a single-chip embedded controller. In this book we are going to consider only the type of microcomputers that consist of a single silicon chip. Such microcomputer systems are also called microcontrollers, and they are used in many household goods such as microwave ovens, TV remote control units, cookers, hi-fi equipment, CD players, personal computers, and refrigerators. Many different microcontrollers are available on the market. In this book we shall be looking at programming and system design for the PIC (programmable interface controller) series of microcontrollers manufactured by Microchip Technology Inc.
1.2 Microcontroller Systems A microcontroller is a single-chip computer. Micro suggests that the device is small, and controller suggests that it is used in control applications. Another term for microcontroller is embedded controller, since most of the microcontrollers are built into (or embedded in) the devices they control. A microprocessor differs from a microcontroller in a number of ways. The main distinction is that a microprocessor requires several other components for its operation,
www.newnespress.com
2
Chapter 1
such as program memory and data memory, input-output devices, and an external clock circuit. A microcontroller, on the other hand, has all the support chips incorporated inside its single chip. All microcontrollers operate on a set of instructions (or the user program) stored in their memory. A microcontroller fetches the instructions from its program memory one by one, decodes these instructions, and then carries out the required operations. Microcontrollers have traditionally been programmed using the assembly language of the target device. Although the assembly language is fast, it has several disadvantages. An assembly program consists of mnemonics, which makes learning and maintaining a program written using the assembly language difficult. Also, microcontrollers manufactured by different firms have different assembly languages, so the user must learn a new language with every new microcontroller he or she uses. Microcontrollers can also be programmed using a high-level language, such as BASIC, PASCAL, or C. High-level languages are much easier to learn than assembly languages. They also facilitate the development of large and complex programs. In this book we shall be learning the programming of PIC microcontrollers using the popular C language known as mikroC, developed by mikroElektronika. In theory, a single chip is sufficient to have a running microcontroller system. In practical applications, however, additional components may be required so the microcomputer can interface with its environment. With the advent of the PIC family of microcontrollers the development time of an electronic project has been reduced to several hours. Basically, a microcomputer executes a user program which is loaded in its program memory. Under the control of this program, data is received from external devices (inputs), manipulated, and then sent to external devices (outputs). For example, in a microcontroller-based oven temperature control system the microcomputer reads the temperature using a temperature sensor and then operates a heater or a fan to keep the temperature at the required value. Figure 1.1 shows a block diagram of a simple oven temperature control system. The system shown in Figure 1.1 is very simple. A more sophisticated system may include a keypad to set the temperature and an LCD to display it. Figure 1.2 shows a block diagram of this more sophisticated temperature control system.
www.newnespress.com
Microcomputer Systems Microcontroller
3
OVEN
output
Heater
output
Fan Sensor
input
Figure 1.1: Microcontroller-based oven temperature control system
LCD
OVEN output output
Heater
output
Fan
inputs
Sensor
Microcontroller
Keypad
Figure 1.2: Temperature control system with a keypad and LCD
www.newnespress.com
4
Chapter 1
We can make the design even more sophisticated (see Figure 1.3) by adding an alarm that activates if the temperature goes outside the desired range. Also, the temperature readings can be sent to a PC every second for archiving and further processing. For example, a graph of the daily temperature can be plotted on the PC. As you can see, because microcontrollers are programmable the final system can be as simple or as complicated as we like. A microcontroller is a very powerful tool that allows a designer to create sophisticated input-output data manipulation under program control. Microcontrollers are classified by the number of bits they process. Microcontrollers with 8 bits are the most popular and are used in most microcontroller-based applications. Microcontrollers with 16 and 32 bits are much more powerful, but are usually more expensive and not required in most small- or medium-size general purpose applications that call for microcontrollers. The simplest microcontroller architecture consists of a microprocessor, memory, and input-output. The microprocessor consists of a central processing unit (CPU) and a LCD
Microcontroller
OVEN
output output
Heater
output
Fan
output input input output
Sensor buzzer
PC
Keypad
Figure 1.3: A more sophisticated temperature controller
www.newnespress.com
Microcomputer Systems
5
control unit (CU). The CPU is the brain of the microcontroller; this is where all the arithmetic and logic operations are performed. The CU controls the internal operations of the microprocessor and sends signals to other parts of the microcontroller to carry out the required instructions. Memory, an important part of a microcontroller system, can be classified into two types: program memory and data memory. Program memory stores the program written by the programmer and is usually nonvolatile (i.e., data is not lost after the power is turned off). Data memory stores the temporary data used in a program and is usually volatile (i.e., data is lost after the power is turned off). There are basically six types of memories, summarized as follows:
1.2.1
RAM
RAM, random access memory, is a general purpose memory that usually stores the user data in a program. RAM memory is volatile in the sense that it cannot retain data in the absence of power (i.e., data is lost after the power is turned off). Most microcontrollers have some amount of internal RAM, 256 bytes being a common amount, although some microcontrollers have more, some less. The PIC18F452 microcontroller, for example, has 1536 bytes of RAM. Memory can usually be extended by adding external memory chips.
1.2.2
ROM
ROM, read only memory, usually holds program or fixed user data. ROM is nonvolatile. If power is removed from ROM and then reapplied, the original data will still be there. ROM memory is programmed during the manufacturing process, and the user cannot change its contents. ROM memory is only useful if you have developed a program and wish to create several thousand copies of it.
1.2.3
PROM
PROM, programmable read only memory, is a type of ROM that can be programmed in the field, often by the end user, using a device called a PROM programmer. Once a PROM has been programmed, its contents cannot be changed. PROMs are usually used in low production applications where only a few such memories are required.
www.newnespress.com
6
Chapter 1
1.2.4
EPROM
EPROM, erasable programmable read only memory, is similar to ROM, but EPROM can be programmed using a suitable programming device. An EPROM memory has a small clear-glass window on top of the chip where the data can be erased under strong ultraviolet light. Once the memory is programmed, the window can be covered with dark tape to prevent accidental erasure of the data. An EPROM memory must be erased before it can be reprogrammed. Many developmental versions of microcontrollers are manufactured with EPROM memories where the user program can be stored. These memories are erased and reprogrammed until the user is satisfied with the program. Some versions of EPROMs, known as OTP (one time programmable), can be programmed using a suitable programmer device but cannot be erased. OTP memories cost much less than EPROMs. OTP is useful after a project has been developed completely and many copies of the program memory must be made.
1.2.5
EEPROM
EEPROM, electrically erasable programmable read only memory, is a nonvolatile memory that can be erased and reprogrammed using a suitable programming device. EEPROMs are used to save configuration information, maximum and minimum values, identification data, etc. Some microcontrollers have built-in EEPROM memories. For instance, the PIC18F452 contains a 256-byte EEPROM memory where each byte can be programmed and erased directly by applications software. EEPROM memories are usually very slow. An EEPROM chip is much costlier than an EPROM chip.
1.2.6
Flash EEPROM
Flash EEPROM, a version of EEPROM memory, has become popular in microcontroller applications and is used to store the user program. Flash EEPROM is nonvolatile and usually very fast. The data can be erased and then reprogrammed using a suitable programming device. Some microcontrollers have only 1K flash EEPROM while others have 32K or more. The PIC18F452 microcontroller has 32K bytes of flash memory.
1.3 Microcontroller Features Microcontrollers from different manufacturers have different architectures and different capabilities. Some may suit a particular application while others may be totally
www.newnespress.com
Microcomputer Systems
7
unsuitable for the same application. The hardware features common to most microcontrollers are described in this section.
1.3.1
Supply Voltage
Most microcontrollers operate with the standard logic voltage of þ5V. Some microcontrollers can operate at as low as þ2.7V, and some will tolerate þ6V without any problem. The manufacturer’s data sheet will have information about the allowed limits of the power supply voltage. PIC18F452 microcontrollers can operate with a power supply of þ2V to þ5.5V. Usually, a voltage regulator circuit is used to obtain the required power supply voltage when the device is operated from a mains adapter or batteries. For example, a 5V regulator is required if the microcontroller is operated from a 5V supply using a 9V battery.
1.3.2
The Clock
All microcontrollers require a clock (or an oscillator) to operate, usually provided by external timing devices connected to the microcontroller. In most cases, these external timing devices are a crystal plus two small capacitors. In some cases they are resonators or an external resistor-capacitor pair. Some microcontrollers have built-in timing circuits and do not require external timing components. If an application is not timesensitive, external or internal (if available) resistor-capacitor timing components are the best option for their simplicity and low cost. An instruction is executed by fetching it from the memory and then decoding it. This usually takes several clock cycles and is known as the instruction cycle. In PIC microcontrollers, an instruction cycle takes four clock periods. Thus the microcontroller operates at a clock rate that is one-quarter of the actual oscillator frequency. The PIC18F series of microcontrollers can operate with clock frequencies up to 40MHz.
1.3.3
Timers
Timers are important parts of any microcontroller. A timer is basically a counter which is driven from either an external clock pulse or the microcontroller’s internal oscillator. A timer can be 8 bits or 16 bits wide. Data can be loaded into a timer under program control, and the timer can be stopped or started by program control. Most timers can be
www.newnespress.com
8
Chapter 1
configured to generate an interrupt when they reach a certain count (usually when they overflow). The user program can use an interrupt to carry out accurate timing-related operations inside the microcontroller. Microcontrollers in the PIC18F series have at least three timers. For example, the PIC18F452 microcontroller has three built-in timers. Some microcontrollers offer capture and compare facilities, where a timer value can be read when an external event occurs, or the timer value can be compared to a preset value, and an interrupt is generated when this value is reached. Most PIC18F microcontrollers have at least two capture and compare modules.
1.3.4
Watchdog
Most microcontrollers have at least one watchdog facility. The watchdog is basically a timer that is refreshed by the user program. Whenever the program fails to refresh the watchdog, a reset occurs. The watchdog timer is used to detect a system problem, such as the program being in an endless loop. This safety feature prevents runaway software and stops the microcontroller from executing meaningless and unwanted code. Watchdog facilities are commonly used in real-time systems where the successful termination of one or more activities must be checked regularly.
1.3.5
Reset Input
A reset input is used to reset a microcontroller externally. Resetting puts the microcontroller into a known state such that the program execution starts from address 0 of the program memory. An external reset action is usually achieved by connecting a push-button switch to the reset input. When the switch is pressed, the microcontroller is reset.
1.3.6
Interrupts
Interrupts are an important concept in microcontrollers. An interrupt causes the microcontroller to respond to external and internal (e.g., a timer) events very quickly. When an interrupt occurs, the microcontroller leaves its normal flow of program execution and jumps to a special part of the program known as the interrupt service routine (ISR). The program code inside the ISR is executed, and upon return from the ISR the program resumes its normal flow of execution.
www.newnespress.com
Microcomputer Systems
9
The ISR starts from a fixed address of the program memory sometimes known as the interrupt vector address. Some microcontrollers with multi-interrupt features have just one interrupt vector address, while others have unique interrupt vector addresses, one for each interrupt source. Interrupts can be nested such that a new interrupt can suspend the execution of another interrupt. Another important feature of multi-interrupt capability is that different interrupt sources can be assigned different levels of priority. For example, the PIC18F series of microcontrollers has both low-priority and highpriority interrupt levels.
1.3.7
Brown-out Detector
Brown-out detectors, which are common in many microcontrollers, reset the microcontroller if the supply voltage falls below a nominal value. These safety features can be employed to prevent unpredictable operation at low voltages, especially to protect the contents of EEPROM-type memories.
1.3.8
Analog-to-Digital Converter
An analog-to-digital converter (A/D) is used to convert an analog signal, such as voltage, to digital form so a microcontroller can read and process it. Some microcontrollers have built-in A/D converters. External A/D converter can also be connected to any type of microcontroller. A/D converters are usually 8 to 10 bits, having 256 to 1024 quantization levels. Most PIC microcontrollers with A/D features have multiplexed A/D converters which provide more than one analog input channel. For example, the PIC18F452 microcontroller has 10-bit 8-channel A/D converters. The A/D conversion process must be started by the user program and may take several hundred microseconds to complete. A/D converters usually generate interrupts when a conversion is complete so the user program can read the converted data quickly. A/D converters are especially useful in control and monitoring applications, since most sensors (e.g., temperature sensors, pressure sensors, force sensors, etc.) produce analog output voltages.
1.3.9
Serial Input-Output
Serial communication (also called RS232 communication) enables a microcontroller to be connected to another microcontroller or to a PC using a serial cable. Some
www.newnespress.com
10
Chapter 1
microcontrollers have built-in hardware called USART (universal synchronousasynchronous receiver-transmitter) to implement a serial communication interface. The user program can usually select the baud rate and data format. If no serial input-output hardware is provided, it is easy to develop software to implement serial data communication using any I/O pin of a microcontroller. The PIC18F series of microcontrollers has built-in USART modules. We shall see in Chapter 6 how to write mikroC programs to implement serial communication with and without a USART module. Some microcontrollers (e.g., the PIC18F series) incorporate SPI (serial peripheral interface) or I2C (integrated interconnect) hardware bus interfaces. These enable a microcontroller to interface with other compatible devices easily.
1.3.10
EEPROM Data Memory
EEPROM-type data memory is also very common in many microcontrollers. The advantage of an EEPROM memory is that the programmer can store nonvolatile data there and change this data whenever required. For example, in a temperature monitoring application, the maximum and minimum temperature readings can be stored in an EEPROM memory. If the power supply is removed for any reason, the values of the latest readings are available in the EEPROM memory. The PIC18F452 microcontroller has 256 bytes of EEPROM memory. Other members of the PIC18F family have more EEPROM memory (e.g., the PIC18F6680 has 1024 bytes). The mikroC language provides special instructions for reading and writing to the EEPROM memory of a PIC microcontroller.
1.3.11
LCD Drivers
LCD drivers enable a microcontroller to be connected to an external LCD display directly. These drivers are not common since most of the functions they provide can be implemented in software. For example, the PIC18F6490 microcontroller has a built-in LCD driver module.
1.3.12
Analog Comparator
Analog comparators are used where two analog voltages need to be compared. Although these circuits are implemented in most high-end PIC microcontrollers, they are not common in other microcontrollers. The PIC18F series of microcontrollers has built-in analog comparator modules.
www.newnespress.com
Microcomputer Systems
1.3.13
11
Real-time Clock
A real-time clock enables a microcontroller to receive absolute date and time information continuously. Built-in real-time clocks are not common in most microcontrollers, since the same function can easily be implemented by either a dedicated real-time clock chip or a program written for this purpose.
1.3.14
Sleep Mode
Some microcontrollers (e.g., PICs) offer built-in sleep modes, where executing this instruction stops the internal oscillator and reduces power consumption to an extremely low level. The sleep mode’s main purpose is to conserve battery power when the microcontroller is not doing anything useful. The microcontroller is usually woken up from sleep mode by an external reset or a watchdog time-out.
1.3.15
Power-on Reset
Some microcontrollers (e.g., PICs) have built-in power-on reset circuits which keep the microcontroller in the reset state until all the internal circuitry has been initialized. This feature is very useful, as it starts the microcontroller from a known state on power-up. An external reset can also be provided, where the microcontroller is reset when an external button is pressed.
1.3.16
Low-Power Operation
Low-power operation is especially important in portable applications where microcontroller-based equipment is operated from batteries. Some microcontrollers (e.g., PICs) can operate with less than 2mA with a 5V supply, and around 15mA at a 3V supply. Other microcontrollers, especially microprocessor-based systems with several chips, may consume several hundred milliamperes or even more.
1.3.17
Current Sink/Source Capability
Current sink/source capability is important if the microcontroller is to be connected to an external device that might draw a large amount of current to operate. PIC microcontrollers can source and sink 25mA of current from each output port pin. This current is usually sufficient to drive LEDs, small lamps, buzzers, small relays, etc. The
www.newnespress.com
12
Chapter 1
current capability can be increased by connecting external transistor switching circuits or relays to the output port pins.
1.3.18
USB Interface
USB is currently a very popular computer interface specification used to connect various peripheral devices to computers and microcontrollers. Some PIC microcontrollers provide built-in USB modules. The PIC18F2x50, for example, has built-in USB interface capabilities.
1.3.19
Motor Control Interface
Some PIC microcontrollers, for example the PIC18F2x31, provide motor control interface capability.
1.3.20
CAN Interface
CAN bus is a very popular bus system used mainly in automation applications. Some PIC18F-series microcontrollers (e.g., the PIC18F4680) provide CAN interface capability.
1.3.21
Ethernet Interface
Some PIC microcontrollers (e.g., the PIC18F97J60) provide Ethernet interface capabilities and thus are easily used in network-based applications.
1.3.22
ZigBee Interface
ZigBee, an interface similar to Bluetooth, is used in low-cost wireless home automation applications. Some PIC18F-series microcontrollers provide ZigBee interface capabilities, making the design of such wireless systems very easy.
1.4 Microcontroller Architectures Two types of architectures are conventional in microcontrollers (see Figure 1.4). Von Neumann architecture, used by a large percentage of microcontrollers, places all memory space on the same bus; instruction and data also use the same bus.
www.newnespress.com
Microcomputer Systems
Data memory
CPU
a) Von Neumann architecture
Program memory
CPU
13
Program memory
b) Harvard architecture
Figure 1.4: Von Neumann and Harvard architectures
In Harvard architecture (used by PIC microcontrollers), code and data are on separate buses, which allows them to be fetched simultaneously, resulting in an improved performance.
1.4.1
RISC and CISC
RISC (reduced instruction set computer) and CISC (complex instruction computer) refer to the instruction set of a microcontroller. In an 8-bit RISC microcontroller, data is 8 bits wide but the instruction words are more than 8 bits wide (usually 12, 14, or 16 bits) and the instructions occupy one word in the program memory. Thus the instructions are fetched and executed in one cycle, which improves performance. In a CISC microcontroller, both data and instructions are 8 bits wide. CISC microcontrollers usually have over two hundred instructions. Data and code are on the same bus and cannot be fetched simultaneously.
1.5 Number Systems To use a microprocessor or microcontroller efficiently requires a working knowledge of binary, decimal, and hexadecimal numbering systems. This section provides background information about these numbering systems for readers who are unfamiliar with them or do not know how to convert from one number system to another. Number systems are classified according to their bases. The numbering system used in everyday life is base 10, or the decimal number system. The numbering system most
www.newnespress.com
14
Chapter 1
commonly used in microprocessor and microcontroller applications is base 16, or hexadecimal. Base 2, or binary, and base 8, or octal, number systems are also used.
1.5.1
Decimal Number System
The numbers in the decimal number system, of course, are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. The subscript 10 indicates that a number is in decimal format. For example, the decimal number 235 is shown as 23510. In general, a decimal number is represented as follows: an 10n þ an
1
10n
1
þ an
2
10n
2
þ ::::::::: þ a0 100
For example, decimal number 82510 can be shown as: 82510 ¼ 8 102 þ 2 101 þ 5 100 Similarly, decimal number 2610 can be shown as: 2610 ¼ 2 101 þ 6 100 or 335910 ¼ 3 103 þ 3 102 þ 5 101 þ 9 100
1.5.2
Binary Number System
The binary number system consists of two numbers: 0 and 1. A subscript 2 indicates that a number is in binary format. For example, the binary number 1011 would be 10112. In general, a binary number is represented as follows: a n 2n þ a n
1
2n
1
þ an
2
2n
2
þ ::::::::: þ a0 20
For example, binary number 11102 can be shown as: 11102 ¼ 1 23 þ 1 22 þ 1 21 þ 0 20
www.newnespress.com
Microcomputer Systems
15
Similarly, binary number 100011102 can be shown as: 100011102 ¼ 1 27 þ 0 26 þ 0 25 þ 0 24 þ 1 23 þ 1 22 þ 1 21 þ 0 20
1.5.3
Octal Number System
In the octal number system, the valid numbers are 0, 1, 2, 3, 4, 5, 6, 7. A subscript 8 indicates that a number is in octal format. For example, the octal number 23 appears as 238. In general, an octal number is represented as: a n 8n þ a n
1
8n
1
þ an
2
8n
2
þ ::::::::: þ a0 80
For example, octal number 2378 can be shown as: 2378 ¼ 2 82 þ 3 81 þ 7 80 Similarly, octal number 17778 can be shown as: 17778 ¼ 1 83 þ 7 82 þ 7 81 þ 7 80
1.5.4
Hexadecimal Number System
In the hexadecimal number system, the valid numbers are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. A subscript 16 or subscript H indicates that a number is in hexadecimal format. For example, hexadecimal number 1F can be written as 1F16 or as 1FH. In general, a hexadecimal number is represented as: an 16n þ an
1
16n
1
þ an
2
16n
2
þ ::::::::: þ a0 160
For example, hexadecimal number 2AC16 can be shown as: 2AC16 ¼ 2 162 þ 10 161 þ 12 160 Similarly, hexadecimal number 3FFE16 can be shown as: 3FFE16 ¼ 3 163 þ 15 162 þ 15 161 þ 14 160
www.newnespress.com
16
Chapter 1
1.6 Converting Binary Numbers into Decimal To convert a binary number into decimal, write the number as the sum of the powers of 2. Example 1.1 Convert binary number 10112 into decimal. Solution 1.1 Write the number as the sum of the powers of 2: 10112 ¼ 1 23 þ 0 22 þ 1 21 þ 1 20 ¼ 8þ0þ2þ1 ¼ 11 or; 10112 ¼ 1110 Example 1.2 Convert binary number 110011102 into decimal. Solution 1.2 Write the number as the sum of the powers of 2: 110011102 ¼ 1 27 þ 1 26 þ 0 25 þ 0 24 þ 1 23 þ 1 22 þ 1 21 þ 0 20 ¼ 128 þ 64 þ 0 þ 0 þ 8 þ 4 þ 2 þ 0 ¼ 206 or; 110011102 ¼ 20610 Table 1.1 shows the decimal equivalent of numbers from 0 to 31.
1.7 Converting Decimal Numbers into Binary To convert a decimal number into binary, divide the number repeatedly by 2 and take the remainders. The first remainder is the least significant digit (LSD), and the last remainder is the most significant digit (MSD). Example 1.3 Convert decimal number 2810 into binary.
www.newnespress.com
Microcomputer Systems
17
Table 1.1: Decimal equivalent of binary numbers Binary
Decimal
Binary
Decimal
00000000
0
00010000
16
00000001
1
00010001
17
00000010
2
00010010
18
00000011
3
00010011
19
00000100
4
00010100
20
00000101
5
00010101
21
00000110
6
00010110
22
00000111
7
00010111
23
00001000
8
00011000
24
00001001
9
00011001
25
00001010
10
00011010
26
00001011
11
00011011
27
00001100
12
00011100
28
00001101
13
00011101
29
00001110
14
00011110
30
00001111
15
00011111
31
Solution 1.3 Divide the number into 2 repeatedly and take the remainders: 28/2 14/2 7/2 3/2 1/2
! ! ! ! !
14 7 3 1 0
Remainder Remainder Remainder Remainder Remeinder
0 (LSD) 0 1 1 1 (MSD)
The binary number is 111002.
www.newnespress.com
18
Chapter 1
Example 1.4 Convert decimal number 6510 into binary. Solution 1.4 Divide the number into 2 repeatedly and take the remainders: 65/2 32/2 16/2 8/2 4/2 2/2 1/2
! 32 ! 16 ! 8 ! 4 ! 2 ! 1 ! 0
Remainder Remainder Remainder Remainder Remainder Remainder Remainder
1 (LSD) 0 0 0 0 0 1 (MSD)
The binary number is 10000012. Example 1.5 Convert decimal number 12210 into binary. Solution 1.5 Divide the number into 2 repeatedly and take the remainders: 122/2 61/2 30/2 15/2 7/2 3/2 1/2
! ! ! ! ! ! !
61 30 15 7 3 1 0
Remainder 0 (LSD) Remainder 1 Remainder 0 Remainder 1 Remainder 1 Remainder 1 Remainder 1 (MSD)
The binary number is 11110102.
1.8 Converting Binary Numbers into Hexadecimal To convert a binary number into hexadecimal, arrange the number in groups of four and find the hexadecimal equivalent of each group. If the number cannot be divided exactly into groups of four, insert zeros to the left of the number as needed so the number of digits are divisible by four.
www.newnespress.com
Microcomputer Systems
19
Example 1.6 Convert binary number 100111112 into hexadecimal. Solution 1.6 First, divide the number into groups of four, then find the hexadecimal equivalent of each group: 10011111 = 1001 1111 9 F
The hexadecimal number is 9F16. Example 1.7 Convert binary number 11101111000011102 into hexadecimal. Solution 1.7 First, divide the number into groups of four, then find the hexadecimal equivalent of each group: 1110111100001110 = 1110 1111 0000 1110 E F 0 E
The hexadecimal number is EF0E16. Example 1.8 Convert binary number 1111102 into hexadecimal. Solution 1.8 Since the number cannot be divided exactly into groups of four, we have to insert, in this case, two zeros to the left of the number so the number of digits is divisible by four: 111110 = 0011 1110 3 E
The hexadecimal number is 3E16. Table 1.2 shows the hexadecimal equivalent of numbers 0 to 31.
www.newnespress.com
20
Chapter 1 Table 1.2: Hexadecimal equivalent of decimal numbers Decimal
Hexadecimal
Decimal
Hexadecimal
0
0
16
10
1
1
17
11
2
2
18
12
3
3
19
13
4
4
20
14
5
5
21
15
6
6
22
16
7
7
23
17
8
8
24
18
9
9
25
19
10
A
26
1A
11
B
27
1B
12
C
28
1C
13
D
29
1D
14
E
30
1E
15
F
31
1F
1.9 Converting Hexadecimal Numbers into Binary To convert a hexadecimal number into binary, write the 4-bit binary equivalent of each hexadecimal digit. Example 1.9 Convert hexadecimal number A916 into binary.
www.newnespress.com
Microcomputer Systems
21
Solution 1.9 Writing the binary equivalent of each hexadecimal digit: A = 10102
9 = 10012
The binary number is 101010012. Example 1.10 Convert hexadecimal number FE3C16 into binary. Solution 1.10 Writing the binary equivalent of each hexadecimal digit: F = 11112
E = 11102
3 = 00112
C = 11002
The binary number is 11111110001111002.
1.10 Converting Hexadecimal Numbers into Decimal To convert a hexadecimal number into decimal, calculate the sum of the powers of 16 of the number. Example 1.11 Convert hexadecimal number 2AC16 into decimal. Solution 1.11 Calculating the sum of the powers of 16 of the number: 2AC16 ¼ 2 162 þ 10 161 þ 12 160 ¼ 512 þ 160 þ 12 ¼ 684 The required decimal number is 68410. Example 1.12 Convert hexadecimal number EE16 into decimal.
www.newnespress.com
22
Chapter 1
Solution 1.12 Calculating the sum of the powers of 16 of the number: EE16 ¼ 14 161 þ 14 160 ¼ 224 þ 14 ¼ 238 The decimal number is 23810.
1.11 Converting Decimal Numbers into Hexadecimal To convert a decimal number into hexadecimal, divide the number repeatedly by 16 and take the remainders. The first remainder is the LSD, and the last remainder is the MSD. Example 1.13 Convert decimal number 23810 into hexadecimal. Solution 1.13 Dividing the number repeatedly by 16: 238/16 14/16
! 14 ! 0
Remainder 14 (E) (LSD) Remainder 14 (E) (MSD)
The hexadecimal number is EE16. Example 1.14 Convert decimal number 68410 into hexadecimal. Solution 1.14 Dividing the number repeatedly by 16: 684/16 42/16 2/16
! 42 ! 2 ! 0
Remainder 12 (C) (LSD) Remainder 10 (A) Remainder 2 (MSD)
The hexadecimal number is 2AC16.
www.newnespress.com
Microcomputer Systems
23
1.12 Converting Octal Numbers into Decimal To convert an octal number into decimal, calculate the sum of the powers of 8 of the number. Example 1.15 Convert octal number 158 into decimal. Solution 1.15 Calculating the sum of the powers of 8 of the number: 158 ¼ 1 81 þ 5 80 ¼ 8þ5 ¼ 13 The decimal number is 1310. Example 1.16 Convert octal number 2378 into decimal. Solution 1.16 Calculating the sum of the powers of 8 of the number: 2378 ¼ 2 82 þ 3 81 þ 7 80 ¼ 128 þ 24 þ 7 ¼ 159 The decimal number is 15910.
1.13 Converting Decimal Numbers into Octal To convert a decimal number into octal, divide the number repeatedly by 8 and take the remainders. The first remainder is the LSD, and the last remainder is the MSD. Example 1.17 Convert decimal number 15910 into octal.
www.newnespress.com
24
Chapter 1
Solution 1.17 Dividing the number repeatedly by 8: 159/8 ! 19 Remainder 7 (LSD) 19/8 ! 2 Remainder 3 2/8 ! 0 Remainder 2 (MSD)
The octal number is 2378. Example 1.18 Convert decimal number 46010 into octal. Solution 1.18 Dividing the number repeatedly by 8: 460/8 57/8 7/8
! 57 ! 7 ! 0
Remainder 4 (LSD) Remainder 1 Remainder 7 (MSD)
The octal number is 7148. Table 1.3 shows the octal equivalent of decimal numbers 0 to 31.
1.14 Converting Octal Numbers into Binary To convert an octal number into binary, write the 3-bit binary equivalent of each octal digit. Example 1.19 Convert octal number 1778 into binary. Solution 1.19 Write the binary equivalent of each octal digit: 1 = 0012
7 = 1112
7 = 1112
The binary number is 0011111112.
www.newnespress.com
Microcomputer Systems
25
Table 1.3: Octal equivalent of decimal numbers Decimal
Octal
Decimal
Octal
0
0
16
20
1
1
17
21
2
2
18
22
3
3
19
23
4
4
20
24
5
5
21
25
6
6
22
26
7
7
23
27
8
10
24
30
9
11
25
31
10
12
26
32
11
13
27
33
12
14
28
34
13
15
29
35
14
16
30
36
15
17
31
37
Example 1.20 Convert octal number 758 into binary. Solution 1.20 Write the binary equivalent of each octal digit: 7 = 1112
5 = 1012
The binary number is 1111012.
www.newnespress.com
26
Chapter 1
1.15 Converting Binary Numbers into Octal To convert a binary number into octal, arrange the number in groups of three and write the octal equivalent of each digit. Example 1.21 Convert binary number 1101110012 into octal. Solution 1.21 Arranging in groups of three: 110111001 = 110 111 001 6 7 1
The octal number is 6718.
1.16 Negative Numbers The most significant bit of a binary number is usually used as the sign bit. By convention, for positive numbers this bit is 0, and for negative numbers this bit is 1. Figure 1.5 shows the 4-bit positive and negative numbers. The largest positive and negative numbers are þ7 and 8 respectively. Binary number 0111 0110 0101 0100 0011 0010 0001 0000 1111 1110 1101 1100 1011 1010 1001 1000
Decimal equivalent +7 +6 +5 +4 +3 +2 +1 0 −1 −2 −3 −4 −5 −6 −7 −8
Figure 1.5: 4-bit positive and negative numbers
www.newnespress.com
Microcomputer Systems
27
To convert a positive number to negative, take the complement of the number and add 1. This process is also called the 2’s complement of the number. Example 1.22 Write decimal number
6 as a 4-bit number.
Solution 1.22 First, write the number as a positive number, then find the complement and add 1: 0110 1001 1 –––– 1010
þ6 complement add 1 which is
6
Example 1.23 Write decimal number
25 as a 8-bit number.
Solution 1.23 First, write the number as a positive number, then find the complement and add 1: 00011001 þ25 11100110 complement 1 add 1 ––––––– 11100111 which is 25
1.17 Adding Binary Numbers The addition of binary numbers is similar to the addition of decimal numbers. Numbers in each column are added together with a possible carry from a previous column. The primitive addition operations are:
www.newnespress.com
28 0 0 1 1 1
þ þ þ þ þ
Chapter 1 0 1 0 1 1
=0 =1 =1 = 10 þ 1 = 11
generate a carry bit generate a carry bit
Some examples follow. Example 1.24 Find the sum of binary numbers 011 and 110. Solution 1.24 We can add these numbers as in the addition of decimal numbers: 011 þ 110 ––––– 1001
First column: 1þ0¼1 Second column: 1 þ 1 ¼ 0 and a carry bit Third column: 1 þ 1 ¼ 10
Example 1.25 Find the sum of binary numbers 01000011 and 00100010. Solution 1.25 We can add these numbers as in the addition of decimal numbers: 01000011 þ 00100010 –––––––––01100101
First column: Second column: Third column: Fourth column: Fifth column: Sixth column: Seventh column: Eighth column:
www.newnespress.com
1þ0¼1 1 þ 1 ¼ 10 0 þ carry ¼ 1 0þ0¼0 0þ0¼0 0þ1¼1 1þ0¼1 0þ0¼0
Microcomputer Systems
29
1.18 Subtracting Binary Numbers To subtract one binary number from another, convert the number to be subtracted into negative and then add the two numbers. Example 1.26 Subtract binary number 0010 from 0110. Solution 1.26 First, convert the number to be subtracted into negative: 0010 number to be subtracted 1101 complement 1 add 1 –––– 1110 Now add the two numbers: 0110 þ 1110 –––––– 0100
Since we are using only 4 bits, we cannot show the carry bit.
1.19
Multiplication of Binary Numbers
Multiplication of two binary numbers is similar to the multiplication of two decimal numbers. The four possibilities are: 0 0 1 1
0 1 0 1
¼ ¼ ¼ ¼
0 0 0 1
Some examples follow.
www.newnespress.com
30
Chapter 1
Example 1.27 Multiply the two binary numbers 0110 and 0010. Solution 1.27 Multiplying the numbers: 0110 0010 -- -- -- -0000 0110 0000 0000 -- -- -- -- -- -001100 or 1100 In this example 4 bits are needed to show the final result.
Example 1.28 Multiply binary numbers 1001 and 1010. Solution 1.28 Multiplying the numbers: 1001 1010 -- -- -- -0000 1001 0000 1001 -- -- -- -- -- -1011010 In this example 7 bits are required to show the final result.
www.newnespress.com
Microcomputer Systems
31
1.20 Division of Binary Numbers Division with binary numbers is similar to division with decimal numbers. An example follows. Example 1.29 Divide binary number 1110 into binary number 10. Solution 1.29 Dividing the numbers: 111 10 j――― 1110 10 -- -- -- -11 10 -- -- -- -10 10 -- -- -- -00 gives the result 1112.
1.21 Floating Point Numbers Floating point numbers are used to represent noninteger fractional numbers, for example, 3.256, 2.1, 0.0036, and so forth. Floating point numbers are used in most engineering and technical calculations. The most common floating point standard is the IEEE standard, according to which floating point numbers are represented with 32 bits (single precision) or 64 bits (double precision). In this section we are looking at the format of 32-bit floating point numbers only and seeing how mathematical operations can be performed with such numbers.
www.newnespress.com
32
Chapter 1
According to the IEEE standard, 32-bit floating point numbers are represented as: 31 30 23 22 0 X XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXX " " " sign exponent mantissa The most significant bit indicates the sign of the number, where 0 indicates the number is positive, and 1 indicates it is negative. The 8-bit exponent shows the power of the number. To make the calculations easy, the sign of the exponent is not shown; instead, the excess-128 numbering system is used. Thus, to find the real exponent we have to subtract 127 from the given exponent. For example, if the mantissa is “10000000,” the real value of the mantissa is 128 – 127 ¼ 1. The mantissa is 23 bits wide and represents the increasing negative powers of 2. For example, if we assume that the mantissa is “1110000000000000000000,” the value of this mantissa is calculated as 2 1 þ 2 2 þ 2 3 ¼ 7/8. The decimal equivalent of a floating point number can be calculated using the formula: Number ¼ ð 1Þs 2e
127
1:f
where s ¼ 0 for positive numbers, 1 for negative numbers e ¼ exponent (between 0 and 255) f ¼ mantissa As shown in this formula, there is a hidden 1 in front of the mantissa (i.e, the mantissa is shown as 1.f ). The largest number in 32-bit floating point format is: 0 11111110 11111111111111111111111
This number is (2 – 2 23) 2127 or decimal 3.403 1038. The numbers keep their precision up to 6 digits after the decimal point.
www.newnespress.com
Microcomputer Systems
33
The smallest number in 32-bit floating point format is: 0 00000001 00000000000000000000000
This number is 2
126
or decimal 1.175 10
38
.
1.22 Converting a Floating Point Number into Decimal To convert a given floating point number into decimal, we have to find the mantissa and the exponent of the number and then convert into decimal as just shown. Some examples are given here. Example 1.30 Find the decimal equivalent of the floating point number: 0 10000001 10000000000000000000000 Solution 1.30 Here sign = positive exponent = 129 – 127 = 2 mantissa = 2-1 = 0.5
The decimal equivalent of this number is þ1.5 22 ¼ þ6.0. Example 1.31 Find the decimal equivalent of the floating point number: 0 10000010 11000000000000000000 Solution 1.31 In this example, sign = positive exponent = 130 – 127 = 3 mantissa = 2-1 þ 2-2 = 0.75
The decimal equivalent of the number is þ1.75 23 ¼ 14.0.
www.newnespress.com
34
Chapter 1
1.22.1 Normalizing Floating Point Numbers Floating point numbers are usually shown in normalized form. A normalized number has only one digit before the decimal point (a hidden number 1 is assumed before the decimal point). To normalize a given floating point number, we have to move the decimal point repeatedly one digit to the left and increase the exponent after each move. Some examples follow. Example 1.32 Normalize the floating point number 123.56 Solution 1.32 If we write the number with a single digit before the decimal point we get: 1.2356 x 102
Example 1.33 Normalize the binary number 1011.12 Solution 1.33 If we write the number with a single digit before the decimal point we get: 1.0111 x 23
1.22.2 Converting a Decimal Number into Floating Point To convert a given decimal number into floating point, carry out the following steps: Write the number in binary. Normalize the number. Find the mantissa and the exponent. Write the number as a floating point number. Some examples follow:
www.newnespress.com
Microcomputer Systems
35
Example 1.34 Convert decimal number 2.2510 into floating point. Solution 1.34 Write the number in binary: 2:2510 ¼ 10:012 Normalize the number: 10:012 ¼ 1:0012 21 Here, s ¼ 0, e – 127 ¼ 1 or e ¼ 128, and f ¼ 00100000000000000000000. (Remember that a number 1 is assumed on the left side, even though it is not shown in the calculation). The required floating point number can be written as: s 0
e 10000000
f (1)001 0000 0000 0000 0000 0000
or, the required 32-bit floating point number is: 01000000000100000000000000000000
Example 1.35 Convert the decimal number 134.062510 into floating point. Solution 1.35 Write the number in binary: 134.062510 = 10000110.0001
Normalize the number: 10000110.0001 = 1.00001100001 x 27
Here, s ¼ 0, e – 127 ¼ 7 or e ¼ 134, and f ¼ 00001100001000000000000.
www.newnespress.com
36
Chapter 1
The required floating point number can be written as: s 0
e 10000110
f (1)00001100001000000000000
or, the required 32-bit floating point number is: 01000011000001100001000000000000
1.22.3 Multiplication and Division of Floating Point Numbers Multiplication and division of floating point numbers are rather easy. Here are the steps: Add (or subtract) the exponents of the numbers. Multiply (or divide) the mantissa of the numbers. Correct the exponent. Normalize the number. The sign of the result is the EXOR of the signs of the two numbers. Since the exponent is processed twice in the calculations, we have to subtract 127 from the exponent. An example showing the multiplication of two floating point numbers follows. Example 1.36 Show the decimal numbers 0.510 and 0.7510 in floating point and then calculate their multiplication. Solution 1.36 Convert the numbers into floating point as: 0.510 = 1.0000 x 2-1 here, s = 0, e – 127 = -1 or e = 126 and f = 0000 or, 0.510 = 0 01110110 (1)000 0000 0000 0000 0000 0000 Similarly, 0.7510 = 1.1000 x 2-1
www.newnespress.com
Microcomputer Systems
37
here, s = 0, e = 126 and f = 1000 or, 0.7510 = 0 01110110 (1)100 0000 0000 0000 0000 0000
Multiplying the mantissas results in “(1)100 0000 0000 0000 0000 0000.” The sum of the exponents is 126 þ 126 ¼ 252. Subtracting 127 from the mantissa, we obtain 252 – 127 ¼ 125. The EXOR of the signs of the numbers is 0. Thus, the result can be shown in floating point as: 0
01111101
(1)100 0000 0000 0000 0000 0000
This number is equivalent to decimal 0.375 (0.5 0.75 ¼ 0.375), which is the correct result.
1.22.4 Addition and Subtraction of Floating Point Numbers The exponents of floating point numbers must be the same before they can be added or subtracted. The steps to add or subtract floating point numbers are: Shift the smaller number to the right until the exponents of both numbers are the same. Increment the exponent of the smaller number after each shift. Add (or subtract) the mantissa of each number as an integer calculation, without considering the decimal points. Normalize the result.
An example follows. Example 1.37 Show decimal numbers 0.510 and 0.7510 in floating point and then calculate the sum of these numbers. Solution 1.37 As shown in Example 1.36, we can convert the numbers into floating point as: 0.510 = 0 01110110
(1)000 0000 0000 0000 0000 0000
www.newnespress.com
38
Chapter 1
Similarly, 0.7510 = 0 01110110 (1)100 0000 0000 0000 0000 0000
Since the exponents of both numbers are the same, there is no need to shift the smaller number. If we add the mantissa of the numbers without considering the decimal points, we get: (1)000 0000 0000 0000 0000 0000 (1)100 0000 0000 0000 0000 0000 þ (10)100 0000 0000 0000 0000 0000
To normalize the number, shift it right by one digit and then increment its exponent. The resulting number is: 0 01111111
(1)010 0000 0000 0000 0000 0000
This floating point number is equal to decimal number 1.25, which is the sum of decimal numbers 0.5 and 0.75. A program for converting floating point numbers into decimal, and decimal numbers into floating point, is available for free on the following web site: http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html
1.23 BCD Numbers BCD (binary coded decimal) numbers are usually used in display systems such as LCDs and 7-segment displays to show numeric values. In BCD, each digit is a 4-bit number from 0 to 9. As an example, Table 1.4 shows the BCD numbers between 0 and 20. Example 1.38 Write the decimal number 295 as a BCD number. Solution 1.38 Write the 4-bit binary equivalent of each digit: 2 = 00102
9 = 10012
5 = 01012
The BCD number is 0010 1001 01012.
www.newnespress.com
Microcomputer Systems
39
Table 1.4: BCD numbers between 0 and 20 Decimal
BCD
Binary
0
0000
0000
1
0001
0001
2
0010
0010
3
0011
0011
4
0100
0100
5
0101
0101
6
0110
0110
7
0111
0111
8
1000
1000
9
1001
1001
10
0001 0000
1010
11
0001 0001
1011
12
0001 0010
1100
13
0001 0011
1101
14
0001 0100
1110
15
0001 0101
1111
16
0001 0110
1 0000
17
0001 0111
1 0001
18
0001 1000
1 0010
19
0001 1001
1 0011
20
0010 0000
1 0100
www.newnespress.com
40
Chapter 1
Example 1.39 Write the decimal equivalent of BCD number 1001 1001 0110 00012. Solution 1.39 Writing the decimal equivalent of each group of 4-bit yields the decimal number: 9961
1.24 Summary Chapter 1 has provided an introduction to the microprocessor and microcontroller systems. The basic building blocks of microcontrollers were described briefly. The chapter also provided an introduction to various number systems, and described how to convert a given number from one base into another. The important topics of floating point numbers and floating point arithmetic were also described with examples.
1.25 Exercises 1. What is a microcontroller? What is a microprocessor? Explain the main difference between a microprocessor and a microcontroller. 2. Identify some applications of microcontrollers around you. 3. Where would you use an EPROM memory? 4. Where would you use a RAM memory? 5. Explain the types of memory usually used in microcontrollers. 6. What is an input-output port? 7. What is an analog-to-digital converter? Give an example of how this converter is used. 8. Explain why a watchdog timer could be useful in a real-time system. 9. What is serial input-output? Where would you use serial communication? 10.
Why is the current sink/source capability important in the specification of an output port pin?
www.newnespress.com
Microcomputer Systems
41
11.
What is an interrupt? Explain what happens when an interrupt is recognized by a microcontroller?
12.
Why is brown-out detection important in real-time systems?
13.
Explain the difference between an RISC-based microcontroller and a CISC-based microcontroller. What type of microcontroller is PIC?
14.
Convert the following decimal numbers into binary:
15.
a) 23
b) 128
c) 255
e) 120
f) 32000 g) 160
d) 1023 h) 250
Convert the following binary numbers into decimal: a) 1111
b) 0110
c) 11110000
d) 00001111 e) 10101010 f) 10000000 16.
17.
18.
19.
20.
Convert the following octal numbers into decimal: a) 177
b) 762
c) 777
d) 123
e) 1777
f) 655
g) 177777 h) 207
Convert the following decimal numbers into octal: a) 255
b) 1024
c) 129
d) 2450
e) 4096
f) 256
g) 180
h) 4096
Convert the following hexadecimal numbers into decimal: a) AA
b) EF
c) 1FF
d) FFFF
e) 1AA
f) FEF
g) F0
h) CC
Convert the following binary numbers into hexadecimal: a) 0101
b) 11111111
c) 1111
d) 1010
e) 1110
f) 10011111 g) 1001
h) 1100
Convert the following binary numbers into octal: a) 111000 b) 000111
c) 1111111
d) 010111
e) 110001 f) 11111111 g) 1000001 h) 110000
www.newnespress.com
42 21.
Chapter 1 Convert the following octal numbers into binary: a) 177
b) 7777
c) 555
d) 111
e) 1777777 f) 55571 g) 171 22.
23.
24.
Convert the following hexadecimal numbers into octal: a) AA
b) FF
c) FFFF
e) CC
f) EE
g) EEFF h) AB
d) 1AC
Convert the following octal numbers into hexadecimal: a) 177
b) 777
c) 123
d) 23
e) 1111
f) 17777777 g) 349
h) 17
Convert the following decimal numbers into floating point: a) 23.45 b) 1.25 c) 45.86
25.
h) 1777
d) 0.56
Convert the following decimal numbers into floating point and then calculate their sum: 0.255 and 1.75
26.
Convert the following decimal numbers into floating point and then calculate their product: 2.125 and 3.75
27.
Convert the following decimal numbers into BCD: a) 128
b) 970
c) 900
www.newnespress.com
d) 125
CHAPTER 2
PIC18F Microcontroller Series PIC16-series microcontrollers have been around for many years. Although these are excellent general purpose microcontrollers, they have certain limitations. For example, the program and data memory capacities are limited, the stack is small, and the interrupt structure is primitive, all interrupt sources sharing the same interrupt vector. PIC16series microcontrollers also do not provide direct support for advanced peripheral interfaces such as USB, CAN bus, etc., and interfacing with such devices is not easy. The instruction set for these microcontrollers is also limited. For example, there are no multiplication or division instructions, and branching is rather simple, being a combination of skip and goto instructions. Microchip Inc. has developed the PIC18 series of microcontrollers for use in high-pincount, high-density, and complex applications. The PIC18F microcontrollers offer costefficient solutions for general purpose applications written in C that use a real-time operating system (RTOS) and require a complex communication protocol stack such as TCP/IP, CAN, USB, or ZigBee. PIC18F devices provide flash program memory in sizes from 8 to 128Kbytes and data memory from 256 to 4Kbytes, operating at a range of 2.0 to 5.0 volts, at speeds from DC to 40MHz. The basic features of PIC18F-series microcontrollers are: 77 instructions PIC16 source code compatible Program memory addressing up to 2Mbytes Data memory addressing up to 4Kbytes
www.newnespress.com
44
Chapter 2 DC to 40MHz operation 8 8 hardware multiplier Interrupt priority levels 16-bit-wide instructions, 8-bit-wide data path Up to two 8-bit timers/counters Up to three 16-bit timers/counters Up to four external interrupts High current (25mA) sink/source capability Up to five capture/compare/PWM modules Master synchronous serial port module (SPI and I2C modes) Up to two USART modules Parallel slave port (PSP) Fast 10-bit analog-to-digital converter Programmable low-voltage detection (LVD) module Power-on reset (POR), power-up timer (PWRT), and oscillator start-up timer (OST) Watchdog timer (WDT) with on-chip RC oscillator In-circuit programming
In addition, some microcontrollers in the PIC18F family offer the following special features: Direct CAN 2.0 bus interface Direct USB 2.0 bus interface Direct LCD control interface TCP/IP interface ZigBee interface Direct motor control interface
www.newnespress.com
PIC18F Microcontroller Series
45
Most devices in the PIC18F family are source compatible with each other. Table 2.1 gives the characteristics of some of the popular devices in this family. This chapter offers a detailed study of the PIC18FXX2 microcontrollers. The architectures of most of the other microcontrollers in the PIC18F family are similar. The reader may be familiar with the programming and applications of the PIC16F series. Before going into the details of the PIC18F series, it is worthwhile to compare the features of the PIC18F series with those of the PIC16F series. The following are similarities between PIC16F and PIC18F: Similar packages and pinouts Similar special function register (SFR) names and functions Similar peripheral devices Table 2.1: The 18FXX2 microcontroller family Feature
PIC18F242
PIC18F252
PIC18F442
PIC18F452
Program memory (Bytes)
16K
32K
16K
32K
Data memory (Bytes)
768
1536
768
1536
EEPROM (Bytes)
256
256
256
256
I/O Ports
A,B,C
A,B,C
A,B,C,D,E
A,B,C,D,E
Timers
4
4
4
4
Interrupt sources
17
17
18
18
Capture/compare/PWM
2
2
2
2
Serial communication
MSSP USART
MSSP USART
MSSP USART
MSSP USART
A/D converter (10-bit)
5 channels
5 channels
8 channels
8 channels
Low-voltage detect
yes
yes
yes
yes
Brown-out reset
yes
yes
yes
yes
Packages
28-pin DIP 28-pin SOIC
28-pin DIP 28-pin SOIC
40-pin DIP 44-pin PLCC 44-pin TQFP
40-pin DIP 44-pin PLCC 44-pin TQFP
www.newnespress.com
46
Chapter 2 Subset of PIC18F instruction set Similar development tools
The following are new with the PIC18F series: Number of instructions doubled 16-bit instruction word Hardware 8 8 multiplier More external interrupts Priority-based interrupts Enhanced status register Increased program and data memory size Bigger stack Phase-locked loop (PLL) clock generator Enhanced input-output port architecture Set of configuration registers Higher speed of operation Lower power operation
2.1 PIC18FXX2 Architecture As shown in Table 2.1, the PIC18FXX2 series consists of four devices. PIC18F2X2 microcontrollers are 28-pin devices, while PIC18F4X2 microcontrollers are 40-pin devices. The architectures of the two groups are almost identical except that the larger devices have more input-output ports and more A/D converter channels. In this section we shall be looking at the architecture of the PIC18F452 microcontroller in detail. The architectures of other standard PIC18F-series microcontrollers are similar, and the knowledge gained in this section should be enough to understand the operation of other PIC18F-series microcontrollers. The pin configuration of the PIC18F452 microcontroller (DIP package) is shown in Figure 2.1. This is a 40-pin microcontroller housed in a DIL package, with a pin configuration similar to the popular PIC16F877.
www.newnespress.com
PIC18F Microcontroller Series
47
Figure 2.1: PIC18F452 microcontroller DIP pin configuration
Figure 2.2 shows the internal block diagram of the PIC18F452 microcontroller. The CPU is at the center of the diagram and consists of an 8-bit ALU, an 8-bit working accumulator register (WREG), and an 8 8 hardware multiplier. The higher byte and the lower byte of a multiplication are stored in two 8-bit registers called PRODH and PRODL respectively. The program counter and program memory are shown in the upper left portion of the diagram. Program memory addresses consist of 21 bits, capable of accessing 2Mbytes of program memory locations. The PIC18F452 has only 32Kbytes of program memory, which requires only 15 bits. The remaining 6 address bits are redundant and not used. A table pointer provides access to tables and to the data stored in program memory. The program memory contains a 31-level stack which is normally used to store the interrupt and subroutine return addresses. The data memory can be seen at the top center of the diagram. The data memory bus is 12 bits wide, capable of accessing 4Kbytes of data memory locations. As we shall see later, the data memory consists of special function registers (SFR) and general purpose registers, all organized in banks.
www.newnespress.com
48
Chapter 2
Figure 2.2: Block diagram of the PIC18F452 microcontroller
www.newnespress.com
PIC18F Microcontroller Series
49
The bottom portion of the diagram shows the timers/counters, capture/compare/PWM registers, USART, A/D converter, and EEPROM data memory. The PIC18F452 consists of: 4 timers/counters 2 capture/compare/PWM modules 2 serial communication modules 8 10-bit A/D converter channels 256 bytes EEPROM The oscillator circuit, located at the left side of the diagram, consists of: Power-up timer Oscillator start-up timer Power-on reset Watchdog timer Brown-out reset Low-voltage programming In-circuit debugger PLL circuit Timing generation circuit The PLL circuit is new to the PIC18F series and provides the option of multiplying up the oscillator frequency to speed up the overall operation. The watchdog timer can be used to force a restart of the microcontroller in the event of a program crash. The in-circuit debugger is useful during program development and can be used to return diagnostic data, including the register values, as the microcontroller is executing a program. The input-output ports are located at the right side of the diagram. The PIC18F452 has five parallel ports named PORTA, PORTB, PORTC, PORTD, and PORTE. Most port pins have multiple functions. For example, PORTA pins can be used as parallel inputs-outputs or analog inputs. PORTB pins can be used as parallel inputs-outputs or as interrupt inputs.
www.newnespress.com
50
Chapter 2
2.1.1
Program Memory Organization
The program memory map is shown in Figure 2.3. All PIC18F devices have a 21-bit program counter and hence are capable of addressing 2Mbytes of memory space. User memory space on the PIC18F452 microcontroller is 00000H to 7FFFH. Accessing a nonexistent memory location (8000H to 1FFFFFH) will cause a read of all 0s. The reset vector, where the program starts after a reset, is at address 0000. Addresses 0008H and
Figure 2.3: Program memory map of PIC18F452
www.newnespress.com
PIC18F Microcontroller Series
51
0018H are reserved for the vectors of high-priority and low-priority interrupts respectively, and interrupt service routines must be written to start at one of these locations. The PIC18F microcontroller has a 31-entry stack that is used to hold the return addresses for subroutine calls and interrupt processing. The stack is not part of the program or the data memory space. The stack is controlled by a 5-bit stack pointer which is initialized to 00000 after a reset. During a subroutine call (or interrupt) the stack pointer is first incremented, and the memory location it points to is written with the contents of the program counter. During the return from a subroutine call (or interrupt), the memory location the stack pointer has pointed to is decremented. The projects in this book are based on using the C language. Since subroutine and interrupt call/return operations are handled automatically by the C language compiler, their operation is not described here in more detail. Program memory is addressed in bytes, and instructions are stored as two bytes or four bytes in program memory. The least significant byte of an instruction word is always stored in an even address of the program memory. An instruction cycle consists of four cycles: A fetch cycle begins with the program counter incrementing in Q1. In the execution cycle, the fetched instruction is latched into the instruction register in cycle Q1. This instruction is decoded and executed during cycles Q2, Q3, and Q4. A data memory location is read during the Q2 cycle and written during the Q4 cycle.
2.1.2
Data Memory Organization
The data memory map of the PIC18F452 microcontroller is shown in Figure 2.4. The data memory address bus is 12 bits with the capability to address up to 4Mbytes. The memory in general consists of sixteen banks, each of 256 bytes, where only 6 banks are used. The PIC18F452 has 1536 bytes of data memory (6 banks 256 bytes each) occupying the lower end of the data memory. Bank switching happens automatically when a high-level language compiler is used, and thus the user need not worry about selecting memory banks during programming. The special function register (SFR) occupies the upper half of the top memory bank. SFR contains registers which control operations such as peripheral devices, timers/ counters, A/D converter, interrupts, and USART. Figure 2.5 shows the SFR registers of the PIC18F452 microcontroller.
www.newnespress.com
52
Chapter 2
Figure 2.4: The PIC18F452 data memory map
2.1.3
The Configuration Registers
PIC18F452 microcontrollers have a set of configuration registers (PIC16-series microcontrollers had only one configuration register). Configuration registers are programmed during the programming of the flash program memory by the programming device. These registers are shown in Table 2.2. Descriptions of
www.newnespress.com
PIC18F Microcontroller Series
53
Figure 2.5: The PIC18F452 SFR registers these registers are given in Table 2.3. Some of the more important configuration registers are described in this section in detail.
CONFIG1H The CONFIG1H configuration register is at address 300001H and is used to select the microcontroller clock sources. The bit patterns are shown in Figure 2.6.
www.newnespress.com
54
File Name
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Default/ Unprogrammed Value
FOSC2
FOSC1
FOSC0
--1- -111
300001h
CONFIG1H
—
—
OSCSEN
—
300002h
CONFIG2L
—
—
—
—
BORV1
BORV0
BOREN
PWRTEN
---- 1111
300003h
CONFIG2H
—
—
—
—
WDTPS2
WDTPS1
WDTPS0
WDTEN
---- 1111
300005h
CONFIG3H
—
—
—
—
—
—
—
CCP2MX
---- ---1
300006h
CONFIG4L
DEBUG
—
—
—
—
—
STVREN
1--- -1-1
300008h
CONFIG5L
—
—
—
—
CP0
---- 1111
300009h
CONFIG5H
CPD
CPB
—
—
30000Ah
CONFIG6L
—
—
—
—
30000Bh
CONFIG6H
WRTD
WRTB
30000Ch
CONFIG7L
—
—
—
—
30000Dh
CONFIG7H
—
EBTRB
—
—
3FFFFEh
DEVID1
DEV2
DEV1
DEV0
3FFFFFh
DEVID2
DEV10
DEV9
DEV8
WRTC
—
—
CP3 — WRT3 — EBTR3
LVP CP2 — WRT2 — EBTR2
CP1 —
—
11-- ----
WRT1
WRT0
---- 1111
—
—
111- ----
EBTR1
EBTR0
---- 1111 -1-- ----
—
—
—
—
REV4
REV3
REV2
REV1
REV0
(1)
DEV7
DEV6
DEV5
DEV4
DEV3
0000 0100
Legend: x ¼ unknown, u ¼ unchanged, – ¼ unimplemented, q ¼ value depends on condition. Shaded cells are unimplemented, read as ‘0’.
Chapter 2
www.newnespress.com
Table 2.2: PIC18F452 configuration registers
PIC18F Microcontroller Series
55
Table 2.3: PIC18F452 configuration register descriptions Configuration bits
Description
OSCSEN
Clock source switching enable
FOSC2:FOSC0
Oscillator modes
BORV1:BORV0
Brown-out reset voltage
BOREN
Brown-out reset enable
PWRTEN
Power-up timer enable
WDTPS2:WDTPS0
Watchdog timer postscale bits
WDTEN
Watchdog timer enable
CCP2MX
CCP2 multiplex
DEBUG
Debug enable
LVP
Low-voltage program enable
STVREN
Stack full/underflow reset enable
CP3:CP0
Code protection
CPD
EEPROM code protection
CPB
Boot block code protection
WRT3:WRT0
Program memory write protection
WRTD
EPROM write protection
WRTB
Boot block write protection
WRTC
Configuration register write protection
EBTR3:EBTR0
Table read protection
EBTRB
Boot block table read protection
DEV2:DEV0
Device ID bits (001 = 18F452)
REV4:REV0
Revision ID bits
DEV10:DEV3
Device ID bits
www.newnespress.com
56
Chapter 2
Figure 2.6: CONFIG1H register bits
CONFIG2L The CONFIG2L configuration register is at address 300002H and is used to select the brown-out voltage bits. The bit patterns are shown in Figure 2.7.
Figure 2.7: CONFIG2L register bits
www.newnespress.com
PIC18F Microcontroller Series
57
CONFIG2H The CONFIG2H configuration register is at address 300003H and is used to select the watchdog operations. The bit patterns are shown in Figure 2.8.
2.1.4
The Power Supply
The power supply requirements of the PIC18F452 microcontroller are shown in Figure 2.9. As shown in Figure 2.10, PIC18F452 can operate with a supply voltage of 4.2V to 5.5V at the full speed of 40MHz. The lower power version, PIC18LF452, can operate from 2.0 to 5.5 volts. At lower voltages the maximum clock frequency is 4MHz, which rises to 40MHz at 4.2V. The RAM data retention voltage is specified as 1.5V and will be lost if the power supply voltage is lowered below this value. In practice, most microcontroller-based systems are operated with a single þ5V supply derived from a suitable voltage regulator.
2.1.5
The Reset
The reset action puts the microcontroller into a known state. Resetting a PIC18F microcontroller starts execution of the program from address 0000H of the
Figure 2.8: CONFIG2H register bits
www.newnespress.com
58
Chapter 2
Figure 2.9: The PIC8F452 power supply parameters program memory. The microcontroller can be reset during one of the following operations: Power-on reset (POR) MCLR reset Watchdog timer (WDT) reset Brown-out reset (BOR) Reset instruction
www.newnespress.com
PIC18F Microcontroller Series
59
Figure 2.10: Operation of PIC18LF452 at different voltages
Stack full reset Stack underflow reset Two types of resets are commonly used: power-on reset and external reset using the MCLR pin.
Power-on Reset The power-on reset is generated automatically when power supply voltage is applied to the chip. The MCLR pin should be tied to the supply voltage directly or, preferably, through a 10K resistor. Figure 2.11 shows a typical reset circuit. For applications where the rise time of the voltage is slow, it is recommended to use a diode, a capacitor, and a series resistor as shown in Figure 2.12. In some applications the microcontroller may have to be reset externally by pressing a button. Figure 2.13 shows the circuit that can be used to reset the microcontroller externally. Normally the MCLR input is at logic 1. When the RESET button is pressed, this pin goes to logic 0 and resets the microcontroller.
www.newnespress.com
60
Chapter 2
Figure 2.11: Typical reset circuit
Figure 2.12: Reset circuit for slow-rising voltages
2.1.6
The Clock Sources
The PIC18F452 microcontroller can be operated from an external crystal or ceramic resonator connected to the microcontroller’s OSC1 and OSC2 pins. In addition, an external resistor and capacitor, an external clock source, and in some models internal oscillators can be used to provide clock pulses to the microcontroller. There are eight clock sources on the PIC18F452 microcontroller, selected by the configuration register CONFIG1H. These are: Low-power crystal (LP) Crystal or ceramic resonator (XT)
www.newnespress.com
PIC18F Microcontroller Series
61
Figure 2.13: External reset circuit High-speed crystal or ceramic resonator (HS) High-speed crystal or ceramic resonator with PLL (HSPLL) External clock with FOSC/4 on OSC2 (EC) External clock with I/O on OSC2 (port RA6) (ECIO) External resistor/capacitor with FOSC/4 output on OSC2 (RC) External resistor/capacitor with I/O on OSC2 (port RA6) (RCIO)
Crystal or Ceramic Resonator Operation The first several clock sources listed use an external crystal or ceramic resonator that is connected to the OSC1 and OSC2 pins. For applications where accuracy of timing is important, a crystal should be used. And if a crystal is used, a parallel resonant crystal must be chosen, since series resonant crystals do not oscillate when the system is first powered. Figure 2.14 shows how a crystal is connected to the microcontroller. The capacitor values depend on the mode of the crystal and the selected frequency. Table 2.4 gives the recommended values. For example, for a 4MHz crystal frequency, use 15pF capacitors. Higher capacitance increases the oscillator stability but also increases the start-up time. Resonators should be used in low-cost applications where high accuracy in timing is not required. Figure 2.15 shows how a resonator is connected to the microcontroller.
www.newnespress.com
62
Chapter 2
Figure 2.14: Using a crystal as the clock input Table 2.4: Capacitor values Mode LP
XT
HS
Frequency
C1,C2 (pF)
32 KHz
33
200 KHz
15
200 KHz
22–68
1.0 MHz
15
4.0 MHz
15
4.0 MHz
15
8.0 MHz
15–33
20.0 MHz
15–33
25.0 MHz
15–33
The LP (low-power) oscillator mode is advised in applications to up to 200KHz clock. The XT mode is advised to up to 4MHz, and the HS (high-speed) mode is advised in applications where the clock frequency is between 4MHz to 25MHz. An external clock source may also be connected to the OSC1 pin in the LP, XT, or HS modes as shown in Figure 2.16.
www.newnespress.com
PIC18F Microcontroller Series
63
Figure 2.15: Using a resonator as the clock input
Figure 2.16: Connecting an external clock in LP, XT, or HS modes
External Clock Operation An external clock source can be connected to the OSC1 input of the microcontroller in EC and ECIO modes. No oscillator start-up time is required after a power-on reset. Figure 2.17 shows the operation with the external clock in EC mode. Timing pulses at the frequency FOSC/4 are available on the OSC2 pin. These pulses can be used for test purposes or to provide pulses to external devices. The ECIO mode is similar to the EC mode, except that the OSC2 pin can be used as a general purpose digital I/O pin. As shown in Figure 2.18, this pin becomes bit 6 of PORTA (i.e., pin RA6).
www.newnespress.com
64
Chapter 2
Figure 2.17: External clock in EC mode
Figure 2.18: External clock in ECIO mode
Resistor/Capacitor Operation In the many applications where accurate timing is not required we can use an external resistor and a capacitor to provide clock pulses. The clock frequency is a function of the resistor, the capacitor, the power supply voltage, and the temperature. The clock frequency is not accurate and can vary from unit to unit due to manufacturing and component tolerances. Table 2.5 gives the approximate clock frequency with various resistor and capacitor combinations. A close approximation of the clock frequency is 1/(4.2RC), where R should be between 3K and 100K and C should be greater than 20pF. In RC mode, the oscillator frequency divided by 4 (FOSC/4) is available on pin OSC2 of the microcontroller. Figure 2.19 shows the operation at a clock frequency of approximately 2MHz, where R ¼ 3.9K and C ¼ 30pF. In this application the clock frequency at the output of OSC2 is 2MHz/4 ¼ 500KHz.
www.newnespress.com
PIC18F Microcontroller Series
65
Table 2.5: Clock frequency with RC C (pF)
R (K)
22
3.3
3.3
4.7
2.3
10
1.08
3.3
2.4
4.7
1.7
10
0.793
30
Frequency (MHz)
Figure 2.19: 2MHz clock in RC mode RCIO mode is similar to RC mode, except that the OSC2 pin can be used as a general purpose digital I/O pin. As shown in Figure 2.20, this pin becomes bit 6 of PORTA (i.e., pin RA6).
Crystal or Resonator with PLL One of the problems with using high-frequency crystals or resonators is electromagnetic interference. A Phase Locked Loop (PLL) circuit is provided that can be enabled to multiply the clock frequency by 4. Thus, for a crystal clock frequency of 10MHz, the
www.newnespress.com
66
Chapter 2
Figure 2.20: 2MHz clock in RCIO mode
internal operation frequency will be multiplied to 40MHz. The PLL mode is enabled when the oscillator configuration bits are programmed for HS mode.
Internal Clock Some devices in the PIC18F family have internal clock modes (although the PIC18F452 does not). In this mode, OSC1 and OSC2 pins are available for general purpose I/O (RA6 and RA7) or as FOSC/4 and RA7. An internal clock can be from 31KHz to 8MHz and is selected by registers OSCCON and OSCTUNE. Figure 2.21 shows the bits of internal clock control registers.
Clock Switching It is possible to switch the clock from the main oscillator to a low-frequency clock source. For example, the clock can be allowed to run fast in periods of intense activity and slower when there is less activity. In the PIC18F452 microcontroller this is controlled by bit SCS of the OSCCON register. In microcontrollers of the PIC18F family that do support an internal clock, clock switching is controlled by bits SCS0 and SCS1 of OSCCON. It is important to ensure that during clock switching unwanted glitches do not occur in the clock signal. PIC18F microcontrollers contain circuitry to ensure error-free switching from one frequency to another.
www.newnespress.com
PIC18F Microcontroller Series OSCCON register IDLEN IRCF2 IRCF1 IDLEN
0 1
IRCF0
OSTS
I0FS
SCSI
67
SCS0
Run mode enabled Idle mode enabled
IRCF2:IRCF0 000 001 010 011 100 101 110 111
31 KHz 125 KHz 250 KHz 500 KHz 1 MHz 2 MHz 4 MHz 8 MHz
OSTS
0 1
Oscillator start-up timer running Oscillator start-up timer expired
IOFS
0 1
Internal oscillator unstable Internal oscillator stable
SCSI:SCS0
00 01 10 11
Primary oscillator Timer 1 oscillator Internal oscillator Internal oscillator
OSCTUNE register X X T5
T4
XX011111
Maximum frequency
XX000001 XX000000 XX111111
Center frequency
XX100000
Minimum frequency
T3
T2
T1
T0
Figure 2.21: Internal clock control registers
2.1.7
Watchdog Timer
In PIC18F-series microcontrollers family members the watchdog timer (WDT) is a freerunning on-chip RC-based oscillator and does not require any external components. When the WDT times out, a device RESET is generated. If the device is in SLEEP mode, the WDT time-out will wake it up and continue with normal operation. The watchdog is enabled/disabled by bit SWDTEN of register WDTCON. Setting SWDTEN = 1 enables the WDT, and clearing this bit turns off the WDT. On the PIC18F452 microcontroller an 8-bit postscaler is used to multiply the basic time-out
www.newnespress.com
68
Chapter 2
period from 1 to 128 in powers of 2. This postscaler is controlled from configuration register CONFIG2H. The typical basic WDT time-out period is 18ms for a postscaler value of 1.
2.1.8
Parallel I/O Ports
The parallel ports in PIC18F microcontrollers are very similar to those of the PIC16 series. The number of I/O ports and port pins varies depending on which PIC18F microcontroller is used, but all of them have at least PORTA and PORTB. The pins of a port are labeled as RPn, where P is the port letter and n is the port bit number. For example, PORTA pins are labeled RA0 to RA7, PORTB pins are labeled RB0 to RB7, and so on. When working with a port we may want to: Set port direction Set an output value Read an input value Set an output value and then read back the output value The first three operations are the same in the PIC16 and the PIC18F series. In some applications we may want to send a value to the port and then read back the value just sent. The PIC16 series has a weakness in the port design such that the value read from a port may be different from the value just written to it. This is because the reading is the actual port bit pin value, and this value can be changed by external devices connected to the port pin. In the PIC18F series, a latch register (e.g., LATA for PORTA) is introduced to the I/O ports to hold the actual value sent to a port pin. Reading from the port reads the latched value, which is not affected by any external device. In this section we shall be looking at the general structure of I/O ports.
PORTA In the PIC18F452 microcontroller PORTA is 7 bits wide and port pins are shared with other functions. Table 2.6 shows the PORTA pin functions.
www.newnespress.com
PIC18F Microcontroller Series
69
Table 2.6: PIC18F452 PORTA pin functions Pin
Description
RA0/AN0 RA0
Digital I/O
AN0
Analog input 0
RA1/AN1 RA1
Digital I/O
AN1
Analog input 1
RA2/AN2/VREF RA2
Digital I/O
AN2
Analog input 2
VREF
A/D reference voltage (low) input
RA3/AN3/VREFþ RA3
Digital I/O
AN3
Analog input 3
VREFþ
A/D reference voltage (high) input
RA4/T0CKI RA4
Digital I/O
T0CKI
Timer 0 external clock input
RA5/AN4/SS/LVDIN RA5
Digital I/O
AN4
Analog input 4
SS
SPI Slave Select input
RA6
Digital I/O
www.newnespress.com
70
Chapter 2
The architecture of PORTA is shown in Figure 2.22. There are three registers associated with PORTA: Port data register—PORTA Port direction register—TRISA Port latch register—LATA
Figure 2.22: PIC18F452 PORTA RA0–RA3 and RA5 pins
www.newnespress.com
PIC18F Microcontroller Series
71
PORTA is the name of the port data register. The TRISA register defines the direction of PORTA pins, where a logic 1 in a bit position defines the pin as an input pin, and a 0 in a bit position defines it as an output pin. LATA is the output latch register which shares the same data latch as PORTA. Writing to one is equivalent to writing to the other. But reading from LATA activates the buffer at the top of the diagram, and the value held in the PORTA/LATA data latch is transferred to the data bus independent of the state of the actual output pin of the microcontroller. Bits 0 through 3 and 5 of PORTA are also used as analog inputs. After a device reset, these pins are programmed as analog inputs and RA4 and RA6 are configured as digital inputs. To program the analog inputs as digital I/O, the ADCON1 register (A/D register) must be programmed accordingly. Writing 7 to ADCON1 configures all PORTA pins as digital I/O. The RA4 pin is multiplexed with the Timer 0 clock input (T0CKI). This is a Schmitt trigger input and an open drain output. RA6 can be used as a general purpose I/O pin, as the OSC2 clock input, or as a clock output providing FOSC/4 clock pulses.
PORTB In PIC18F452 microcontroller PORTB is an 8-bit bidirectional port shared with interrupt pins and serial device programming pins. Table 2.7 gives the PORTB bit functions. PORTB is controlled by three registers: Port data register—PORTB Port direction register—TRISB Port latch register—LATB The general operation of PORTB is similar to that of PORTA. Figure 2.23 shows the architecture of PORTB. Each port pin has a weak internal pull-up which can be enabled by clearing bit RBPU of register INTCON2. These pull-ups are disabled on a power-on reset and when the port pin is configured as an output. On a power-on reset, PORTB pins are configured as digital inputs. Internal pull-ups allow input devices such as switches to be connected to PORTB pins without the use of external pull-up resistors. This saves costs because the component count and wiring requirements are reduced.
www.newnespress.com
72
Chapter 2 Table 2.7: PIC18F452 PORTB pin functions Pin
Description
RB0/INT0 RB0
Digital I/O
INT0
External interrupt 0
RB1/INT1 RB1
Digital I/O
INT1
External interrupt 1
RB2/INT2 RB2
Digital I/O
INT2
External interrupt 2
RB3/ CCP2 RB3
Digital I/O
CCP2
Capture 2 input, compare 2, and PWM2 output
RB4
Digital I/O, interrupt on change pin
RB5/PGM RB5
Digital I/O, interrupt on change pin
PGM
Low-voltage ICSP programming pin
RB6/PGC RB6
Digital I/O, interrupt on change pin
PGC
In-circuit debugger and ICSP programming pin
RB7/PGD RB7
Digital I/O, interrupt on change pin
PGD
In-circuit debugger and ICSP programming pin
www.newnespress.com
PIC18F Microcontroller Series
73
Figure 2.23: PIC18F452 PORTB RB4–RB7 pins
Port pins RB4–RB7 can be used as interrupt-on-change inputs, whereby a change on any of pins 4 through 7 causes an interrupt flag to be set. The interrupt enable and flag bits RBIE and RBIF are in register INTCON.
PORTC, PORTD, PORTE, and Beyond In addition to PORTA and PORTB, the PIC18F452 has 8-bit bidirectional ports PORTC and PORTD, and 3-bit PORTE. Each port has its own data register (e.g., PORTC), data
www.newnespress.com
74
Chapter 2
direction register (e.g., TRISC), and data latch register (e.g., LATC). The general operation of these ports is similar to that of PORTA.2.1. In the PIC18F452 microcontroller PORTC is multiplexed with several peripheral functions as shown in Table 2.8. On a power-on reset, PORTC pins are configured as digital inputs. In the PIC18F452 microcontroller, PORTD has Schmitt trigger input buffers. On a power-on reset, PORTD is configured as digital input. PORTD can be configured as an 8-bit parallel slave port (i.e., a microprocessor port) by setting bit 4 of the TRISE register. Table 2.9 shows functions of PORTD pins. In the PIC18F452 microcontroller, PORTE is only 3 bits wide. As shown in Table 2.10, port pins are shared with analog inputs and with parallel slave port read/write control bits. On a power-on reset, PORTE pins are configured as analog inputs and register ADCON1 must be programmed to change these pins to digital I/O.
2.1.9
Timers
The PIC18F452 microcontroller has four programmable timers which can be used in many tasks, such as generating timing signals, causing interrupts to be generated at specific time intervals, measuring frequency and time intervals, and so on. This section introduces the timers available in the PIC18F452 microcontroller.
Timer 0 Timer 0 is similar to the PIC16 series Timer 0, except that it can operate either in 8-bit or in 16-bit mode. Timer 0 has the following basic features: 8-bit or 16-bit operation 8-bit programmable prescaler External or internal clock source Interupt generation on overflow Timer 0 control register is T0CON, shown in Figure 2.24. The lower 6 bits of this register have similar functions to the PIC16-series OPTION register. The top two bits are used to select the 8-bit or 16-bit mode of operation and to enable/disable the timer.
www.newnespress.com
Table 2.8: PIC18F452 PORTC pin functions Pin
Description
RC0/T1OSO/T1CKI RC0
Digital I/O
T1OSO
Timer 1 oscillator output
T1CKI
Timer 1/Timer 3 external clock input
RC1/T1OSI/CCP2 RC1
Digital I/O
T1OSI
Timer 1 oscillator input
CCP2
Capture 2 input, Compare 2 and PWM2 output
RC2/CCP1 RC2
Digital I/O
CCP1
Capture 1 input, Compare 1 and PWM1 output
RC3/SCK/SCL RC3
Digital I/O
SCK
Synchronous serial clock input/output for SPI
SCL
Synchronous serial clock input/output for I2C
RC4/SDI/SDA RC4
Digital I/O
SDI
SPI data in
SDA
I2C data I/O
RC5/SDO RC5
Digital I/O
SDO
SPI data output
RC6/TX/CK RC6
Digital I/O
TX
USART transmit pin
CK
USART synchronous clock pin
RC7/RX/DT RC7
Digital I/O
RX
USART receive pin
DT
USART synchronous data pin
www.newnespress.com
76
Chapter 2 Table 2.9: PIC18F452 PORTD pin functions Pin
Description
RD0/PSP0 RD0
Digital I/O
PSP0
Parallel slave port bit 0
RD1/PSP1 RD1
Digital I/O
PSP1
Parallel slave port bit 1
RD2/PSP2 RD2
Digital I/O
PSP2
Parallel slave port bit 2
RD3/PSP3 RD3
Digital I/O
PSP3
Parallel slave port bit 3
RD4/PSP4 RD4
Digital I/O
PSP4
Parallel slave port bit 4
RD5/PSP5 RD5
Digital I/O
PSP5
Parallel slave port bit 5
RD6/PSP6 RD6
Digital I/O
PSP6
Parallel slave port bit 6
RD7/PSP7 RD7
Digital I/O
PSP7
Parallel slave port bit 7
www.newnespress.com
PIC18F Microcontroller Series
77
Table 2.10: PIC18F452 PORTE pin functions Pin
Description
RE0/RD/AN5 RE0
Digital I/O
RD
Parallel slave port read control pin
AN5
Analog input 5
RE1/WR/ AN6 RE1
Digital I/O
WR
Parallel slave port write control pin
AN6
Analog input 6
RE2/CS/AN7 RE2
Digital I/O
CS
Parallel slave port CS
AN7
Analog input 7
Timer 0 can be operated either as a timer or as a counter. Timer mode is selected by clearing the T0CS bit, and in this mode the clock to the timer is derived from FOSC/4. Counter mode is selected by setting the T0CS bit, and in this mode Timer 0 is incremented on the rising or falling edge of input RA4/T0CKI. Bit T0SE of T0CON selects the edge triggering mode. An 8-bit prescaler can be used to change the timer clock rate by a factor of up to 256. The prescaler is selected by bits PSA and T0PS2:T0PS0 of register T0CON. 8-Bit Mode Figure 2.25 shows Timer 0 in 8-bit mode. The following operations are normally carried out in a timer application: Clear T0CS to select clock FOSC/4 Use bits T0PS2:T0PS0 to select a suitable prescaler value Clear PSA to select the prescaler
www.newnespress.com
78
Chapter 2
Figure 2.24: Timer 0 control register, T0CON
Figure 2.25: Timer 0 in 8-bit mode
www.newnespress.com
PIC18F Microcontroller Series
79
Load timer register TMR0L Optionally enable Timer 0 interrupts The timer counts up and an interrupt is generated when the timer value overflows from FFH to 00H in 8-bit mode (or from FFFFH to 0000H in 16-bit mode) By loading a value into the TMR0 register we can control the count until an overflow occurs. The formula that follows can be used to calculate the time it will take for the timer to overflow (or to generate an interrupt) given the oscillator period, the value loaded into the timer, and the prescaler value: Overflow time ¼ 4 TOSC Prescaler ð256 TMR0Þ
ð2:1Þ
where Overflow time is in ms TOSC is the oscillator period in ms Prescaler is the prescaler value TMR0 is the value loaded into TMR0 register For example, assume that we are using a 4MHz crystal, and the prescaler is chosen as 1:8 by setting bits PS2:PS0 to 010. Also assume that the value loaded into the timer register TMR0 is decimal 100. The overflow time is then given by: 4MHZ clock has a period; T ¼ 1=f ¼ 0:25ms using the above formula Overflow time ¼ 4 0:25 8 ð256 100Þ ¼ 1248ms Thus, the timer will overflow after 1.248msec, and a timer interrupt will be generated if the timer interrupt and global interrupts are enabled. What we normally want is to know what value to load into the TMR0 register for a required overflow time. This can be calculated by modifying Equation (2.1) as follows: TMR0 ¼ 256 ðOverflow timeÞ=ð4 TOSC PrescalerÞ
ð2:2Þ
www.newnespress.com
80
Chapter 2
Figure 2.26: Timer 0 in 16-bit mode For example, suppose we want an interrupt to be generated after 500ms and the clock and the prescaler values are as before. The value to be loaded into the TMR0 register can be calculated using Equation (2.2) as follows: TMR0 ¼ 256 500=ð4 0:25 8Þ ¼ 193:5 The closest number we can load into TMR0 register is 193. 16-Bit Mode The Timer 0 in 16-bit mode is shown in Figure 2.26. Here, two timer registers named TMR0L and TMR0 are used to store the 16-bit timer value. The low byte TMR0L is directly loadable from the data bus. The high byte TMR0 can be loaded through a buffer called TMR0H. During a read of TMR0L, the high byte of the timer (TMR0) is also loaded into TMR0H, and thus all 16 bits of the timer value can be read. To read the 16-bit timer value, first we have to read TMR0L, and then read TMR0H in a later instruction. Similarly, during a write to TMR0L, the high byte of the timer is also updated with the contents of TMR0H, allowing all 16 bits to be written to the timer. Thus, to write to the timer the program should first write the required high byte to TMR0H. When the low byte is written to TMR0L, then the value stored in TMR0H is automatically transferred to TMR0, thus causing all 16 bits to be written to the timer.
Timer 1 PIC18F452 Timer 1 is a 16-bit timer controlled by register T1CON, as shown in Figure 2.27. Figure 2.28 shows the internal structure of Timer 1.
www.newnespress.com
PIC18F Microcontroller Series
81
Figure 2.27: Timer 1 control register, T1CON
Timer 1 can be operated as either a timer or a counter. When bit TMR1CS of register T1CON is low, clock FOSC/4 is selected for the timer. When TMR1CS is high, the module operates as a counter clocked from input T1OSI. A crystal oscillator circuit, enabled from bit T1OSCEN of T1CON, is built between pins T1OSI and T1OSO where a crystal up to 200KHz can be connected between these pins. This oscillator is primarily intended for a 32KHz crystal operation in real-time clock applications. A prescaler is used in Timer 1 that can change the timing rate as a factor of 1, 2, 4, or 8.
www.newnespress.com
82
Chapter 2
Figure 2.28: Internal structure of Timer 1
Timer 1 can be configured so that read/write can be performed either in 16-bit mode or in two 8-bit modes. Bit RD16 of register T1CON controls the mode. When RD16 is low, timer read and write operations are performed as two 8-bit operations. When RD16 is high, the timer read and write operations are as in Timer 0 16-bit mode (i.e., a buffer is used between the timer register and the data bus) (see Figure 2.29). If the Timer 1 interrupts are enabled, an interrupt will be generated when the timer value rolls over from FFFFH to 0000H.
Timer 2 Timer 2 is an 8-bit timer with the following features: 8-bit timer (TMR2) 8-bit period register (PR2) Programmable prescaler Programmable postscaler Interrupt when TM2 matches PR2 Timer 2 is controlled from register T2CON, as shown in Figure 2.30. Bits T2CKPS1: T2CKPS0 set the prescaler for a scaling of 1, 4, and 16. Bits TOUTPS3:TOUTPS0 set
www.newnespress.com
PIC18F Microcontroller Series
83
Figure 2.29: Timer 1 in 16-bit mode
Figure 2.30: Timer 2 control register, T2CON
www.newnespress.com
84
Chapter 2
Figure 2.31: Timer 2 block diagram
the postscaler for a scaling of 1:1 to 1:16. The timer can be turned on or off by setting or clearing bit TMR2ON. The block diagram of Timer 2 is shown in Figure 2.31. Timer 2 can be used for the PWM mode of the CCP module. The output of Timer 2 can be software selected by the SSP module as a baud clock. Timer 2 increments from 00H until it matches PR2 and sets the interrupt flag. It then resets to 00H on the next cycle.
Timer 3 The structure and operation of Timer 3 is the same as for Timer 1, having registers TMR3H and TMR3L. This timer is controlled from register T3CON as shown in Figure 2.32. The block diagram of Timer 3 is shown in Figure 2.33.
2.1.10
Capture/Compare/PWM Modules (CCP)
The PIC18F452 microcontroller has two capture/compare/PWM (CCP) modules, and they work with Timers 1, 2, and 3 to provide capture, compare, and pulse width modulation (PWM) operations. Each module has two 8-bit registers. Module 1 registers are CCPR1L and CCPR1H, and module 2 registers are CCPR2L and CCPR2H. Together, each register pair forms a 16-bit register and can be used to capture, compare, or generate waveforms with a specified duty cycle. Module 1 is controlled by register
www.newnespress.com
PIC18F Microcontroller Series
85
Figure 2.32: Timer 3 control register, T3CON
CCP1CON, and module 2 is controlled by CCP2CON. Figure 2.34 shows the bit allocations of the CCP control registers.
Capture Mode In capture mode, the registers operate like a stopwatch. When an event occurs, the time of the event is recorded, although the clock continues running (a stopwatch, on the other hand, stops when the event time is recorded).
www.newnespress.com
86
Chapter 2
Figure 2.33: Block diagram of Timer 3
Figure 2.35 shows the capture mode of operation. Here, CCP1 will be considered, but the operation of CCP2 is identical with the register and port names changed accordingly. In this mode CCPR1H:CCPR1L captures the 16-bit value of the TMR1 or TMR3 registers when an event occurs on pin RC2/CCP1 (pin RC2/CCP1 must be configured as an input pin using TRISC). An external signal can be prescaled by 4 or 16. The event is selected by control bits CCP1M3:CCP1M0, and any of the following events can be selected: Every falling edge Every rising edge Every fourth rising edge Every sixteenth rising edge If the capture interrupt is enabled, the occurrence of an event causes an interrupt to be generated in software. If another capture occurs before the value in register CCPR1 is read, the old captured value is overwritten by the new captured value. Either Timer 1 or Timer 3 can be used in capture mode. They must be running in timer mode, or in synchronized counter mode, selected by register T3CON.
www.newnespress.com
PIC18F Microcontroller Series
87
Figure 2.34: CCPxCON register bit allocations
Compare Mode In compare mode, a digital comparator is used to compare the value of Timer 1 or Timer 3 to the value in a 16-bit register pair. When a match occurs, the output state of a pin is changed. Figure 2.36 shows the block diagram of compare mode in operation. Here only module CCP1 is considered, but the operation of module CCP2 is identical. The value of the 16-bit register pair CCPR1H:CCPR1L is continuously compared against the Timer 1 or Timer 3 value. When a match occurs, the state of the RC2/CCP1
www.newnespress.com
88
Chapter 2
Figure 2.35: Capture mode of operation
pin is changed depending on the programming of bits CCP1M2:CCP1M0 of register CCP1CON. The following changes can be programmed: Force RC2/CCP1 high Force RC2/CCP1 low Toggle RC2/CCP1 pin (low to high or high to low) Generate interrupt when a match occurs No change Timer 1 or Timer 3 must be running in timer mode or in synchronized counter mode, selected by register T3CON.
www.newnespress.com
PIC18F Microcontroller Series
89
Figure 2.36: Compare mode of operation
PWM Module The pulse width modulation (PWM) mode produces a PWM output at 10-bit resolution. A PWM output is basically a square waveform with a specified period and duty cycle. Figure 2.37 shows a typical PWM waveform.
Period
Duty Cycle
Figure 2.37: Typical PWM waveform
www.newnespress.com
90
Chapter 2
Figure 2.38 shows the PWM module block diagram. The module is controlled by Timer 2. The PWM period is given by: PWM period ¼ ðPR2 þ 1Þ TMR2PS 4 TOSC
ð2:3Þ
or PR2 ¼
PWM period 1 TMR2PS 4 TOSC
ð2:4Þ
where PR2 is the value loaded into Timer 2 register TMR2PS is the Timer 2 prescaler value TOSC is the clock oscillator period (seconds) The PWM frequency is defined as 1/(PWM period). The resolution of the PWM duty cycle is 10 bits. The PWM duty cycle is selected by writing the eight most significant bits into the CCPR1L register and the two least
Figure 2.38: PWM module block diagram
www.newnespress.com
PIC18F Microcontroller Series
91
significant bits into bits 4 and 5 of CCP1CON register. The duty cycle (in seconds) is given by: PWM duty cycle ¼ ðCCPR1L : CCP1CON < 5 : 4 >Þ TMR2PS TOSC
ð2:5Þ
or CCPR1L : CCP1CON < 5 : 4 > ¼
PWM duty cycle TMR2PS TOSC
ð2:6Þ
The steps to configure the PWM are as follows: Specify the required period and duty cycle. Choose a value for the Timer 2 prescaler (TMR2PS). Calculate the value to be written into the PR2 register using Equation (2.2). Calculate the value to be loaded into the CCPR1L and CCP1CON registers using Equation (2.6). Clear bit 2 of TRISC to make CCP1 pin an output pin. Configure the CCP1 module for PWM operation using register CCP1CON. The following example shows how the PWM can be set up. Example 2.1 PWM pulses must be generated from pin CCP1 of a PIC18F452 microcontroller. The required pulse period is 44ms and the required duty cycle is 50%. Assuming that the microcontroller operates with a 4MHz crystal, calculate the values to be loaded into the various registers. Solution 2.1 Using a 4MHz crystal; TOSC ¼ 1=4 ¼ 0:25 106 The required PWM duty cycle is 44/2 = 22ms. From Equation (2.4), assuming a timer prescaler factor of 4, we have: PR2 ¼
PWM period 1 TMR2PS 4 TOSC
www.newnespress.com
92
Chapter 2
or PR2 ¼
44106 1 ¼ 10 4 4 0:25106
i:e:; 0AH
and from Equation (2.6) CCPR1L : CCP1CON < 5 : 4 > ¼
PWM duty cycle TMR2PS TOSC
or CCPR1L : CCP1CON < 5 : 4 > ¼
22106 ¼ 22 4 0:25106
But the equivalent of number 22 in 10-bit binary is: “00 00010110” Therefore, the value to be loaded into bits 4 and 5 of CCP1CON is “00.” Bits 2 and 3 of CCP1CON must be set to high for PWM operation. Therefore, CCP1CON must be set to bit pattern (“X” is “don’t care”): XX001100 Taking the don’t-care entries as 0, we can set CCP1CON to hexadecimal 0CH. The value to be loaded into CCPR1L is “00010110” (i.e., hexadecimal number 16H). The required steps are summarized as follows: Load Timer 2 with prescaler of 4 (i.e., load T2CON) with 00000101 (i.e., 05H). Load 0AH into PR2. Load 16H into CCPR1L. Load 0 into TRISC (make CCP1 pin output). Load 0CH into CCP1CON. One period of the generated PWM waveform is shown in Figure 2.39.
www.newnespress.com
PIC18F Microcontroller Series
93
44µs
22µs
22µs
Figure 2.39: Generated PWM waveform
2.1.11
Analog-to-Digital Converter (A/D) Module
An analog-to-digital converter (A/D) is another important peripheral component of a microcontroller. The A/D converts an analog input voltage into a digital number so it can be processed by a microcontroller or any other digital system. There are many analog-to-digital converter chips available on the market, and an embedded systems designer should understand the characteristics of such chips so they can be used efficiently. As far as the input and output voltage are concerned A/D converters can be classified as either unipolar and bipolar. Unipolar A/D converters accept unipolar input voltages in the range 0 to þ0V, and bipolar A/D converters accept bipolar input voltages in the range V. Bipolar converters are frequently used in signal processing applications, where the signals by nature are bipolar. Unipolar converters are usually cheaper, and they are used in many control and instrumentation applications. Figure 2.40 shows the typical steps involved in reading and converting an analog signal into digital form, a process also known as signal conditioning. Signals received from sensors usually need to be processed before being fed to an A/D converter. This
Scaling
Filter
Sample & Hold
Mux.
A/D Converter
Analog signal
Figure 2.40: Signal conditioning and A/D conversion process
www.newnespress.com
94
Chapter 2
processing usually begins with scaling the signal to the correct value. Unwanted signal components are then removed by filtering the signal using classical filters (e.g., a lowpass filter). Finally, before feeding the signal to an A/D converter, the signal is passed through a sample-and-hold device. This is particularly important with fast real-time signals whose value may be changing between the sampling instants. A sample-andhold device ensures that the signal stays at a constant value during the actual conversion process. Many applications required more than one A/D, which normally involves using an analog multiplexer at the input of the A/D. The multiplexer selects only one signal at any time and presents this signal to the A/D converter. An A/D converter usually has a single analog input and a digital parallel output. The conversion process is as follows: Apply the processed signal to the A/D input Start the conversion Wait until conversion is complete Read the converted digital data The A/D conversion starts by triggering the converter. Depending on the speed of the converter, the conversion process itself can take several microseconds. At the end of the conversion, the converter either raises a flag or generates an interrupt to indicate that the conversion is complete. The converted parallel output data can then be read by the digital device connected to the A/D converter. Most members of the PIC18F family contain a 10-bit A/D converter. If the chosen voltage reference is +5V, the voltage step value is:
5V ¼ 0:00489V or 4:89mV 1023
Therefore, for example, if the input voltage is 1.0V, the converter will generate a digital output of 1.0/0.00489 = 205 decimal. Similarly, if the input voltage is 3.0V, the converter will generate 3.0/0.00489 = 613. The A/D converter used by the PIC18F452 microcontroller has eight channels, named AN0–AN7, which are shared by the PORTA and PORTE pins. Figure 2.41 shows the block diagram of the A/D converter.
www.newnespress.com
PIC18F Microcontroller Series
95
Figure 2.41: Block diagram of the PIC18F452 A/D converter The A/D converter has four registers. Registers ADRESH and ADRESL store the higher and lower results of the conversion respectively. Register ADCON0, shown in Figure 2.42, controls the operation of the A/D module, such as selecting the conversion clock together with register ADCON1, selecting an input channel, starting a conversion, and powering up and shutting down the A/D converter. Register ADCON1 (see Figure 2.43) is used for selecting the conversion format, configuring the A/D channels for analog input, selecting the reference voltage, and selecting the conversion clock together with register ADCON0. A/D conversion starts by setting the GO/DONE bit of ADCON0. When the conversion is complete, the 2 bits of the converted data is written into register ADRESH, and the remaining 8 bits are written into register ADRESL. At the same time the GO/DONE bit is cleared to indicate the end of conversion. If required, interrupts can be enabled so that a software interrupt is generated when the conversion is complete.
www.newnespress.com
96
Chapter 2
Figure 2.42: ADCON0 register
www.newnespress.com
PIC18F Microcontroller Series
97
Figure 2.43: ADCON1 register
www.newnespress.com
98
Chapter 2
The steps in carrying out an A/D conversion are as follows: Use ADCON1 to configure required channels as analog and configure the reference voltage. Set the TRISA or TRISE bits so the required channel is an input port. Use ADCON0 to select the required analog input channel. Use ADCON0 and ADCON1 to select the conversion clock. Use ADCON0 to turn on the A/D module. Configure the A/D interrupt (if desired). Set the GO/DONE bit to start conversion. Wait until the GO/DONE bit is cleared, or until a conversion complete interrupt is generated. Read the converted data from ADRESH and ADRESL. Repeat these steps as required. For correct A/D conversion, the A/D conversion clock must be selected to ensure a minimum bit conversion time of 1.6ms. Table 2.11 gives the recommended A/D clock sources for various microcontroller operating frequencies. For example, if the Table 2.11: A/D conversion clock selection A/D clock source Operation
ADCS2:ADCS0
Maximum microcontroller frequency
2 TOSC
000
1.25 MHz
4 TOSC
100
2.50 MHz
8 TOSC
001
5.0 MHz
16 TOSC
101
10.0 MHz
32 TOSC
010
20.0 MHz
64 TOSC
110
40.0 MHz
RC
011
–
www.newnespress.com
PIC18F Microcontroller Series
99
Figure 2.44: Formatting the A/D conversion result microcontroller is operated from a 10MHz clock, the A/D clock source should be FOSC/16 or higher (e.g., FOSC/32). Bit ADFM of register ADCON1 controls the format of a conversion. When ADFM is cleared, the 10-bit result is left justified (see Figure 2.44) and lower 6 bits of ADRESL are cleared to 0. When ADFM is set to 1 the result is right justified and the upper 6 bits of ADRESH are cleared to 0. This is the mode most commonly used, in which ADRESL contains the lower 8 bits, and bits 0 and 1 of ADRESH contain the upper 2 bits of the 10-bit result.
Analog Input Model and Acquisition Time An understanding of the A/D analog input model is necessary to interface the A/D to external devices. Figure 2.45 shows the analog input model of the A/D. The analog input voltage VAIN and the source resistance RS are shown on the left side of the diagram. It is recommended that the source resistance be no greater than 2.5K. The analog signal is applied to the pin labeled ANx. There is a small capacitance (5pF) and a leakage current to the ground of approximately 500nA. RIC is the interconnect resistance, which has a value of less than 1K. The sampling process is shown with switch SS having a resistance RSS whose value depends on the voltage as shown in the
www.newnespress.com
100
Chapter 2
Figure 2.45: Analog input model of the A/D converter small graph at the bottom of Figure 2.45. The value of RSS is approximately 7K at 5V supply voltage. The A/D converter is based on a switched capacitor principle, and capacitor CHOLD shown in Figure 2.45 must be charged fully before the start of a conversion. This is a 120pF capacitor which is disconnected from the input pin once the conversion is started. The acquisition time can be calculated by using Equation (2.7), provided by Microchip Inc: TACQ ¼ Amplifier settling time þ Holding capacitor charging time þ temperature coefficient
ð2:7Þ
The amplifier settling time is specified as a fixed 2ms. The temperature coefficient, which is only applicable if the temperature is above 25 C, is specified as: Temperature coefficient ¼ ðTemperature 25 CÞð0:05ms= CÞ
ð2:8Þ
Equation (2.8) shows that the effect of the temperature is very small, creating about 0.5ms delay for every 10 C above 25 C. Thus, assuming a working environment
www.newnespress.com
PIC18F Microcontroller Series
101
between 25 C and 35 C, the maximum delay due to temperature will be 0.5ms, which can be ignored for most practical applications. The holding capacitor charging time as specified by Microchip Inc is: Holding capacitor charging time ¼ ð120pFÞð1K þ RSS þ RS ÞLnð1=2048Þ
ð2:9Þ
Assuming that RSS ¼ 7K, RS ¼ 2.5K, Equation (2.9) gives the holding capacitor charging time as 9.6ms. The acquisition time is then calculated as: TACQ ¼ 2 þ 9:6 þ 0:5 ¼ 12:1ms A full 10-bit conversion takes 12 A/D cycles, and each A/D cycle is specified at a minimum of 1.6ms. Thus, the fastest conversion time is 19.2ms. Adding this to the best possible acquisition time gives a total time to complete a conversion of 19.2 þ 12.1 ¼ 31.3ms. When a conversion is complete, it is specified that the converter should wait for two conversion periods before starting a new conversion. This corresponds to 2 1.6 ¼ 3.2ms. Adding this to the best possible conversion time of 31.3ms gives a complete conversion time of 34.5ms. Assuming the A/D converter is used successively, and ignoring the software overheads, this implies a maximum sampling frequency of about 29KHz.
2.1.12
Interrupts
An interrupt is an event that requires the CPU to stop normal program execution and then execute a program code related to the event causing the interrupt. Interrupts can be generated internally (by some event inside the chip) or externally (by some external event). An example of an internal interrupt is a timer overflowing or the A/D completing a conversion. An example of an external interrupt is an I/O pin changing state. Interrupts can be useful in many applications such as: Time critical applications. Applications which require the immediate attention of the CPU can use interrupts. For example, in an emergency such as a power failure or fire in a plant the CPU may have to shut down the system immediately in an orderly manner. In such applications an external interrupt can force the CPU to stop whatever it is doing and take immediate action.
www.newnespress.com
102
Chapter 2
Performing routine tasks. Many applications require the CPU to perform routine work at precise times, such as checking the state of a peripheral device exactly every millisecond. A timer interrupt scheduled with the required timing can divert the CPU from normal program execution to accomplish the task at the precise time required. Task switching in multi-tasking applications. In multi-tasking applications, each task may have a finite time to execute its code. Interrupt mechanisms can be used to stop a task should it consume more than its allocated time. To service peripheral devices quickly. Some applications may need to know when a task, such as an A/D conversion, is completed. This can be accomplished by continuously checking the completion flag of the A/D converter. A more elegant solution would be to enable the A/D completion interrupt so the CPU is forced to read the converted data as soon as it becomes available. The PIC18F452 microcontroller has both core and peripheral interrupt sources. The core interrupt sources are: External edge-triggered interrupt on INT0, INT1, and INT2 pins. PORTB pins change interrupts (any one of the RB4–RB7 pins changing state) Timer 0 overflow interrupt The peripheral interrupt sources are: Parallel slave port read/write interrupt A/D conversion complete interrupt USART receive interrupt USART transmit interrupt Synchronous serial port interrupt CCP1 interrupt TMR1 overflow interrupt TMR2 overflow interrupt Comparator interrupt
www.newnespress.com
PIC18F Microcontroller Series
103
EEPROM/FLASH write interrupt Bus collision interrupt Low-voltage detect interrupt Timer 3 overflow interrupt CCP2 interrupt Interrupts in the PIC18F family can be divided into two groups: high priority and low priority. Applications that require more attention can be placed in the higher priority group. A high-priority interrupt can stop a low-priority interrupt that is in progress and gain access to the CPU. However, high-priority interrupts cannot be stopped by low-priority interrupts. If the application does not need to set priorities for interrupts, the user can choose to disable the priority scheme so all interrupts are at the same priority level. High-priority interrupts are vectored to address 00008H and low-priority ones to address 000018H of the program memory. Normally, a user program code (interrupt service routine, ISR) should be at the interrupt vector address to service the interrupting device. In the PIC18F452 microcontroller there are ten registers that control interrupt operations. These are: RCON INTCON INTCON2 INTCON3 PIR1, PIR2 PIE1, PIE2 IPR1, IPR2 Every interrupt source (except INT0) has three bits to control its operation. These bits are: A flag bit to indicate whether an interrupt has occurred. This bit has a name ending in . . .IF
www.newnespress.com
104
Chapter 2
An interrupt enable bit to enable or disable the interrupt source. This bit has the name ending in . . .IE A priority bit to select high or low priority. This bit has a name ending in . . .IP
RCON Register The top bit of the RCON register, called IPEN, is used to enable the interrupt priority scheme. When IPEN ¼ 0, interrupt priority levels are disabled and the microcontroller interrupt structure is similar to that of the PIC16 series. When IPEN ¼ 1, interrupt priority levels are enabled. Figure 2.46 shows the bits of register RCON.
Enabling/Disabling Interrupts—No Priority Structure When the IPEN bit is cleared, the priority feature is disabled. All interrupts branch to address 00008H of the program memory. In this mode, bit PEIE of register INTCON enables/disables all peripheral interrupt sources. Similarly, bit GIE of INTCON enables/disables all interrupt sources. Figure 2.47 shows the bits of register INTCON.
Figure 2.46: RCON register bits
www.newnespress.com
PIC18F Microcontroller Series
105
Figure 2.47: INTCON register bits
www.newnespress.com
106
Chapter 2
For an interrupt to be accepted by the CPU the following conditions must be satisfied: The interrupt enable bit of the interrupt source must be enabled. For example, if the interrupt source is external interrupt pin INT0, then bit INT0IE of register INTCON must be set to 1. The interrupt flag of the interrupt source must be cleared. For example, if the interrupt source is external interrupt pin INT0, then bit INT0IF of register INTCON must be cleared to 0. The peripheral interrupt enable/disable bit PEIE of INTCON must be set to 1 if the interrupt source is a peripheral. The global interrupt enable/disable bit GIE of INTCON must be set to 1. With an external interrupt source we normally have to define whether the interrupt should occur on the low-to-high or high-to-low transition of the interrupt source. With INT0 interrupts, for example, this is done by setting/clearing bit INTEDG0 of register INTCON2. When an interrupt occurs, the CPU stops its normal flow of execution, pushes the return address onto the stack, and jumps to address 00008H in the program memory where the user interrupt service routine program resides. Once the CPU is in the interrupt service routine, the global interrupt enable bit (GIE) is cleared to disable further interrupts. When multiple interrupt sources are enabled, the source of the interrupt can be determined by polling the interrupt flag bits. The interrupt flag bits must be cleared in the software before reenabling interrupts to avoid recursive interrupts. When the CPU has returned from the interrupt service routine, the global interrupt bit GIE is automatically set by the software.
Enabling/Disabling Interrupts—Priority Structure When the IPEN bit is set to 1, the priority feature is enabled and the interrupts are grouped into two: low priority and high priority. Low-priority interrupts branch to address 00008H and high-priority interrupts branch to address 000018H of the program memory. Setting the priority bit makes the interrupt source a high-priority interrupt, and clearing this bit makes the interrupt source a low-priority interrupt.
www.newnespress.com
PIC18F Microcontroller Series
107
Setting the GIEH bit of INTCON enables all high-priority interrupts that have the priority bit set. Similarly, setting the GIEL bit of INTCON enables all low-priority interrupts (the priority is bit cleared). For a high-priority interrupt to be accepted by the CPU, the following conditions must be satisfied: The interrupt enable bit of the interrupt source must be enabled. For example, if the interrupt source is external interrupt pin INT1, then bit INT1IE of register INTCON3 must be set to 1. The interrupt flag of the interrupt source must be cleared. For example, if the interrupt source is external interrupt pin INT1, then bit INT1IF of register INTCON3 must be cleared to 0. The priority bit must be set to 1. For example, if the interrupt source is external interrupt INT1, then bit INT1P of register INTCON3 must be set to 1. The global interrupt enable/disable bit GIEH of INTCON must be set to 1. For a low-priority interrupt to be accepted by the CPU, the following conditions must be satisfied: The interrupt enable bit of the interrupt source must be enabled. For example, if the interrupt source is external interrupt pin INT1, then bit INT1IE of register INTCON3 must be set to 1. The interrupt flag of the interrupt source must be cleared. For example, if the interrupt source is external interrupt pin INT1, then bit INT1IF of register INTCON3 must be cleared to 0. The priority bit must be cleared to 0. For example, if the interrupt source is external interrupt INT1, then bit INT1P of register INTCON3 must be cleared to 0. Low-priority interrupts must be enabled by setting bit GIEL of INTCON to 1. The global interrupt enable/disable bit GIEH of INTCON must be set to 1. Table 2.12 gives a listing of the PIC18F452 microcontroller interrupt bit names and register names for every interrupt source.
www.newnespress.com
108
Chapter 2 Table 2.12: PIC18F452 interrupt bits and registers
Interrupt source
Flag bit
Enable bit
INT0 external
INT0IF
INT0IE
–
INT1 external
INT1IF
INT1IE
INT1IP
INT2 external
INT2IF
INT2IE
INT2IP
RB port change
RBIF
RBIE
RBIP
TMR0 overflow
TMR0IF
TMR0IE
TMR0IP
TMR1overflow
TMR1IF
TMR1IE
TMR1IP
TMR2 match PR2
TMR2IF
TMR2IE
TMR2IP
TMR3 overflow
TMR3IF
TMR3IE
TMR3IP
A/D complete
ADIF
ADIE
ADIP
CCP1
CCP1IF
CCP1IE
CCP1IP
CCP2
CCP2IF
CCP2IE
CCP2IP
USART RCV
RCIF
RCIE
RCIP
USART TX
TXIF
TXIE
TXIP
Parallel slave port
PSPIF
PSPIE
PSPIP
Sync serial port
SSPIF
SSPIE
SSPIP
Low-voltage detect
LVDIF
LVDIE
LVDIP
Bus collision
BCLIF
BCLIE
BCLIP
EEPROM/FLASH write
EEIF
EEIE
EEIP
www.newnespress.com
Priority bit
PIC18F Microcontroller Series
109
Figures 2.48 to 2.55 show the bit definitions of interrupt registers INTCON2, INTCON3, PIR1, PIR2, PIE1, PIE2, IPR1, and IPR2. Examples are given in this section to illustrate how the CPU can be programmed for an interrupt. Example 2.2 Set up INT1 as a falling-edge triggered interrupt input having low priority. Solution 2.2 The following bits should be set up before the INT1 falling-edge triggered interrupts can be accepted by the CPU in low-priority mode:
Figure 2.48: INTCON2 bit definitions
www.newnespress.com
110
Chapter 2
Figure 2.49: INTCON3 bit definitions
Enable the priority structure. Set IPEN ¼ 1 Make INT1 an input pin. Set TRISB ¼ 1 Set INT1 interrupts for falling edge. SET INTEDG1 ¼ 0 Enable INT1 interrupts. Set INT1IE ¼ 1 Enable low priority. Set INT1IP ¼ 0 Clear INT1 flag. Set INT1IF ¼ 0 Enable low-priority interrupts. Set GIEL ¼ 1 Enable all interrupts. Set GIEH ¼ 1
www.newnespress.com
PIC18F Microcontroller Series
111
Figure 2.50: PIR1 bit definitions
www.newnespress.com
112
Chapter 2
Figure 2.51: PIR2 bit definitions When an interrupt occurs, the CPU jumps to address 00008H in the program memory to execute the user program at the interrupt service routine. Example 2.3 Set up INT1 as a rising-edge triggered interrupt input having high priority. Solution 2.3 The following bits should be set up before the INT1 rising-edge triggered interrupts can be accepted by the CPU in high-priority mode: Enable the priority structure. Set IPEN ¼ 1 Make INT1 an input pin. Set TRISB ¼ 1
www.newnespress.com
PIC18F Microcontroller Series
113
Figure 2.52: PIE1 bit definitions Set INT1 interrupts for rising edge. SET INTEDG1 ¼ 1 Enable INT1 interrupts. Set INT1IE ¼ 1 Enable high priority. Set INT1IP ¼ 1 Clear INT1 flag. Set INT1IF ¼ 0 Enable all interrupts. Set GIEH ¼ 1 When an interrupt occurs, the CPU jumps to address 000018H of the program memory to execute the user program at the interrupt service routine.
www.newnespress.com
114
Chapter 2
Figure 2.53: PIE2 bit definitions
Figure 2.54: IPR1 bit definitions
www.newnespress.com
PIC18F Microcontroller Series
115
Figure 2.55: IPR2 bit definitions
2.2 Summary This chapter has described the architecture of the PIC18F family of microcontrollers. The PIC18F452 was used as a typical sample microcontroller in this family. Other members of the same family, such as the PIC18F242, have smaller pin counts and less functionality. And some, such as the PIC18F6680, have larger pin counts and more functionality. Important parts and peripheral circuits of the PIC18F series have been described, including data memory, program memory, clock circuits, reset circuits, watchdog timer, general purpose timers, capture and compare module, PWM module, A/D converter, and the interrupt structure.
2.3 Exercises 1. Describe the data memory structure of the PIC18F452 microcontroller. What is a bank? How many banks are there? 2. Explain the differences between a general purpose register (GPR) and a special function register (SFR).
www.newnespress.com
116
Chapter 2
3. Explain the various ways the PIC18F microcontroller can be reset. Draw a circuit diagram to show how an external push-button switch can be used to reset the microcontroller. 4. Describe the various clock sources that can be used to provide a clock to a PIC18F452 microcontroller. Draw a circuit diagram to show how a 10MHz crystal can be connected to the microcontroller. 5. Draw a circuit diagram to show how a resonator can be connected to a PIC18F microcontroller. 6. In a non-time-critical application a clock must be provided for a PIC18F452 microcontroller using an external resistor and a capacitor. Draw a circuit diagram to show how this can be done and find the component values for a required clock frequency of 5MHz. 7. Explain how an external clock can provide clock pulses to a PIC18F microcontroller. 8. What are the registers of PORTA? Explain the operation of the port by drawing the port block diagram. 9. The watchdog timer must be set to provide an automatic reset every 0.5 seconds. Describe how to do this, including the appropriate register bits. 10.
PWM pulses must be generated from pin CCP1 of a PIC18F452 microcontroller. The required pulse period is 100ms, and the required duty cycle is 50%. Assuming the microcontroller is operating with a 4MHz crystal, calculate the values to be loaded into the various registers.
11.
Again, with regard to PWM pulses generated from pin CCP1 of a PIC18F452 microcontroller: If the required pulse frequency is 40KHz, and the required duty cycle is 50%, and assuming the microcontroller is operating with a 4MHz crystal, calculate the values to be loaded into the various registers.
12.
An LM35DZ-type analog temperature sensor is connected to analog port AN0 of a PIC18F452 microcontroller. The sensor provides an analog output voltage proportional to the temperature (i.e., V0 = 10 mV/ C). Show the steps required to read the temperature.
13.
Explain the difference between a priority interrupt and a nonpriority interrupt.
www.newnespress.com
PIC18F Microcontroller Series
117
14.
Show the steps required to set up INT2 as a falling-edge triggered interrupt input having low priority. What is the interrupt vector address?
15.
Show the steps required to set up both INT1 and INT2 as falling-edge triggered interrupt inputs having low priority.
16.
Show the steps required to set up INT1 as falling-edge triggered and INT2 as rising-edge triggered interrupt inputs having high priorities. Explain how to find the source of the interrupt when an interrupt occurs.
17.
Show the steps required to set up Timer 0 to generate interrupts every millisecond with a high priority. What is the interrupt vector address?
18.
In an application the CPU registers have been configured to accept interrupts from external sources INT0, INT1, and INT2. An interrupt has been detected. Explain how to find the source of the interrupt.
www.newnespress.com
This page intentionally left blank
CHAPTER 3
C Programming Language There are several C compilers on the market for the PIC18 series of microcontrollers. These compilers have many similar features, and they can all be used to develop C-based high-level programs for PIC18 microcontrollers. Some of the C compilers used most often in commercial, industrial, and educational PIC18 microcontroller applications are: mikroC PICC18 C18 CCS The popular and powerful mikroC, developed by MikroElektronika (web site: www.microe.com), is easy to learn and comes with rich resources, such as a large number of library functions and an integrated development environment with a built-in simulator and an in-circuit debugger (e.g., mikroICD). A demo version of the compiler with a 2K program limit is available from MikroElektronika. PICC18, another popular C compiler, was developed by Hi-Tech Software (web site: www.htsoft.com) and is available in two versions: standard and professional. A powerful simulator and an integrated development environment (Hi-Tide) are provided by the company. PICC18 is supported by the PROTEUS simulator (www.labcenter.co.uk) which can be used to simulate PIC microcontroller–based systems. A limited-period demo version of this compiler is available on the developer’s web site.
www.newnespress.com
120
Chapter 3
C18 is a product of Microchip Inc. (web site: www.microchip.com). A limited-period demo version, as well as a limited functionality version of C18 with no time limit, are available from the Microchip web site. C18 includes a simulator and supports hardware and software development tools such as in-circuit emulators (e.g., ICE2000) and in-circuit debuggers (e.g., ICD2). CCS has been developed by the Custom Computer Systems Inc. (web site: www. ccsinfo.com). The company offers a limited-period demo version of their compiler. CCS provides a large number of built-in functions and supports an in-circuit debugger (e.g., ICD-U40) which are very helpful in the development of PIC18 microcontroller– based systems. In this book we are mainly concentrating on the use of the mikroC compiler, and most of the projects are based on this compiler.
3.1 Structure of a mikroC Program Figure 3.1 shows the simplest structure of a mikroC program. This program flashes an LED connected to port RB0 (bit 0 of PORTB) of a PIC microcontroller in one-second /∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ LED FLASHING PROGRAM ********************************* This program flashes an LED connected to port pin RB0 of PORTB with one second intervals. Programmer : D. Ibrahim File : LED.C Date : May, 2007 Micro : PIC18F452 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/ void main() { for(;;) { TRISB = 0; PORTB.0 = 0; Delay_Ms(1000); PORTB.0 = 1; Delay_Ms(1000); } }
// Endless loop // Configure PORTB as output // RB0 = 0 // Wait 1 second // RB0 = 1 // Wait 1 second // End of loop
Figure 3.1: Structure of a simple C program
www.newnespress.com
C Programming Language
121
intervals. Do not worry if you don’t understand the operation of the program at this stage, as all will come clear as this chapter progresses. Some of the programming elements in Figure 3.1 are described in detail here.
3.1.1
Comments
Comments are used to clarify the operation of the program or a programming statement. Comment lines are ignored and not compiled by the compiler. In mikroC programs comments can be of two types: long comments, extending several lines, and short comments, occupying only a single line. Comment lines at the beginning of a program can describe briefly the program’s operation and provide the author’s name, the program filename, the date the program was written, and a list of version numbers, together with the modifications in each version. As shown in Figure 3.1, comments can also be added after statements to describe the operations that the statements perform. Clear and succinct comment lines are important for the maintenance and thus the lifetime of a program, as a program with good comments is easier to modify and/or update. As shown in Figure 3.1, long comments start with the character “/*” and terminate with the character “*/”. Similarly, short comments start with the character “//” and do not need a terminating character.
3.1.2
Beginning and Ending of a Program
In C language, a program begins with the keywords: void main ()
After this, a curly opening bracket is used to indicate the beginning of the program body. The program is terminated with a closing curly bracket. Thus, as shown in Figure 3.1, the program has the following structure: void main() { program body }
3.1.3
Terminating Program Statements
In C language, all program statements must be terminated with the semicolon (“;”) character; otherwise a compiler error will be generated:
www.newnespress.com
122
Chapter 3
j ¼ 5; j¼5
3.1.4
// correct // error
White Spaces
White spaces are spaces, blanks, tabs, and newline characters. The C compiler ignores all white spaces. Thus, the following three sequences are identical: int i; or int i; char j; or int i;
char j;
char j;
Similarly, the following sequences are identical: i ¼ j þ 2; or i¼j þ 2;
3.1.5
Case Sensitivity
In general, C language is case sensitive and variables with lowercase names are different from those with uppercase names. Currently, however, mikroC variables are not case sensitive (although future releases of mikroC may offer case sensitivity) so the following variables are equivalent: total
TOTAL
Total
ToTal
total
totaL
The only exception is the identifiers main and interrupt, which must be written in lowercase in mikroC. In this book we are assuming that the variables are case sensitive, for the sake of compatibility with other C compilers, and variables with the same name but different cases are not used.
www.newnespress.com
C Programming Language
3.1.6
123
Variable Names
In C language, variable names can begin with an alphabetical character or with the underscore character. In essence, variable names can include any of the characters a to z and A to Z, the digits 0 to 9, and the underscore character “_”. Each variable name should be unique within the first 31 characters of its name. Variable names can contain uppercase and lowercase characters (see Section 3.1.5), and numeric characters can be used inside a variable name. Examples of valid variable names are: Sum count _myName
sum100
counter
i1
UserName
Some names are reserved for the compiler itself and cannot be used as variable names in a program. Table 3.1 gives a list of these reserved names.
3.1.7
Variable Types
The mikroC language supports the variable types shown in Table 3.2. Examples of variables are given in this section.
Table 3.1: mikroC reserved names asm
enum
signed
auto
extern
sizeof
break
float
static
case
for
struct
char
goto
switch
const
if
typedef
continue
int
union
default
long
unsigned
do
register
void
double
return
volatile
else
short
while
www.newnespress.com
124
Chapter 3 Table 3.2: mikroC variable types Type
Size (bits)
Range
unsigned char
8
0 to 255
unsigned short int
8
0 to 255
unsigned int
16
0 to 65535
unsigned long int
32
0 to 4294967295
signed char
8
128 to 127
signed short int
8
128 to 127
signed int
16
32768 to 32767
signed long int
32
2147483648 to 2147483647
float
32
1.17549435082E-38 to 6.80564774407E38
double
32
1.17549435082E-38 to 6.80564774407E38
long double
32
1.17549435082E-38 to 6.80564774407E38
(unsigned) char or unsigned short (int) The variables (unsigned) char, or unsigned short (int), are 8-bit unsigned variables with a range of 0 to 255. In the following example two 8-bit variables named total and sum are created, and sum is assigned decimal value 150: unsigned char total, sum; sum ¼ 150;
or char total, sum; sum ¼ 150;
Variables can be assigned values during their declaration. Thus, the above statements can also be written as: char total, sum ¼ 150;
www.newnespress.com
C Programming Language
125
signed char or (signed) short (int) The variables signed char, or (signed) short (int), are 8-bit signed character variables with a range of 128 to þ127. In the following example a signed 8-bit variable named counter is created with a value of 50: signed char counter ¼
50;
or short counter ¼
50;
or short int counter ¼
50;
(signed) int Variables called (signed) int are 16-bit variables with a range following example a signed integer named Big is created:
32768 to þ32767. In the
int Big;
unsigned (int) Variables called (unsigned) int are 16-bit unsigned variables with a range 0 to 65535. In the following example an unsigned 16-bit variable named count is created and is assigned value 12000: unsigned int count ¼ 12000;
(signed) long (int) Variables called (signed) long (int) are 32 bits long with a range þ2147483647. An example is:
2147483648 to
signed long LargeNumber;
unsigned long (int) Variables called (unsigned) long (int) are 32-bit unsigned variables having the range 0 to 4294967295. An example is: unsigned long VeryLargeNumber;
www.newnespress.com
126
Chapter 3
float or double or long double The variables called float or double or long double, are floating point variables implemented in mikroC using the Microchip AN575 32-bit format, which is IEEE 754 compliant. Floating point numbers range from 1.17549435082E-38 to 6.80564774407E38. In the following example, a floating point variable named area is created and assigned the value 12.235: float area; area ¼ 12.235;
To avoid confusion during program development, specifying the sign of the variable (signed or unsigned) as well as the type of variable is recommended. For example, use unsigned char instead of char only, and unsigned int instead of unsigned only. In this book we are using the following mikroC data types, which are easy to remember and also compatible with most other C compilers: unsigned char
0 to 255
signed char unsigned int
128 to 127 0 to 65535
signed int unsigned long
32768 to 32767 0 to 4294967295
signed long float
3.1.8
2147483648 to 2147483647 1.17549435082E-38 to 6.80564774407E38
Constants
Constants represent fixed values (numeric or character) in programs that cannot be changed. Constants are stored in the flash program memory of the PIC microcontroller, thus not wasting valuable and limited RAM memory. In mikroC, constants can be integers, floating points, characters, strings, or enumerated types.
Integer Constants Integer constants can be decimal, hexadecimal, octal, or binary. The data type of a constant is derived by the compiler from its value. But suffixes can be used to change the type of a constant.
www.newnespress.com
C Programming Language
127
In Table 3.2 we saw that decimal constants can have values from 2147483648 to þ4294967295. For example, constant number 210 is stored as an unsigned char (or unsigned short int). Similarly, constant number 200 is stored as a signed int. Using the suffix u or U forces the constant to be unsigned. Using the suffix L or l forces the constant to be long. Using both U (or u) and L (or l) forces the constant to be unsigned long. Constants are declared using the keyword const and are stored in the flash program memory of the PIC microcontroller, thus not wasting valuable RAM space. In the following example, constant MAX is declared as 100 and is stored in the flash program memory of the PIC microcontroller: const MAX ¼ 100;
Hexadecimal constants start with characters 0x or 0X and may contain numeric data 0 to 9 and hexadecimal characters A to F. In the following example, constant TOTAL is given the hexadecimal value FF: const TOTAL ¼ 0xFF;
Octal constants have a zero at the beginning of the number and may contain numeric data 0 to 7. In the following example, constant CNT is given octal value 17: const CNT ¼ 017;
Binary constant numbers start with 0b or 0B and may contain only 0 or 1. In the following example a constant named Min is declared as having the binary value 11110000: const Min ¼ 0b11110000
Floating Point Constants Floating point constant numbers have integer parts, a dot, a fractional part, and an optional e or E followed by a signed integer exponent. In the following example, a constant named TEMP is declared as having the fractional value 37.50: const TEMP ¼ 37.50
or const TEMP ¼ 3.750E1
www.newnespress.com
128
Chapter 3
Character Constants A character constant is a character enclosed within single quote marks. In the following example, a constant named First_Alpha is declared as having the character value “A”: const First_Alpha ¼ ‘A’;
String Constants String constants are fixed sequences of characters stored in the flash memory of the microcontroller. The string must both begin and terminate with a double quote character (“). The compiler automatically inserts a null character as a terminator. An example string constant is: “This is an example string constant”
A string constant can be extended across a line boundary by using a backslash character (“ \”): “This is first part of the string \ and this is the continuation of the string”
This string constant declaration is the same as: “This is first part of the string and this is the continuation of the string”
Enumerated Constants Enumerated constants are integer type and are used to make a program easier to follow. In the following example, constant colors stores the names of colors. The first element is given the value 0: enum colors {black, brown, red, orange, yellow, green, blue, gray, white};
3.1.9
Escape Sequences
Escape sequences are used to represent nonprintable ASCII characters. Table 3.3 shows some commonly used escape sequences and their representation in C language. For example, the character combination “ \ n” represents the newline character.
www.newnespress.com
C Programming Language
129
Table 3.3: Some commonly used escape sequences Escape sequence
Hex value
Character
\a
007
BEL (bell)
\b
008
BS (backspace)
\t
009
HT (horizontal tab)
\n
00A
LF (linefeed)
\v
00B
VT (vertical feed)
\f
00C
FF (formfeed)
\r
00D
CR (carriage return)
\xH
String of hex digits
An ASCII character can also be represented by specifying its hexadecimal code after a backslash. For example, the newline character can also be represented as “\x0A”.
3.1.10
Static Variables
Static variables are local variables used in functions (see Chapter 4) when the last value of a variable between successive calls to the function must be preserved. As the following example shows, static variables are declared using the keyword static: static unsigned int count;
3.1.11
External Variables
Using the keyword extern before a variable name declares that variable as external. It tells the compiler that the variable is declared elsewhere in a separate source code module. In the following example, variables sum1 and sum2 are declared as external unsigned integers: extern int sum1, sum2;
www.newnespress.com
130
3.1.12
Chapter 3
Volatile Variables
Volatile variables are especially important in interrupt-based programs and input-output routines. Using the keyword volatile indicates that the value of the variable may change during the lifetime of the program independent of the normal flow of the program. Variables declared as volatile are not optimized by the compiler, since their values can change unexpectedly. In the following example, variable Led is declared as a volatile unsigned char: volatile unsigned char Led;
3.1.13
Enumerated Variables
Enumerated variables are used to make a program more readable. In an enumerated variable, a list of items is specified and the value of the first item is set to 0, the next item is set to 1, and so on. In the following example, type Week is declared as an enumerated list and MON ¼ 0, TUE ¼ 1, WED ¼ 2, and so on): enum Week {MON, TUE, WED, THU, FRI, SAT, SUN};
It is possible to imply the values of the elements in an enumerated list. In the following example, black ¼ 2, blue ¼ 3, red ¼ 4, and so on. enum colors {black ¼ 2, blue, red, white, gray};
Similarly, in the following example, black ¼ 2, blue ¼ 3, red ¼ 8, and gray ¼ 9: enum colors {black ¼ 2, blue, red ¼ 8, gray};
Variables of type enumeration can be declared by specifying them after the list of items. For example, to declare variable My_Week of enumerated type Week, use the following statement: enum Week {MON, TUE, WED, THU, FRI, SAT, SUN} My_Week;
Now we can use variable My_Week in a program: My_Week ¼ WED
// assign 2 to My_Week
or My_Week ¼ 2
// same as above
www.newnespress.com
C Programming Language
131
After defining the enumerated type Week, we can declare variables This_Week and Next_Week of type Week as: enum Week This_Week, Next_Week;
3.1.14
Arrays
Arrays are used to store related items in the same block of memory and under a specified name. An array is declared by specifying its type, name, and the number of elements it will store. For example: unsigned int Total[5];
This array of type unsigned int has the name Total and has five elements. The first element of an array is indexed with 0. Thus, in this example, Total[0] refers to the first element of the array and Total[4] refers to the last element. The array Total is stored in memory in five consecutive locations as follows: Total[0] Total[1] Total[2] Total[3] Total[4]
Data can be stored in the array by specifying the array name and index. For example, to store 25 in the second element of the array we have to write: Total[1] ¼ 25;
Similarly, the contents of an array can be read by specifying the array name and its index. For example, to copy the third array element to a variable called Temp we have to write: Temp ¼ Total[2];
The contents of an array can be initialized during the declaration of the array by assigning a sequence of comma-delimited values to the array. An example follows where array months has twelve elements and months[0] ¼ 31, months[1] ¼ 28, and so on: unsigned char months[12] ¼ {31,28,31,30,31,30,31,31,30,31,30,31};
www.newnespress.com
132
Chapter 3
The same array can also be declared without specifying its size: unsigned char months[ ] ¼ {31,28,31,30,31,30,31,31,30,31,30,31};
Character arrays can be declared similarly. In the following example, a character array named Hex_Letters is declared with 6 elements: unsigned char Hex_Letters[ ] ¼ {‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’};
Strings are character arrays with a null terminator. Strings can be declared either by enclosing the string in double quotes, or by specifying each character of the array within single quotes and then terminating the string with a null character. The two string declarations in the following example are identical, and both occupy five locations in memory: unsigned char Mystring[ ] ¼ “COMP”;
and unsigned char Mystring[ ] ¼ {‘C’, ‘O’, ‘M’, ‘P’, ‘\0’};
In C programming language, we can also declare arrays with multiple dimensions. One-dimensional arrays are usually called vectors, and two-dimensional arrays are called matrices. A two-dimensional array is declared by specifying the data type of the array, the array name, and the size of each dimension. In the following example, a two-dimensional array named P is created having three rows and four columns. Altogether, the array has twelve elements. The first element of the array is P[0][0], and the last element is P[2][3]. The structure of this array is shown below: P[0][0]
P[0][1]
P[0][2]
P[0][3]
P[1][0]
P[1][1]
P[1][2]
P[1][3]
P[2][0]
P[2][1]
P[2][2]
P[2][3]
Elements of a multidimensional array can be specified during the declaration of the array. In the following example, two-dimensional array Q has two rows and two columns, its diagonal elements are set to 1, and its nondiagonal elements are cleared to 0: unsigned char Q[2][2] ¼ { {1,0}, {0,1} };
www.newnespress.com
C Programming Language
3.1.15
133
Pointers
Pointers are an important part of the C language, as they hold the memory addresses of variables. Pointers are declared in the same way as other variables, but with the character (“*”) in front of the variable name. In general, pointers can be created to point to (or hold the addresses of) character variables, integer variables, long variables, floating point variables, or functions (although mikroC currently does not support pointers to functions). In the following example, an unsigned character pointer named pnt is declared: unsigned char *pnt;
When a new pointer is created, its content is initially unspecified and it does not hold the address of any variable. We can assign the address of a variable to a pointer using the (“&”) character: pnt ¼ &Count;
Now pnt holds the address of variable Count. Variable Count can be set to a value by using the character (“*”) in front of its pointer. For example, Count can be set to 10 using its pointer: *pnt ¼ 10;
// Count ¼ 10
which is the same as Count ¼ 10;
// Count ¼ 10
Or, the value of Count can be copied to variable Cnt using its pointer: Cnt ¼ *pnt;
// Cnt ¼ Count
Array Pointers In C language the name of an array is also a pointer to the array. Thus, for the array: unsigned int Total[10];
The name Total is also a pointer to this array, and it holds the address of the first element of the array. Thus the following two statements are equal: Total[2] ¼ 0;
www.newnespress.com
134
Chapter 3
and *(Total þ 2) ¼ 0;
Also, the following statement is true: &Total[j] ¼ Total þ j
In C language we can perform pointer arithmetic which may involve: Comparing two pointers Adding or subtracting a pointer and an integer value Subtracting two pointers Assigning one pointer to another Comparing a pointer to null For example, let’s assume that pointer P is set to hold the address of array element Z[2]: P ¼ &Z[2];
We can now clear elements 2 and 3 of array Z, as in the two examples that follow. The two examples are identical except that in the first example pointer P holds the address of Z[3] at the end of the statements, and it holds the address of Z[2] at the end of the second set of statements: *P ¼ 0; P ¼ P þ 1; *P ¼ 0;
// Z[2] ¼ 0 // P now points to element 3 of Z // Z[3] ¼ 0
or *P ¼ 0; *(P þ 1) ¼ 0;
// Z[2] ¼ 0 // Z[3] ¼ 0
A pointer can be assigned to another pointer. In the following example, variables Cnt and Tot are both set to 10 using two different pointers: unsigned int *i, *j; unsigned int Cnt, Tot;
www.newnespress.com
// declare 2 pointers // declare two variables
C Programming Language i ¼ &Cnt; *i ¼ 10; j ¼ i; Tot ¼ *j;
3.1.16
135
// i points to Cnt // Cnt ¼ 10 // copy pointer i to pointer j // Tot ¼ 10
Structures
A structure can be used to collect related items that are then treated as a single object. Unlike an array, a structure can contain a mixture of data types. For example, a structure can store the personal details (name, surname, age, date of birth, etc.) of a student. A structure is created by using the keyword struct, followed by a structure name and a list of member declarations. Optionally, variables of the same type as the structure can be declared at the end of the structure. The following example declares a structure named Person: struct Person { unsigned unsigned unsigned unsigned }
char char char char
name[20]; surname[20]; nationality[20]; age;
Declaring a structure does not occupy any space in memory; rather, the compiler creates a template describing the names and types of the data objects or member elements that will eventually be stored within such a structure variable. Only when variables of the same type as the structure are created do these variables occupy space in memory. We can declare variables of the same type as the structure by giving the name of the structure and the name of the variable. For example, two variables Me and You of type Person can be created by the statement: struct Person Me, You;
Variables of type Person can also be created during the declaration of the structure as follows:
www.newnespress.com
136
Chapter 3
struct Person { unsigned unsigned unsigned unsigned } Me, You;
char char char char
name[20]; surname[20]; nationality[20]; age;
We can assign values to members of a structure by specifying the name of the structure, followed by a dot (“.”) and the name of the member. In the following example, the age of structure variable Me is set to 25, and variable M is assigned to the value of age in structure variable You: Me.age ¼ 25; M ¼ You.age;
Structure members can be initialized during the declaration of the structure. In the following example, the radius and height of structure Cylinder are initialized to 1.2 and 2.5 respectively: struct Cylinder { float radius; float height; } MyCylinder ¼ {1.2, 2.5};
Values can also be set to members of a structure using pointers by defining the variable types as pointers. For example, if TheCylinder is defined as a pointer to structure Cylinder, then we can write: struct Cylinder { float radius; float height; } *TheCylinder; TheCylinder TheCylinder
> radius ¼ 1.2; > height ¼ 2.5;
The size of a structure is the number of bytes contained within the structure. We can use the sizeof operator to get the size of a structure. Considering the above example, sizeof(MyCylinder)
returns 8, since each float variable occupies 4 bytes in memory.
www.newnespress.com
C Programming Language
137
Bit fields can be defined using structures. With bit fields we can assign identifiers to bits of a variable. For example, to identify bits 0, 1, 2, and 3 of a variable as LowNibble and to identify the remaining 4 bits as HighNibble we can write: struct { LowNibble HighNibble } MyVariable;
: 4; : 4;
We can then access the nibbles of variable MyVariable as: MyVariable.LowNibble ¼ 12; MyVariable.HighNibble ¼ 8;
In C language we can use the typedef statements to create new types of variables. For example, a new structure data type named Reg can be created as follows: typedef struct { unsigned char name[20]; unsigned char surname[20]; unsigned age; } Reg;
Variables of type Reg can then be created in the same way other types of variables are created. In the following example, variables MyReg, Reg1, and Reg2 are created from data type Reg: Reg MyReg, Reg1, Reg2;
The contents of one structure can be copied to another structure, provided that both structures are derived from the same template. In the following example, structure variables of the same type, P1 and P2, are created, and P2 is copied to P1: struct Person { unsigned char name[20]; unsigned char surname[20]; unsigned int age; unsigned int height; unsigned weight; } struct Person P1, P2;
www.newnespress.com
138
Chapter 3
........................ ........................ P2 ¼ P1;
3.1.17
Unions
Unions are used to overlay variables. A union is similar to a structure and is even defined in a similar manner. Both are based on templates, and the members of both are accessed using the “.” or “->” operators. A union differs from a structure in that all variables in a union occupy the same memory area, that is, they share the same storage. An example of a union declaration is: union flags { unsigned char x; unsigned int y; } P;
In this example, variables x and y occupy the same memory area, and the size of this union is 2 bytes long, which is the size of the biggest member of the union. When variable y is loaded with a 2-byte value, variable x will have the same value as the low byte of y. In the following example, y is loaded with 16-bit hexadecimal value 0xAEFA, and x is loaded with 0xFA: P.y ¼ 0xAEFA;
The size of a union is the size (number of bytes) of its largest member. Thus, the statement: sizeof(P)
returns 2. This union can also be declared as: union flags { unsigned char x; unsigned int y; } union flags P;
www.newnespress.com
C Programming Language
3.1.18
139
Operators in C
Operators are applied to variables and other objects in expressions to cause certain conditions or computations to occur. mikroC language supports the following operators: Arithmetic operators Relational operators Logical operators Bitwise operators Assignment operators Conditional operators Preprocessor operators
Arithmetic Operators Arithmetic operators are used in arithmetic computations. Arithmetic operators associate from left to right, and they return numerical results. The mikroC arithmetic operators are listed in Table 3.4.
Table 3.4: mikroC arithmetic operators Operator þ
Operation Addition Subtraction
*
Multiplication
/
Division
%
Remainder (integer division)
þþ
Auto increment Auto decrement
www.newnespress.com
140
Chapter 3
The following example illustrates the use of arithmetic operators: /* Adding two integers */ 5 þ 12
// equals 17
/* Subtracting two integers */ 120 – 5 10 – 15
// equals 115 // equals 5
/* Dividing two integers */ 5/3 12 / 3
// equals 1 // equals 4
/* Multiplying two integers */ 3 * 12
// equals 36
/* Adding two floating point numbers */ 3.1 þ 2.4
// equals 5.5
/* Multiplying two floating point numbers */ 2.5 * 5.0
// equals 12.5
/* Dividing two floating point numbers */ 25.0 / 4.0
// equals 6.25
/* Remainder (not for float) */ 7%3
// equals 1
/* Post-increment operator */ j ¼ 4; k ¼ jþþ;
// k ¼ 4, j ¼ 5
/* Pre-increment operator */ j ¼ 4; k ¼ þþj;
// k ¼ 5, j ¼ 5
/* Post-decrement operator */ j ¼ 12; k¼j ;
www.newnespress.com
// k ¼ 12, j ¼ 11
C Programming Language
141
/* Pre-decrement operator */ j ¼ 12; k¼ j;
// k ¼ 11, j ¼ 11
Relational Operators Relational operators are used in comparisons. If the expression evaluates to TRUE, a 1 is returned; otherwise a 0 is returned. All relational operators associate from left to right. A list of mikroC relational operators is given in Table 3.5. The following example illustrates the use of relational operators: x x x x x x x x
¼ 10 >8 ¼ ¼ 10 < 100 > 20 !¼ 10 >¼ 10
Greater than
¼
Greater than or equal to
0 && x < 10 > 0 || x < 10 >¼0 && x ¼0 && x < 5
a a b a
¼ > > >
// returns 1 // returns 1 // returns 1 // returns 0
10; b ¼ 20; c ¼ 30; d ¼ 40; b && c > d // returns 0 a && d > c // returns 1 b || d > c // returns 1
Bitwise Operators Bitwise operators are used to modify the bits of a variable. The mikroC bitwise operators are listed in Table 3.7. Bitwise AND returns 1 if both bits are 1, otherwise it returns 0. Bitwise OR returns 0 if both bits are 0, otherwise it returns 1. Bitwise XOR returns 1 if both bits are complementary, otherwise it returns 0. Bitwise complement inverts each bit. Bitwise shift left and shift right move the bits to the left or right respectively.
www.newnespress.com
C Programming Language
143
Table 3.7: mikroC bitwise operators Operator
Operation
&
Bitwise AND
|
Bitwise OR
∧
Bitwise EXOR
Bitwise complement
>
Shift right
The following example illustrates the use of bitwise operators: i. 0xFA & 0xEE returns 0xEA 0xFA: 1111 1010 0xEE: 1110 1110 -----------0xEA: 1110 1010 ii. 0x01 | 0xFE returns 0xFF 0x08: 0000 0001 0xFE: 1111 1110 -----------0xFE: 1111 1111 iii. 0xAA ∧ 0x1F returns 0xAA: 1010 1010 0x1F: 0001 1111 -----------0xB5: 1011 0101 iv.
0xAA returns 0x 0xAA: 1010 1010 : 0101 0101 -----------0x55: 0101 0101
www.newnespress.com
144
Chapter 3
v. 0x14 >> 1 returns 0x08 (shift 0x14 right by 1 digit) 0x14: 0001 0100 >>1 : 0000 1010 -----------0x0A: 0000 1010 vi. 0x14 >> 2 returns 0x05 (shift 0x14 right by 2 digits) 0x14: 0001 0100 >> 2: 0000 0101 -----------0x05: 0000 0101 vii. 0x235A 0 && x < 10) { Total þ¼ Sum; Sumþþ; } else { Total ¼ 0; Sum ¼ 0; }
www.newnespress.com
150
Chapter 3
switch Statement The switch statement is used when a number of conditions and different operations are performed if a condition is true. The syntax of the switch statement is: switch (condition) { case condition1: Statements; break; case condition2: Statements; break; ..................... ..................... case condition: Statements; break; default: Statements; }
The switch statement functions as follows: First the condition is evaluated. The condition is then compared to condition1 and if a match is found, statements in that case block are evaluated and control jumps outside the switch statement when the break keyword is encountered. If a match is not found, condition is compared to condition2 and if a match is found, statements in that case block are evaluated and control jumps outside the switch statements, and so on. The default is optional, and statements following default are evaluated if the condition does not match any of the conditions specified after the case keywords. In the following example, the value of variable Cnt is evaluated. If Cnt ¼ 1, A is set to 1. If Cnt ¼ 10, B is set to 1, and if Cnt ¼ 100, C is set to 1. If Cnt is not equal to 1, 10, or 100 then D is set to 1: switch (Cnt) { case 1: A ¼ 1; break; case 10: B ¼ 1; break;
www.newnespress.com
C Programming Language
151
case 100: C ¼ 1; break; default: D ¼ 1; }
Because white spaces are ignored in C language we can also write the preceding code as: switch (Cnt) { case 1: case 10: case 100: default: }
A ¼ 1; B ¼ 1; C ¼ 1; D ¼ 1;
break; break; break;
Example 3.1 In an experiment the relationship between X and Y values are found to be: X 1 2 3 4 5
Y 3.2 2.5 8.9 1.2 12.9
Write a switch statement that will return the Y value, given the X value. Solution 3.1 The required switch statement is: switch (X) { case 1: Y ¼ 3.2; break; case 2: Y ¼ 2.5; break;
www.newnespress.com
152
Chapter 3 case 3: Y ¼ 8.9; break; case 4: Y ¼ 1.2; break; case 5: Y ¼ 12.9;
}
Iteration Statements Iteration statements enable us to perform loops in a program, where part of a code must be repeated a number of times. In mikroC iteration can be performed in four ways. We will look at each one with examples: Using for statement Using while statement Using do statement Using goto statement for Statement The syntax of a for statement is: for(initial expression; condition expression; increment expression) { Statements; }
The initial expression sets the starting variable of the loop, and this variable is compared against the condition expression before entry into the loop. Statements inside the loop are executed repeatedly, and after each iteration the value of the increment expression is incremented. The iteration continues until the condition expression becomes false. An endless loop is formed if the condition expression is always true. The following example shows how a loop can be set up to execute 10 times. In this example, variable i starts from 0 and increments by 1 at the end of each iteration. The loop terminates when i ¼10, in which case the condition i < 10 becomes false. On exit from the loop, the value of i is 10:
www.newnespress.com
C Programming Language
153
for(i ¼ 0; i < 10; i þþ) { statements; }
This loop could also be started by an initial expression with a nonzero value. Here, i starts with 1 and the loop terminates when i ¼ 11. Thus, on exit from the loop, the value of i is 11: for(i ¼ 1; i ¼ 2) if(a ¼¼ 10) if(a > 0)
11.
Write a program to calculate whether a number is odd or even.
12.
Determine the value of the following bitwise operations using AND, OR, and EXOR operations: Operand 1: Operand 2:
13.
00010001 11110001
How many times does each of the following loops iterate, and what is the final value of the variable j in each case? a) b) c) d) e)
for(j ¼ 0; for(j ¼ 1; for(j ¼ 0; for(j ¼ 0; for(j ¼ 10;
j < 5; jþþ) j < 10; jþþ) j b) ? 0 : 1 b) (x < y) ? (a > b) : (c > d)
19.
Given that f1 and f2 are both floating point variables, explain why the following test expression controlling the while loop may not be safe: do { . . .. . .. . .. . .. . . . . .. . .. . .. . .. . . } while(f1 !¼ f2);
Why would the problem not occur if both f1 and f2 were integers? How would you correct this while loop? 20.
What can you say about the following while loop? k ¼ 0; Total ¼ 0; while (k < 10) { Sumþþ; Total þ¼ Sum; }
21.
What can you say about the following for loop? Cnt ¼ 0; for(;;) { Cntþþ; }
www.newnespress.com
This page intentionally left blank
CHAPTER 4
Functions and Libraries in mikroC 4.1 mikroC Functions A function is a self-contained section of code written to perform a specifically defined action. Functions are usually created when a single operation must be performed in different parts of the main program. It is, moreover, good programming practice to divide a large program into a number of smaller, independent functions. The statements within a function are executed by calling (or invoking) the function. The general syntax of a function definition is shown in Figure 4.1. The data type indicates the type of data returned by the function. This is followed by the name of the function and then a set of parentheses, within which the arguments, separated by commas, are declared. The body of the function, which includes the function’s operational code, is written inside a set of curly brackets. In the sample function definition that follows, the function, named Mult, receives two integer arguments, a and b, and returns their product. Note that using parentheses in a return statement is optional: int Mult(int a, int b) { return (a*b); }
When a function is called, it generally expects to be given the number of arguments expressed in the function’s argument list. For example, the preceding function can be called as: z ¼ Mult(x, y);
www.newnespress.com
170
Chapter 4 type name (parameter1, parameter2,…..) { …………. function body …………. }
Figure 4.1: General syntax of a function definition where variable z has the data type int. Note that the arguments declared in the function definition and the arguments passed when the function is called are independent of each other, even if they have the same name. In the preceding example, when the function is called, variable x is copied to a and variable y is copied to b on entry into function Mult. Some functions do not return any data. The data type of such functions must be declared as void. For example: void LED(unsigned char D) { PORTB ¼ D; }
void functions can be called without any assignment statements, but the parentheses are needed to tell the compiler that a function call is made: LED( );
Also, some functions do not have any arguments. In the following example, the function, named Compl, complements PORTC of the microcontroller. It returns no data and has no arguments: void Compl( ) { PORTC ¼ PORTC; }
This function can be called as: Compl( );
Functions are normally defined before the start of the main program. Some function definitions and their use in main programs are illustrated in the following examples:
www.newnespress.com
Functions and Libraries in mikroC
171
Example 4.1 Write a function called Circle_Area to calculate the area of a circle where the radius is to be used as an argument. Use this function in a main program to calculate the area of a circle whose radius is 2.5cm. Store the area in a variable called Circ. Solution 4.1 The data type of the function is declared as float. The area of a circle is calculated by the formula: Area ¼ pr2
where r is the radius of the circle. The area is calculated and stored in a local variable called s, which is then returned from the function: float Circle_Area(float radius) { float s; s ¼ PI * radius * radius; return s; }
Figure 4.2 shows how the function Circle_Area can be used in a main program to calculate the area of a circle whose radius is 2.5cm. The function is defined before the main program. Inside the main program the function is called to calculate and store the area in variable Circ. Example 4.2 Write a function called Area and a function called Volume to calculate the area and volume of a cylinder respectively. Then write a main program to calculate the area and the volume of cylinder whose radius is 2.0cm and height is 5.0cm. Store the area in variable cyl_area and the volume in variable cyl_volume. Solution 4.2 The area of a cylinder is calculated by the formula: Area ¼ 2prh
www.newnespress.com
172
Chapter 4 /∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ AREA OF A CIRCLE ================ This program calls to function Circle_Area to calculate the area of a circle. Programmer: Dogan Ibrahim File: CIRCLE.C Date: May, 2007 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ /∗ This function calculates the area of a circle given the radius ∗/ float Circle_Area(float radius) { float s; s = PI ∗ radius ∗ radius; return s; } /∗ Start of main program. Calculate the area of a circle where radius = 2.5 ∗/ void main() { float r, Circ; r = 2.5; Circ = Circle_Area(r); }
Figure 4.2: Program to calculate the area of a circle
where r and h are the radius and height of the cylinder. The volume of a cylinder is calculated by the formula: Volume ¼ pr2h
Figure 4.3 shows the functions that calculate the area and volume of a cylinder. The main program that calculates the area and volume of a cylinder whose radius = 2.0cm and height = 5.0cm is shown in Figure 4.4. Example 4.3 Write a function called LowerToUpper to convert a lowercase character to uppercase.
www.newnespress.com
Functions and Libraries in mikroC
173
float Area(float radius, float height) { float s; s = 2.0∗PI ∗ radius∗height; return s; } float Volume(float radius, float height) { float s; s = PI ∗radius∗radius∗height; return s; }
Figure 4.3: Functions to calculate cylinder area and volume
Solution 4.3 The ASCII value of the first uppercase character (‘A’) is 041. Similarly, the ASCII value of the first lowercase character (‘a’) is 061. An uppercase character can be converted to its equivalent lowercase by subtracting 020 from the character. The required function listing is shown in Figure 4.5. Example 4.4 Use the function you created in Example 4.3 in a main program to convert letter ‘r’ to uppercase. Solution 4.4 The required program is shown in Figure 4.6. Function LowerToUpper is called to convert the lowercase character in variable Lc to uppercase and store in Uc.
4.1.1
Function Prototypes
If a function is not defined before it is called, the compiler will generate an error message. One way around this problem is to create a function prototype. A function prototype is easily constructed by making a copy of the function’s header and appending a semicolon to it. If the function has parameters, giving names to these
www.newnespress.com
174
Chapter 4 /∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ AREA AND VOLUME OF A CYLINDER =============================== This program calculates the area and volume of a cylinder whose radius is 2.0cm and height is 5.0cm. Programmer: Dogan Ibrahim File: CYLINDER.C Date: May, 2007 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/ /∗ Function to calculate the area of a cylinder ∗/ float Area(float radius, float height) { float s; s = 2.0∗PI ∗ radius*height; return s; } /∗ Function to calculate the volume of a cylinder ∗/ float Volume(float radius, float height) { float s; s = PI ∗radius∗radius∗height; return s; } /∗ Start of the main program ∗/ void main() { float r = 2.0, h = 5.0; float cyl_area, cyl_volume; cyl_area = Area(r, h); cyl_volume(r, h); }
Figure 4.4: Program that calculates the area and volume of a cylinder
parameters is not compulsory, but the data type of the parameters must be defined. An example follows in which a function prototype called Area is declared and the function is expected to have a floating point type parameter: float Area(float radius);
www.newnespress.com
Functions and Libraries in mikroC
175
unsigned char LowerToUpper(unsigned char c) { if(c >= ‘a’ && c = ‘a’ && c = ‘a’ && c Software PIC Simulator.
www.newnespress.com
PIC18 Development Tools
271
Step 3 From the drop-down menu select Run -> Start Debugger. Step 4 Select variables Sum, i, and PORTC from the Watch window as described in Example 5.2. Step 5 To set a breakpoint at the end of the program, click the mouse at the last closing bracket of the program, which is at line 27, and press F5. As shown in Figure 5.49, you should see a red line at the breakpoint and a little marker in the left column of the Code Editor window. Step 6 Now, start the debugger, and press F6 key to run the program. The program will stop at the breakpoint, displaying variables as shown in Figure 5.48. This is the end of the simulation. Select from drop-down menu Run -> Stop Debugger. To clear a breakpoint, move the cursor over the line where the breakpoint is and then press F5. To clear all breakpoints in a program, press the SHIFTþCTRLþF5 keys. To display the breakpoints in a program, press the SHIFTþF4 keys. The following are some other useful debugger commands: Step Into [F7] Executes the current instruction and then halts. If the instruction is a call to a routine, the program enters the routine and halts at the first instruction.
Figure 5.49: Setting a breakpoint at line 27
www.newnespress.com
272
Chapter 5 Step Over [F8] Executes the current instruction and then halts. If the instruction is a call to a routine, it skips it and halts at the first instruction following the call. Step Out [CTRLþF8] Executes the current instruction and then halts. If the instruction is within a routine, it executes the instruction and halts at the first instruction following the call. Run to Cursor [F4] Executes all instructions between the current instruction and the cursor position. Jump to Interrupt [F2] Jumps to the interrupt service routine address (address 0x08 for PIC18 microcontrollers) and executes the procedure located at that address.
5.3.4
Using the mikroICD In-Circuit Debugger
This section discusses how to use the mikroICD in-circuit debugger (also called the PICFlash 2 programmer) to debug the program developed in Example 5.1. First of all, we have to build the hardware and then connect the in-circuit debugger device. In this example, the hardware is built on a breadboard, and a PICFlash 2 mikroICD in-circuit debugger is used to debug the system. Note that pins RB6 and RB7 are used by the mikroICD and are not available for I/O while mikroICD is active.
The Circuit Diagram The project’s circuit diagram is shown in Figure 5.50. The mikroICD in-circuit debugger is connected to the development circuit using the following pins of the microcontroller: MCLR RB6 RB7 þ5V GND
www.newnespress.com
PIC18 Development Tools
273
Figure 5.50: Circuit diagram of the project The mikroICD has two modes of operation. In inactive mode all lines from the microcontroller used by the debugger device are connected to the development system. In active mode the MCLR, RB6, and RB7 pins are disconnected from the development system and used to program the microcontroller. After the programming, these lines are restored. The mikroICD debugger device has a 10-way IDC connector and can be connected to the target system with a 10-way IDC header. Once the development is finished and the mikroICD debugger is removed, opposite pairs of the IDC header can be connected with jumpers. Figure 5.51 shows the system built on a breadboard.
Debugging After building the hardware we are ready to program the microcontroller and test the system’s operation with the in-circuit debugger. The steps are as follows: Step 1 Start the mikroC IDE, making sure the program developed in Example 5.1 is displayed in the Code Editor window.
www.newnespress.com
274
Chapter 5
Figure 5.51: System built on a breadboard
Step 2 Click the Edit Project button (Figure 5.52) and set DEBUG_ON as shown in Figure 5.53.
Edit Project button
Figure 5.52: Edit Project button
Step 3 Select ICD Debug in the Project Setup window as shown in Figure 5.54. Step 4 Click the Build Project icon to compile the program with the debugger. After a successful compilation you should see the message Success (ICD Build) in the Message Window.
www.newnespress.com
PIC18 Development Tools
275
Figure 5.53: Set the DEBUG_ON
Step 5 Make sure the mikroICD debugger device is connected as in Figure 5.50, and select Tools -> PicFlash Programmer from the drop-down menu to program the microcontroller. Step 6 From the drop-down menu select Debugger -> Select Debugger -> mikroICD Debugger as shown in Figure 5.55.
www.newnespress.com
276
Chapter 5
Figure 5.54: Select the ICD Debug
Figure 5.55: Selecting the mikroICD debugger Step 7 From the drop-down menu select Run -> Start Debugger. The debugger form will pop up and select variables Sum, i, and PORTC as described in Example 5.2. Step 8 Single-step through the program by pressing the F8 key. You should see the values of variables changing. At the end of the program, decimal value 55 will be sent to PORTC, and LEDs 0,1,2,4, and 5 should be turned ON, as shown in Figure 5.56, corresponding to this number.
Figure 5.56: Decimal number 55 shown in LEDs
www.newnespress.com
PIC18 Development Tools
277
Step 9 Stop the debugger. In routines that contain delays, the Step Into [F7] and Step Over [F8] commands can take a long time. Run to Cursor [F4] and breakpoints should be used instead.
5.3.5
Using a Development Board
It is easy to develop microcontroller-based applications with the help of a development board. This section explains how to use the development board BIGPIC4, described earlier in this chapter. The program written in Example 5.1 is compiled and then loaded to the microcontroller using the on-board mikroICD in-circuit emulator. Then the program runs and displays the sum of the numbers 1 to 10 on the LEDs connected to PORTC. However, before using the development board we need to know how the BIGPIC4 is organized and how to use the various devices on the board.
BIGPIC4 Development Board Figure 5.57 shows the BIGPIC4 development board with the functions of various devices identified with arrows. The board can be powered either from an external power supply (8- to 16-C AC/DC) or from the USB port of a computer, using a jumper. In this application, the board is powered from a USB port. A 2-row by 16-column LCD can be connected in the board’s upper left corner. The contrast of the LCD can be adjusted with a small potentiometer. The forty-six LEDs on the board can be connected to the output ports of the microcontroller, selected by switch S2. Figure 5.58 shows how to select the LEDs, using PORTC as an example. 1K resistors are used in series with the LEDs to limit the current. For example, to connect eight LEDs to PORTC we have to set the switch arm marked PORTC of switch S2 to the ON position. The forty-six push-button switches on the board can be used to program digital inputs to the microcontroller. There is also a push-button switch that acts as the RESET. Jumper J12 determines whether a button press will bring logical 0 or logical 1 to the microcontroller. When the button is not pressed, the pin state is determined by jumper J5.
www.newnespress.com
278
Chapter 5 External Power supply USB
RS232 ports mikroICD
PC keyboard
Port connectors LCD Processor LEDs Contrast Reset
Push-button switches
Graphics LCD
MMC/SD slot
Figure 5.57: BIGPIC4 development board
Figure 5.58: LED and push-button switch connections
www.newnespress.com
PIC18 Development Tools
279
At the bottom central position, a 128 64 pixel graphics LCD can be connected to the board. The contrast of the LCD can be adjusted by a small potentiometer. The MMC/SD card slot at the bottom right-hand corner of the board supports cards up to 2GB storage capacity. The RESET button is located just above the MMC/SD card slot. Above the RESET button are two potentiometers for analog-to-digital converter applications. All of the microcontroller port pins are available at the connectors situated along the right-hand side of the board. In the top middle portion of the board are two RS232 ports and a connection to a PC keyboard. The board supports both 64-pin and 80-pin microcontrollers. The board comes with a PIC18F8520 microcontroller connected to the board, operating with a 10MHz crystal. Further details about the operation of the board can be found in the BIGPIC4 user’s manual. The steps in developing an application using the BIGPIC4 board are as follows: Step 1 Double-click the mikroC icon to start the IDE. Step 2 Create a new project called EXAMPLE2 (see Figure 5.59) and select the microcontroller type as PIC18F8520, the clock as 10MHz, and device flags as:
_OSC_HS_1H
_WDT_OFF_2H
_LVP_OFF_4L
_DEBUG_ON_4L
www.newnespress.com
280
Chapter 5
Figure 5.59: Creating a new project Step 3 Enter the following program into the Code Editor section of the IDE: /****************************************************************** EXAMPLE PROGRAM This program uses the PICBIG4 Development Board. 8 LEDs are connected To PORTC of the microcontroller which is a PIC18F8520 operating at 10MHz. This program calculates the sum of integer numbers from 1 to 10 And then displays the sum on PORTC of the microcontroller.
www.newnespress.com
PIC18 Development Tools
281
Author: Dogan Ibrahim File: EXAMPLE2.C ****************************************************************/ void main() { unsigned int Sum,i; TRISC ¼ 0; Sum ¼ 0; for(i¼1; i Save As. Step 5 Tick option ICD Debug in the Project Setup window. Compile the project by pressing CTRLþF9 or by clicking the Build Project button. Step 6 Connect the BIGPIC4 development board to the USB port on the computer. Configure the development board by routing eight LEDs to PORTC: Set the arm marked PORTC on switch S2 to the ON position. Step 7 Select Tools -> PicFlash Programmer from the drop-down menu to program the microcontroller. Step 8 Select Debugger -> Select Debugger -> mikroICD Debugger. Step 9 Start the debugger by clicking Run -> Start Debugger and select variables Sum, i, and PORTC from the Watch window. Step 10 Single-step through the program until the end by repeatedly pressing F8. At the end of the program, the PORTC LEDs will turn ON to display decimal 55 (i.e., LEDs 0,1,2,4, and 5 will turn ON). Step 11 Stop the debugger. View the EEPROM Window The mikroICD EEPROM window is invoked from the mikroC IDE drop-down menu when the mikroICD debug mode is selected and started, and it displays contents of the PIC internal EEPROM memory. To view the
www.newnespress.com
282
Chapter 5
Figure 5.60: Display of EEPROM memory memory, click View -> Debug Windows -> View EEPROM. Figure 5.60 shows an example EEPROM window display. View the RAM Window The mikroICD RAM window is invoked from the mikroC IDE drop-down menu when the mikroICD debug mode is selected and started, and it displays contents of the PIC internal RAM memory. To view the memory, click View -> Debug Windows -> View RAM. Figure 5.61 shows an example RAM window display. View the Code Window The mikroICD Code window is invoked from the mikroC IDE drop-down menu when the mikroICD debug mode is selected and started, and it displays the contents of the PIC internal code memory. To view
www.newnespress.com
PIC18 Development Tools
283
Figure 5.61: Display of RAM memory
the memory, click View -> Debug Windows -> View Code. Figure 5.62 shows an example Code window display. View the Statistics The Statistics window is invoked from the mikroC IDE dropdown menu and it displays various statistical data about our program. To view the statistics window, click View -> View Statistics. Figure 5.63 shows an example Statistics window, which consists of several tabs. The Memory Usage tab displays the amount of RAM (data memory) and ROM (code memory) used. The Procedures tabs display information about the size and locations of the procedures. The RAM and ROM tabs display memory usage in detail.
www.newnespress.com
Figure 5.62: Display of Code memory
Figure 5.63: Display of Statistics window
www.newnespress.com
PIC18 Development Tools
285
5.4 Summary This chapter has described the PIC microcontroller software development tools (such as text editors, assemblers, compilers, and simulators) and hardware development tools (including development boards and kits, programming devices, in-circuit debuggers, and in-circuit emulators). The mikroC compiler was used in the examples and projects. The steps in developing and testing a mikroC-based C program were presented both with and without a hardware in-circuit debugger, followed by an example of how to use the BIGPIC4 development board, with the on-board in-circuit debugger enabled.
5.5 Exercises 1. Describe the phases of the microcontroller-based system development cycle. 2. Describe briefly the microcontroller development tools. 3. Explain the advantages and disadvantages of assemblers and compilers. 4. Explain why a simulator can be a useful tool while developing a microcontrollerbased product. 5. Explain in detail what a device programmer is. Give some examples of device programmers for the PIC18 series of microcontrollers. 6. Describe briefly the differences between in-circuit debuggers and in-circuit emulators. List the advantages and disadvantages of both debugging tools. 7. Enter the following program into the mikroC IDE and compile the program, correcting any syntax errors. Then, using the software ICD, simulate the operation of the program by single-stepping through the code, and observe the values of the variables during the simulation. /*¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼ A SIMPLE LED PROJECT This program flashes the 8 LEDs connected to PORTC of a PIC18F452 microcontroller. ¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼*/ void main() { TRISC ¼ 0;
// PORTC is output
www.newnespress.com
286
Chapter 5 do { PORTC ¼ 0xFF; PORTC ¼ 0; } while(1);
// Turn ON LEDs on PORTC // Turn OFF LEDs on PORTC // Endless loop
}
8. Describe the steps in using the mikroICD in-circuit debugger. 9. The following C program contains some deliberately introduced errors. Compile the program to find and correct the errors. void main() { unsigned char i,j,k i ¼ 10; j ¼ i þ 1; for(i ¼ 0; i < 10; iþþ) { Sum ¼ Sum þ i; jþþ } } }
10.
The following C program contains some deliberately introduced errors. Compile the program to find and correct the errors. int add(int a, int b) { result ¼ a þ b } void main() { int p,q; p ¼ 12; q ¼ 10; z ¼ add(p, q) zþþ; for(i ¼ 0; i < z; iþþ)pþþ } }
www.newnespress.com
CHAPTER 6
Simple PIC18 Projects In this chapter we will look at the design of simple PIC18 microcontroller-based projects, with the idea of becoming familiar with basic interfacing techniques and learning how to use the various microcontroller peripheral registers. We will look at the design of projects using LEDs, push-button switches, keyboards, LED arrays, sound devices, and so on, and we will develop programs in C language using the mikroC compiler. The hardware is designed on a low-cost breadboard, but development kits such as BIGPIC4 can be used for these projects. We will start with very simple projects and proceed to more complex ones. It is recommended that the reader moves through the projects in their given order. The following are provided for each project: Description of the program Description of the hardware Circuit diagram Algorithm description (in PDL) Program listing Suggestions for further development The program’s algorithm can be described in a variety of graphic and text-based methods, some of the common ones being a flow diagram, a structure chart, and program description language. In this book we are using program description language (PDL).
www.newnespress.com
288
Chapter 6
6.1 Program Description Language (PDL) Program description language (PDL) is free-format English-like text which describes the flow of control in a program. PDL is not a programming language but rather is a tool which helps the programmer to think about the logic of the program before the program has been developed. Commonly used PDL keywords are described as follows.
6.1.1
START-END
Every PDL program description (or subprogram) should begin with a START keyword and terminate with an END keyword. The keywords in a PDL code should be highlighted in bold to make the code more clear. It is also a good practice to indent program statements between PDL keywords in order to enhance the readability of the code. Example: START ...... ...... END
6.1.2
Sequencing
For normal sequencing in a program, write the statements as short English text as if you are describing the program. Example: Turn on the LED Wait 1 second Turn off the LED
6.1.3
IF-THEN-ELSE-ENDIF
Use IF, THEN, ELSE, and ENDIF keywords to describe the flow of control in a program.
www.newnespress.com
Simple PIC18 Projects
289
Example: IF switch ¼ 1 THEN Turn on LED 1 ELSE Turn on LED 2 Start the motor ENDIF
6.1.4
DO-ENDDO
Use Do and ENDDO keywords to show iteration in the PDL code. Example: To create an unconditional loop in a program we can write: Turn on LED DO 10 times Set clock to 1 Wait for 10ms Set clock to 0 ENDDO
A variation of the DO-ENDDO construct is to use other keywords like DO-FOREVER, DO-UNTIL, etc. as shown in the following examples. Example: To create a conditional loop in a program we can write: Turn off buzzer IF switch ¼ 1 THEN DO UNTIL Port 1 ¼ 1 Turn on LED Wait for 10ms Read Port 1 ENDDO ENDIF
www.newnespress.com
290
Chapter 6
The following construct can be used when an endless loop is required: DO FOREVER Read data from Port 1 Send data to PORT 2 Wait for 1 second ENDDO
6.1.5
REPEAT-UNTIL
REPEAT-UNTIL is another control construct used in PDL codes. In the following example the program waits until a switch value is equal to 1. Example: REPEAT Turn on buzzer Read switch value UNTIL switch ¼ 1
Notice that the REPEAT-UNTIL loop is always executed at least once, and more than once if the condition at the end of the loop is not met.
PROJECT 6.1—Chasing LEDs Project Description In this project eight LEDs are connected to PORTC of a PIC18F452-type microcontroller, and the microcontroller is operated from a 4MHz resonator. When power is applied to the microcontroller (or when the microcontroller is reset), the LEDs turn ON alternately in an anticlockwise manner where only one LED is ON at any time. There is a one-second delay between outputs so the LEDs can be seen turning ON and OFF. An LED can be connected to a microcontroller output port in two different modes: current sinking and current sourcing.
Current Sinking Mode As shown in Figure 6.1, in current sinking mode the anode leg of the LED is connected to the þ5V supply, and the cathode leg is connected to the microcontroller output port through a current limiting resistor.
www.newnespress.com
Simple PIC18 Projects
291
Figure 6.1: LED connected in current sinking mode
The voltage drop across an LED varies between 1.4V and 2.5V, with a typical value of 2V. The brightness of the LED depends on the current through the LED, and this current can vary between 8 and 16mA, with a typical value of 10mA. The LED is turned ON when the output of the microcontroller is at logic 0 so the current flows through the LED. Assuming the microcontroller output voltage is about 0.4V when the output is low, we can calculate the value of the required resistor as follows: R ¼
VS
VLED ILED
VL
ð6:1Þ
where VS is the supply voltage (5V) VLED is the voltage drop across the LED (2V) VL is the maximum output voltage when the output port is low (0.4V) ILED is the current through the LED (10mA) Substituting the values into Equation (6.1) we get, R ¼
5
2 0:4 ¼ 260 ohm 10
The nearest physical resistor is 270 ohms.
www.newnespress.com
292
Chapter 6
Current Sourcing Mode As shown in Figure 6.2, in current sourcing mode the anode leg of the LED is connected to the microcontroller output port and the cathode leg is connected to the ground through a current limiting resistor.
Figure 6.2: LED connected in current sourcing mode In this mode the LED is turned ON when the microcontroller output port is at logic 1 (i.e., þ5V). In practice, the output voltage is about 4.85V and the value of the resistor can be determined as: R ¼
VO
VLED ILED
ð6:2Þ
where VO is the output voltage of the microcontroller port when at logic 1 (þ4.85V). Thus, the value of the required resistor is: R ¼
4:85 2 ¼ 285 ohm 10
The nearest physical resistor is 290 ohm.
Project Hardware The circuit diagram of the project is shown in Figure 6.3. LEDs are connected to PORTC in current sourcing mode with eight 290-ohm resistors. A 4MHz resonator is connected between the OSC1 and OSC2 pins. Also, an external reset push button is connected to the MCLR input to reset the microcontroller when required.
www.newnespress.com
Simple PIC18 Projects
293
Figure 6.3: Circuit diagram of the project
Project PDL The PDL of this project is very simple and is given in Figure 6.4.
START Configure PORTC pins as output Initialise J = 1 DO FOREVER Set PORTC = J Shift left J by 1 digit IF J = 0 THEN J=1 ENDIF Wait 1 second ENDDO END
Figure 6.4: PDL of the project
www.newnespress.com
294
Chapter 6
Project Program The program is named as LED1.C, and the program listing is given in Figure 6.5. At the beginning of the program PORTC pins are configured as outputs by setting TRISC ¼ 0. Then an endless for loop is formed, and the LEDs are turned ON alternately in an anticlockwise manner to create a chasing effect. The program checks continuously so that when LED 7 is turned ON, the next LED to be turned ON is LED 0. This program can be compiled using the mikroC compiler. Project settings should be configured to 4MHz clock, XT crystal mode, and WDT OFF. The HEX file (LED1.HEX) should be loaded to the PIC18F452 microcontroller using either an in-circuit debugger or a programming device.
/************************************************************************************************* CHASING LEDS ============ In this project 8 LEDs are connected to PORTC of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz resonator. The program turns on the LEDs in an anti-clockwise manner with one second delay between each output. The net result is that the LEDs seem to be chasing each other.
Author: Dogan Ibrahim Date: July 2007 File: LED1.C ***************************************************************************************************/ void main() { unsigned char J = 1; TRISC = 0; for(;;) { PORTC = J; Delay_ms(1000); J = J 6
}
Figure 6.10: Program listing
www.newnespress.com
300
Chapter 6 /****************************************************************************************** SIMPLE DICE =========== In this project 7 LEDs are connected to PORTC of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz resonator. The LEDs are organized as the faces of a real dice. When a push-button switch connected to RB0 is pressed a dice pattern is displayed on the LEDs. The display remains in this state for 3 seconds and after this period the LEDs all turn OFF to indicate that the system is ready for the button to be pressed again. In this program a pseudorandom number generator function is used to generate the dice numbers between 1 and 6. Author: Dogan Ibrahim Date: July 2007 File: LED3.C *******************************************************************************************/ #define Switch PORTB.F0 #define Pressed 0 // // This function generates a pseudo random integer number // between 1 and Lim // unsigned char Number(int Lim, int Y) { unsigned char Result; static unsigned int Y; Y = (Y * 32719 + 3) % 32749; Result = ((Y % Lim) + 1); return Result; } // // Start of MAIN program // void main() { unsigned char J,Pattern,Seed = 1; unsigned char DICE[] = {0,0x08,0x22,0x2A,0x55,0x5D,0x77}; TRISC = 0; TRISB = 1; PORTC = 0;
// PORTC outputs // RB0 input // Turn OFF all LEDs
for(;;)
// Endless loop
Figure 6.11: Dice program using a pseudorandom number generator
www.newnespress.com
Simple PIC18 Projects
301
{ if(Switch == Pressed) { J = Number(6,seed); Pattern = DICE[J]; PORTC = Pattern; Delay_ms(3000); PORTC = 0; }
// Is switch pressed ? // Generate a number between 1 and 6 // Get LED pattern // Turn on LEDs // Delay 3 second // Turn OFF all LEDs
} }
Figure 6.11: (Cont’d) defines the number set to be generated. In this example, the seed is set to 1. Every time the function is called, a number between 1 and 6 is generated. The operation of the program is basically same as in Figure 6.10. When the push-button switch is pressed, function Number is called to generate a new dice number between 1 and 6, and this number is used as an index in array DICE in order to find the bit pattern to be sent to the LEDs.
PROJECT 6.3—Two-Dice Project Project Description This project is similar to Project 2, but here a pair of dice are used—as in many dice games such as backgammon—instead of a single dice. The circuit shown in Figure 6.8 can be modified by adding another set of seven LEDs for the second dice. For example, the first set of LEDs can be driven from PORTC, the second set from PORTD, and the push-button switch can be connected to RB0 as before. Such a design requires fourteen output ports just for the LEDs. Later on we will see how the LEDs can be combined in order to reduce the input/output requirements. Figure 6.12 shows the block diagram of the project.
Project Hardware The circuit diagram of the project is shown in Figure 6.13. The circuit is basically same as in Figure 6.8, with the addition of another set of LEDs connected to PORTD.
www.newnespress.com
302
Chapter 6
Push-button switch
PIC18F452
DICE
Figure 6.12: Block diagram of the project
Figure 6.13: Circuit diagram of the project
Project PDL The operation of the project is very similar to that for Project 2. Figure 6.14 shows the PDL for this project. At the beginning of the program the PORTC and PORTD pins are configured as outputs, and bit 0 of PORTB (RB0) is configured as input. The program then executes in a loop continuously and checks the state of the push-button switch. When the switch is pressed, two pseudorandom numbers between 1 and 6 are
www.newnespress.com
Simple PIC18 Projects
303
START Create DICE table Configure PORTC as outputs Configure PORTD as outputs Configure RB0 as input DO FOREVER IF button pressed THEN Get a random number between 1 and 6 Find bit pattern Turn ON LEDs on PORTC Get second random number between 1 and 6 Find bit pattern Turn on LEDs on PORTD Wait 3 seconds Turn OFF all LEDs ENDIF ENDDO END
Figure 6.14: PDL of the project generated, and these numbers are sent to PORTC and PORTD. The LEDs remain at this state for 3 seconds, after which all the LEDs are turned OFF to indicate that the push-button switch can be pressed again for the next pair of numbers.
Project Program The program is called LED4.C, and the program listing is given in Figure 6.15. At the beginning of the program Switch is defined as bit 0 of PORTB, and Pressed is defined as 0. The relationships between the dice numbers and the LEDs to be turned on are stored in an array called DICE, as in Project 2. Variable Pattern is the data sent to the LEDs. Program enters an endless for loop where the state of the push-button switch is checked continuously. When the switch is pressed, two random numbers are generated by calling function Number. The bit patterns to be sent to the LEDs are then determined and sent to PORTC and PORTD. The program then repeats inside the endless loop, checking the state of the push-button switch.
PROJECT 6.4—Two-Dice Project Using Fewer I/O Pins Project Description This project is similar to Project 3, but here LEDs are shared, which uses fewer input/ output pins.
www.newnespress.com
304
Chapter 6 /∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ TWO DICE ======== In this project 7 LEDs are connected to PORTC of a PIC18F452 microcontroller and 7 LEDs to PORTD. The microcontroller is operated from a 4MHz resonator. The LEDs are organized as the faces of a real dice. When a push-button switch connected to RB0 is pressed a dice pattern is displayed on the LEDs. The display remains in this state for 3 seconds and after this period the LEDs all turn OFF to indicate that the system is ready for the button to be pressed again. In this program a pseudorandom number generator function is used to generate the dice numbers between 1 and 6. Author: Dogan Ibrahim Date: July 2007 File: LED4.C ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/ #define Switch PORTB.F0 #define Pressed 0 // // This function generates a pseudo random integer number // between 1 and Lim // unsigned char Number(int Lim, int Y) { unsigned char Result; static unsigned int Y; Y = (Y * 32719 + 3) % 32749; Result = ((Y % Lim) + 1); return Result; }
// // Start of MAIN program // void main() { unsigned char J,Pattern,Seed = 1; unsigned char DICE[] = {0,0x08,0x22,0x2A,0x55,0x5D,0x77}; TRISC = 0; TRISD = 0; TRISB = 1; PORTC = 0; PORTD = 0;
// PORTC are outputs // PORTD are outputs // RB0 input // Turn OFF all LEDs // Turn OFF all LEDs
Figure 6.15: Program listing
www.newnespress.com
Simple PIC18 Projects for(;;) { if(Switch == Pressed) { J = Number(6,seed); Pattern = DICE[J]; PORTC = Pattern; J = Number(6,seed); Pattern = DICE[J]; PORTD = Pattern; Delay_ms(3000); PORTC = 0; PORTD = 0; } }
305
// Endless loop // Is switch pressed ? // Generate first dice number // Get LED pattern // Turn on LEDs for first dice // Generate second dice number // Get LED pattern // Turn on LEDs for second dice // Delay 3 seconds // Turn OFF all LEDs // Turn OFF all LEDS
}
Figure 6.15: (Cont’d) The LEDs in Table 6.1 can be grouped as shown in Table 6.3. Looking at this table we can say that: D4 can appear on its own D2 and D6 are always together D1 and D3 are always together D5 and D7 are always together Thus, we can drive D4 on its own and then drive the D2,D6 pair together in series, the D1,D3 pair together in series, and also the D5,D7 pair together in series. (Actually, we Table 6.3: Grouping the LEDs Required number
LEDs to be turned on
1
D4
2
D2 D6
3
D2 D6 D4
4
D1 D3 D5 D7
5
D1 D3 D5 D7 D4
6
D2 D6 D1 D3 D5 D7
www.newnespress.com
306
Chapter 6
could share D1,D3,D5,D7 but this would require 8 volts to drive if the LEDs are connected in series. Connecting them in parallel would call for even more current, and a driver IC would be required.) Altogether, four lines are needed to drive the seven LEDs of each dice. Thus, a pair of dice can easily be driven from an 8-bit output port.
Project Hardware The circuit diagram of the project is shown in Figure 6.16. PORTC of a PIC18F452 microcontroller is used to drive the LEDs as follows: RC0 drives D2,D6 of the first dice RC1 drives D1,D3 of the first dice RC2 drives D5,D7 of the first dice RC3 drives D4 of the first dice RC4 drives D2,D6 of the second dice
Figure 6.16: Circuit diagram of the project
www.newnespress.com
Simple PIC18 Projects
307
RC5 drives D1,D3 of the second dice RC6 drives D5,D7 of the second dice RC7 drives D4 of the second dice Since two LEDs are being driven on some outputs, we can calculate the required value of the current limiting resistors. Assuming that the voltage drop across each LED is 2V, the current through the LED is 10mA, and the output high voltage of the microcontroller is 4.85V, the required resistors are: R ¼
4:85
2 10
2
¼ 85 ohms
We will choose 100-ohm resistors. We now need to find the relationship between the dice numbers and the bit pattern to be sent to the LEDs for each dice. Table 6.4 shows the relationship between the first dice numbers and the bit pattern to be sent to port pins RC0-RC3. Similarly, Table 6.5 shows the relationship between the second dice numbers and the bit pattern to be sent to port pins RC4-RC7. Table 6.4: First dice bit patterns Dice number
RC3 RC2 RC1 RC0
Hex value
1
1
0
0
0
8
2
0
0
0
1
1
3
1
0
0
1
9
4
0
1
1
0
6
5
1
1
1
0
E
6
0
1
1
1
7
We can now find the 8-bit number to be sent to PORTC to display both dice numbers as follows: Get the first number from the number generator, call this P Index the DICE table to find the bit pattern for low nibble (i.e., L ¼ DICE[P]) Get the second number from the number generator, call this P
www.newnespress.com
308
Chapter 6 Table 6.5: Second dice bit patterns Dice number
RC7 RC6 RC5 RC4
Hex value
1
1
0
0
0
8
2
0
0
0
1
1
3
1
0
0
1
9
4
0
1
1
0
6
5
1
1
1
0
E
6
0
1
1
1
7
Index the DICE table to find the bit pattern for high nibble (i.e., U ¼ DICE[P]) Multiply high nibble by 16 and add low nibble to find the number to be sent to PORTC (i.e., R ¼ 16*U þ L), where R is the 8-bit number to be sent to PORTC to display both dice values.
Project PDL The operation of this project is very similar to that of Project 2. Figure 6.17 shows the PDL of the project. At the beginning of the program the PORTC pins are START Create DICE table Configure PORTC as outputs Configure RB0 as input DO FOREVER IF button pressed THEN Get a random number between 1 and 6 Find low nibble bit pattern Get second random number between 1 and 6 High high nibble bit pattern Calculate data to be sent to PORTC Wait 3 seconds Turn OFF all LEDs ENDIF ENDDO END
Figure 6.17: PDL of the project
www.newnespress.com
Simple PIC18 Projects
309
configured as outputs, and bit 0 of PORTB (RB0) is configured as input. The program then executes in a loop continuously and checks the state of the push-button switch. When the switch is pressed, two pseudorandom numbers between 1 and 6 are generated, and the bit pattern to be sent to PORTC is found by the method just described. This bit pattern is then sent to PORTC to display both dice numbers at the same time. The display shows the dice numbers for 3 seconds, and then all the LEDs turn OFF to indicate that the system is waiting for the push-button to be pressed again to display the next set of numbers.
Project Program The program is called LED5.C, and the program listing is given in Figure 6.18. At the beginning of the program Switch is defined as bit 0 of PORTB, and Pressed is defined as 0. The relationships between the dice numbers and the LEDs to be turned on are stored in an array called DICE as in Project 2. Variable Pattern is the data sent to the LEDs. The program enters an endless for loop where the state of the push-button switch is checked continuously. When the switch is pressed, two random numbers are generated by calling function Number. Variables L and U store the lower and higher nibbles of the bit pattern to be sent to PORTC. The bit pattern to be sent to PORTC is then determined using the method described in the Project Hardware section and stored in variable R. This bit pattern is then sent to PORTC to display both dice numbers at the same time. The dice numbers are displayed for 3 seconds, after which the LEDs are turned OFF to indicate that the system is ready.
Modifying the Program The program given in Figure 6.18 can made more efficient by combining the two dice nibbles into a single table value as described here. There are thirty-six possible combinations of the two dice values. Referring to Table 6.4, Table 6.5, and Figure 6.16, we can create Table 6.6 to show all the possible two-dice values and the corresponding numbers to be sent to PORTC. The modified program (program name LED6.C) is given in Figure 6.19. In this program array DICE contains the thirty-six possible dice values. The program enters an endless
www.newnespress.com
310
Chapter 6 /∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ TWO DICE - USING FEWER I/O PINS ============================== In this project LEDs are connected to PORTC of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz resonator. The LEDs are organized as the faces of a real dice. When a push-button switch connected to RB0 is pressed a dice pattern is displayed on the LEDs. The display remains in this state for 3 seconds and after this period the LEDs all turn OFF to indicate that the system is ready for the button to be pressed again. In this program a pseudorandom number generator function is used to generate the dice numbers between 1 and 6. Author: Dogan Ibrahim Date: July 2007 File: LED5.C ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/ #define Switch PORTB.F0 #define Pressed 0 // // This function generates a pseudo random integer number // between 1 and Lim // unsigned char Number(int Lim, int Y) { unsigned char Result; static unsigned int Y; Y = (Y * 32719 + 3) % 32749; Result = ((Y % Lim) + 1); return Result; }
// // Start of MAIN program // void main() { unsigned char J,L,U,R,Seed = 1; unsigned char DICE[] = {0,0x08,0x01,0x09,0x06,0x0E,0x07}; TRISC = 0; TRISB = 1; PORTC = 0;
// PORTC are outputs // RB0 input // Turn OFF all LEDs
for(;;)
// Endless loop
Figure 6.18: Program listing
www.newnespress.com
{ if(Switch == Pressed) { J = Number(6,seed); L = DICE[J]; J = Number(6,seed); U = DICE[J]; R = 16*U + L; PORTC = R; Delay_ms(3000); PORTC = 0; }
// Is switch pressed ? // Generate first dice number // Get LED pattern // Generate second dice number // Get LED pattern // Bit pattern to send to PORTC // Turn on LEDs for both dice // Delay 3 seconds // Turn OFF all LEDs
} }
Figure 6.18: (Cont’d) Table 6.6: Two-dice combinations and the number to be sent to PORTC Dice numbers
PORTC value
Dice numbers
PORTC value
1,1
088
4,1
086
1,2
018
4,2
016
1,3
098
4,3
096
1,4
068
4,4
066
1,5
0E8
4,5
0E6
1,6
078
4,6
076
2,1
081
5,1
08E
2,2
011
5,2
01E
2,3
091
5,3
09E
2,4
061
5,4
06E
2,5
0E1
5,5
0EE
2,6
071
5,6
07E
3,1
089
6,1
087
3,2
019
6,2
017
3,3
099
6,3
097
3,4
069
6,4
067
3,5
0E9
6,5
0E7
3,6
079
6,6
077
www.newnespress.com
312
Chapter 6 /***************************************************************************************** TWO DICE - USING FEWER I/O PINS ============================= In this project LEDs are connected to PORTC of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz resonator. The LEDs are organized as the faces of a real dice. When a push-button switch connected to RB0 is pressed a dice pattern is displayed on the LEDs. The display remains in this state for 3 seconds and after this period the LEDs all turn OFF to indicate that the system is ready for the button to be pressed again. In this program a pseudorandom number generator function is used to generate the dice numbers between 1 and 6. Author: Dogan Ibrahim Date: July 2007 File: LED6.C ******************************************************************************************/ #define Switch PORTB.F0 #define Pressed 0 // // Start of MAIN program // void main() { unsigned char Pattern, J = 1; unsigned char DICE[] = {0,0x88,0x18,0x98,0x68,0xE8,0x78, 0x81,0x11,0x91,0x61,0xE1,0x71, 0x89,0x19,0x99,0x69,0xE9,0x79, 0x86,0x16,0x96,0x66,0xE6,0x76, 0x8E,0x1E,0x9E,0x6E,0xEE,0x7E, 0x87,0x17,0x97,0x67,0xE7,0x77}; TRISC = 0; TRISB = 1; PORTC = 0;
// PORTC are outputs // RB0 input // Turn OFF all LEDs
for(;;) { if(Switch == Pressed) { Pattern = DICE[J]; PORTC = Pattern; Delay_ms(3000); PORTC = 0; } J++; if(J == 37) J = 1; }
// Endless loop // Is switch pressed ? // Number to send to PORTC // send to PORTC // 3 seconds delay // Clear PORTC // Increment J // If J = 37, reset to 1
}
Figure 6.19: Modified program
www.newnespress.com
Simple PIC18 Projects
313
for loop, and inside this loop the state of the push-button switch is checked. Also, a variable is incremented from 1 to 36. When the button is pressed, the value of this variable is used as an index to array DICE to determine the bit pattern to be sent to PORTC. As before, the program displays the dice numbers for 3 seconds and then turns OFF all LEDs to indicate that it is ready.
PROJECT 6.5—7-Segment LED Counter Project Description This project describes the design of a 7-segment LED-based counter which counts from 0 to 9 continuously with a one-second delay between counts. The project shows how a 7-segment LED can be interfaced and used in a PIC microcontroller project. 7-segment displays are used frequently in electronic circuits to show numeric or alphanumeric values. As shown in Figure 6.20, a 7-segment display consists basically of 7 LEDs connected such that the numbers from 0 to 9 and some letters can be displayed. Segments are identified by the letters from a to g, and Figure 6.21 shows the segment names of a typical 7-segment display.
Figure 6.20: Some 7-segment displays
www.newnespress.com
314
Chapter 6 a b
f g
c
e d
Figure 6.21: Segment names of a 7-segment display Figure 6.22 shows how the numbers from 0 to 9 are obtained by turning ON different segments of the display.
Figure 6.22: Displaying numbers 0 to 9 7-segment displays are available in two different configurations: common cathode and common anode. As shown in Figure 6.23, in common cathode configuration, all the cathodes of all segment LEDs are connected together to the ground. The segments are turned ON by applying a logic 1 to the required segment LED via current limiting resistors. In common cathode configuration the 7-segment LED is connected to the microcontroller in current sourcing mode. In common anode configuration, the anode terminals of all the LEDs are connected together as shown in Figure 6.24. This common point is then normally connected to the
www.newnespress.com
Simple PIC18 Projects
315
Figure 6.23: Common cathode configuration
Figure 6.24: Common anode configuration
supply voltage. A segment is turned ON by connecting its cathode terminal to logic 0 via a current limiting resistor. In common anode configuration the 7-segment LED is connected to the microcontroller in current sinking mode. In this project, a Kingbright SA52-11 red common anode 7-segment display is used. This is a 13mm (0.52 inch) display with ten pins that includes a segment LED for the decimal point. Table 6.7 shows the pin configuration of this display.
Project Hardware The circuit diagram of the project is shown in Figure 6.25. A PIC18F452 type microcontroller is used with a 4MHz resonator. Segments a to g of the display are connected to PORTC of the microcontroller through 290-ohm current limiting resistors. Before driving the display, we have to know the relationship between the numbers to be displayed and the corresponding segments to be turned ON, and this is shown in Table 6.8. For example, to display number 3 we have to send the hexadecimal number 04F to PORTC, which turns ON segments a,b,c,d, and g. Similarly, to display number 9 we have to send the hexadecimal number 06F to PORTC which turns ON segments a,b,c,d,f, and g.
www.newnespress.com
316
Chapter 6 Table 6.7: SA52-11 pin configuration Pin number
Segment
1
e
2
d
3
common anode
4
c
5
decimal point
6
b
7
a
8
common anode
9
f
10
g
Figure 6.25: Circuit diagram of the project
www.newnespress.com
Simple PIC18 Projects
317
Table 6.8: Displayed number and data sent to PORTC Number
x g f e dcba
PORTC Data
0
00111111
03F
1
00000110
006
2
01011011
05B
3
01001111
04F
4
01100110
066
5
01101101
06D
6
01111101
07D
7
00000111
007
8
01111111
07F
9
01101111
06F
x is not used, taken as 0.
Project PDL The operation of the project is shown in Figure 6.26 with a PDL. At the beginning of the program an array called SEGMENT is declared and filled with the relationships between the numbers 0 and 9 and the data to be sent to PORTC. The PORTC pins are then configured as outputs, and a variable is initialized to 0. The program then enters an
START Create SEGMENT table Configure PORTC as outputs Initialize CNT to 0 DO FOREVER Get bit pattern from SEGMENT corresponding to CNT Send this bit pattern to PORTC Increment CNT between 0 and 9 Wait 1 second ENDDO END
Figure 6.26: PDL of the project
www.newnespress.com
318
Chapter 6
endless loop where the variable is incremented between 0 and 9 and the corresponding bit pattern to turn ON the appropriate segments is sent to PORTC continuously with a one-second delay between outputs.
Project Program The program is called SEVEN1.C and the listing is given in Figure 6.27. At the beginning of the program character variables Pattern and Cnt are declared, and Cnt is cleared to 0. Then Table 6.8 is implemented using array SEGMENT. After configuring the PORTC pins as outputs, the program enters an endless loop using a for statement. Inside the loop the bit pattern corresponding to the contents of Cnt is
/************************************************************************************* 7-SEGMENT DISPLAY ================= In this project a common anode 7-segment LED display is connected to PORTC of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz resonator. The program displays numbers 0 to 9 on the display with a one second delay between each output. Author: Dogan Ibrahim Date: July 2007 File: SEVEN1.C **************************************************************************************/ void main() { unsigned char Pattern, Cnt = 0; unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D, 0x7D,0x07,0x7F,0x6F}; TRISC = 0;
// PORTC are outputs
for(;;) { Pattern = SEGMENT[Cnt]; Pattern = ~Pattern; PORTC = Pattern; Cnt++; if(Cnt == 10) Cnt = 0; Delay_ms(1000); }
// Endless loop // Number to send to PORTC // Invert bit pattern // Send to PORTC // Cnt is between 0 and 9 // 1 second delay
}
Figure 6.27: Program listing
www.newnespress.com
Simple PIC18 Projects
319
found and stored in variable Pattern. Because we are using a common anode display, a segment is turned ON when it is at logic 0 and thus the bit pattern is inverted before it is sent to PORTC. The value of Cnt is then incremented between 0 and 9, after which the program waits for a second before repeating the above sequence.
Modified Program Note that the program can be made more readable if we create a function to display the required number and then call this function from the main program. Figure 6.28 shows the modified program (called SEVEN2.C). A function called Display is created with an argument called no. The function gets the bit pattern from local array SEGMENT indexed by no, inverts it, and then returns the resulting bit pattern to the calling program.
PROJECT 6.6—Two-Digit Multiplexed 7-Segment LED Project Description This project is similar to Project 6.5, but here multiplexed two digits are used instead of just one digit and a fixed number. In this project the number 25 is displayed. In multiplexed LED applications (see Figure 6.29) the LED segments of all the digits are tied together and the common pins of each digit are turned ON separately by the microcontroller. When each digit is displayed only for several milliseconds, the eye cannot tell that the digits are not ON all the time. This way we can multiplex any number of 7-segment displays together. For example, to display the number 53, we have to send 5 to the first digit and enable its common pin. After a few milliseconds, number 3 is sent to the second digit and the common point of the second digit is enabled. When this process is repeated continuously, it appears to the user that both displays are ON continuously. Some manufacturers provide multiplexed multidigit displays, such as 2-, 4-, or 8-digit multiplexed displays, in single packages. The display used in this project is the DC5611EWA, which is a red 0.56-inch common-cathode two-digit display having 18 pins and the pin configuration as shown in Table 6.9. This display can be controlled from the microcontroller as follows: Send the segment bit pattern for digit 1 to segments a to g Enable digit 1
www.newnespress.com
320
Chapter 6 /*************************************************************************************** 7-SEGMENT DISPLAY ================== In this project a common anode 7-segment LED display is connected to PORTC of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz resonator. The program displays numbers 0 to 9 on the display with a one second delay between each output. In this version of the program a function called "Display" is used to display the number. Author: Dogan Ibrahim Date: July 2007 File: SEVEN2.C *****************************************************************************************/ // // This function displays a number on the 7-segment LED. // The number is passed in the argument list of the function. // unsigned char Display(unsigned char no) { unsigned char Pattern; unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D, 0x7D,0x07,0x7F,0x6F}; Pattern = SEGMENT[no]; Pattern = ~ Pattern; return (Pattern);
// Pattern to return
}
// // Start of MAIN Program // void main() { unsigned char Cnt = 0; TRISC = 0;
// PORTC are outputs
for(;;) { PORTC = Display(Cnt); Cnt++; if(Cnt == 10) Cnt = 0; Delay_ms(1000); }
// Endless loop // Send to PORTC // Cnt is between 0 and 9 // 1 second delay
}
Figure 6.28: Modified program listing
www.newnespress.com
Simple PIC18 Projects
321
Figure 6.29: Two multiplexed 7-segment displays
Table 6.9: Pin configuration of DC56-11EWA dual display Pin no.
Segment
1,5
E
2,6
D
3,8
C
14
Digit 1 enable
17,7
G
15,10
B
16,11
A
18,12
F
13
Digit 2 enable
4
Decimal point 1
9
Decimal point 2
www.newnespress.com
322
Chapter 6
Wait for a few milliseconds Disable digit 1 Send the segment bit pattern for digit 2 to segments a to g Enable digit 2 Wait for a few milliseconds Disable digit 2 Repeat these steps continuously The segment configuration of the DC56-11EWA display is shown in Figure 6.30. In a multiplexed display application the segment pins of corresponding segments are connected together. For example, pins 11 and 16 are connected as the common a segment, pins 15 and 10 are connected as the common b segment, and so on.
Figure 6.30: DC56-11EWA display segment configuration
Project Hardware The block diagram of this project is shown in Figure 6.31. The circuit diagram is given in Figure 6.32. The segments of the display are connected to PORTC of a PIC18F452type microcontroller, operated with a 4MHz resonator. Current limiting resistors are used on each segment of the display. Each digit is enabled using a BC108-type transistor switch connected to port pins RB0 and RB1 of the microcontroller. A segment is turned on when a logic 1 is applied to the base of the corresponding segment transistor.
www.newnespress.com
Simple PIC18 Projects
323
2-digit display PIC18F452
PORTC
Enable 1 PORTB Enable 2
Figure 6.31: Block diagram of the project
Figure 6.32: Circuit diagram of the project
www.newnespress.com
324
Chapter 6
Project PDL At the beginning of the program PORTB and PORTC pins are configured as outputs. The program then enters an endless loop where first of all the Most Significant Digit (MSD) of the number is calculated, function Display is called to find the bit pattern and then sent to the display, and digit 1 is enabled. Then, after a small delay, digit 1 is disabled, the Least Significant Digit (LSD) of the number is calculated, function Display is called to find the bit pattern and then sent to the display, and digit 2 is enabled. Then again after a small delay, digit 2 is disabled, and this process repeats indefinitely. Figure 6.33 shows the PDL of the project. START Create SEGMENT table Configure PORTB as outputs Configure PORTC as outputs Initialize CNT to 25 DO FOREVER Find MSD digit Get bit pattern from SEGMENT Enable digit 1 Wait for a while Disable digit 1 Find LSD digit Get bit pattern from SEGMENT Enable digit 2 Wait for a while Disable digit 2 ENDDO END
Figure 6.33: PDL of the project
Project Program The program is named SEVEN3.C, and the listing is shown in Figure 6.34. DIGIT1 and DIGIT2 are defined as equal to bit 0 and bit 1 of PORTB respectively. The value to be displayed (the number 25) is stored in variable Cnt. An endless loop is formed using a for statement. Inside the loop, the MSD of the number is calculated by dividing the number by 10. Function Display is then called to find the bit pattern to send to PORTC. Then digit 1 is enabled by setting DIGIT1 ¼ 1 and the program waits for 10ms. After this, digit 1 is disabled and the LSD of the number is calculated using the mod operator (“%”) and sent to PORTC. At the same time, digit 2 is enabled by setting DIGIT2 ¼ 1 and the program waits for 10ms. After this time digit 2 is disabled, and the program repeats forever.
www.newnespress.com
Simple PIC18 Projects
325
/∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ Dual 7-SEGMENT DISPLAY ====================== In this project two common cathode 7-segment LED displays are connected to PORTC of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz resonator. Digit 1 (left digit) enable pin is connected to port pin RB0 and digit 2 (right digit) enable pin is connected to port pin RB1 of the microcontroller. The program displays number 25 on the displays. Author: Dogan Ibrahim Date: July 2007 File: SEVEN3.C ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/ #define DIGIT1 PORTB.F0 #define DIGIT2 PORTB.F1 // // This function finds the bit pattern to be sent to the port to display a number // on the 7-segment LED. The number is passed in the argument list of the function. // unsigned char Display(unsigned char no) { unsigned char Pattern; unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D, 0x7D,0x07,0x7F,0x6F}; Pattern = SEGMENT[no]; return (Pattern);
// Pattern to return
}
// // Start of MAIN Program // void main() { unsigned char Msd, Lsd, Cnt = 25; TRISC = 0; TRISB = 0;
// PORTC are outputs // RB0, RB1 are outputs
DIGIT1 = 0; DIGIT2 = 0;
// Disable digit 1 // Disable digit 2
for(;;) {
// Endless loop
Figure 6.34: Program listing
(Continued)
www.newnespress.com
326
Chapter 6 Msd = Cnt / 10; PORTC = Display(Msd); DIGIT1 = 1; Delay_Ms(10);
// MSD digit // Send to PORTC // Enable digit 1 // Wait a while
DIGIT1 = 0; Lsd = Cnt % 10; PORTC = Display(Lsd); DIGIT2 = 1; Delay_Ms(10); DIGIT2 = 0;
// Disable digit 1 // LSD digit // Send to PORTC // Enable digit 2 // Wait a while // Disable digit 2
} }
Figure 6.34: (Cont’d)
PROJECT 6.7—Two-Digit Multiplexed 7-Segment LED Counter with Timer Interrupt Project Description This project is similar to Project 6 but here the microcontroller’s timer interrupt is used to refresh the displays. In Project 6 the microcontroller was busy updating the displays every 10ms and could not perform any other tasks. For example, the program given in Project 6 cannot be used to make a counter with a one-second delay between counts, as the displays cannot be updated while the program waits for one second. In this project a counter is designed to count from 0 to 99, and the display is refreshed every 5ms inside the timer interrupt service routine. The main program can then perform other tasks, in this example incrementing the count and waiting for one second between counts. In this project Timer 0 is operated in 8-bit mode. The time for an interrupt is given by: Time ¼ ð4 clock periodÞ Prescaler ð256
TMR0LÞ
where Prescaler is the selected prescaler value, and TMR0L is the value loaded into timer register TMR0L to generate timer interrupts every Time period. In our application the clock frequency is 4MHz, that is, clock period ¼ 0.25ms, and Time ¼ 5ms. Selecting a prescaler value of 32, the number to be loaded into TMR0L can be calculated as follows:
www.newnespress.com
Simple PIC18 Projects
327
Time 4 clockperiod prescaler
TMR0L ¼ 256 or
5000 ¼ 100 4 0:25 32
TMR0L ¼ 256
Thus, TMR0L should be loaded with 100. The value to be loaded into TMR0 control register T0CON can then be found as: T0CON 1 1
0
0
0
1
Enable TMR0 8-bit mode Internal clock Low-high
0
0
1:32 prescaler
transition Use prescaler
Thus, T0CON register should be loaded with hexadecimal 0C4. The next register to be configured is the interrupt control register INTCON, where we will disable priority based interrupts and enable the global interrupts and TMR0 interrupts: INTCON 1
Enable global interrupts
X
1
Enable TMR0 int.
0
0
0
X
X
Disable INT0 Disable int. RB change int. Clear TMR0IF
www.newnespress.com
328
Chapter 6
Taking the don’t-care entries (X) as 0, the hexadecimal value to be loaded into register INTCON is 0A0. When an interrupt occurs, the program automatically jumps to the interrupt service routine. Inside this routine we have to reload register TMR0L, reenable the TMR0 interrupts, and clear the TMR0 interrupt flag bit. Setting INTCON register to 020 reenables the TMR0 interrupts and at the same time clears the TMR0 interrupt flag. The operations to be performed can thus be summarized as follows: In the main program:
Load TMR0L with 100
Set T0CON to 0C4
Set INTCON to 0A0
Increment the counter with 1-second delays
In the interrupt service routine:
Re-load TMR0L to 100
Refresh displays
Set INTCON to 020 (reenable TMR0 interrupts and clear timer interrupt flag)
Project Hardware The circuit diagram of this project is same as in Figure 6.32 where a dual 7-segment display is connected to PORTB and PORTC of a PIC18F452 microcontroller.
Project PDL The PDL of the project is shown in Figure 6.35. The program is in two sections: the main program and the interrupt service routine. Inside the main program, TMR0 is configured to generate interrupts every 5ms and the counter is incremented with a onesecond delay. Inside the interrupt service routine, the timer interrupt is reenabled and the display digits are refreshed alternately every 5ms.
www.newnespress.com
Simple PIC18 Projects
329
MAIN PROGRAM: START Configure PORTB as outputs Configure PORTC as outputs Clear variable Cnt to 0 Configure TMR0 to generate interrupts every 5ms DO FOREVER Increment Cnt between 0 and 99 Delay 1 second ENDO END
INTERRUPT SERVICE ROUTINE: START Re-configure TMR0 IF Digit 1 updated THEN Update digit 2 ELSE Update digit 1 END END
Figure 6.35: PDL of the project
Project Program The program is called SEVEN4.C, and the program listing is given in Figure 6.36. At the beginning of the main program PORTB and PORTC are configured as outputs. Then register T0CON is loaded with 0C4 to enable the TMR0 and set the prescaler to 32. TMR0L register is loaded with 100 so that an interrupt is generated after 5ms. The program then enters an endless loop where the value of Cnt is incremented every second. Inside the interrupt service routine, register TMR0L is reloaded, TMR0 interrupts are reenabled, and the timer interrupt flag is cleared so that further timer interrupts can be generated. The display digits are then updated alternately. A variable called Flag is used to determine which digit to update. Function Display is called, as in Project 6, to find the bit pattern to be sent to PORTC.
Modifying the Program In Figure 6.36 the display counts as 00 01. . .09 10 11. . .99 00 01. . . (i.e., the first digit is shown as 0 for numbers less than 10). The program could be modified so the first
www.newnespress.com
330
Chapter 6 /∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ Dual 7-SEGMENT DISPLAY COUNTER ============================== In this project two common cathode 7-segment LED displays are connected to PORTC of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz resonator. Digit 1 (left digit) enable pin is connected to port pin RB0 and digit 2 (right digit) enable pin is connected to port pin RB1 of the microcontroller. The program counts up from 0 to 99 with one second delay between each count. The display is updated in a timer interrupt service routine at every 5ms. Author: Dogan Ibrahim Date: July 2007 File: SEVEN4.C ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/ #define DIGIT1 PORTB.F0 #define DIGIT2 PORTB.F1 unsigned char Cnt = 0; unsigned char Flag = 0; // // This function finds the bit pattern to be sent to the port to display a number // on the 7-segment LED. The number is passed in the argument list of the function. // unsigned char Display(unsigned char no) { unsigned char Pattern; unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D, 0x7D,0x07,0x7F,0x6F}; Pattern = SEGMENT[no]; return (Pattern);
// Pattern to return
}
// // TMR0 timer interrupt service routine. The program jumps to the ISR at // every 5ms. // void interrupt () { unsigned char Msd, Lsd; TMR0L = 100; // Re-load TMR0 INTCON = 0x20; // Set T0IE and clear T0IF Flag = ~ Flag; // Toggle Flag if(Flag == 0) // Do digit 1 {
Figure 6.36: Program of the project
www.newnespress.com
Simple PIC18 Projects DIGIT2 = 0; Msd = Cnt / 10; PORTC = Display(Msd); DIGIT1 = 1;
// MSD digit // Send to PORTC // Enable digit 1
} else { DIGIT1 = 0; Lsd = Cnt % 10; PORTC = Display(Lsd); DIGIT2 = 1;
// Do digit 2 // Disable digit 1 // LSD digit // Send to PORTC // Enable digit 2
331
} }
// // Start of MAIN Program. configure PORTB and PORTC as outputs. // In addition, configure TMR0 to interrupt at every 10ms // void main() { TRISC = 0; // PORTC are outputs TRISB = 0; // RB0, RB1 are outputs DIGIT1 = 0; DIGIT2 = 0;
// Disable digit 1 // Disable digit 2
// // Configure TMR0 timer interrupt // T0CON = 0xC4; TMR0L = 100; INTCON = 0xA0; Delay_ms(1000); for(;;) { Cnt++; if(Cnt == 100) Cnt = 0; Delay_ms(1000); }
// Prescaler = 32 // Load TMR0L with 100 // Enable TMR0 interrupt
// Endless loop // Increment Cnt // Count between 0 and 99 // Wait 1 second
}
Figure 6.36: (Cont’d)
digit is blanked if the number to be displayed is less than 10. The modified program (called SEVEN5.C) is shown in Figure 6.37. Here, the first digit (MSD) is not enabled if the number to be displayed is 0.
www.newnespress.com
332
Chapter 6 /∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ Dual 7-SEGMENT DISPLAY COUNTER ============================== In this project two common cathode 7-segment LED displays are connected to PORTC of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz resonator. Digit 1 (left digit) enable pin is connected to port pin RB0 and digit 2 (right digit) enable pin is connected to port pin RB1 of the microcontroller. The program counts up from 0 to 99 with one second delay between each count. The display is updated in a timer interrupt service routine at every 5ms. In this version of the program the first digit is blanked if the number is 0. Author: Dogan Ibrahim Date: July 2007 File: SEVEN5.C ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/ #define DIGIT1 PORTB.F0 #define DIGIT2 PORTB.F1 unsigned char Cnt = 0; unsigned char Flag = 0; // // This function finds the bit pattern to be sent to the port to display a number // on the 7-segment LED. The number is passed in the argument list of the function. // unsigned char Display(unsigned char no) { unsigned char Pattern; unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D, 0x7D,0x07,0x7F,0x6F}; Pattern = SEGMENT[no]; return (Pattern);
// Pattern to return
}
// // TMR0 timer interrupt service routine. The program jumps to the // ISR at every 5ms. // void interrupt () { unsigned char Msd, Lsd;
Figure 6.37: Modified program
www.newnespress.com
Simple PIC18 Projects TMR0L = 100; INTCON = 0x20; Flag = ~ Flag; if(Flag == 0) { DIGIT2 = 0; Msd = Cnt / 10; if(Msd != 0) { PORTC = Display(Msd); DIGIT1 = 1; } } else { DIGIT1 = 0; Lsd = Cnt % 10; PORTC = Display(Lsd); DIGIT2 = 1;
333
// Re-load TMR0 // Set T0IE and clear T0IF // Toggle Flag // Do digit 1
// MSD digit
// Send to PORTC // Enable digit 1
// Do digit 2 // Disable digit 1 // LSD digit // Send to PORTC // Enable digit 2
} }
// // Start of MAIN Program. configure PORTB and PORTC as outputs. // In addition, configure TMR0 to interrupt at every 10ms // void main() { TRISC = 0; // PORTC are outputs TRISB = 0; // RB0, RB1 are outputs DIGIT1 = 0; DIGIT2 = 0;
// Disable digit 1 // Disable digit 2
// // Configure TMR0 timer interrupt // T0CON = 0xC4; TMR0L = 100; INTCON = 0xA0; Delay_ms(1000);
// Prescaler = 32 // Load TMR0 with 100 // Enable TMR0 interrupt
for(;;) { Cnt++; if(Cnt == 100) Cnt = 0; Delay_ms(1000); }
// Endless loop // Increment Cnt // Count between 0 and 99 // Wait 1 second
}
Figure 6.37: (Cont’d)
www.newnespress.com
334
Chapter 6
PROJECT 6.8—Voltmeter with LCD Display Project Description In this project a voltmeter with LCD display is designed. The voltmeter can be used to measure voltages 0–5V. The voltage to be measured is applied to one of the analog inputs of a PIC18F452-type microcontroller. The microcontroller reads the analog voltage, converts it into digital, and then displays it on an LCD. In microcontroller systems the output of a measured variable is usually displayed using LEDs, 7-segment displays, or LCD displays. LCDs make it possible to display alphanumeric or graphical data. Some LCDs have forty or more character lengths with the capability to display several lines. Other LCD displays can be used to display graphics images. Some modules offer color displays, while others incorporate backlighting so they can be viewed in dimly lit conditions. There are basically two types of LCDs as far as the interface technique is concerned: parallel and serial. Parallel LCDs (e.g., Hitachi HD44780) are connected to a microcontroller by more than one data line and the data is transferred in parallel form. Both four and eight data lines are commonly used. A four-wire connection saves I/O pins but is slower since the data is transferred in two stages. Serial LCDs are connected to the microcontroller by only one data line, and data is usually sent to the LCD using the standard RS-232 asynchronous data communication protocol. Serial LCDs are much easier to use, but they cost more than the parallel ones. The programming of a parallel LCD is a complex task and requires a good understanding of the internal operation of the LCD controllers, including the timing diagrams. Fortunately, the mikroC language provides special library commands for displaying data on alphanumeric as well as graphic LCDs. All the user has to do is connect the LCD to the microcontroller, define the LCD connection in the software, and then send special commands to display data on the LCD.
HD44780 LCD Module The HD44780 is one of the most popular alphanumeric LCD modules and is used both in industry and by hobbyists. This module is monochrome and comes in different sizes.
www.newnespress.com
Simple PIC18 Projects
335
Modules with 8, 16, 20, 24, 32, and 40 columns are available. Depending on the model chosen, the number of rows may be 1, 2, or 4. The display provides a 14-pin (or 16-pin) connector to a microcontroller. Table 6.10 gives the pin configuration and pin functions of a 14-pin LCD module. The following is a summary of the pin functions: VSS is the 0V supply or ground. The VDD pin should be connected to the positive supply. Although the manufacturers specify a 5V DC supply, the modules will usually work with as low as 3V or as high as 6V. Pin 3, named VEE, is the contrast control pin. This pin is used to adjust the contrast of the display and should be connected to a variable voltage supply. A potentiometer is normally connected between the power supply lines with its wiper arm connected to this pin so that the contrast can be adjusted.
Table 6.10: Pin configuration of HD44780 LCD module Pin no.
Name
Function
1
VSS
Ground
2
VDD
þ ve supply
3
VEE
Contrast
4
RS
Register select
5
R/W
Read/write
6
E
Enable
7
D0
Data bit 0
8
D1
Data bit 1
9
D2
Data bit 2
10
D3
Data bit 3
11
D4
Data bit 4
12
D5
Data bit 5
13
D6
Data bit 6
14
D7
Data bit 7
www.newnespress.com
336
Chapter 6
Pin 4 is the register select (RS), and when this pin is LOW, data transferred to the display is treated as commands. When RS is HIGH, character data can be transferred to and from the module. Pin 5 is the read/write (R/W) line. This pin is pulled LOW in order to write commands or character data to the LCD module. When this pin is HIGH, character data or status information can be read from the module. Pin 6 is the enable (E) pin, which is used to initiate the transfer of commands or data between the module and the microcontroller. When writing to the display, data is transferred only on the HIGH-to-LOW transition of this line. When reading from the display, data becomes available after the LOW-to-HIGH transition of the enable pin, and this data remains valid as long as the enable pin is at logic HIGH. Pins 7 to 14 are the eight data bus lines (D0 to D7). Data can be transferred between the microcontroller and the LCD module using either a single 8-bit byte or as two 4-bit nibbles. In the latter case, only the upper four data lines (D4 to D7) are used. The 4-bit mode means that four fewer I/O lines are used to communicate with the LCD. In this book we are using only an alphanumeric-based LCD and only the 4-bit interface.
Connecting the LCD The mikroC compiler assumes by default that the LCD is connected to the microcontroller as follows: LCD
Microcontroller port
D7 D6 D5 D4 E RS
Bit Bit Bit Bit Bit Bit
7 6 5 4 3 2
of of of of of of
the the the the the the
port port port port port port
where port is the port name specified using the Lcd_Init statement. For example, we can use the statement Lcd_Init(&PORTB) if the LCD is connected to PORTB with the default connection.
www.newnespress.com
Simple PIC18 Projects
337
It is also possible to connect the LCD differently, using the command Lcd_Config to define the connection.
Project Hardware Figure 6.38 shows the block diagram of the project. The microcontroller reads the analog voltage, converts it to digital, formats it, and then displays it on the LCD.
PIC18F452
LCD
Input voltage
Figure 6.38: Block diagram of the project The circuit diagram of the project is shown in Figure 6.39. The voltage to be measured (between 0 and 5V) is applied to port AN0 where this port is configured as an analog
Figure 6.39: Circuit diagram of the project
www.newnespress.com
338
Chapter 6
input in software. The LCD is connected to PORTC of the microcontroller as in the default four-wire connection. A potentiometer is used to adjust the contrast of the LCD display.
Project PDL The PDL of the project is shown in Figure 6.40. At the beginning of the program PORTC is configured as output and PORTA is configured as input. Then the LCD and the A/D converter are configured. The program then enters an endless loop where analog input voltage is converted to digital and displayed on the LCD. The process is repeated every second.
START Configure PORTC as outputs Configure PORTA as input Configure the LCD Configure the A/D converter DO FOREVER Read analog data (voltage) from channel 0 Format the data Display the data (voltage) Wait one second ENDO END
Figure 6.40: PDL of the project
Project Program The program is called SEVEN6.C, and the program listing is given in Figure 6.41. At the beginning of the program PORTC is defined as output and PORTA as input. Then the LCD is configured and the text “VOLTMETER” is displayed on the LCD for two seconds. The A/D is then configured by setting register ADCON1 to 080 so the A/D result is right-justified, Vref voltage is set to VDD (þ5V), and all PORTA pins are configured as analog inputs. The main program loop starts with a for statement. Inside this loop the LCD is cleared, and analog data is read from channel 0 (pin AN0) using the statement Adc_Read(0). The converted digital data is stored in variable Vin which is declared as an unsigned long. The A/D converter is 10-bits wide and thus there are 1024 steps
www.newnespress.com
Simple PIC18 Projects
339
/∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ VOLTMETER WITH LCD DISPLAY ============================ In this project an LCD is connected to PORTC. Also, input port AN0 is used as analog input. Voltage to be measured is applied to AN0. The microcontroller reads the analog voltage, converts into digital, and then displays on the LCD. Analog input range is 0 to 5V. A PIC18F452 type microcontroller is used in this project, operated with a 4MHz resonator. Analog data is read using the Adc_Read built-in function. This function uses the internal RC clock for A/D timing. The LCD is connected to the microcontroller as follows: Microcontroller RC7 RC6 RC5 RC4 RC3 RC2
LCD D7 D6 D5 D4 Enable RS
Author: Dogan Ibrahim Date: July 2007 File: SEVEN6.C ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/ // // Start of MAIN Program. Configure LCD and A/D converter // void main() { unsigned long Vin, mV; unsigned char op[12]; unsigned char i,j,lcd[5]; TRISC = 0; TRISA = 0xFF;
// PORTC are outputs (LCD) // PORTA is input
// // Configure LCD // Lcd_Init(&PORTC); Lcd_Cmd(LCD_CLEAR); Lcd_Out(1,1,"VOLTMETER"); Delay_ms(2000); //
// LCD is connected to PORTC
Figure 6.41: Program listing
(Continued)
www.newnespress.com
340
Chapter 6 // Configure A/D converter. AN0 is used in this project // ADCON1 = 0x80; // Use AN0 and Vref=+5V // // Program loop // for(;;) // Endless loop { Lcd_Cmd(LCD_CLEAR); Vin = Adc_Read(0); // Read from channel 0 (AN0) Lcd_Out(1,1,"mV = "); // Display "mV = " mV = (Vin * 5000) >> 10; // mv = Vin x 5000 / 1024 LongToStr(mV,op); // Convert to string in "op" // // Remove leading blanks // j=0; for(i=0;i