PICmicro MCU C_ An Introduction to Programming the Microchip PIC in CCS C [Gardner 2002-08-19].pdf

(2291 KB) Pobierz
738330635 UNPDF
PICmicro MCU C ®
An introduction to programming
The Microchip PIC in CCS C
By Nigel Gardner
The information contained in this publication regarding device application
and the like is intended by way of suggestion only and may be superseded by
updates. No representation or warranty is given and no liability is assumed by
Bluebird Electronics, Microchip Technology Inc., or CCS Inc., with respect to
the accuracy or use of such information, or infringement of patents arising from
such use or their compliance to EMC standards or otherwise. Use of Bluebird
Electronics, Microchip Technology Inc. or CCS Inc. products as critical
components in life support systems is not authorized except with express
written approval by above mentioned companies. No licenses are conveyed,
implicitly or otherwise, under intellectual property rights.
Copyright ® Bluebird Electronics 2002. All rights reserved. Except as permitted
under the copyright Act of 1976 US Code 102 101-122, no part of this
publication may be reproduced or distributed in any form or by any means, or
stored in a database or retrieval system, without the prior written permission of
Bluebird Electronics, with the exception of the program listings which may be
entered, stored, and executed in a computer system, but may not be
reproduced for publication.
PIC® and PICmicro, is registered trademark of Microchip Technologies Inc. in
the USA and other countries.
Printed and bound in the USA.
Cover Art by Loni Zarling.
Circuit diagrams produced with Labcentre Isis Illustrator. Flowcharts produced
with Corel Flow.
2
Preface
Thanks go to Rodger Richey of Microchip Technology Inc. for the use of this
notes on C for the PICmicro®MCU, Mark at CCS, Inc. and Val Bellamy for
proofreading this book.
This book is dedicated to my wise June and daughter Emma.
3
Contents
Introduction
History
Why use C?
PC based versus PICmicro®MCU Based Program Development
Product Development
Terminology
Trying and Testing Code
C Coding Standards
Basics
1 C Fundamentals
Structure of C Programs
Components of a C Program
#pragma
main()
#include
printf Function
Variables
Constants
Comments
Functions
C Keywords
2 Variables
Data Types
Variable Declaration
Variable Assignment
Enumeration
typedef
Type Conversions
3 Functions
Functions
Function Prototypes
Using Function Arguments
Using Function to Return Values
Classic and Modern Function Declarations
4 Operators
Arithmetic
Relational
Logical
Bitwise
4
Increment and Decrement
Precedence of
5 Program Control Statements
If
If-else
?
for Loop
while Loop
do-while Loop
Nesting Program Control Statements
Break
Continue
Null
Return
6 Arrays / Strings
One Dimensional Arrays
Strings
Multidimensional Arrays
Initializing Arrays
Arrays of Strings
7 Pointers
Pointer Basics
Pointers and Arrays
Passing Pointer to Functions
8 Structures / Unions
Structure Basics
Pointers to Structures
Nested Structures
Union Basics
Pointers to Unions
9 PICmicro®MCU Specific C
Inputs and Outputs
Mixing C and Assembler
Advanced BIT Manipulation
Timers
A/D Conversion
Data Communications
I 2 C Communications
SPI Communications
PWM
LCD Driving
5
Zgłoś jeśli naruszono regulamin