You are previewing Embedded C Programming.
O'Reilly logo
Embedded C Programming

Book Description

This book provides a hands-on introductory course on concepts of C programming using a PIC® microcontroller and CCS C compiler. Through a project-based approach, this book provides an easy to understand method of learning the correct and efficient practices to program a PIC® microcontroller in C language. Principles of C programming are introduced gradually, building on skill sets and knowledge. Early chapters emphasize the understanding of C language through experience and exercises, while the latter half of the book covers the PIC® microcontroller, its peripherals, and how to use those peripherals from within C in great detail.

This book demonstrates the programming methodology and tools used by most professionals in embedded design, and will enable you to apply your knowledge and programming skills for any real-life application. Providing a step-by-step guide to the subject matter, this book will encourage you to alter, expand, and customize code for use in your own projects.



  • A complete introduction to C programming using PIC microcontrollers, with a focus on real-world applications, programming methodology and tools
  • Each chapter includes C code project examples, tables, graphs, charts, references, photographs, schematic diagrams, flow charts and compiler compatibility notes to channel your knowledge into real-world examples
  • Online materials include presentation slides, extended tests, exercises, quizzes and answers, real-world case studies, videos and weblinks

Table of Contents

  1. Cover image
  2. Title page
  3. Table of Contents
  4. Copyright
  5. Introduction
  6. Chapter 1: C Overview and Program Structure
    1. Abstract
    2. C Source Code
    3. Comments
    4. Program Structure
    5. C Preprocessor Directives
    6. Functions
    7. Declarations
    8. Statements and Expressions
    9. Time
    10. Typing Accuracy
    11. Text Formatting
    12. Compatibility Notes
    13. Summary
  7. Chapter 2: Constants
    1. Abstract
    2. Bits, Bytes, Etc.
    3. Syntax of C Constants
    4. Tri-Graph Sequences
    5. Compatibility Notes
    6. Design Documentation
    7. Summary
  8. Chapter 3: Preprocessor Directives
    1. Abstract
    2. Standard Preprocessor Directives
    3. Nonstandard Pragmas
    4. Summary
  9. Chapter 4: Data Variable and Types
    1. Abstract
    2. Data Types
    3. Declaring Variables
    4. Design Documentation
    5. RAM
    6. Summary
  10. Chapter 5: Expressions and Operators
    1. Abstract
    2. Mathematical Operators
    3. Compatibility Notes
    4. Operator Precedence
    5. Expression Type and Type Conversion
    6. Relational Operators
    7. Binary Bitwise Operators
    8. Compatibility Notes
    9. Assignment Operators
    10. Increment/Decrement Operators
    11. Other Operators
    12. Sequence Points
    13. Expression Examples
    14. Summary
  11. Chapter 6: Statements
    1. Abstract
    2. Statement Definitions
    3. Side Effects
    4. Nesting, Indentation, and Use of Braces
    5. Design Documentation
    6. Program Complexity
    7. Summary
  12. Chapter 7: Functions
    1. Abstract
    2. main() FUNCTION
    3. Function Definitions
    4. Function Parameters
    5. Advanced Features
    6. Return Values
    7. Inline Functions
    8. Nested Functions
    9. Recursive Functions
    10. A Little More on Sequence Points
    11. Well-Structured Programs
    12. Design Documentation
    13. Implementation Details
    14. Summary
  13. Chapter 8: Arrays
    1. Abstract
    2. Array Initializers
    3. Constant Arrays
    4. String Variables
    5. Dimensionless Arrays
    6. Multidimensional Arrays
    7. Index Range
    8. Example Array Usage
    9. Lookup Tables
    10. Searching Arrays
    11. Sorting Arrays
    12. Summary
  14. Chapter 9: Structures
    1. Abstract:
    2. Structure Nesting and Arrays
    3. Structure Layout in Memory
    4. Bit Fields
    5. Unions
    6. Example of Structures in a Program
    7. Summary
  15. Chapter 10: Memory and Pointers
    1. Abstract
    2. Memory
    3. Address-of Operator
    4. Indirection Operator
    5. Forcing a Variable Address
    6. Pointer Types
    7. Pointer Math
    8. Back to Subscripts
    9. Back to Function Parameters
    10. Back to Structures
    11. Function Pointers
    12. Other Uses for Pointers
    13. Bad Ideas
    14. Common Mistakes
    15. ROM Pointers
    16. User-Defined Memory
    17. Compatibility Note
    18. Over the Hill
    19. Summary
  16. Chapter 11: Built-in Functions
    1. Abstract
    2. Math
    3. Memory
    4. Dynamic Memory
    5. A Few More Cool Functions
    6. Variable Argument List
    7. Text Input/Output
    8. Implementation Constants
    9. Compatibility Notes
    10. Bit and Byte Manipulation
    11. Non-volatile Memory
    12. Watchdog Timer
    13. Delays
    14. Multiple Clock Speeds
    15. A Few More Standard Functions
    16. Coming Up
    17. Summary
  17. Chapter 12: Strings
    1. Abstract
    2. String Copy and Length
    3. String Search
    4. String Compare
    5. String Manipulation
    6. String Input and Output
    7. String Conversion to/from Numbers
    8. Character Manipulation
    9. Unicode
    10. Constant String Management
    11. Summary
  18. Chapter 13: Function-like Macros
    1. Abstract
    2. Arguments
    3. Macro Names
    4. Concatenation Operator
    5. Stringize Operator
    6. Variadic Macro Syntax
    7. Function-Like Macros vs. Inline Functions
    8. Readability
    9. Advanced Example
    10. Debugging Macro Problems
    11. Summary
  19. Chapter 14: Conditional Compilation
    1. Abstract
    2. Basic Directives
    3. Relational Expressions
    4. Special Macros
    5. Special Defines
    6. Global Defines
    7. Strange Errors
    8. Examples of Conditional Compilation
    9. Summary
  20. Chapter 15: PIC® Microcontroller
    1. Abstract
    2. PIC® Architecture
    3. Minimal Hardware Connections
    4. Device Programming
    5. Hex Files
    6. Power-Up Considerations
    7. Clock Configurations
    8. Debugging
    9. Bootloading
    10. Summary
  21. Chapter 16: Discrete Input and Output
    1. Abstract
    2. Input Voltages
    3. Drive Current
    4. Driving More Current
    5. Open Collector Outputs
    6. Direction
    7. Button Input
    8. Pull-Ups
    9. Debounce
    10. Filtering
    11. Memory-Mapping Ports
    12. Summary
  22. Chapter 17: Interrupts
    1. Abstract
    2. Simple Interrupt Example
    3. Where Does the Time Go?
    4. Debounce Revisited
    5. It’s Not Always a Good Time to Interrupt
    6. Why Do We Need Interrupts?
    7. What is Really Happening
    8. Handle Your Interrupts Right
    9. Multiple Interrupt Considerations
    10. Latency
    11. Reentrancy
    12. Compatibility Notes
    13. Summary
  23. Chapter 18: Timers/Counters
    1. Abstract
    2. Timer Components
    3. PIC® Specifics
    4. C Code
    5. Delay Using Timer
    6. Precision Loop
    7. Interrupts
    8. Virtual Timers
    9. Summary
  24. Chapter 19: Advanced Timing
    1. Abstract
    2. PWM
    3. Using the PWM Library
    4. Capture
    5. Compare
    6. Compatibility Notes
    7. Summary
  25. Chapter 20: Analog Techniques
    1. Abstract
    2. Digital to Analog Conversion
    3. Analog to Digital Conversion
    4. Comparator
    5. Voltage Detect
    6. Compatibility Notes
    7. Summary
  26. Chapter 21: Internal Serial Busses
    1. Abstract
    2. Serial Peripheral Interface
    3. I2C
    4. Summary
  27. Chapter 22: External Serial Busses
    1. Abstract
    2. RS-232
    3. Source Code
    4. UART
    5. Incoming Data Interrupts
    6. Outgoing Data Interrupts
    7. Modem Control Signals
    8. Hardware Flow Control
    9. Software Flow Control
    10. Protocol
    11. RS-232 Future
    12. RS-422
    13. RS-485
    14. Documentation
    15. Summary
  28. Chapter 23: Multi-Tasking
    1. Abstract
    2. Preemptive Scheduling
    3. Dispatcher Scheduling
    4. Deterministic Scheduling
    5. Semaphores
    6. Message Passing
    7. await()
    8. Task Management
    9. Summary
  29. Chapter 24: Inline Assembly
    1. Abstract
    2. Assembly Code with C Code
    3. Inline Assembly Code
    4. PIC16/PIC Simple Move Instructions
    5. Accessing C Variables from Assembly
    6. PIC16/PIC18 Math Instructions
    7. PIC16/PIC18 Bit Instructions
    8. PIC16/18 Branch Instructions
    9. PIC16/18 Literal Instructions
    10. Compiler Modifications to the Assembly
    11. SFR Access
    12. About the FSR
    13. What Not To Do
    14. Optimized Assembly
    15. PIC24 Instructions
    16. dsPIC® Instructions
    17. Summary
  30. Chapter 25: Debugging
    1. Abstract
    2. Overview
    3. ICSP
    4. ICSP Jacks
    5. Breakpoints
    6. Viewing Memory
    7. Stepping
    8. Power Debugging
    9. Monitor
    10. Data Streaming
    11. Real-Time Issues
    12. Use of a Scope
    13. Diagnostic Interface
    14. Record/Playback
    15. Profile Tool
    16. Profiling Code
    17. Design Verification
    18. Summary
  31. Bibliography
  32. Appendix-A
  33. Appendix-B
  34. Appendix-C