0%

For makers looking to use the smallest microcontrollers or to wring the highest performance out of larger ones, the C language is still the best option. This practical book provides a solid grounding in C basics for anyone who tinkers with programming microcontrollers. You'll explore the many ways C enables developers and makers to get big results out of tiny devices.

Author Marc Loy shows you how to write clean, maintainable C code from scratch. This language and its cousin, C++, are still widely used to write low-level code for device drivers or operating systems. By understanding C syntax and its quirks, you'll gain an enduring computer language literacy that will help you pick up new languages and styles more easily.

  • Learn C fundamentals, such as data types, flow control, and functions
  • Explore memory management including how programs work on small devices
  • Understand answers provided in online forums such as Reddit or Stack Overflow
  • Write efficient, custom C code that's both readable and maintainable
  • Analyze the performance of your code and weigh optimizations
  • Evaluate third-party libraries for use in your own projects
  • Create your own libraries to share with others

Table of Contents

  1. Preface
    1. How to Use This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
  2. 1. The ABCs of C
    1. Strengths and Weaknesses
    2. Getting Started
    3. Tools Required
    4. Creating a C “Hello, World”
    5. Compiling Your Code
    6. Running Your Code
    7. Next Steps
  3. 2. Storing and Stating
    1. Statements in C
    2. Statement Separators
    3. Statement Flow
    4. Variables and Types
    5. Getting User Input
    6. Strings and Characters
    7. Numbers
    8. Variable Names
    9. Variable Assignments
    10. printf() and scanf()
    11. printf() Formats
    12. Tailored Output
    13. scanf() and Parsing Inputs
    14. Operators and Expressions
    15. Arithmetic Operators
    16. Order of Operations
    17. Type Casting
    18. Next Steps
  4. 3. Flow of Control
    1. Boolean Values
    2. Comparison Operators
    3. Logical Operators
    4. Branching
    5. The if Statement
    6. The switch Statement
    7. The Ternary Operator and Conditional Assignment
    8. Loop Statements
    9. The for Statement
    10. The while Statement
    11. The do/while Variation
    12. Nesting
    13. Nested Loops and Tables
    14. Variable Scope
    15. Exercises
    16. Next Steps
  5. 4. Bits and (Many) Bytes
    1. Storing Multiple Things with Arrays
    2. Creating and Manipulating Arrays
    3. Review of Strings
    4. Multidimensional Arrays
    5. Accessing Elements in Multidimensional Arrays
    6. Storing Bits
    7. Binary, Octal, Hexadecimal
    8. Octal and Hexadecimal Literals in C
    9. Input and Output of Octal and Hex Values
    10. Bitwise Operators
    11. Mixing Bits and Bytes
    12. Conversion Answers
    13. Next Steps
  6. 5. Functions
    1. Familiar Functions
    2. Function Flow
    3. Simple Functions
    4. Sending Information to Functions
    5. Passing Simple Types
    6. Passing Strings to Functions
    7. Multiple Types
    8. Exiting a Function
    9. Returning Information
    10. Using Returned Values
    11. Ignoring Returned Values
    12. Nested Calls and Recursion
    13. Recursive Functions
    14. Variable Scope
    15. Global Variables
    16. The main() Function
    17. Return values and main()
    18. Command-Line Arguments and main()
    19. Next Steps
  7. 6. Pointers and References
    1. Addresses in C
    2. The NULL Value and Pointer Errors
    3. Arrays
    4. Local Variables and the Stack
    5. Global Variables and the Heap
    6. Pointer Arithmetic
    7. Array Pointers
    8. Functions and Pointers
    9. Managing Memory with Arrays
    10. Allocating with malloc()
    11. Deallocating with free()
    12. C Structures
    13. Defining Structures
    14. Assigning and Accessing Structure Members
    15. Pointers to Structures
    16. Functions and Structures
    17. Pointer Syntax Recap
    18. Next Steps
  8. 7. Libraries
    1. The C Standard Library
    2. stdio.h
    3. stdlib.h
    4. string.h
    5. math.h
    6. time.h
    7. ctype.h
    8. Putting It Together
    9. Filling In Strings
    10. Finding Our Interest
    11. Finding New Libraries
    12. Next Steps
  9. 8. Real-World C With Arduino
    1. Arduino IDE (Win, Mac, Linux)
    2. Installing on Windows
    3. Installing on macOS
    4. Installing on Linux
    5. Your First Arduino Project
    6. Selecting Your Board
    7. Hello, LED!
    8. An External LED Upgrade
    9. Arduino Libraries
    10. Managing Libraries
    11. Using Arduino Libraries
    12. Arduino Sketches and C++
    13. C++ Objects and Variables
    14. More Practice with Objects
    15. C++ Considerations
    16. Object Homework
    17. Next Steps
  10. 9. Smaller Systems
    1. The Arduino Environment
    2. Special Values
    3. Special Types
    4. “Built-In” Functions
    5. Trying Out the Arduino “Stuff”
    6. Microcontroller I/O
    7. Sensors and Analog Input
    8. The Serial Monitor
    9. Is It Hot in Here?
    10. Segmented Displays
    11. Buttons and Digital Input
    12. Just How Hot Is It?
    13. Memory Management on Arduino
    14. Flash (PROGMEM)
    15. SRAM
    16. EEPROM
    17. Remembering Choices
    18. Interrupts
    19. Interrupt Service Routines
    20. Interrupt-Driven Programming
    21. Exercises
    22. Next Steps
  11. 10. Faster Code
    1. The Setup
    2. Floating-Point Versus Integer Math
    3. Floating-Point Math Alternatives
    4. Integer Math Versus No Math
    5. Lookup Tables
    6. The Project So Far
    7. The Power of Powers of 2
    8. Loop Optimizations
    9. Unrolling for Fun and Profit
    10. Recursion Versus Iteration
    11. String Versus char[]
    12. Our Final Offer
    13. Next Steps
  12. 11. Custom Libraries
    1. Creating Your Own Library
    2. Preprocessor Directives
    3. Preprocessor Macros
    4. Custom Type Definitions
    5. Our Car Project
    6. Multifile Projects
    7. Code (.ino) Files
    8. Header Files
    9. Importing Custom Libraries
    10. Facilitating Communication
    11. Retrofitting Our Car
    12. Creating a Controller
    13. Creating the Library
    14. Updating the Car Project
    15. Getting It Under Control
    16. Go Driving!
    17. Documentation and Distribution
    18. Next Steps
  13. 12. Next Next Steps
    1. Intermediate and Advanced Topics
    2. IoT and Arduino
    3. Arduino Source Code
    4. Other Microcontrollers
    5. Industry C/C++
    6. Back to the Future
  14. A. Hardware and Software
    1. Getting the Code
    2. Getting the Hardware: Adafruit
    3. VS Code
    4. Arduino IDE
    5. Fritzing
    6. GNU Compiler Collection
  15. B. printf() Format Specifier Details
    1. Specifier Syntax
    2. Specifier Types
    3. Specifier Flags
    4. Width and Precision
    5. Common Formats
  16. Index
18.222.69.152