Arduino Due Port Manipulation

Hi there,

I am new to programming with the Arduino Due, and I have been tasked with updating some code from a previous project. I wish to set the first eight bits of port C to output and then write 0x04 to those corresponding bits. I have seen similar posts to this, but I haven't seen any answers that write data to a certain number of bits on a port.

I have written:

  1. DDRC = b11111111
  2. PORTC = 0x04

I am confident that the first line achieves what I want (setting the first eight bits of port C to output). I believe the statement in the second step writes 0x04 to all of the bits that are in port c, but I wish to only write 0x04 to the first eight bits of port C. Any suggestions as to how to do this?

Thank you

this board has other processor and its registers named different

Hi @cm392420

Here's an Arduino Due example writing to the first 4 bits of PORTD:

void setup() {
  PIOD->PIO_OWER = 0xFFFFFFFF;     // Enable writes to whole of PortD's 32-bit, Output Data Status (ODSR) register: (B11111111111111111111111111111111)
  PIOD->PIO_OER =  0x0000000F;     // Set the lowest 4-bits of PortD to outputs: (B00000000000000000000000000001111)
}

void loop() {   
  PIOD->PIO_ODSR = PIO_ODSR_P0;   // Set the output on P0 (digital pin 25) high, other outputs low: (B00000000000000000000000000000001)
  PIOD->PIO_ODSR = PIO_ODSR_P1;   // Set the output on P1 (digital pin 26) high, other outputs low: (B00000000000000000000000000000010)
  PIOD->PIO_ODSR = PIO_ODSR_P2;   // Set the output on P2 (digital pin 27) high, other outputs low: (B00000000000000000000000000000100)
  PIOD->PIO_ODSR = PIO_ODSR_P3;   // Set the output on P3 (digital pin 28) high, other outputs low: (B00000000000000000000000000001000)
}
1 Like

DDR and PORT are AVR registers.
As @kolaha said, the DUE does not have an AVR processor!

On the SAM3X8E, the ports are called PIO:

So, as @MartinL showed, it's the PIO registers you need to be using ...

Would writing 0x04 to a port just be setting the third bit high? Ie B0000000000000000000000000000100

(1<<3) = 0x04

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.