The C language is great for beginner computer technology learners to build a better understanding of microcontroller programming and interfacing due to its close connections to the underlying hardware. Unfortunately, the C program syntax and its required supporting structure and functions can also be overwhelming for new learners, and this is why many recent beginner microcontroller boards have made the move to higher-level programming languages like Python, or use block coding.
But, these initially easier solutions come at the expense of the user having full control over the system, and make it more difficult for learners to able to build a full understanding of how the software and hardware work together. This is especially important in a multi-year or multi-semester course that leads from introductory programming and interfacing to students eventually designing, building, testing, and programming their own microcontroller circuits.
I have used a variety of techniques over the years to make it easier for beginners to build their programming skills in C, and these students then find it much easier to move to other programming languages and environments later due to their comprehensive understanding of the structure of typical C programs. Below are some tips that I have had great success with in my classroom.
Treat C expressions as sentences and paragraphs
- get students to understand that much like sentences have nouns, verbs, and end with a period, C expressions typically have nouns, verbs, and end with a semi-colon (because a period would be confused with a decimal point). For example, the C statement 'LED2 = 1;' causes the part (noun) LED2 to become equal to 1, and then the statement is done.
- multi-part statements that work together are grouped together using curly braces (similar to paragraphs). For beginners, who often have a difficult time remembering to close curly-braced structures, I find that vertically aligning the opening and closing braces makes it much easier for them to see which parts of the code work together, or if they have forgotten a closing brace.
Simplify the program code to expose only the main( ) function first
- the programs for the introductory learning activities (think beginner programs, but with coding analysis and exploration activities added – described below) are split into four files. This accomplishes two main goals: simplifying the code so beginner learners are exposed to just the code in the main( ) function first, and making the code more modular and portable (the CHRP4 activities are shared with UBMP4, and the earlier CHRP3 used a different microcontroller, but the main( ) code for all three boards is the same – only the header files and support files are different). See the CHRP4 Introductory Programming Activity 1 - Input and Output program on GitHub to view the four files that make up a project, especially looking at the CHRP4-Intro-1-Input-Ouput.c file containing the main( ) function.
- get students to understand that the main( ) code is split into two parts: one part containing statements that run only once, when the program starts, and a loop containing statements that are repeated by the microcontroller forever. This will help them to envision how the microcontroller seems to respond instantly to a button press for example, while the state of the button is actually being checked many times per second.
Promote guided exploration and real-world/fun activities
- two sections of activities have been added as comments below the main( ) code: Program Analysis Activities designed to help students both understand the program and to carefully scaffold related new concepts onto the program code, and Programming Activities designed to give students practice implementing their own solutions using related programming concepts. Adding these activities to the programs makes it easy for students to copy and paste the examples into their code, answer questions directly in the comments where they remain associated to their code for later reference (and easier grading by the instructor), and allows them to submit one file containing all of their explanations and code modifications for marking.
- the first activity introduces the process of input and output through if structures, the use of the time delay functions in MPLAB, adds exploration of logic gate concepts, and then makes it fun for students to create their own light flashing patterns and beeper sounds, as well as providing them the challenge of implementing a useful real-world equivalent of start/stop buttons.
- quick learners and more advanced students can be challenged with additional related programming tasks to keep them focused while practicing their skills. For this first activity they could be given the tasks of making a short tune using musical note frequencies for example, or exploring code sequencing by trying to make a light show on the LEDs while sounds are playing.
Slowly fold in advanced concepts
- as students progress through the rest of the introductory learning activities they will be introduced to variables, loops, and more advanced programming concepts and functions.
- an especially important feature of CHRP4 for building student understanding is the complete visibility into its function code. The header (.h) and program (.c) files for each activity are easily able to be opened, explored, and modified as the students progress, helping them develop a better understanding of software development as a whole.
Provide code templates for end-of-course challenge projects
- students will be ready for more advanced or end-of-semester challenge projects after having progressed through the introductory learning activities, but beginner learners often find a blank slate approach daunting. They can be eased into the challenges by being provided with a code template and encouraged to work on one small section at a time using the concepts they learned in the introductory activities. For example, a starter template for a Sumo robot program might look like this:
// SUMO robot code template
#include "xc.h" // Microchip XC8 compiler include file
#include "stdint.h" // Include integer definitions
#include "stdbool.h" // Include Boolean (true/false) definitions
#include "CHRP4.h" // Include CHRP4 constants and functions
// Program constant definitions
#define search 0 // Search mode
#define push 1 // Push mode
// Program variable definitions
unsigned char mode = search; // Start in search mode
// Motor function (create a motor function here)
// SONAR function (create a SONAR function here)
int main(void) // Run this code on every startup
{
OSC_config(); // Set oscillator for 48 MHz operation
CHRP4_config(); // Set up I/O ports for on-board CHRP4 devices
// Create the code to search for your opponent
while(mode == search)
{
}
// Create the code to push your opponent out of the ring
while(mode == push)
{
}
}
- in this example, students are able to see an overview of the structure of a simple Sumo robot control program. They can start by writing the code to make their robot's motors move, and then convert their code into a function for use by both the search and push code structures.
- depending on the students' skill levels and the amount of time remaining in the course, an instructor can choose to demonstrate more difficult tasks, such as the creation of a SONAR function, with the class as a whole, develop the function with a group of students, or provide a pre-made function to the class as an example (and an exemplar of good coding and commenting practices) for the students to use
A methodical curriculum combined with a simple robot built using an inexpensive circuit and parts (such as CHRP4 with its simple robot platform) will go a long way to providing new learners with a comprehensive understanding of microcontroller programming and interfacing concepts, and will make it easy for them to progress further using other microcontrollers and programming languages later in their computing careers.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.