Here are two programs that use direct writes to turn on and off LED's connected to an Arduino Due.
The first example, controls LEDs on pins 2 - 6. Takes one or two direct writes since pin2 is on port B and pins 3 - 6 are on port C.
the second example moves the LEDS to pins 3 - 7. This can be done with one direct write since all the pins are on port C
//EXAMPLE1
//example of blinking LEDs connected to pins 2 - 6 on an Arduino DUE
// this uses 1 or 2 direct writes, if pin 2 is included takes 2 direct writes.
// if you can move to pins 3 - 7 it only needs one direct write, see next example
unsigned char pcin = 31; //simulate input from Serial.read
unsigned char pcinr = 0; //working variable for pin selection
unsigned long mypin2 = (1u << 25); // sets 32bit mypin2 to be Sam's pin value for Arduino Due pin2. (combined later with port selection.
void setup() {
// put your setup code here, to run once:
//Serial.begin(115200); //to see the pcin & pcinr values, uncomment here and the Serial.write below
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
delay(2000);
}
void loop() {
// Note the pin mapping on the Arduino Due. See the pin map from the Arduino.cc product page, Arduino Due
// Arduino Pin Sam/Due internal pin
// 2 PB25 B register pin 25
// 3 PC28 C register pin 28
// 4 PC26 C register pin 26 NOTE: gap of one between 3 & 4
// 5 PC25 C register pin 25
// 6 PC24 C register pin 24
pcinr = 0;
if (pcin & B00010000) { // Pin 6 PC24
pcinr |= B00000001; // reverse to because the assigned pins PC28 - PC24 count down
}
if (pcin & B00001000) { // Pin 5 PC25
pcinr |= B00000010;
}
if (pcin & B00000100) { // Pin 4 PC26
pcinr |= B00000100;
}
// skips PC27
if (pcin & B00000010) { // Pin 3 PC28
pcinr |= B00010000;
}
if (pcin & B00000001) { // Pin 2
REG_PIOB_SODR = mypin2; // PIOB means port B. So needs to be separate. SODR is the write HIGH register
}
REG_PIOC_SODR = (((unsigned long)(pcinr)) << 24); // write Pins 3 - 6 in PIOC, port C.
delay(1000);
if (pcin & B00000001) {
REG_PIOB_CODR = mypin2; // CODR to write LOW
}
REG_PIOC_CODR = (((unsigned long)(pcinr)) << 24);
//Serial.println(pcin,BIN);
//Serial.println(pcinr,BIN);
delay(1000);
}
Here is example 2:
//EXAMPLE2
//example of blinking LEDs connected to pins 3 - 7 on an Arduino DUE
// this uses 1 direct write.
unsigned char pcin = 31; //simulate input from Serial.read
unsigned char pcinr = 0; //working variable for pin selection
unsigned long mypin2 = (1u << 25); // sets 32bit mypin2 to be Sam's pin value for Arduino Due pin2. (combined later with port selection.
void setup() {
//Serial.begin(115200); //to see the pcin & pcinr values, uncomment here and the Serial.write below
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
delay(2000);
}
void loop() {
// Note the pin mapping on the Arduino Due. See the pin map from the Arduino.cc product page, Arduino Due
// Arduino Pin Sam/Due internal pin
// 2 PB25 B register pin 25
// 3 PC28 C register pin 28
// 4 PC26 C register pin 26 NOTE: gap of one between 3 & 4
// 5 PC25 C register pin 25
// 6 PC24 C register pin 24
pcinr = 0;
if (pcin & B00010000) { // Pin 7 PC23
pcinr |= B00000001; // reverse to because the assigned pins PC28 - PC23 count down
}
if (pcin & B00001000) { // Pin 6 PC24
pcinr |= B00000010;
}
if (pcin & B00000100) { // Pin 5 PC25
pcinr |= B00000100;
}
if (pcin & B00000010) { // Pin 4 PC26
pcinr |= B00001000;
}
if (pcin & B00000001) { // Pin 3 Sam/Due internal PC28, skip one form pin 4
pcinr |= B00100000;
}
REG_PIOC_SODR = (((unsigned long)(pcinr)) << 23); // write Pins 3 - 7 HIGH in PIOC, port C.
delay(1000);
REG_PIOC_CODR = (((unsigned long)(pcinr)) << 23); // write LOW
//Serial.println(pcin,BIN);
//Serial.println(pcinr,BIN);
delay(1000);
}