Electronic – Understand Atmega168P Datasheet

atmegaavrerror

I am in the process of reading through the above mentioned doc from here.

In page-21, second paragraph in section 8.5 I/O Memory, line 6 is given as below:

When using the I/O specific commands IN and OUT, the I/O addresses 0x00 – 0x3F must be used.

However, in page-224, row 1 of table, first column, Assembly Code Example has the last line as follows:

out TWCR, r16

But as per page-21 statment, above instruction is wrong as TWCR has memory address 0xBC as given in page-396 row 6.

Is there something wrong here, or am I understanding it wrong?

Please help.

Edit: definitions file

Best Answer

You're understanding it wrong. Yes, TWCR has a memory address of 0xBC, but that isn't its IO address.

There are two ways of accessing the port - through IO, and through memory. In C, when you assign a value to that address it changes the address to be the IO address instead and uses an in or an out instruction rather than load or store instructions.

-- edit --

Looking at the definitions file you have posted, there is this little bit:

; ***** I/O REGISTER DEFINITIONS *****************************************
; NOTE:
; Definitions marked "MEMORY MAPPED"are extended I/O ports
; and cannot be used with IN/OUT instructions
....
.equ    TWCR    = 0xbc  ; MEMORY MAPPED

I suspect actually that the data sheet is incorrect. It wouldn't be surprising if that whole section about the TWI were copied verbatim from another data sheet whose TWI interface is IO mapped not memory mapped, such as the ATMega32. It's not uncommon.

Related Topic