Build Blinky Program in Keil

From EdWiki

Revision as of 07:04, 16 May 2020 by Jshankar (Talk | contribs) (Add New Item to Project)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Building a First Program

Now that the toolchain and the Packs are installed, we can look at setting up a project for a typical small Cortex-M4-based microcontroller. Once the project is configured, we can get familiar with the μVision IDE, build the code, and take our first steps with the debugger.

Create New uVision Project

  • Start a new project by selecting Project New μVision project...
    This will open a menu asking for a project name and directory. You can give the project any name you want but make sure you select the path. This directory contains the “C” source code files which we will use in our project.
  • Enter your project name and click Save.

Once you have selected the project directory and saved the project name, a new dialog with a device database will be launched. Here, we must select the microcontroller that we are going to use for this project. Navigate the device database and select Tiva C Series, the “TM4C123x Series” and then the TM4C123GH6PM and click OK. This will configure the project for this device; this includes setting up the correct compiler options, linker script file, simulation model, debugger connection, and Flash programming algorithms.

  • Next, select the microcontroller to use in the project
Keil project select device.png
Fig:: Select the TM4C123GH6PM from the device database.
  • When you have selected the TM4C123GH6PM, click OK.

Now, the IDE will display the “Run-Time Environment” (RTE) Manager. The RTE allows you to select software components that have been installed through the pack system and add them to our project. This allows you to build complex software platforms very quickly. For help on any of the software components, click on the blue link in the description column.

Keil project select rte.png
Fig:: The Run-Time Environment manager allows you to add software components to your project to rapidly build a development “platform.”

For now, we need to add the minimum support for our initial project. Select the “Core” and “Startup” components.

  • In the RTE, tick the “CMSIS::Core” box and the “Device::Startup box.”
    This adds the initial Startup code and also support functions for the Cortex-M processor.

Now that we have selected all of the components required by our project, press the OK button, and the support files will be added to the project window.

  • Double click on the startup_TM4C123.s file to open it in the editor.
  • Click on the configuration Wizard tab at the bottom of the editor window (Configuration files can be viewed as Text or as configuration wizards). This converts the plain text source file to a view that shows the configuration options within the file.
Keil startup config.png
Fig:: The configuration wizard allows you to view and modify #defines within a header or source file.

Changing the values in the configuration wizard modifies the underlying source code. In this case we can set the size of the stack space and heap space.

Add New Item to Project

  • Download blinky.c file
  • Switch back to the project view and add “C” source files to project.
  • Highlight the Source Group 1 folder in the project window
  • Right click and select Add Existing Files Group 'Source Group 1'..
Keil add file to group.png

  • This will open an “Add files to Group” dialog. In the dialog, locate/select blinky.c
  • Click on Add button followed by Close button

Keil add blinky file.png

The project should now contain blinky.c and the RTE components.

Keil blinky project window.png

Set Options for Target

  • Switch to Project Window and Open the Options for Target dialog. Place the cursor on the Target 1 and right click (Alt+F7)
  • Select Options for Target "Target 1"...
Keil target options menu.png
Fig:: Open the project global options.

This can be done in the project menu by right clicking the project name and selecting “Options for Target” or by selecting the same option in the project menu from the main toolbar.

  • The “Options for Target” dialog holds all of the global project settings.
Keil target options.png
Fig: The target menu defines the project memory map.
  • Set Optimization level to 0
Keil target optimization options.png

Build Project

  • Build the project by selecting Project Build Target
Keil project build target.png

This will compile each of the “.c” modules in turn and then link them together to make a final application program. The output window shows the result of the build process and reports any errors or warnings.

Keil build output window.png
Fig:: The final program size is reported in the Build Output window.

The program size is also reported

Section Description
Code Size of the executable image
RO Data Size of the code constants in the Flash memory
RW Data Size of the initialized variable in SRAM
ZI Size on uninitialized variables in the SRAM

If errors or warnings are reported in the build window clicking on them will take you to the line of code in the editor window.