PIC Projects and Applications using C: A Project-based Approach
4/5
()
About this ebook
PIC Projects and Applications Using C details how to program the PIC microcontroller in the C language. The book takes a learn-by-doing approach, with applications covering topics such as inputs, outputs, keypads, alphanumeric displays, analogue-to-digital conversion, radio transmitters and receivers, data EEPROM, interrupts and timing. To aid debugging, the book provides a section detailing the use of the simulator and in-circuit debugger.
With this book you will learn:
- How to program the PIC microcontroller in C
- Techniques for using the simulator and debuggers to find faults on your code
- The ins and outs of interfacing circuits, such as radio modules and liquid crystal displays
- How to use the PIC on-board functions, such as interrupts and timing modules, and make analogue measurements
- Relevant parts of the language are introduced and explained when required for those new to the subject
- Core principles are introduced gradually for self-paced learning
- Explains how and why a software program works, and how to alter and expand the code
David W Smith
David Smith has had 30 years experience in the Electronics Industry. Before arriving at MMU he worked as an Electronics Design Engineer for ICL and Marconi. His teaching interests are focused on enabling Design and Technology students to implement microcontroller designs into their projects.
Read more from David W Smith
Marine Auxiliary Machinery Rating: 4 out of 5 stars4/5Stumbling toward Zion: Recovering the Biblical Tradition of Lament in the Era of World Christianity Rating: 0 out of 5 stars0 ratingsSeeking a City with Foundations: Theology for an Urban World Rating: 0 out of 5 stars0 ratingsOther People's Problems Rating: 0 out of 5 stars0 ratings
Related to PIC Projects and Applications using C
Related ebooks
PIC Microcontrollers: An Introduction to Microelectronics Rating: 5 out of 5 stars5/5ARM Microcontrollers Programming for Embedded Systems Rating: 5 out of 5 stars5/5Programming 8-bit PIC Microcontrollers in C: with Interactive Hardware Simulation Rating: 3 out of 5 stars3/5ARM-based Microcontroller Projects Using mbed Rating: 5 out of 5 stars5/5PIC32 Microcontrollers and the Digilent Chipkit: Introductory to Advanced Projects Rating: 5 out of 5 stars5/5Microcontroller Projects in C for the 8051 Rating: 5 out of 5 stars5/5Arduino Electronics Blueprints Rating: 4 out of 5 stars4/5Programming 16-Bit PIC Microcontrollers in C: Learning to Fly the PIC 24 Rating: 4 out of 5 stars4/5Projects With Microcontrollers And PICC Rating: 5 out of 5 stars5/5Embedded C Programming: Techniques and Applications of C and PIC MCUS Rating: 3 out of 5 stars3/5C Programming for the Pc the Mac and the Arduino Microcontroller System Rating: 0 out of 5 stars0 ratingsInterfacing PIC Microcontrollers: Embedded Design by Interactive Simulation Rating: 0 out of 5 stars0 ratingsSD Card Projects Using the PIC Microcontroller Rating: 0 out of 5 stars0 ratingsEmbedded Computing and Mechatronics with the PIC32 Microcontroller Rating: 5 out of 5 stars5/5Designing Embedded Systems with PIC Microcontrollers: Principles and Applications Rating: 2 out of 5 stars2/5AVR RISC Microcontroller Handbook Rating: 0 out of 5 stars0 ratingsFast and Effective Embedded Systems Design: Applying the ARM mbed Rating: 5 out of 5 stars5/5Programming Microcontrollers in C Rating: 5 out of 5 stars5/5Internet of Things with ESP8266 Rating: 5 out of 5 stars5/5ARM® Cortex® M4 Cookbook Rating: 4 out of 5 stars4/5Analog Electronics: Circuits, Systems and Signal Processing Rating: 0 out of 5 stars0 ratingsThe Designer's Guide to the Cortex-M Processor Family: A Tutorial Approach Rating: 5 out of 5 stars5/5Designing Embedded Systems with 32-Bit PIC Microcontrollers and MikroC Rating: 5 out of 5 stars5/5Programming the BeagleBone Black: Getting Started with JavaScript and BoneScript Rating: 0 out of 5 stars0 ratingsThe Art of Assembly Language Programming Using PIC® Technology: Core Fundamentals Rating: 0 out of 5 stars0 ratingsThe Definitive Guide to the ARM Cortex-M3 Rating: 4 out of 5 stars4/5Programmable Logic Controllers Rating: 4 out of 5 stars4/5Serial Port Complete: COM Ports, USB Virtual COM Ports, and Ports for Embedded Systems Rating: 4 out of 5 stars4/5Learning Robotics Using Python Rating: 0 out of 5 stars0 ratingsThe PIC Microcontroller: Your Personal Introductory Course Rating: 1 out of 5 stars1/5
Electrical Engineering & Electronics For You
Beginner's Guide to Reading Schematics, Fourth Edition Rating: 4 out of 5 stars4/5Electrician's Pocket Manual Rating: 0 out of 5 stars0 ratingsHow to Diagnose and Fix Everything Electronic, Second Edition Rating: 4 out of 5 stars4/5The Homeowner's DIY Guide to Electrical Wiring Rating: 5 out of 5 stars5/5Electricity for Beginners Rating: 5 out of 5 stars5/5Practical Electrical Wiring: Residential, Farm, Commercial, and Industrial Rating: 4 out of 5 stars4/5Electrical Engineering 101: Everything You Should Have Learned in School...but Probably Didn't Rating: 5 out of 5 stars5/5Electrical Engineering Rating: 4 out of 5 stars4/52022 Adobe® Premiere Pro Guide For Filmmakers and YouTubers Rating: 5 out of 5 stars5/5Two-Stroke Engine Repair and Maintenance Rating: 0 out of 5 stars0 ratingsElectrical Engineering: Know It All Rating: 4 out of 5 stars4/5Beginner's Guide to Reading Schematics, Third Edition Rating: 0 out of 5 stars0 ratingsSchaum's Outline of Basic Electricity, Second Edition Rating: 5 out of 5 stars5/5Understanding Electricity Rating: 4 out of 5 stars4/5Solar & 12 Volt Power For Beginners Rating: 4 out of 5 stars4/5Off-Grid Projects: Step-by-Step Guide to Building Your Own Off-Grid System Rating: 0 out of 5 stars0 ratingsSoldering electronic circuits: Beginner's guide Rating: 4 out of 5 stars4/5Upcycled Technology: Clever Projects You Can Do With Your Discarded Tech (Tech gift) Rating: 5 out of 5 stars5/5Electronics Engineering Rating: 0 out of 5 stars0 ratingsDIY Lithium Battery Rating: 3 out of 5 stars3/5DIY Drones for the Evil Genius: Design, Build, and Customize Your Own Drones Rating: 4 out of 5 stars4/5The Basics of Electronics Rating: 0 out of 5 stars0 ratingsElectronics Explained: Fundamentals for Engineers, Technicians, and Makers Rating: 5 out of 5 stars5/5Ramblings of a Mad Scientist: 100 Ideas for a Stranger Tomorrow Rating: 0 out of 5 stars0 ratingsRaspberry Pi Projects for the Evil Genius Rating: 0 out of 5 stars0 ratingsBasic Electricity Rating: 4 out of 5 stars4/5Basic Electronics: Book 2 Rating: 5 out of 5 stars5/5Forrest Mims Engineer's Notebook Rating: 4 out of 5 stars4/5
Reviews for PIC Projects and Applications using C
2 ratings1 review
- Rating: 5 out of 5 stars5/5I found this book very helpful to build on fundamentals that can easily be expanded into complex projects
Book preview
PIC Projects and Applications using C - David W Smith
Preface
DW Smith, BSc., MSc., Lecturer in Electronics, Manchester Metropolitan University, April 2013
The aim of this book is to enable the reader to program the 18F series of PIC Microcontrollers in the C language using Microchip’s MPLAB C18 compiler. The program examples demonstrate the power of the C language, yet the reader does not have to be a C programmer in order to benefit from this technology as the C language is added and explained as required.
The chapters show numerous applications starting with switching outputs on, then using digital inputs such as switches and keypads. This book continues with making measurements from analogue inputs, writing to alpha numeric displays, using timers and interrupts, and transmitting data via radio links.
There is a section on fault finding using the MPLAB simulator and in- circuit debugger. So that faults can be located easier.
All of the chapters show applications on how to use the program examples. The programs are complete and are clearly explained.
My aim has been to show the reader how to use the Microcontroller to develop programs for projects. I have tried to keep the technical detail down to a minimum and have not gone into a deeper understanding of how the microcontroller is working inside. An understanding of the electronics inside the microcontroller is not necessary to enable the reader to program it.
The reader is encouraged to build the programs, see how they work, and then modify the code to enable a clearer understanding of the principles involved; a development kit is available to do this. My own students have been ingenious in developing faults in code and I have used their efforts, hopefully, to produce an easy-to-understand guide to programming the PIC micro in C.
The programs listed in this book and the details of the development kit are available from the book’s companion website: http://booksite.elsevier.com/9780080971513.
Chapter 1
Introduction to the Microcontroller and C
This chapter is an introduction to the microcontroller memory, in the latest 18F series of devices and how it can be written to using the C programming language.
Keywords
Memory
PIC18F1220
register
Port
C language
Bit
A microcontroller is an integrated circuit that has a number of memory locations embedded inside it which are used to store instructions that are to be executed. These locations are called registers, and instructions are written to these registers to enable the microcontroller to perform an operation.
The memory location is 8 bits wide which means it can store 8 bits of information (Figure 1.1). The 8 bits in the memory are identified by numbers starting on the right with the least significant bit, bit 0, and moving to the left to the most significant bit, bit 7.
Figure 1.1 A microcontroller memory location.
Suppose we wish to turn on an LED connected to an output pin, as shown in Figure 1.2. An instruction has to be written to the output port register to output a logic 1 to turn the LED on or output a logic 0 to turn it off.
Figure 1.2 A basic microcontroller circuit.
The microcontroller we will use in this book is a PIC18F1220 manufactured by Microchip, although the codes can easily be adapted for other Microchip microcontrollers. The PIC18F1220 has 16 inputs/outputs (I/O) which means it has 16 inputs or outputs which can be configured as inputs or outputs by instructing the microcontroller via a register, the tristate (TRIS) register (Figure 1.3). TRIS means the port pin can be (i) an input, or an output which is switched (ii) high or (iii) low, three states.
Figure 1.3 The TRIS register.
The memory locations in the microcontroller are 8 bits wide so 16 I/O will require two 8 bit registers called PORTA and PORTB.
Suppose we wish to turn on an LED which we are going to connect to bit 4 on PORTB. We first of all have to instruct the microcontroller to ensure that PORTB bit 4 is an output. At the moment it does not matter what the rest of PORTB is doing, so now let’s make bit 4 an output and the other 7 bits inputs. We do this with the following instruction:
TRISB = 0b11101111;
0b means the number is a binary one.
Note a 1 sets the pin as an input, a 0 sets the pin as an output.
Now that PORTB bit 4 is an output, we can write a logic1 to it with:
PORTBbits.RB4 = 1; (Figure 1.4).
Figure 1.4 Writing to PORTB.
There are several ways in which we can give the microcontroller instructions, called programming. These program languages are assembly, basic, C, or a number of flowchart languages. The language that we are going to use in this book is the C programming language, which is a high-level language that is very versatile. The previous book PIC in Practice
written by the author, DW Smith, used the assembly language to program the microcontroller.
C is a very comprehensive and versatile language, which usually means there is a lot to learn. Throughout this book I will introduce the C language as and when required and only those instructions that are needed to perform the control. So you will not need to become a C programmer in order to program the micro in C!
Chapter 2
First C Program
This chapter explains how to install and use the two current Microchip Integrated Development Environments, MPLAB and MPLABX. The explanation is centred around a basic program, flashing a light emitting diode on and off. The chapter explains how to install the IDE, how the program is written, compiled and then programmed into the microcontroller using the development tools and C8 compiler. The discussion of the integrated development environment shows how to write and develop the C source code file and how to attach it to a project and fix errors. An explanation of the operation of the code is given.
Keywords
C program
MPLAB
MPLABX
delays
loops
C18
outputs