Skip to main content
Embedded Systems
An Interactive Textbook for ENGS 28
Petra Bonfert-Taylor
Contents
Index
Search Book
Search Results:
No results.
Readability settings
Readability settings
Line height
1.35
Content font size
100%
Theme
System
Light
Dark
Assistive technologies
đź”—
Expose permalinks
Reset All
Prev
Up
Next
\(\newcommand{\N}{\mathbb N} \newcommand{\Z}{\mathbb Z} \newcommand{\R}{\mathbb R} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \definecolor{fillinmathshade}{gray}{0.9} \newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}} \)
Front Matter
Colophon
Acknowledgments
Welcome to ENGS 28!
Course Reference Documents
1
GPIO and Blinky: Your First Embedded Program
1.1
Microcontrollers, GPIO, and Registers
1.1.1
What Is a Microcontroller?
1.1.2
GPIO: The Software-Hardware Bridge
1.1.3
Peripheral Registers: How Software Talks to Hardware
1.1.4
Check Your Understanding
1.2
Day 1 In-Class: Your First Blinky
1.2.1
Part 1: The Nucleo Board
1.2.2
Part 2: Get Blinky Running
1.2.3
Part 3: Inside blinky.c
1.2.4
Part 4: Explore the Registers
1.2.5
Before Day 1X: Design and Build in the Simulator
1.3
Day 1X In-Class: blinkySlowToFast
1.3.1
Part 1: Managing Multiple Source Files
1.3.2
Part 2: Port blinkySlowToFast to the Nucleo
1.3.3
Part 3: Stretch Challenges
1.4
Day 2 In-Class: External LEDs and delay_ms()
1.4.1
Part 1: From C Code to Hardware
1.4.2
Part 2: A Better Delay, delay_ms()
1.4.3
Part 3: External LEDs on the Breadboard
1.4.4
Part 4: Lab 1, Three-LED Experiments
1.4.4
Activity
1.5
Reference: Bit Twiddling
1.5
Check Your Understanding
1.6
Inside the Microcontroller
1.6.1
Processor Architecture
1.6.2
Memory-Mapped I/O
1.6.3
Check Your Understanding
2
Digital Inputs: Switches and Debouncing
2.1
Before Class: Switches, Pull-ups, and Bounce
2.1.1
Reading a Pin: The Input Data Register
2.1.2
Floating Pins and Pull-up Resistors
2.1.3
Contact Bounce
2.1.4
Check Your Understanding
2.2
Day 3 In-Class: Digital Inputs and the Oscilloscope
2.2.1
Part 1: Code Review (blinkyCNT.c)
2.2.2
Part 2: The Analog Discovery 2 Oscilloscope
2.2.3
Part 3: Wiring and Reading a Digital Input
2.2.4
Part 4: Button Exercises
2.3
Configuring a Pin as an Input
2.3
Check Your Understanding
2.4
Day 3x In-Class: Observing and Fixing Bounce
2.4.1
Part 1: Observing and Fixing Bounce
2.4.2
Part 2: The Toggle Problem
2.5
Contact Bounce
2.5.1
Hardware Debouncing: RC Filter
2.5.2
Software Debouncing
2.6
State Machine Design Pattern
2.6
Check Your Understanding
2.7
Day 4 In-Class: State Machines
2.7.1
Part 1: Code Review (toggleLED.c)
2.7.2
Part 2: The State Machine Pattern
2.7.3
Part 3: Designing State Machines
2.7.4
Part 4: Implementing a State Machine in C
2.7.5
Part 5: Lab 2 Introduction
2.8
Lab 2: Switches and State Machines
2.8
Activity
3
Serial Communication: UART
3.1
Before Class: How UART Works
3.1.1
One Wire, Two States
3.1.2
The Baud Rate Problem
3.1.3
The Frame
3.1.4
Full Duplex and Pin Names
3.1.5
UART Is a General-Purpose Protocol
3.1.6
UART vs. USART
3.1.7
Check Your Understanding
3.2
In-Class Session
3.2.1
Part 1: Does printf Work?
3.2.2
Using printf in Your Code
3.2.3
Part 2: How Does the UART Driver Work?
3.2.4
Part 3: Start the Keyboard Counter
3.3
Configuring USART2 at the Register Level
3.3.1
Step 1: GPIO Alternate Function Configuration
3.3.2
Step 2: Enable the USART Clock
3.3.3
Step 3: Set the Baud Rate
3.3.4
Step 4: Enable Transmitter, Receiver, and USART
3.3.5
Sending and Receiving Data
3.3.6
Check Your Understanding
3.4
Inside uart.c: Reading the Driver
3.4.1
The Header File: uart.h
3.4.2
The Complete Implementation: uart.c
3.4.3
The Retarget Layer
3.4.4
Check Your Understanding
4
Reading Component Datasheets
4.1
In-Class: Datasheet Scavenger Hunt
4.1.1
Embedded Systems Need Peripherals
4.1.2
Datasheet Scavenger Hunt
4.1.3
Share-Out
5
Transistors: BJTs and MOSFETs
5.1
Before Class: Transistors as Controlled Switches
5.1.1
The Transistor as a Switch
5.1.2
BJTs: Current-Controlled Switches
5.1.3
MOSFETs: Voltage-Controlled Switches
5.1.4
Check Your Understanding
5.2
In Class: Design and Practice
5.2.1
Part 1: Code Interview
5.2.2
Discussion: The Receive Buffer
5.2.3
Part 2: STM32 Bit-Manipulation Macros
5.2.4
Part 3: Designing a Low-Side NPN Driver
5.2.5
Part 4: MOSFETs
5.2.6
Part 5: Reading Transistor Datasheets
6
Analog-to-Digital Conversion
6.1
Before Class: From Voltage to Number
6.1.1
The Sensor Signal Chain
6.1.2
Quantization: What the Number Means
6.1.3
The Potentiometer: A Voltage You Can Turn
6.1.4
Successive Approximation: How the Answer Is Found
6.1.5
Check Your Understanding
6.2
Day 7 In-Class: Reading a Potentiometer
6.2.1
Part 1: What the Number Means
6.2.2
Part 2: Which Pins Can Do Analog?
6.2.3
Part 3: The Test Circuit
6.2.4
Part 4a: The Registers, One at a Time
6.2.5
Part 4b: Writing ADCPot.c
6.2.6
Part 5: Wait, How Did That Clear the Bit?
6.2.7
Part 6: Successive Approximation, Live
6.2.8
Part 7: Before Next Class
6.3
Reference: The ADC Hardware
6.3.1
Inside the Converter
6.3.2
Source Resistance and Sampling Time
6.3.3
Register Summary
7
Debugging Embedded Programs
7.1
Day 7x In-Class: Debugging Tips and Tools
7.1.1
Part 1: Where Do Bugs Come From?
7.1.2
Part 2a: What the Compiler Is Trying to Tell You
7.1.3
Part 2b: One printf, Placed Well
7.1.4
Part 3a: Launching the Debugger
7.1.5
Part 3b: Watching a Register Change
7.1.6
Part 3c: The Register That Was Not Zero
7.1.7
Part 3d: Breakpoints
7.1.8
Part 4: Watching a Variable Change
7.1.9
Part 5: Choosing the Right Tool
7.2
Reference: The Debugging Method and Toolbox
7.2.1
The Scientific Debugging Method
7.2.2
Preventing Bugs
7.2.3
The Hardware Checklist
7.2.4
Diagnostic printf Patterns
7.2.5
Debugger Controls
7.2.6
The Three Tools, Side by Side
8
Timers and Interrupts
8.1
Before Class: Timers and Interrupts
8.1.1
The Cost of Waiting
8.1.2
A Circuit Whose Whole Job Is Counting
8.1.3
How Fast and How Far to Count
8.1.4
The Idea of an Interrupt
8.1.5
Check Your Understanding
8.2
Day 8 In-Class: Blinky Without Waiting
8.2.1
Part 1: Why a Background Timer
8.2.2
Part 2: Designing the 500 ms Timer
8.2.3
Part 3: Five Lines of Initialization
8.2.4
Part 4: Running blinkyTimerPolled.c
8.2.5
Part 5: Clearing Status Registers
8.2.6
Part 6: Programming an Interrupt
8.2.7
Part 7: Writing blinkyTimerInt.c
8.2.8
Part 8: Before Next Class
8.3
Reference: The TIM14 Hardware
8.3.1
Register Summary
8.3.2
Status-Register Access Types
8.3.3
The Full TIM14 Block Diagram
8.3.4
The Interrupt Mechanism, in Full
8.3.5
What Gets Stacked
8.3.6
Writing an ISR: The Rules, and
volatile
8.3.7
The Rest of the Timer Family
9
GPIO Interrupts
9.1
Before Class: Interrupts From a Pin
9.1.1
The Press That Nobody Saw
9.1.2
Timers Are Not the Only Source
9.1.3
Why a Pin’s Interrupt Needs a Front End
9.1.4
Check Your Understanding
9.2
Day 9 In-Class: A Button That Interrupts
9.2.1
Part 1: Homework Review
9.2.2
Part 2: The Press the Polled Counter Misses
9.2.3
Part 3: Why the EXTI Sits Between the Pin and the NVIC
9.2.4
Part 4a: Choosing the Port (EXTI_EXTICR2)
9.2.5
Part 4b: The Edge, the Mask, and the NVIC
9.2.6
Part 5: The Handler (Which Line Fired, and How to Clear It)
9.2.7
Part 6: Writing counterResetButtonInt.c
9.2.8
Part 7: Changing Data in an ISR, and GPIOx_BSRR
9.2.9
Part 8: Before Next Class
9.3
Reference: The EXTI, and Atomic Pin Writes
9.3.1
The Four EXTICR Registers, in General
9.3.2
Register Summary
9.3.3
Why the Mask Is Separate From the Trigger
9.3.4
ODR, BSRR, and the Pin’s Own Circuit
9.3.5
What These Rules Do and Do Not Cover
9.3.6
Bounce, Now That Edges Matter
10
I2C: A Two-Wire Bus, and a Display Driver
10.1
Day 9X In-Class: Two Wires, and a Display That Works
10.1.1
Part 1: How Many Pins Would You Need?
10.1.2
Part 2: Wire It, and Light It
10.1.3
Part 3a: How Did Two Wires Do That?
10.1.4
Part 3b: One Transaction, Captured on the Scope
10.1.5
Part 3c: Reading Your Own Trace
10.1.6
Part 4: The Numbers Transmitted on the Data Line
10.1.7
Part 5: Write One Digit Yourself
10.1.8
Part 6: Before Next Class
10.2
Before Class: The Display, and the Bus You Have Used
10.2.1
34 LEDs, and Not 34 Wires
10.2.2
What the Backpack Is For
10.2.3
Check Your Understanding
10.2.4
Check Your Understanding
10.3
Day 10 In-Class: The HT16K33 and Its Device Driver
10.3.1
Part 1: Homework Review, and Two AI Solutions
10.3.2
Part 2: Does the Bus Still Work?
10.3.3
Part 3: How the Display Is Actually Built
10.3.4
Part 4: Reading the Command Table
10.3.5
Part 5: The Display RAM, and the Byte That Is Always Zero
10.3.6
Part 6: The Pins, and Why They Must Be Open-Drain
10.3.7
Part 7a: What an I2C Library Has to Provide
10.3.8
Part 7b: Opening i2c1_byteWrite()
10.3.9
Part 8a: Between Hardware and Software Application: Hardware Interface Drivers and Device Drivers
10.3.10
Part 8b: SevenSeg_init(), and One Digit
10.3.11
Part 8c: The Rest of the Driver
10.3.12
Part 9: Break It on Purpose
10.3.13
Part 10: Coding Challenge
10.4
Reference: The Protocol, the I2C Peripheral, and the HT16K33
10.4.1
The Protocol: Two Wires, One Shared Bus
10.4.2
The Protocol: Addressing, and the ACK
10.4.3
The Protocol: START and STOP Conditions
10.4.4
The Protocol: Why There Is No Baud Rate
10.4.5
Register Summary
10.4.6
Where the TIMINGR Numbers Come From
10.4.7
What Is Under I2C1->CR2
10.4.8
I2C_CR2, Field by Field
10.4.9
The Library, and Where It Cuts Corners
10.4.10
What Else Open-Drain Buys
10.4.11
The Register Read/Write Pattern
10.4.12
HT16K33 Quick Reference
11
DC Motors and PWM
11.1
Before Class: DC Motors and Speed Control
11.1.1
How a DC Motor Works
11.1.2
Reversing Direction: The H-Bridge
11.1.3
Setting Speed: Pulse-Width Modulation
11.1.4
Check Your Understanding
11.2
Day 11 In-Class: Motors, the H-Bridge, and PWM
11.2.1
Part 1: The Actuator Chain, and What Sets the Speed
11.2.2
Part 2: The H-Bridge Reverses the Motor
11.2.3
Part 3: The TB6612 Driver and Its Truth Table
11.2.4
Part 4: First Exercise, Direction by Hand
11.2.5
Part 5: PWM Sets the Speed
11.2.6
Part 6: Second Exercise, Ramping the Motor with PWM
11.3
Day 11x In-Class: Writing the PWM Code
11.3.1
Part 1: The Timer That Makes the Waveform
11.3.2
Part 2: Which Register Does Each Job
11.3.3
Part 3: Where Those Registers Are Located in the Hardware
11.3.4
Part 4: Reading the Driver
11.3.5
Part 5: Resolution, and What Comes Next
11.4
Before Class: Counting Pulses to Measure Speed
11.4.1
How the Photointerrupter Turns Rotation Into Pulses
11.4.2
From Pulses per Second to Revolutions per Minute
11.4.3
Check Your Understanding
11.5
Day 12 In-Class: Measuring the Motor’s Speed
11.5.1
Part 1: Questions About TTmotor_ramp.c
11.5.2
Part 2: Wire the Sensor and Look at Its Output
11.5.3
Part 3: From a Pulse Train to a Number
11.5.4
Part 4: A Loop That Does Three Things at Three Rates
11.5.5
Part 5: The Whole Build
11.5.6
Part 6: Build
11.6
Reference: PWM on TIM14, the TB6612, and Counting Pulses
11.6.1
Register Summary: TIM14 in PWM Mode
11.6.2
The PWM Arithmetic
11.6.3
The TB6612 as a Lookup
11.6.4
Counting Pulses: the Arithmetic
11.6.5
Direction, and Quadrature
11.6.6
The Photointerrupter, and Its Pull-Up
11.6.7
Polling or an Interrupt
11.6.8
Where to Look Things Up
12
Accelerometers
12.1
Before Class: A Second I2C Device
12.1.1
The Bus You Already Have
12.1.2
The LSM303AGR and Its Breakout Board
12.1.3
The WHO_AM_I Register
12.1.4
Check Your Understanding
12.2
Day 13 In-Class: The Accelerometer as an I2C Device
12.2.1
Part 1: Back to I2C, a Second Device on the Bus
12.2.2
Part 2: Wire It and Verify the Link
12.2.3
Part 3: What Just Ran
12.2.4
Part 4: Put the Analyzer on It
12.2.5
Part 5: Digging Deeper
12.2.6
Part 6: Break It on Purpose
12.2.7
Part 7: The Accelerometer’s Device Driver
12.2.8
Part 8: Homework for Thursday
12.3
Day 13x In-Class: What a MEMS Accelerometer Is
12.3.1
Part 1: The Physics of a Proof Mass
12.3.2
Part 2: Accelerometers Are Everywhere
12.3.3
Part 3: The LSM303AGR, and Its Datasheet
12.3.4
Part 4: The Data Format
12.3.5
Part 5: Settings for Basic Accelerometer Operation
12.3.6
Homework for Thursday
12.4
Before Class: The Datasheet Scavenger Hunt
12.4.1
What a Stationary Accelerometer Reads
12.4.2
CTRL_REG1_A: Data Rate, Power Mode, and Axes
12.4.3
CTRL_REG4_A: Range and Resolution
12.4.4
The Rest of the Hunt
12.4.5
Check Your Understanding
12.5
Day 14 In-Class: Finish the Driver, See the Data, Measure Tilt
12.5.1
Part 2: The Settings, Committed and Revealed
12.5.2
Part 3: Verify the Wiring, Stage the Project
12.5.3
Part 4: Complete the Driver
12.5.4
Part 5: Run It
12.5.5
Part 6: Make It a Library
12.5.6
Part 7: Plot It
12.5.7
Part 8: Tilt
12.5.8
Part 9: Floating Point
12.5.9
Part 10: Lab 7
12.6
Reference: The LSM303AGR and Floating Point
12.6.1
The Differential Capacitors Inside the Sensor
12.6.2
The Register Map, and lsm303agr.h
12.6.3
The Data Format, and the Conversion in All Three Modes
12.6.4
Shifting a Signed Value Is Not Quite Dividing It
12.6.5
Calibrating the Zero-g Offset
12.6.6
Full Orientation from Three Axes (AN-1057)
12.6.7
The float Format: Precision Traded for Range
13
Servomotors
13.1
Before Class: What a Servomotor Is and How It Is Commanded
13.1.1
What Is Inside a Servomotor
13.1.2
What a Servomotor Does
13.1.3
Our Servomotor and Its Datasheet
13.1.4
How a Servomotor Is Commanded
13.1.5
Where the Servo’s Power Comes From
13.1.6
Check Your Understanding
13.2
Day 15 In-Class: Commanding a Servomotor
13.2.1
Part 1: A Motor with a Loop Inside It
13.2.2
Part 2: How We Command the Servo
13.2.3
Part 3: Designing the Timer for the Servo’s Pulse
13.2.4
Part 4: Reading the Program
13.2.5
Part 5: Design Exercise, Verifying the Pulse on the Oscilloscope
13.2.6
Part 6: Powering the Servo
13.3
Day 15x In-Class: Completing the Servo Exercise
13.3.1
Part 1: Finishing the Design Exercise
13.4
Reference: The Servo Command, the Timer Arithmetic, and the Driver
13.4.1
The Command Pulse
13.4.2
The Timer Arithmetic
13.4.3
The Driver: tim.c
13.4.4
Scaling One Integer Range onto Another
14
Photosensors and the Solar Tracker
14.1
Before Class: The Photocell and the Voltage Divider
14.1.1
What a Photocell Is
14.1.2
Sensors Based on Resistance
14.1.3
The Voltage Divider as the Interface
14.1.4
The Photocell’s Datasheet
14.1.5
What a Solar Tracker Is
14.1.6
Check Your Understanding
14.2
Day 16 In-Class: Photosensors and the Solar Tracker
14.2.1
Part 1: The Photocell in the Divider
14.2.2
Part 2: Part 1 of Lab 8, Technical Study of Photocells
14.2.3
Part 3: Solar Tracker Assembly
14.2.4
Part 4: Read "A Solar Tracker" in the Lab Handout
14.2.5
Part 5: Starting on Your Tracker
14.3
Reference: The Divider, the Two-Channel Read, and the Loop
14.3.1
The Divider and the Fixed Resistor
14.3.2
Reading Two Channels
14.3.3
The Tracker’s Update Rule
15
Bluetooth Low Energy
15.1
Before Class: Bluetooth Low Energy
15.1.1
What BLE Is For
15.1.2
BLE Connection Modes
15.1.3
Central and Peripheral
15.1.4
How a Connection Is Made: GAP
15.1.5
How Communication Happens: GATT
15.1.6
The Bluefruit Module: BLE as a UART
15.1.7
Check Your Understanding
15.2
Day 17 In-Class: Bluetooth Low Energy, then Project Work Time
15.2.1
Part 1: Using USART1 and Hardware Flow Control
15.2.2
Part 2: Your USART1 Driver, and the Passthrough Test
15.2.3
Part 3: Connecting the Bluefruit, and Its Two Modes
15.2.4
Part 4: The Final Project, and Your Paper Design
15.2.5
Part 5: Project Work Time
15.3
Reference: Talking to the Bluefruit
15.3.1
Hardware Flow Control
15.3.2
The AD2 as a UART: the Protocol Tab
15.3.3
The AT Commands, and the Module’s Timing Rules
16
Power Management
16.1
Day 17x In-Class: Project Work, and Power Management
16.1.1
Part 1: Where Does the Power Go?
16.1.2
Part 2: The Four Low-Power Modes, and How You Enter One
16.1.3
Part 3: The Watchdog
16.1.4
Part 4: A Super Quick Modification, then Project Time
16.2
Reference: The Low-Power Modes and the Watchdog
16.2.1
The Four Modes, and Their Entry Values
16.2.2
Configuring the IWDG
17
The Board Simulator
Back Matter
A
C for Python Programmers
A.1
Compiling a C Program
A.2
The Shape of a C Program
A.3
Hexadecimal and Binary Numbers
A.3
Check Your Understanding
A.4
Types and Declarations
A.5
Integer Division and Casting
A.5
Check Your Understanding
A.6
Conditions and Loops
A.6
Check Your Understanding
A.7
Functions, Prototypes and Scope
A.8
Arrays
A.9
Coding Style
B
Key STM32C031C6 Registers Reference
C
Bit-Manipulation Quick Reference
D
Setting Up STM32CubeIDE and Your Projects
D.1
Installing STM32CubeIDE
D.1.1
Step 1: Prepare for the Installation
D.1.2
Step 2: Install STM32CubeIDE
D.2
Creating Your TemplateProject
D.3
Building and Running Blinky
D.4
Firmware and ST-LINK Server Messages
D.4.1
Updating the ST-LINK Firmware
D.4.2
Installing ST-LINK Server
D.5
Starting Each New Project
D.6
Installing CoolTerm and Waveforms
E
Starter Files
F
Datasheets and Reference Documents
F.1
The STM32C031C6 and Nucleo-C031C6 documents
F.2
Component datasheets
F.2.1
Sensors
F.2.2
Light sensing
F.2.3
Transistors
F.2.4
Display, motor driver and servomotors
Index
Section
9.3
Reference: The EXTI, and Atomic Pin Writes
This section is lookup material for the registers and conventions this chapter uses. Nothing here is new. It is the same information, arranged for the moment you come back needing one fact.
đź”—
9.3.1
The Four EXTICR Registers, in General
9.3.2
Register Summary
9.3.3
Why the Mask Is Separate From the Trigger
9.3.4
ODR, BSRR, and the Pin’s Own Circuit
9.3.5
What These Rules Do and Do Not Cover
9.3.6
Bounce, Now That Edges Matter
đź”—