0
\$\begingroup\$

I have written a piece of code which manipulates the DDR for port B:

#include <avr/io.h>

int main(void)
{
    DDRB = 0;

    DDRB = 1;
    DDRB = 2;
    DDRB = 4;
    DDRB = 8;
    DDRB = 16;
    DDRB = 32;
    DDRB = 64;
    DDRB = 128;

    return 0;
}

I'm using the simulator and I'm having a hard time in figuring out how this maps in the I/O view, and what will happen on the actual device. I'm stepping over the DDRB=1..128 instructions.

As expected, DDRB values in the I/O view go from 0x1 to 0x20 as I execute the first 6 instructions, DDRB=1..32. However the other two instructions DDRB=64,128 have the effect of setting DDRB in the I/O view to 0x0. In a way this is not entirely surprising, as the ATtiny85 has only 6 PB pins. But is this behavior expected?

The real question is: what are the little squares in the I/O view next to the value of DDRB saying? Here is the state of DDRB in I/O view after each single instruction in main:

DDRB=  0;  --> DDRB    0x37    0x0    000000
DDRB=  1;  --> DDRB    0x37    0x1    000000
DDRB=  2;  --> DDRB    0x37    0x2    000000
DDRB=  4;  --> DDRB    0x37    0x4    000001
DDRB=  8;  --> DDRB    0x37    0x8    000010
DDRB= 16;  --> DDRB    0x37    0x10   000100
DDRB= 32;  --> DDRB    0x37    0x20   001000
DDRB= 64;  --> DDRB    0x37    0x0    000000
DDRB=128;  --> DDRB    0x37    0x0    000000

Also, if I hover the mouse on the little squares, their names range from Bit 2 (the rightmost) to Bit 7 (the leftmost). How does this map to the pins of the real device? and also Why are they named from 2 to 7 instead than from 0 to 5, as on the device (PB0-PB5)?.

I thought initially that the configuration fuses in the simulator are responsible for certain bits of DDRB not being programmable. For instance I know that RSTDISBL must be unprogrammed to enable PB5, and I guess the same is true for the corresponding DDR. But which column is PB5 on the I/O view?.

I also tried to set the fuses in C code:

FUSES =
{
    .low = 0x62,
    .high = 0x5f,
    .extended = 0xff
};

The setting 0x5f for the high fuse should mean that RSTDISBL is disabled, according to the online fuse calculator. However the behavior in I/O view is still the same.

\$\endgroup\$
1
  • \$\begingroup\$ This is embarassing, but basically the reason of most of my torment is that on the 10" screen of my netbook the Bits column in the I/O View is simply too narrow, and shows only the 6 most significant bits (2-7). Expanding the column shows the existence of the first two bits, Bit 0 and Bit 1. \$\endgroup\$
    – damix911
    Feb 22, 2014 at 17:04

2 Answers 2

1
\$\begingroup\$

In a way this is not entirely surprising, as the ATtiny85 has only 6 PB pins. But is this behavior expected?

According to the ATtiny85 datasheet

enter image description here

bits 7:6 of PORTB, DDRB and PINB registers are read only locations with an initial value of 0.
So essentially you are writing to a location that the manufacturer says it is read only in which case all bets are off regarding the expected behaviour. It may work fine or it may cause problems.

what are the little squares in the I/O view next to the value of DDRB saying? But which column is PB5 on the I/O view?.

Each square is one bit, starting from 0 (right-most side) to 7 (left-most side). My version is slightly older (v6.x) and shown the bit numbers fine

enter image description here

enter image description here

I have added with red the bit representation of each box


Regarding the fuses, I don't think they have any effect on the simulation and you can't change them at runtime either.
Adding them in the code (I have never used that way) is just a way to tell the programmer to set them at a specific value during the device programming, it doesn't have an effect during the code execution.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your explanation, very clear. And especially thanks for the screenshots, which led me to understand that there was something wrong with the interface of Atmel Studio on my screen. For the fuses, my understanding is that the C code simply adds custom sections the ELF file; programmers and probably some emulators (not the simulator, apparently) inspect the sections and decide what to do with the fuses. I agree that they have no effect at runtime. \$\endgroup\$
    – damix911
    Feb 22, 2014 at 17:09
1
\$\begingroup\$

The ATtiny85 has no pin PB6 and PB7 so you are trying to address hardware pins that simply aren't there. According to the datasheet (paragraph 10.4.2 - 10.4.4) these bits for DDRB, PINB and PORTB are always 0.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for pointing me to the right page in the documentation. I knew it had to be something like that but since Bit 0 and Bit 1 were not visible on my screen I started to question whether I was interpreting the bits in the right way. \$\endgroup\$
    – damix911
    Feb 22, 2014 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.