Home Blog Blog Details

Correct settings for C8051f120 microcontroller programs exceeding 64K

December 27 2024
Ampheo 29

Inquiry

Global electronic component supplier AMPHEO PTY LTD: Rich inventory for one-stop shopping. Inquire easily, and receive fast, customized solutions and quotes.

QUICK RFQ
ADD TO RFQ LIST
When writing programs for the C8051F120 microcontroller that exceed 64 KB, you need to configure the memory model and compiler settings correctly, as the C8051F120 uses a 8051-compatible architecture with extended memory addressing capabilities.

When writing programs for the C8051F120 microcontroller that exceed 64 KB, you need to configure the memory model and compiler settings correctly, as the C8051F120 uses a 8051-compatible architecture with extended memory addressing capabilities. Here’s a step-by-step guide:


📚 Understanding the Memory Model in C8051F120

  • The C8051F120 microcontroller has 128 KB of Flash memory and 8 KB of internal RAM.
  • The 8051 architecture inherently supports up to 64 KB of code memory and 64 KB of external data memory (XDATA).
  • Programs exceeding 64 KB require far memory pointers and proper memory banking configuration.

⚙️ Compiler and Linker Settings

1. Enable Extended Memory Model (Far Memory Access)

  • In your Keil µVision IDE:
    • Go to Project → Options for Target → C/C++ Tab
    • Set the Memory Model to Large (Far Pointers).
      • Small Model: 64 KB limit
      • Large Model: Supports more than 64 KB (far pointers)

2. Enable Far Memory Pointers

  • Ensure far pointers are enabled in your compiler directives:
    c
     
    far unsigned char myVar;
    far void myFunction(void);

3. Configure the Linker for Larger Memory Spaces

  • Go to Project → Options for Target → Linker Tab
  • Ensure the Code Banking or Extended Code Memory (xmem) option is enabled.
  • In the BL51 Locate Tab:
    • Set the Code Memory Range to reflect the entire Flash memory (e.g., C:0x0000-0x1FFFF).
    • If using banking, configure Code Banking (BANK0, BANK1, etc.).

4. Define Code Banking (If Required)

  • Add the following to your linker control file (L51_BANK.A51):
    asm
     
    ?B_MODE SEGMENT CODE
  • Enable BANKED memory mode in your startup code.

5. Modify Startup File

  • If using Keil, ensure the Startup Code (STARTUP.A51) is set up for far memory and code banking.

🛠️ Code Example for Memory Access

Accessing Far Memory in C

c
 
#include <c8051f120.h>
 
// Place a variable in far memory
far unsigned int largeArray[20000];
 
void main() {
far unsigned int i;
for (i = 0; i < 20000; i++) {
largeArray[i] = i; // Access far memory properly
}
while (1);
}

Memory Management Macros

  • Some compilers offer macros for far access:
    c
     
    #define FARDATA __far
    FARDATA unsigned char buffer[128000];

Verification Checklist

  1. Memory Model: Set to Large (Far Pointers) in compiler settings.
  2. Linker Memory Range: Extended to cover the full Flash range.
  3. Code Banking Enabled (if needed): Set up in startup files and linker scripts.
  4. Startup File Configured: Ensure it supports large memory models.
  5. Variables/Functions Defined with far: Proper access syntax used.

📝 Final Notes

  • Ensure your Linker Map File is reviewed to confirm the proper memory allocation.
  • If issues persist, consult the C8051F120 reference manual and Keil µVision documentation for specific configurations.
Ampheo