NanoCore12 CodeWarrior Tutorial

Note:  this tutorial assumes you have already downloaded and installed CodeWarrior on your computer.  If not, please take a moment to do so now.  The free Special Edition can be found here: It is also assumed that you have read and understood the applicable NanoCore12 manual so that you know how to correctly apply power and achieve serial communications with it.  If not, please visit the Documentation Library, here, for relevant documents:


The simple Project called NotGate ( turns on an LED while a pushbutton is pressed, and turns it off when the pushbutton is released.  If you are use the Docking Module or School Board, the pushbutton switch is SW1 (attached to NanoCore12 pin PAD07) and the LED is D2 (attached to pin PT0).  If you are using a NanoCore12 with your own breadboard or prototyping board, attach a momentary normally-open (N.O.) pushbutton switch between PAD7 and Ground, and attach the anode end of an LED to pin PT0.  Attach the cathode end of the LED to a current-limiting resistor (value anywhere in the range of 470 Ohms to 2K).  (Note:  If you are using a NanoCore12MAX (40-pin module) with the Docking Moudule, you'll need to change the code to use PT3 instead of PT0 for controlling LED D2.)
Launch CodeWarrior, and Select File/Open.  Then double-click on the project file (ending in .mcp).  The file name is NotGate_NC12.mcp.

A file directory tree will be displayed in the left-hand side of the CodeWarrior IDE.  Double-click on the file inside the Sources folder called main.c.  The contents of the file will be displayed in the primary display area.


In preparation for loading the program into your NanoCore12 module, make sure the Load/Run switch is in the Load position, and then reset it (or apply power).  This activates the on-chip Serial Monitor, which will coordinate loading of the program with CodeWarrior.

Now,  select Project/Debug (or press F5) to Make the project (i.e. compile, assemble, and link the program, producing a file in the correct format for the serial monitor).  This will launch the debugger and the serial monitor connection.

If CodeWarrior can't connect with the Serial Monitor, you will be prompted to select the Host Serial port.

Or you may see the screen below, which means:

  1. that you forgot to set the Load/Run switch to Load and press the reset button, or
  2. the serial cable is not connected, or
  3. you don't have your module powered up
Correct the problem and click Retry.
Once you have set things up properly, the Serial Monitor will erase the NanoCore12 program memory automatically, and load the new program in its place.  At that point, slide the switch to Run and press the Reset button.  Your program is now running.  The LED on PT0 will be On, and it will go off while you are pressing the pushbutton SW1.  Congratulations--  you have now accomplished digital input and digital output functions with NanoCore12 in C!



All program comments are preceded with double-slashes (//).  Comments are for the convenience of the reader, and are ignored by the compiler.
Next are some compiler directives, which are simply instructions to the compiler.  For example, #include <hidef.h> tells the compiler to look for and include the header file which includes some standard definitions.  The statement #include<mc9s12c32.h> is important because it includes a list of all the registers inside the 9S12C and their actual memory addresses.  When writing a program, you can refer to the registers by name, instead of a numerical address, making the code more readable.  These compiler directives are generated automatically by CodeWarrior when you use its Startup Wizard to create a project.  We’ll do that in a later section.

Every C program has a main function, and there is no returned value.  So, it starts with the familiar nomenclature
void main(void){
followed by one or more statements separated by the semi-colon (;), and terminated with a closing curly brace (}).
To use a digital i/o pin, you first need to define its direction (input or output) by way of its Data Direction Register.  To use bit 0 of Port T (i.e. pin PT0 on NanoCore12) as an output, you need to set the corresponding bit to logic 1.  In this example, we are only focusing on PT0, so we don’t want to influence the other pins in Port T.  Thus, we use a mask to ensure we affect only the bit of interest (i.e. DDRT0).  The statement
DDRT |= 0x01;
reads the current value of DDRT and logically ORs it with hexadecimal value 1, storing the result back into DDRT, thus defining PT0 as an output, and leaving the direction of the other pins of Port T untouched. Doing a simple equate would also work (i.e. DDRT = 0x01), but it would explicitly define the other bits of the port as well.  This is not considered good programming practice since, in a more complex program, those direction bits may get defined in some other function(s).
Defining PAD07 as an input is done through the DDRAD register, by NANDing it with a mask containing 1 in the most significant bit position.  With the Port AD pins, there is one more register that needs to be set up, since these port pins can be used as both analog and digital input.  This register is called ATDIEN (Analog-to-Digital Digital Input Enable register).  Here you enable the input driver so that reads of the PTAD register will reflect the digital value of the applied inputs.  This needs a logic 1 in the most significant position (i.e. PTAD7), so that the pushbutton can be sensed.  It takes awhile to become familiar with the register names, so studying some code examples and browsing mc9S12c32.h is recommended.
Now that the data directions for the two pins have been set up, it’s time to write the actual code to accomplish our mission (turning on the LED except when the button is pressed).  An endless loop was implemented using a For statement:
Inside the endless loop there is a simple if/else test that produces the desired results.  The port register PTAD reflects the applied logic levels present on the actual pins.  We are only interested in PAD07, so we mask off the other bits to get the result for the comparison.  The result is used to turn off or turn on the LED attached to PT0, which is directly controlled by the value we write to register PTT.
As you can see, doing simple digital input and output mainly involves masking off the bits of interest in the ports that are involved.  So it would be wise to familiarize yourself with the logical operators and comparison operators of C.