Suchen und Finden
Mehr zum Inhalt
Advanced PIC Microcontroller Projects in C - From USB to RTOS with the PIC 18F Series
Front Cover
1
Advanced PIC Microcontroller Projects in C
4
Copyright Page
5
Contents
6
Preface
14
Acknowledgments
16
Chapter 1: Microcomputer Systems
17
1.1 Introduction
17
1.2 Microcontroller Systems
17
1.2.1 RAM
21
1.2.2 ROM
21
1.2.3 PROM
21
1.2.4 EPROM
22
1.2.5 EEPROM
22
1.2.6 Flash EEPROM
22
1.3 Microcontroller Features
22
1.3.1 Supply Voltage
23
1.3.2 The Clock
23
1.3.3 Timers
23
1.3.4 Watchdog
24
1.3.5 Reset Input
24
1.3.6 Interrupts
24
1.3.7 Brown-out Detector
25
1.3.8 Analog-to-Digital Converter
25
1.3.9 Serial Input-Output
25
1.3.10 EEPROM Data Memory
26
1.3.11 LCD Drivers
26
1.3.12 Analog Comparator
26
1.3.13 Real-time Clock
27
1.3.14 Sleep Mode
27
1.3.15 Power-on Reset
27
1.3.16 Low-Power Operation
27
1.3.17 Current Sink/Source Capability
27
1.3.18 USB Interface
28
1.3.19 Motor Control Interface
28
1.3.20 CAN Interface
28
1.3.21 Ethernet Interface
28
1.3.22 ZigBee Interface
28
1.4 Microcontroller Architectures
28
1.4.1 RISC and CISC
29
1.5 Number Systems
29
1.5.1 Decimal Number System
30
1.5.2 Binary Number System
30
1.5.3 Octal Number System
31
1.5.4 Hexadecimal Number System
31
1.6 Converting Binary Numbers into Decimal
32
1.7 Converting Decimal Numbers into Binary
32
1.8 Converting Binary Numbers into Hexadecimal
34
1.9 Converting Hexadecimal Numbers into Binary
36
1.10 Converting Hexadecimal Numbers into Decimal
37
1.11 Converting Decimal Numbers into Hexadecimal
38
1.12 Converting Octal Numbers into Decimal
39
1.13 Converting Decimal Numbers into Octal
39
1.14 Converting Octal Numbers into Binary
40
1.15 Converting Binary Numbers into Octal
42
1.16 Negative Numbers
42
1.17 Adding Binary Numbers
43
1.18 Subtracting Binary Numbers
45
1.19 Multiplication of Binary Numbers
45
1.20 Division of Binary Numbers
47
1.21 Floating Point Numbers
47
1.22 Converting a Floating Point Number into Decimal
49
1.22.1 Normalizing Floating Point Numbers
50
1.22.2 Converting a Decimal Number into Floating Point
50
1.22.3 Multiplication and Division of Floating Point Numbers
52
1.22.4 Addition and Subtraction of Floating Point Numbers
53
1.23 BCD Numbers
54
1.24 Summary
56
1.25 Exercises
56
Chapter 2: PIC18F Microcontroller Series
59
2.1 PIC18FXX2 Architecture
62
2.1.1 Program Memory Organization
66
2.1.2 Data Memory Organization
67
2.1.3 The Configuration Registers
68
2.1.4 The Power Supply
73
2.1.5 The Reset
73
2.1.6 The Clock Sources
76
2.1.7 Watchdog Timer
83
2.1.8 Parallel I/O Ports
84
2.1.9 Timers
90
2.1.10 Capture/Compare/PWM Modules (CCP)
100
2.1.11 Analog-to-Digital Converter (A/D) Module
109
2.1.12 Interrupts
117
2.2 Summary
131
2.3 Exercises
131
Chapter 3: C Programming Language
135
3.1 Structure of a mikroC Program
136
3.1.1 Comments
137
3.1.2 Beginning and Ending of a Program
137
3.1.3 Terminating Program Statements
137
3.1.4 White Spaces
138
3.1.5 Case Sensitivity
138
3.1.6 Variable Names
139
3.1.7 Variable Types
139
3.1.8 Constants
142
3.1.9 Escape Sequences
144
3.1.10 Static Variables
145
3.1.11 External Variables
145
3.1.12 Volatile Variables
146
3.1.13 Enumerated Variables
146
3.1.14 Arrays
147
3.1.15 Pointers
149
3.1.16 Structures
151
3.1.17 Unions
154
3.1.18 Operators in C
155
3.1.19 Modifying the Flow of Control
164
3.1.20 Mixing mikroC with Assembly Language Statements
175
3.2 PIC Microcontroller Input-Output Port Programming
176
3.3 Programming Examples
177
3.4 Summary
181
3.5 Exercises
181
Chapter 4: Functions and Libraries in mikroC
185
4.1 mikroC Functions
185
4.1.1 Function Prototypes
189
4.1.2 Passing Arrays to Functions
193
4.1.3 Passing Variables by Reference to Functions
196
4.1.4 Variable Number of Arguments
197
4.1.5 Function Reentrancy
200
4.1.6 Static Function Variables
200
4.2 mikroC Built-in Functions
200
4.3 mikroC Library Functions
204
4.3.1 EEPROM Library
205
4.3.2 LCD Library
208
4.3.3 Software UART Library
215
4.3.4 Hardware USART Library
220
4.3.5 Sound Library
222
4.3.6 ANSI C Library
224
4.3.7 Miscellaneous Library
228
4.4 Summary
234
4.5 Exercises
235
Chapter 5: PIC18 Development Tools
237
5.1 Software Development Tools
238
5.1.1 Text Editors
238
5.1.2 Assemblers and Compilers
238
5.1.3 Simulators
239
5.1.4 High-Level Language Simulators
240
5.1.5 Integrated Development Environments (IDEs)
240
5.2 Hardware Development Tools
240
5.2.1 Development Boards
241
5.2.2 Device Programmers
255
5.2.3 In-Circuit Debuggers
258
5.2.4 In-Circuit Emulators
261
5.2.5 Breadboards
264
5.3 mikroC Integrated Development Environment (IDE)
267
5.3.1 mikroC IDE Screen
267
5.3.2 Creating and Compiling a New File
274
5.3.3 Using the Simulator
281
5.3.4 Using the mikroICD In-Circuit Debugger
288
5.3.5 Using a Development Board
293
5.4 Summary
301
5.5 Exercises
301
Chapter 6: Simple PIC18 Projects
303
6.1 Program Description Language (PDL)
304
6.1.1 START-END
304
6.1.2 Sequencing
304
6.1.3 IF-THEN-ELSE-ENDIF
304
6.1.4 DO-ENDDO
305
6.1.5 REPEAT-UNTIL
306
PROJECT 6.1-Chasing LEDs
306
Project Description
306
Project Hardware
308
Project PDL
309
Project Program
310
Further Development
311
PROJECT 6.2-LED Dice
311
Project Description
311
Project Hardware
312
Project PDL
312
Project Program
314
Using a Pseudorandom Number Generator
314
PROJECT 6.3-Two-Dice Project
317
Project Description
317
Project Hardware
317
Project PDL
318
Project Program
319
PROJECT 6.4-Two-Dice Project Using Fewer I/O Pins
319
Project Description
319
Project Hardware
322
Project PDL
324
Project Program
325
Modifying the Program
325
PROJECT 6.5-7-Segment LED Counter
329
Project Description
329
Project Hardware
331
Project PDL
333
Project Program
334
Modified Program
335
PROJECT 6.6-Two-Digit Multiplexed 7-Segment LED
335
Project Description
335
Project Hardware
338
Project PDL
340
Project Program
340
PROJECT 6.7-Two-Digit Multiplexed 7-Segment LED Counter with Timer Interrupt
342
Project Description
342
Project Hardware
344
Project PDL
344
Project Program
345
Modifying the Program
345
PROJECT 6.8-Voltmeter with LCD Display
350
Project Description
350
HD44780 LCD Module
350
Connecting the LCD
352
Project Hardware
353
Project PDL
354
Project Program
354
A More Accurate Display
357
PROJECT 6.9-Calculator with Keypad and LCD
357
Project Description
357
Project Hardware
362
Project PDL
362
Project Program
362
Program Using a Built-in Keypad Function
368
PROJECT 6.10-Serial Communication-Based Calculator
368
Project Description
368
Project Hardware
374
Project PDL
374
Project Program
375
Testing the Program
376
Using Software-Based Serial Communication
380
Chapter 7: Advanced PIC18 Projects-SD Card Projects
387
7.1 The SD Card
387
7.1.1 The SPI Bus
389
7.1.2 Operation of the SD Card in SPI Mode
393
7.2 mikroC Language SD Card Library Functions
400
PROJECT 7.1-Read CID Register and Display on a PC Screen
401
PROJECT 7.2-Read/Write to SD Card Sectors
408
PROJECT 7.3-Using the Card Filing System
408
PROJECT 7.4-Temperature Logger
413
Chapter 8: Advanced PIC18 Projects-USB Bus Projects
425
8.1 Speed Identification on the Bus
429
8.2 USB States
429
8.3 USB Bus Communication
430
8.3.1 Packets
430
8.3.2 Data Flow Types
432
8.3.3 Enumeration
433
8.4 Descriptors
434
8.4.1 Device Descriptors
434
8.4.2 Configuration Descriptors
437
8.4.3 Interface Descriptors
439
8.4.4 HID Descriptors
441
8.4.5 Endpoint Descriptors
442
8.5 PIC18 Microcontroller USB Bus Interface
443
8.6 mikroC Language USB Bus Library Functions
445
PROJECT 8.1-USB-Based Microcontroller Output Port
446
The PC Software
447
The Microcontroller Software
454
Testing the Project
467
Using a USB Protocol Analyzer
468
Using the HID Terminal of mikroC
472
PROJECT 8.2-USB-Based Microcontroller Input/ Output
472
Testing the Project
474
PROJECT 8.3-USB-Based Ambient Pressure Display on the PC
480
Chapter 9: Advanced PIC18 Projects-CAN Bus Projects
491
9.1 Data Frame
497
9.1.1 Start of Frame (SOF)
498
9.1.2 Arbitration Field
498
9.1.3 Control Field
500
9.1.4 Data Field
500
9.1.5 CRC Field
500
9.1.6 ACK Field
501
9.2 Remote Frame
501
9.3 Error Frame
501
9.4 Overload Frame
501
9.5 Bit Stuffing
502
9.6 Types of Errors
502
9.7 Nominal Bit Timing
502
9.8 PIC Microcontroller CAN Interface
505
9.9 PIC18F258 Microcontroller
507
9.9.1 Configuration Mode
509
9.9.2 Disable Mode
509
9.9.3 Normal Operation Mode
509
9.9.4 Listen-only Mode
509
9.9.5 Loop-Back Mode
510
9.9.6 Error Recognition Mode
510
9.9.7 CAN Message Transmission
510
9.9.8 CAN Message Reception
510
9.9.9 Calculating the Timing Parameters
512
9.10 mikroC CAN Functions
514
9.10.1 CANSetOperationMode
515
9.10.2 CANGetOperationMode
516
9.10.3 CANInitialize
516
9.10.4 CANSetBaudRate
517
9.10.5 CANSetMask
517
9.10.6 CANSetFilter
518
9.10.7 CANRead
518
9.10.8 CANWrite
519
9.11 CAN Bus Programming
520
PROJECT 9.1-Temperature Sensor CAN Bus Project
520
The DISPLAY Processor
522
The COLLECTOR Processor
522
DISPLAY Program
523
COLLECTOR Program
530
Chapter 10: Multi-Tasking and Real-Time Operating Systems
531
10.1 State Machines
532
10.2 The Real-Time Operating System (RTOS)
534
10.2.1 The Scheduler
534
10.3 RTOS Services
537
10.4 Synchronization and Messaging Tools
537
10.5 CCS PIC C Compiler RTOS
538
10.5.1 Preparing for RTOS
539
10.5.2 Declaring a Task
540
PROJECT 10.1-LEDs
540
PROJECT 10.2-Random Number Generator
544
PROJECT 10.3-Voltmeter with RS232 Serial Output
548
Using a Semaphore
552
Index
557
Alle Preise verstehen sich inklusive der gesetzlichen MwSt.