H8 Blinky

The 'hello world' of the embedded programming game - this simple project will just set some lights flashing on the H8 target device. Since it is the first software project here, I have shown detailed steps in creating the project workspace. Future software projects will assume you can do this.

HEW organises your work into projects within workspaces. We will first create a workspace called hewtest which will hold our projects. Open HEW and select Setup | options from the main menu. In the dialog that opens, select the workspace tab. This is where we choose the default folder where future workspaces will be created. You need only do this once. enter a suitable location for the default folder:

hew_new_folder

Once you have done this, you can create a new workspace in that folder. Select File | New workspace.. from the main menu and enter the appropriate values:

hew_new_workspace

Notice that by default HEW generates a project with the same name as the workspace. Rename the project to blinky1. We will be creating a number of projects in this workspace so give them descriptive names.

You will next be asked to say what processor is to be used. Note that it is not so straightforward to change this later.:

hew_select_processor

You can select "Finish" at this stage and HEW will generate a number of files for you and select the appropriate header file. There will seem to be quite a lot of them:

projectfiles

However, they have fairly sensible names and you can work out quite easily what each is supposed to do. At this point, you can build the project by pressing F7 or by selecting "Build" from the "Build" menu. Progress can be monitored in the output window at the bottom of the screen. You should see a successful build with no errors and no warnings. Not too surprising as the HEW has generated all the code and really should not get that wrong.

Before going any further, we need to set some build options. Select "Build | Compiler" and click the "list" tab in the dialog. Click on the "generate list file" tick box and ensure that all the options are also ticked.

buildcompilerlist

Select "OK" and open the Linker options dialog by selecting the "Linker" option from the "Build" menu. Again select the List tab and ensure that "generate map file" is selected. This creates a file with information about the size and location of all the symbols declared in your program which can be useful when you are trying to sort out some kinds of problem. It is easiest to get in the habit of turning this on now.

buildlinkerlist

Select OK and now build your project again. There should still be no errors or warnings. If you now look in the output folder (The Debug folder within the project folder) you will see a .map file and a number of .lst files, one for each source module. Have a look at each of these and try to make some sense of what each one contains. You may not be be any the wiser but you may as well find that out for yourself.

Open main.c and you will find it contains a little bit of (useless) code created by the project generator. You can delete everything after the #include line and replace it with the following:

unsigned char count; 
int temp;

int main (void)
{
  IO.PCR8= 0xFF; // all bits are output
  while (1)
  {
    count++;
    IO.PDR8.BYTE = count;
    temp = 10000;
    while (temp) 
    {
      temp--;
    }
  }
  return (0); 
}

This little section of code will just put a constantly increasing binary value out to IO port 8.

If you have left all the other settings at their default value, you will have generated an output file with the extension .mot in the Debug folder of your project. This is the file that gets loaded into the flash memory of the tartget board.

To download the image to the target board, start the FDT in Basic mode and select the output file that you have created.

fdt_basic_select

Connect a serial lead to the target and place the target board into boot mode. Generally this means changing a slide or toggle switch to pull NMI followed by pressing reset. Now Click the Program Flash button and you should see messages indicating that the program has transferred. This can go wrong. If it does, first check the settings under options from the main menu. Ensure the board really is in boot mode - cycle the modes and/or power if necessary. Check your serialconnection. Although the bootloader is simple and reliable, there are many ways things can go wrong so check and double check. For example, if you had selected the wrong processor earlier on, your code may not download and, even if it did, it may not run. Remember to disconnect after downloading.

All being well, you can switch the processor back into normal mode and a press of reset will bring the program to life. LEDs connected to port 8 will begin to flash. The least significant bit - P80 - flashes at about 30Hz on my target board which runs at 16MHz. Each more significant bit will flash at half the rate of the one below. Thus P87 should take about 4 seconds to cycle. If you have changed any other settings, your timings will probably vary.

Making this project work is a vital first step to checking out your target board and that you are using the tools correctly. Don't move on until it works. Once you have it working you can modify the code to see what happens.. For example:

If we start temp with a value of 1000 do the lights flash ten time faster?

Comments are closed.