Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings 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\,}$}}}
\)
Subsection 7.1.1 Part 1: Where Do Bugs Come From?
Open your old Blinky project in the STM32CubeIDE.
Click build and upload to your Nucleo. Make sure it still runs.
Weโll need it when we get to the debugger.
Where do bugs come from?
What the compiler is trying to tell you
One printf, placed well
The debugger, hands-on: watch a register change
Watch a variable change
Which tool, when, then Lab 4
Activity 7.1.1 . Whose Bugs Are These?
Think back over the programming you have done, in any language.
What kinds of errors have you made? Enter as many as you can.
Bugs in C programs come from a short list of slips, and the same few account for most of the time we will spend debugging in this course:
Missing or extra semicolon, ), or }
Incorrect register name
Missing #include files
Incorrect constant (#define) values
Undeclared or uninitialized variables
Misspelled names
Type mismatches
No return statement in a function
= or ==? & or &&? | or ||? ~ or !?
Alas, many othersโฆ
Some of these will be caught by the compiler. The ones that are not caught are the dangerous ones: they compile cleanly and then run incorrectly.
A few habits prevent a large part of that list before it happens: plan the design on paper before typing; write in small chunks and test each one before moving on; give every function one clear job; use named constants instead of magic numbers; and keep the code readable, with descriptive names. The full checklist, with the reasoning behind each item, is in
Subsectionย 7.2.2 .
Activity 7.1.2 . Which Habit Would Have Caught It?
Pick one error from your list.
Which good habit would have caught it?
Most of what you entered is on this list:
Missing or extra semicolon, ), or }
Incorrect register name ยท misspelled names
Missing #include ยท wrong #define value
Undeclared or uninitialized variables ยท type mismatches
No return statement in a function
= or ==? & or &&? | or ||? ~ or !?
Alas, many othersโฆ
Habits that prevent errors:
Plan the design on paper first: block diagram, pseudocode, state diagram.
Write in small chunks; test each before moving on.
One clear job per function: โset up the UARTโ, โget a value from the ADCโ.
Named constants, not magic numbers.
Readable code with descriptive names, and comments as you go.