C Programming For The PIC Microcontroller

2y ago
55 Views
10 Downloads
4.50 MB
282 Pages
Last View : 2d ago
Last Download : 3m ago
Upload by : Dahlia Ryals
Transcription

TECHNOLOGY IN AC TION C Programmingfor the PICMicrocontrollerDemystify Coding with EmbeddedProgramming—Hubert Henry Ward

C Programmingfor the PICMicrocontrollerDemystify Codingwith Embedded ProgrammingHubert Henry Ward

C Programming for the PIC Microcontroller: Demystify Coding withEmbedded ProgrammingHubert Henry WardLancashire, UKISBN-13 (pbk): 2-5525-4ISBN-13 (electronic): 978-1-4842-5525-4Copyright 2020 by Hubert Henry WardThis work is subject to copyright. All rights are reserved by the Publisher, whether the whole orpart of the material is concerned, specifically the rights of translation, reprinting, reuse ofillustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,and transmission or information storage and retrieval, electronic adaptation, computer software,or by similar or dissimilar methodology now known or hereafter developed.Trademarked names, logos, and images may appear in this book. Rather than use a trademarksymbol with every occurrence of a trademarked name, logo, or image we use the names, logos,and images only in an editorial fashion and to the benefit of the trademark owner, with nointention of infringement of the trademark.The use in this publication of trade names, trademarks, service marks, and similar terms, even ifthey are not identified as such, is not to be taken as an expression of opinion as to whether or notthey are subject to proprietary rights.While the advice and information in this book are believed to be true and accurate at the date ofpublication, neither the authors nor the editors nor the publisher can accept any legalresponsibility for any errors or omissions that may be made. The publisher makes no warranty,express or implied, with respect to the material contained herein.Managing Director, Apress Media LLC: Welmoed SpahrAcquisitions Editor: Natalie PaoDevelopment Editor: James MarkhamCoordinating Editor: Jessica VakiliDistributed to the book trade worldwide by Springer Science Business Media New York,233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is aCalifornia LLC and the sole member (owner) is Springer Science Business Media Finance Inc(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.For information on translations, please e-mail rights@apress.com, or visit http://www.apress.com/rights-permissions.Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBookversions and licenses are also available for most titles. For more information, reference our Printand eBook Bulk Sales web page at http://www.apress.com/bulk-sales.Any source code or other supplementary material referenced by the author in this book is availableto readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-5524-7.For more detailed information, please visit http://www.apress.com/source-code.Printed on acid-free paper

Dedicated to my wife Ann

Table of ContentsAbout the Author xiiiAbout the Technical Reviewer xvIntroduction xviiChapter 1: Introduction 1Programmable Industrial Controllers 1Programming Languages 3Machine Code 3Assembler Language 5C Programming Language 5Different Programming Languages 6The IDE 7Summary 8Chapter 2: Our First Program 9The PORTS of the PIC 9Good Programming Practice 10The Algorithm 10The Flowchart 11The Program Listing 11Using MPLABX IDE 12Creating the Project in MPLABX 13The First Program Turning On and Off an Output 23v

Table of ContentsThe Main Aspects of a ‘C’ Program 23The Comments and PIC Configuration 23The TRISA and TRISB 29A TRIS Example 31Exercise 2-1 31Setting the PORTS 31The ADC (Analogue to Digital Converter) 33Setting Up the Oscillator 36Exercise 2-2 39Waiting for an Input 39Waiting to Turn the LED Off 42Exercise 2-3 51Comments 52Testing the Program 52Compiling and Running the Program 56Testing the Program Practically 59Summary 62Exercise Answers 63Chapter 3: Updating the Program 65If This Then Do That 65Saving the Old Program 66Labels and the Goto Instruction 69Exercise 3-1 69While vs. If Then 69Slowing the Micro Down 70T0CON Register 71vi

Table of ContentsAdding a One-Second Delay 74Exercise 3-2 75Delaying the Turn Off 77Using Subroutines 79Defining and Calling a Subroutine 79The delay Subroutine 80Calling the Subroutine from Within the Main Program 80The Variable Delay and the For Do Loop 82Local and Global Variables and Data Types 85Type Char 85Type Unsigned char 86Type int 86Local Variables 87Global Variables 87Exercise 3-3 90Summary 91Exercise Answers 91Chapter 4: Applying What We’ve Learned 93Controlling a Single Set of Traffic Lights 93The Algorithm 94The Configuration Words 100The Analysis of the Program 101Downloading the Program to a Prototype Board 106Extending the Program to the Crossroads Traffic Lights 109The Algorithm 110Summary 118vii

Table of ContentsChapter 5: Real-World Inputs 119Using Analogue Inputs 119The ADCON0 Control Register 120The ADCON1 Register 122The ADCON2 Register 124Creating the Required Acquisition Time 128Example 1 129Example 2 129Changing the ADC Input Channels 130A Basic Setup for the ADC 131A Basic Program for the ADC 132The Algorithm 132Analysis of the Program 134Summary 135Chapter 6: Using the LCD 137The LCD Controller 137Instruction or Command Mode 137Data Mode 138Initializing the LCD 142The Subroutine lcdOut () 159The Subroutine sendInfo () 159The New Aspects to PIC Programming in This LCD Program 162Arrays 162Using Pointers 163Connecting the LCD in 8-Bit Mode 165viii

Table of ContentsThe Volt Meter Program 175The Algorithm 175Creating Special Characters on the LCD 190Summary 211Chapter 7: Creating a Header File 213Header Files 213Creating a Header File 213Including the Header File into Your Program 217The Global Header File 218Creating a Header File for Your Configuration Instructions 219Summary 221Chapter 8: Understanding Some Useful Code 223The Trace Table 224The Process 224Lines 1–6 231Line 7 unsigned char listpointer; 231Line 10 number1 ; 232Line 11 number1 number1 2; 232Line 12 number2 number1 - 2; 232Line 13 number2 0b1111111111110000; 232Line 17 z y; 233Line 18 z y ; 233Line 19 z (unsigned char) u; 233Line 21 y y; 233Line 23 z y 1; 234Line 24 z y 1; 234ix

Table of ContentsLine 26 y (a 0) ? a : -1; 235Line 27 y (a 0) ? a : -1; 235Line 28 y (a 0) ? z : -1; 235Line 29 listpointer list; 236Line 30 listpointer 2; 236Line 31 listpointer ; 236Line 32 listpointer 5; 236Line 33 listpointer list; 237Line 34 a a & 0xF0; 237Line 37 if ( t && 7 t)m 5; 238Line 38 else m 9; 238Line 39 n 0b00001000; 238Line 40 if (n & 0b00001000)m 5; Line 41 else m 3; 239Line 42 if (n & 0b00000001)t 4; Line 43 else t 2; 239Line 44 n 10; 239Line 45 for (a 0, a 5, a ;) 239Line 47 listpointer n; 240Line 48 listpointer ; 240Line 49 n n 2; 240Line 51 while (1); 241Debugging the Program 241Compiling the Completed Program 242Summary 249x

Table of ContentsAppendix: Additional Resources 251Useful Definitions 251Mathematical and Logic Operators 252Keywords 253Data Types 253Functions 254Loops 256Numbering Systems Within Microprocessor- Based Systems 256Binary Numbers 257

This book looks at programming a PIC microcontroller in C. We’ll study the following aspects of programming the PIC: 1. Looking at some of the background to the program language for micros 2. Creating a project in the Microchip IDE MPLABX 3. Configuring the PIC 4. Setting

Related Documents:

May 02, 2018 · D. Program Evaluation ͟The organization has provided a description of the framework for how each program will be evaluated. The framework should include all the elements below: ͟The evaluation methods are cost-effective for the organization ͟Quantitative and qualitative data is being collected (at Basics tier, data collection must have begun)

Silat is a combative art of self-defense and survival rooted from Matay archipelago. It was traced at thé early of Langkasuka Kingdom (2nd century CE) till thé reign of Melaka (Malaysia) Sultanate era (13th century). Silat has now evolved to become part of social culture and tradition with thé appearance of a fine physical and spiritual .

On an exceptional basis, Member States may request UNESCO to provide thé candidates with access to thé platform so they can complète thé form by themselves. Thèse requests must be addressed to esd rize unesco. or by 15 A ril 2021 UNESCO will provide thé nomineewith accessto thé platform via their émail address.

̶The leading indicator of employee engagement is based on the quality of the relationship between employee and supervisor Empower your managers! ̶Help them understand the impact on the organization ̶Share important changes, plan options, tasks, and deadlines ̶Provide key messages and talking points ̶Prepare them to answer employee questions

Dr. Sunita Bharatwal** Dr. Pawan Garga*** Abstract Customer satisfaction is derived from thè functionalities and values, a product or Service can provide. The current study aims to segregate thè dimensions of ordine Service quality and gather insights on its impact on web shopping. The trends of purchases have

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

Bruksanvisning för bilstereo . Bruksanvisning for bilstereo . Instrukcja obsługi samochodowego odtwarzacza stereo . Operating Instructions for Car Stereo . 610-104 . SV . Bruksanvisning i original

Chính Văn.- Còn đức Thế tôn thì tuệ giác cực kỳ trong sạch 8: hiện hành bất nhị 9, đạt đến vô tướng 10, đứng vào chỗ đứng của các đức Thế tôn 11, thể hiện tính bình đẳng của các Ngài, đến chỗ không còn chướng ngại 12, giáo pháp không thể khuynh đảo, tâm thức không bị cản trở, cái được