8-bit Microcontroller MEMORY

Harvard Architecture — Separate Program & Data Memory Spaces #PIC_Series — Episode 1

J3
Jungletronics
6 min readJul 29, 2023

--

Hi, How the memory is divided in a PIC-MICROCONTROLLER?

Fig. 1. https://docs.google.com/presentation/d/e/2PACX-1vTLeYPbCk7QPnYayMtzEUBmTSO3WfFVnpQPiQzqgFxhTwb0BEWn0b4JW-EDd0RA7RAj5M0n-6VMi968/pub?start=true&loop=true&delayms=3000

The PIC16F877A is an 8-bit microcontroller from Microchip Technology.

It has a Harvard architecture, which means it has separate program memory and data memory spaces.

Let’s dive into the memory organization of the PIC16F877A:

  1. Program Memory:
  • The PIC16F877A has 14-bit program memory addresses, allowing it to address up to 2¹⁴ = 16,384 (0x4000) memory locations.
  • The program memory is used to store the firmware or program that the microcontroller will execute.
  • The program memory is non-volatile, meaning the code remains intact even when power is removed.
  • The program memory is divided into multiple memory regions:
  • The lower region (0x0000 to 0x07FF) is typically used for the actual program code and interrupt vectors.
  • The upper region (0x0800 to 0x3FFF) is typically used for additional program code, constants, and calibration data.
  • The last part (0x4000 to 0xFFFF) is either unused or reserved for other purposes, as the PIC16F877A has a maximum program memory size of 8KB (0x2000).
Video 1 — Just brings the basic…nothing too fancy…

Let’s Practice!

00#Step — With the setup from the previous episode, load the first code (blink led) and go to Read Device Memory to File:

Fig. 2.

Here is the result (edited):

:02 0000 04 0000 FA
:10 0000 00 0A12 8A11 FC2F FF3F FF3F FF3F FF3F FF3F D8
:10 0010 00 FF3F FF3F FF3F FF3F FF3F FF3F FF3F FF3F F0
....
:10 0FA0 00 FF3F FF3F FF3F 8316 0313 8801 7F30 8312 0B
:10 0FB0 00 0313 8800 0630 F200 1330 F100 AD30 F000 6A
:10 0FC0 00 F00B E02F F10B E02F F20B E02F E72F FF30 BB
:10 0FD0 00 8312 0313 8800 0630 F200 1330 F100 AD30 A5
:10 0FE0 00 F000 F00B F12F F10B F12F F20B F12F F82F 96
:10 0FF0 00 D62F 0A12 8A11 0028 8301 0A12 8A11 D32F D0
:10 1000 00 FF3F FF3F FF3F FF3F FF3F FF3F FF3F FF3F F0
....
:10 3FE0 00 FF3F FF3F FF3F FF3F FF3F FF3F FF3F FF3F E1
:10 3FF0 00 FF3F FF3F FF3F FF3F FF3F FF3F FF3F FF3F D1
:02 400E 00 7A3F F7
:10 4200 00 FF00 FF00 FF00 FF00 FF00 FF00 FF00 FF00 B6
....
:08 4000 00 FF3F FF3F FF3F FF3F C0
:00 0000 01 FF
Fig. 3. Each line structure fields analysis (Intel HEX format)

The HEX code provided is a firmware or program memory dump for the PIC16F877A microcontroller running blink led.

The code is in Intel HEX format, which is commonly used to represent binary data in a human-readable ASCII format.

Each line starts with a colon (:) followed by specific fields representing different aspects of the data.

Let’s break down the structure of each line in the HEX file:

  1. Each line starts with a colon (:) character, indicating the beginning of a new HEX record.
  2. The second field (2 digits) represents the byte count, which specifies the number of bytes in the data field.
  3. The third field (4 digits) represents the address of the first byte in the data field.
  4. The fourth field (2 digits) represents the record type, which can be one of the following:
  • 00— Data record: Contains data bytes.
  • 01 — End of file record: Marks the end of the HEX file.
  • 02 — Extended segment address record.
  • 04 — Extended linear address record.

5 . The fifth field contains the actual data bytes (the length is determined by the byte count field).

6 . The last two fields represent the checksum, which is a value calculated to ensure data integrity.

Now, let’s go break down the initial portion of the HEX code:

:02 0000 04 0000 FA
  • :02 indicates that this record contains 2 (0x02) data bytes.
  • 0000 is the starting address for this data block (0x0000).
  • 04 is the record type, which indicates an extended linear address record.
  • 0000 is the extended linear address value, in this case, indicating that the next data records will be addressed in the linear address space starting at 0x000000.
:10 0000 00 0A12 8A11 FC2F FF3F FF3F FF3F FF3F FF3F D8
  • :10 indicates that this record contains 16 (0x10) data bytes.
  • 0000 is the starting address for this data block (0x0000).
  • 00 is the record type, which indicates a data record.
  • The next 16 bytes represent the actual data in this block: 0A12 8A11 FC2F FF3F FF3F FF3F FF3F FF3F D8.
  • D8 is the checksum value calculated to ensure data integrity.

I have divided the result code myself into four parts (separated by ....):

  1. The program for blinking an LED is so simple that was stored in just 16 bits or less. Go from address 0000 to 000F;
  2. From address 0FA0 to 1000 [40004096 in decimal] we discovered maybe the bootloader or some calibration data, as it is in the second region — The upper region (see fig. 1), around 2–16 KB;
:10 0FA0 00 FF3F FF3F FF3F 8316 0313 8801 7F30 8312 0B
:10 0FB0 00 0313 8800 0630 F200 1330 F100 AD30 F000 6A
:10 0FC0 00 F00B E02F F10B E02F F20B E02F E72F FF30 BB
:10 0FD0 00 8312 0313 8800 0630 F200 1330 F100 AD30 A5
:10 0FE0 00 F000 F00B F12F F10B F12F F20B F12F F82F 96
:10 0FF0 00 D62F 0A12 8A11 0028 8301 0A12 8A11 D32F D0
:10 1000 00 FF3F FF3F FF3F FF3F FF3F FF3F FF3F FF3F F0
  1. In the address 3FF0[16.368 in decimal] is the last markationof this PIC technology; PIC16F877A goes right about half of that (8.192 bytes).

The meaning of the data will be specific to the firmware and its intended behavior on the PIC16F877A microcontroller.

01#Step — Data Memory:

  • The PIC16F877A has 8-bit data memory addresses, allowing it to address up to 2⁸ = 256 (0xFF) memory locations.
  • The data memory is used to store variables, temporary data, and special function registers (SFRs).
  • The data memory is volatile, meaning its content is lost when power is removed unless external measures like battery backup are employed.
  • The data memory is further divided into different regions:
  • General Purpose Registers (GPRs): The first 128 bytes (0x00 to 0x7F) are general-purpose registers that can be used for data storage and manipulation.
  • Special Function Registers (SFRs): The next 128 bytes (0x80 to 0xFF) are used for special functions like control and configuration of the microcontroller, controlling peripherals, and more

The memory mapping of the PIC16F877A can vary depending on the specific configuration and programming of the microcontroller. When programming the PIC16F877A, the firmware (instructions) is stored in the program memory and variables or data that the program uses are stored in data memory.

It’s important to note that when writing code for the PIC16F877A, you need to consider the memory organization, especially when accessing data or using specific registers to control the microcontroller’s behavior. The Microchip datasheet and reference manuals for the PIC16F877A provide detailed information about memory organization, special function registers, and other essential aspects of the microcontroller that help you write effective and efficient code.

02#Step — Exploring PicCkit3:

Fig. 4. Open the PicKit3 program go to Tools > Download PICkit OS
Fig. 5. Go to Microchip > PICkit v3 > pk3os*.hex…
Fig. 6. Wait for the loader…it will reset it automatically…
Fig. 7. Now click Read
Fig. 8. And the memory will be read…Click Read Device + Export Hex File to get the file…
Fig. 9. To revert to MPLAB mode, choose Tools > Revert to MPLAB mode button.

And There You Have It!

Author

@linkedin

@GitHub

Related Posts

00#EpisodePIC_Series — PIC 16–7 Awesome MPLABX Projects! — Reviewing C Language Using MPLABX with PIC16F877A

01#EpisodePIC_Series — 8-bit Microcontroller MEMORY — Harvard Architecture — Separate Program & Data Memory Spaces (this one)

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!