This book is ideal for the engineer, technician, hobbyist and student who have knowledge of the basic principles of PIC microcontrollers and want to develop more advanced applications using the 18F series.
The architecture of the PIC 18FXXX series as well as typical oscillator, reset, memory, and input-output circuits is completely detailed. After giving an introduction to programming in C, the book describes the project development cycle in full, giving details of the process of editing, compilation, error handling, programming and the use of specific development tools. The bulk of the book gives full details of tried and tested hands-on projects, such as the 12C BUS, USB BUS, CAN BUS, SPI BUS and real-time operating systems.
- A clear introduction to the PIC 18FXXX microcontroller's architecture
- 20 projects, including developing wireless and sensor network applications, using I2C BUS, USB BUS, CAN BUS and the SPI BUS, which give the block and circuit diagram, program description in PDL, program listing and program description
- Numerous examples of using developmental tools: simulators, in-circuit debuggers (especially ICD2) and emulators
Inhaltsverzeichnis
1;Front Cover;1 2;Advanced PIC Microcontroller Projects in C;4 3;Copyright Page;5 4;Contents;6 5;Preface;14 6;Acknowledgments;16 7;Chapter 1: Microcomputer Systems;17 7.1;1.1 Introduction;17 7.2;1.2 Microcontroller Systems;17 7.2.1;1.2.1 RAM;21 7.2.2;1.2.2 ROM;21 7.2.3;1.2.3 PROM;21 7.2.4;1.2.4 EPROM;22 7.2.5;1.2.5 EEPROM;22 7.2.6;1.2.6 Flash EEPROM;22 7.3;1.3 Microcontroller Features;22 7.3.1;1.3.1 Supply Voltage;23 7.3.2;1.3.2 The Clock;23 7.3.3;1.3.3 Timers;23 7.3.4;1.3.4 Watchdog;24 7.3.5;1.3.5 Reset Input;24 7.3.6;1.3.6 Interrupts;24 7.3.7;1.3.7 Brown-out Detector;25 7.3.8;1.3.8 Analog-to-Digital Converter;25 7.3.9;1.3.9 Serial Input-Output;25 7.3.10;1.3.10 EEPROM Data Memory;26 7.3.11;1.3.11 LCD Drivers;26 7.3.12;1.3.12 Analog Comparator;26 7.3.13;1.3.13 Real-time Clock;27 7.3.14;1.3.14 Sleep Mode;27 7.3.15;1.3.15 Power-on Reset;27 7.3.16;1.3.16 Low-Power Operation;27 7.3.17;1.3.17 Current Sink/Source Capability;27 7.3.18;1.3.18 USB Interface;28 7.3.19;1.3.19 Motor Control Interface;28 7.3.20;1.3.20 CAN Interface;28 7.3.21;1.3.21 Ethernet Interface;28 7.3.22;1.3.22 ZigBee Interface;28 7.4;1.4 Microcontroller Architectures;28 7.4.1;1.4.1 RISC and CISC;29 7.5;1.5 Number Systems;29 7.5.1;1.5.1 Decimal Number System;30 7.5.2;1.5.2 Binary Number System;30 7.5.3;1.5.3 Octal Number System;31 7.5.4;1.5.4 Hexadecimal Number System;31 7.6;1.6 Converting Binary Numbers into Decimal;32 7.7;1.7 Converting Decimal Numbers into Binary;32 7.8;1.8 Converting Binary Numbers into Hexadecimal;34 7.9;1.9 Converting Hexadecimal Numbers into Binary;36 7.10;1.10 Converting Hexadecimal Numbers into Decimal;37 7.11;1.11 Converting Decimal Numbers into Hexadecimal;38 7.12;1.12 Converting Octal Numbers into Decimal;39 7.13;1.13 Converting Decimal Numbers into Octal;39 7.14;1.14 Converting Octal Numbers into Binary;40 7.15;1.15 Converting Binary Numbers into Octal;42 7.16;1.16 Negative Numbers;42 7.17;1.17 Adding Binary Numbers;43 7.18;1.18 Subtracting Binary Numbers;45 7.19;1.19 Multipli
cation of Binary Numbers;45 7.20;1.20 Division of Binary Numbers;47 7.21;1.21 Floating Point Numbers;47 7.22;1.22 Converting a Floating Point Number into Decimal;49 7.22.1;1.22.1 Normalizing Floating Point Numbers;50 7.22.2;1.22.2 Converting a Decimal Number into Floating Point;50 7.22.3;1.22.3 Multiplication and Division of Floating Point Numbers;52 7.22.4;1.22.4 Addition and Subtraction of Floating Point Numbers;53 7.23;1.23 BCD Numbers;54 7.24;1.24 Summary;56 7.25;1.25 Exercises;56 8;Chapter 2: PIC18F Microcontroller Series;59 8.1;2.1 PIC18FXX2 Architecture;62 8.1.1;2.1.1 Program Memory Organization;66 8.1.2;2.1.2 Data Memory Organization;67 8.1.3;2.1.3 The Configuration Registers;68 8.1.4;2.1.4 The Power Supply;73 8.1.5;2.1.5 The Reset;73 8.1.6;2.1.6 The Clock Sources;76 8.1.7;2.1.7 Watchdog Timer;83 8.1.8;2.1.8 Parallel I/O Ports;84 8.1.9;2.1.9 Timers;90 8.1.10;2.1.10 Capture/Compare/PWM Modules (CCP);100 8.1.11;2.1.11 Analog-to-Digital Converter (A/D) Module;109 8.1.12;2.1.12 Interrupts;117 8.2;2.2 Summary;131 8.3;2.3 Exercises;131 9;Chapter 3: C Programming Language;135 9.1;3.1 Structure of a mikroC Program;136 9.1.1;3.1.1 Comments;137 9.1.2;3.1.2 Beginning and Ending of a Program;137 9.1.3;3.1.3 Terminating Program Statements;137 9.1.4;3.1.4 White Spaces;138 9.1.5;3.1.5 Case Sensitivity;138 9.1.6;3.1.6 Variable Names;139 9.1.7;3.1.7 Variable Types;139 9.1.8;3.1.8 Constants;142 9.1.9;3.1.9 Escape Sequences;144 9.1.10;3.1.10 Static Variables;145 9.1.11;3.1.11 External Variables;145 9.1.12;3.1.12 Volatile Variables;146 9.1.13;3.1.13 Enumerated Variables;146 9.1.14;3.1.14 Arrays;147 9.1.15;3.1.15 Pointers;149 9.1.16;3.1.16 Structures;151 9.1.17;3.1.17 Unions;154 9.1.18;3.1.18 Operators in C;155 9.1.19;3.1.19 Modifying the Flow of Control;164 9.1.20;3.1.20 Mixing mikroC with Assembly Language Statements;175 9.2;3.2 PIC Microcontroller Input-Output Port Programming;176 9.3;3.3 Programming Examples;177 9.4;3.4 Summary;181 9.5;3.5 Exercises;181 10;Chapter 4: Functio
ns and Libraries in mikroC;185 10.1;4.1 mikroC Functions;185 10.1.1;4.1.1 Function Prototypes;189 10.1.2;4.1.2 Passing Arrays to Functions;193 10.1.3;4.1.3 Passing Variables by Reference to Functions;196 10.1.4;4.1.4 Variable Number of Arguments;197 10.1.5;4.1.5 Function Reentrancy;200 10.1.6;4.1.6 Static Function Variables;200 10.2;4.2 mikroC Built-in Functions;200 10.3;4.3 mikroC Library Functions;204 10.3.1;4.3.1 EEPROM Library;205 10.3.2;4.3.2 LCD Library;208 10.3.3;4.3.3 Software UART Library;215 10.3.4;4.3.4 Hardware USART Library;220 10.3.5;4.3.5 Sound Library;222 10.3.6;4.3.6 ANSI C Library;224 10.3.7;4.3.7 Miscellaneous Library;228 10.4;4.4 Summary;234 10.5;4.5 Exercises;235 11;Chapter 5: PIC18 Development Tools;237 11.1;5.1 Software Development Tools;238 11.1.1;5.1.1 Text Editors;238 11.1.2;5.1.2 Assemblers and Compilers;238 11.1.3;5.1.3 Simulators;239 11.1.4;5.1.4 High-Level Language Simulators;240 11.1.5;5.1.5 Integrated Development Environments (IDEs);240 11.2;5.2 Hardware Development Tools;240 11.2.1;5.2.1 Development Boards;241 11.2.2;5.2.2 Device Programmers;255 11.2.3;5.2.3 In-Circuit Debuggers;258 11.2.4;5.2.4 In-Circuit Emulators;261 11.2.5;5.2.5 Breadboards;264 11.3;5.3 mikroC Integrated Development Environment (IDE);267 11.3.1;5.3.1 mikroC IDE Screen;267 11.3.2;5.3.2 Creating and Compiling a New File;274 11.3.3;5.3.3 Using the Simulator;281 11.3.4;5.3.4 Using the mikroICD In-Circuit Debugger;288 11.3.5;5.3.5 Using a Development Board;293 11.4;5.4 Summary;301 11.5;5.5 Exercises;301 12;Chapter 6: Simple PIC18 Projects;303 12.1;6.1 Program Description Language (PDL);304 12.1.1;6.1.1 START-END;304 12.1.2;6.1.2 Sequencing;304 12.1.3;6.1.3 IF-THEN-ELSE-ENDIF;304 12.1.4;6.1.4 DO-ENDDO;305 12.1.5;6.1.5 REPEAT-UNTIL;306 12.2;PROJECT 6.1-Chasing LEDs;306 12.2.1;Project Description;306 12.2.2;Project Hardware;308 12.2.3;Project PDL;309 12.2.4;Project Program;310 12.2.5;Further Development;311 12.3;PROJECT 6.2-LED Dice;311 12.3.1;Project Description;311 12.
3.2;Project Hardware;312 12.3.3;Project PDL;312 12.3.4;Project Program;314 12.3.5;Using a Pseudorandom Number Generator;314 12.4;PROJECT 6.3-Two-Dice Project;317 12.4.1;Project Description;317 12.4.2;Project Hardware;317 12.4.3;Project PDL;318 12.4.4;Project Program;319 12.5;PROJECT 6.4-Two-Dice Project Using Fewer I/O Pins;319 12.5.1;Project Description;319 12.5.2;Project Hardware;322 12.5.3;Project PDL;324 12.5.4;Project Program;325 12.5.5;Modifying the Program;325 12.6;PROJECT 6.5-7-Segment LED Counter;329 12.6.1;Project Description;329 12.6.2;Project Hardware;331 12.6.3;Project PDL;333 12.6.4;Project Program;334 12.6.5;Modified Program;335 12.7;PROJECT 6.6-Two-Digit Multiplexed 7-Segment LED;335 12.7.1;Project Description;335 12.7.2;Project Hardware;338 12.7.3;Project PDL;340 12.7.4;Project Program;340 12.8;PROJECT 6.7-Two-Digit Multiplexed 7-Segment LED Counter with Timer Interrupt;342 12.8.1;Project Description;342 12.8.2;Project Hardware;344 12.8.3;Project PDL;344 12.8.4;Project Program;345 12.8.5;Modifying the Program;345 12.9;PROJECT 6.8-Voltmeter with LCD Display;350 12.9.1;Project Description;350 12.9.2;HD44780 LCD Module;350 12.9.3;Connecting the LCD;352 12.9.4;Project Hardware;353 12.9.5;Project PDL;354 12.9.6;Project Program;354 12.9.7;A More Accurate Display;357 12.10;PROJECT 6.9-Calculator with Keypad and LCD;357 12.10.1;Project Description;357 12.10.2;Project Hardware;362 12.10.3;Project PDL;362 12.10.4;Project Program;362 12.10.5;Program Using a Built-in Keypad Function;368 12.11;PROJECT 6.10-Serial Communication-Based Calculator;368 12.11.1;Project Description;368 12.11.2;Project Hardware;374 12.11.3;Project PDL;374 12.11.4;Project Program;375 12.11.5;Testing the Program;376 12.11.6;Using Software-Based Serial Communication;380 13;Chapter 7: Advanced PIC18 Projects-SD Card Projects;387 13.1;7.1 The SD Card;387 13.1.1;7.1.1 The SPI Bus;389 13.1.2;7.1.2 Operation of the SD Card in SPI Mode;393 13.2;7.2 mikroC Language SD Card Library Functions;400 1
3.3;PROJECT 7.1-Read CID Register and Display on a PC Screen;401 13.4;PROJECT 7.2-Read/Write to SD Card Sectors;408 13.5;PROJECT 7.3-Using the Card Filing System;408 13.6;PROJECT 7.4-Temperature Logger;413 14;Chapter 8: Advanced PIC18 Projects-USB Bus Projects;425 14.1;8.1 Speed Identification on the Bus;429 14.2;8.2 USB States;429 14.3;8.3 USB Bus Communication;430 14.3.1;8.3.1 Packets;430 14.3.2;8.3.2 Data Flow Types;432 14.3.3;8.3.3 Enumeration;433 14.4;8.4 Descriptors;434 14.4.1;8.4.1 Device Descriptors;434 14.4.2;8.4.2 Configuration Descriptors;437 14.4.3;8.4.3 Interface Descriptors;439 14.4.4;8.4.4 HID Descriptors;441 14.4.5;8.4.5 Endpoint Descriptors;442 14.5;8.5 PIC18 Microcontroller USB Bus Interface;443 14.6;8.6 mikroC Language USB Bus Library Functions;445 14.7;PROJECT 8.1-USB-Based Microcontroller Output Port;446 14.7.1;The PC Software;447 14.7.2;The Microcontroller Software;454 14.7.3;Testing the Project;467 14.7.4;Using a USB Protocol Analyzer;468 14.7.5;Using the HID Terminal of mikroC;472 14.8;PROJECT 8.2-USB-Based Microcontroller Input/ Output;472 14.8.1;Testing the Project;474 14.9;PROJECT 8.3-USB-Based Ambient Pressure Display on the PC;480 15;Chapter 9: Advanced PIC18 Projects-CAN Bus Projects;491 15.1;9.1 Data Frame;497 15.1.1;9.1.1 Start of Frame (SOF);498 15.1.2;9.1.2 Arbitration Field;498 15.1.3;9.1.3 Control Field;500 15.1.4;9.1.4 Data Field;500 15.1.5;9.1.5 CRC Field;500 15.1.6;9.1.6 ACK Field;501 15.2;9.2 Remote Frame;501 15.3;9.3 Error Frame;501 15.4;9.4 Overload Frame;501 15.5;9.5 Bit Stuffing;502 15.6;9.6 Types of Errors;502 15.7;9.7 Nominal Bit Timing;502 15.8;9.8 PIC Microcontroller CAN Interface;505 15.9;9.9 PIC18F258 Microcontroller;507 15.9.1;9.9.1 Configuration Mode;509 15.9.2;9.9.2 Disable Mode;509 15.9.3;9.9.3 Normal Operation Mode;509 15.9.4;9.9.4 Listen-only Mode;509 15.9.5;9.9.5 Loop-Back Mode;510 15.9.6;9.9.6 Error Recognition Mode;510 15.9.7;9.9.7 CAN Message Transmission;510 15.9.8;9.9.8 CAN Message Reception;510 15.9.9;9.
9.9 Calculating the Timing Parameters;512 15.10;9.10 mikroC CAN Functions;514 15.10.1;9.10.1 CANSetOperationMode;515 15.10.2;9.10.2 CANGetOperationMode;516 15.10.3;9.10.3 CANInitialize;516 15.10.4;9.10.4 CANSetBaudRate;517 15.10.5;9.10.5 CANSetMask;517 15.10.6;9.10.6 CANSetFilter;518 15.10.7;9.10.7 CANRead;518 15.10.8;9.10.8 CANWrite;519 15.11;9.11 CAN Bus Programming;520 15.12;PROJECT 9.1-Temperature Sensor CAN Bus Project;520 15.12.1;The DISPLAY Processor;522 15.12.2;The COLLECTOR Processor;522 15.12.3;DISPLAY Program;523 15.12.4;COLLECTOR Program;530 16;Chapter 10: Multi-Tasking and Real-Time Operating Systems;531 16.1;10.1 State Machines;532 16.2;10.2 The Real-Time Operating System (RTOS);534 16.2.1;10.2.1 The Scheduler;534 16.3;10.3 RTOS Services;537 16.4;10.4 Synchronization and Messaging Tools;537 16.5;10.5 CCS PIC C Compiler RTOS;538 16.5.1;10.5.1 Preparing for RTOS;539 16.5.2;10.5.2 Declaring a Task;540 16.6;PROJECT 10.1-LEDs;540 16.7;PROJECT 10.2-Random Number Generator;544 16.8;PROJECT 10.3-Voltmeter with RS232 Serial Output;548 16.8.1;Using a Semaphore;552 17;Index;557