Skip to main content

Command Palette

Search for a command to run...

How to run microcontroller program in Keil?

Published
3 min read
How to run microcontroller program in Keil?

To run a microcontroller program in Keil µVision, you usually do one of two things:

  1. Run on real hardware (flash + debug via ST-LINK/J-LINK/ULINK)

  2. Run in the simulator (only for some MCUs, with limitations)

Here’s the practical workflow.


1) Create / open the project

  • Open µVisionProject → Open Project… (.uvprojx)

  • Or Project → New µVision Project… and select your MCU.

2) Add startup + CMSIS + device packs (if needed)

  • Pack Installer (left side) → install the pack for your MCU (e.g., STM32F4xx).

  • Make sure you have:

    • Startup file (startup_*.s)

    • System file (system_*.c)

    • CMSIS headers
      (Keil packs usually provide these.)

3) Build the program

  • Click Build (hammer icon) or press F7

  • Fix any errors until you get 0 Errors.

4) Configure the debug adapter

Go to Project → Options for Target…

Debug tab

  • Select your debugger:

    • ST-Link Debugger (common for STM32)

    • J-LINK / ULINK if you have them

  • Click Settings…

    • Choose interface: SWD (recommended) or JTAG

    • Set SWD clock (start lower like 1 MHz if connection is flaky)

Utilities tab

  • Check Use Target Driver for Flash Programming

  • Select the correct flash algorithm for your chip (Keil usually auto-adds it from the pack)

5) Start debugging (“run”)

  • Click Start/Stop Debug Session (or Debug → Start/Stop Debug Session)

  • Keil will:

    • connect to the target

    • download (flash) the program

    • halt at main() (depending on settings)

6) Run / step

While in debug mode:

  • Run: F5

  • Step: F11 (step into), F10 (step over)

  • Reset: Ctrl+F5

  • Set breakpoints by clicking left margin.


B) Run in Keil Simulator (if supported)

Some MCUs/cores are supported by the built-in simulator, but most STM32 peripheral behavior is limited.

  1. Options for Target → Debug

  2. Select Use Simulator

  3. Start debug session

  4. Use Run/Step as above

Good for:

  • basic C logic

  • CPU register inspection
    Not great for:

  • real UART/I2C/SPI, clocks, DMA, etc.


Common gotchas (and fixes)

“Cannot connect to target”

  • Make sure SWDIO/SWCLK/GND are connected (and NRST helps a lot)

  • Lower SWD clock in Debug settings

  • Use “Connect under reset” if available

  • Ensure board is powered and grounds are common

Program downloads but doesn’t “run”

  • Check clock setup (bad PLL config can lock up)

  • Verify you’re not stuck in HardFault (use call stack)

  • Ensure your main() is reached (breakpoint)

Flash programming fails

  • Wrong flash algorithm selected (Utilities tab)

  • Read-out protection enabled on some chips (need mass erase)