The internal registers of the ATmega328P microcontroller

published: 14 December 2020 / updated 14 December 2020

Lire cette page en français

 

The ATmega328 processor

It is an 8-bit processor with RISC architecture:

Here is the internal architecture of the ATmega328 processor:

General purpose registers

Here, in blue, the registers of general use. They are 32 in number. are 8-bit registers, listed R0 through R31 .

Some can be used in 16 bits (for indirect addressing):

All registers are accessible in the SRAM memory space. For FlashForth, these are addresses between 0000 and 0031 ($ 0000 $ 001f).

To see the content of these registers:

: .num ( c --) 
    base c@ >r hex 
        0 <# # # #> type 
    r> base ! 
  ; 
: regs ( --) 
    31 for 
        r@ cr dup 3 u.r c@ .num 
    next 
  ; 

It is more than recommended to write directly to these registers.

FlashForth register usage and memory usage

FlashForth uses some of these registers:

The A and P registers are FlashForth specific registers.

Register A

This register can be used as temporary storage of data, a reference address for example. To manage several processing loops on a memory area, it can be more interesting to relieve the data stack by putting this address in this register A.

To store a value in the A register:

$00ff >a 
hex 
a> a> . . \ display ff ff 

Executions of > a do not have to be balanced with a>

The contents of the A register are not persistent after the execution of a word.

P register

This register is accompanied by a number of definitions allowing it to be used optimally:

Example:

: myStr ( --- addr len) 
    s" FORTH is a powerfull language" ; 
: toUpType ( addr len ---) 
    swap !p 
    for 
        pc@ 
        dup  [char] a  [char] z  within 
        if 
            32 - 
        then 
        emit 
        p+ 
    next 
  ; 
myStr type      \ display: FORTH is a powerfull language 
myStr toUpType  \ display: FORTH IS A POWERFULL LANGUAGE 

Good programming

Marc PETREMANN