help using DDRB

hello i want to ask about this program on arduino UNO

 //Test DDRB
#define LEDMASK 0x01
void setup()
{
unsigned char *portDDRB;
portDDRB = (unsigned char *)0x24;
*portDDRB |= LEDMASK;
}
void loop()
{
unsigned char *portB;
portB = (unsigned char *)0x25;
*portB |= LEDMASK;
delay(2000);
*portB &= (~LEDMASK);
delay(1000);
}

when i run this i though port 10 and 13 always on(from 0x25) and port 8 will be switch between on atau off, but i somehow it onlyworked on port 8. port 10 and 13 is always off with this code, what did i miss?

what is "DDRB"?

gcjr:
what is "DDRB"?

DDRB - Data Direction Register for Port-B of ATmega328P MCU. When we place HIGH at DDRB0-position, the corresponding port line (the PB0) of Port-B works as an output line. The Arduino code is: pinMode(8, OUTPUT);. The following diagram (shown for a bit-2 of Port-D) may help to understand the role of DDRX (X = B, C, D) Register.
pd2.png

pd2.png

If you used the predefined macros for the registers (PORTB, DDRB, PINB) etc, you'd get the correct volatile qualifications for free.

I don't think I'll ever understand this style of programming

unsigned char *portDDRB;
portDDRB = (unsigned char *)0x24;

Please post the actual code that you used to drive pin 10 or 13.

aarg:
Please post the actual code that you used to drive pin 10 or 13.

that's all of the code

 unsigned char *portDDRB;
portDDRB = (unsigned char *)0x24;

did you mean this? from what i understand from these line is it is used to set pin 10 and 13 as output then

unsigned char *portB;
portB = (unsigned char *)0x25;

set pin 10 and 13 as high

No,those lines are simply setting up the pointers to the registers.

Ajeb22:
that's all of the code

 unsigned char *portDDRB;

portDDRB = (unsigned char *)0x24;




did you mean this? from what i understand from these line is it is used to set pin 10 and 13 as output then



unsigned char *portB;
portB = (unsigned char *)0x25;




set pin 10 and 13 as high

Then you do not understand. Pin 10 and 13 would be set with:

#define LEDMASK 0x24

TheMemberFormerlyKnownAsAWOL:
No,those lines are simply setting up the pointers to the registers.

Is there any register named portDDRB in the ATmega328P?

GolamMostafa:
Is there any register named portDDRB in the ATmega328P?

No, but that's OK, because it is simply a variable the OP invented.

volatile unsigned char* portDDRB = (unsigned char*) 0x24;
volatile unsigned char* portB    = (unsigned char*) 0x25;
const unsigned char LED_MASK     = 0x20; // bit 5, pin 13
void setup() 
{
  *portDDRB |= LED_MASK;
}

void loop() 
{
  *portB |= LED_MASK;
  delay (2000);
  *portB &= ~LED_MASK;
  delay (1000);  
}

(compiled and tested on Duemilanove/168)

Or, alternatively

volatile unsigned char* portINB  = (unsigned char*) 0x23;
volatile unsigned char* portDDRB = (unsigned char*) 0x24;
const unsigned char LED_MASK     = 0x20; // bit 5, pin 13
void setup() 
{
  *portDDRB |= LED_MASK;
}

void loop() 
{
  *portINB = LED_MASK;
  delay (500);
  *portINB = LED_MASK;
  delay (500);
}

TheMemberFormerlyKnownAsAWOL:
No, but that's OK, because it is simply a variable the OP invented.

That two lines of codes do not set the direction of DPin-13 as output (verified in UNO).

1 Like

GolamMostafa:
That two lines of codes do not set the direction of DPin-13 as output (verified in UNO).

No-one (except perhaps the OP) ever said that they did.
See reply #6

The OP is setting up his own variables to point to the registers, instead of using the predefined DDRB and PORTB. LEDMASK is the variable that specifies which specific ports to set as output and turn on/off.

TheMemberFormerlyKnownAsAWOL:
No,those lines are simply setting up the pointers to the registers.

i think i get it now, thank you!

the truth is this is not my code, i just found and try to understand it cause i never try those kind of i/o