Go Down

Topic: Fast DigitalWrite for Arduino Due (Read 2456 times) previous topic - next topic


Is there a method for increasing the speed of setting a digital pin to High or Low?  I tried the following on the Due unsuccessfully - unfortunately it was made for the 8-bit boards:



Why? Maybe if you described your actual problem or posted some code we could help with that.
"The problem is in the code you didn't post."


Apr 12, 2015, 12:18 am Last Edit: Apr 12, 2015, 12:18 am by MarkT
There are various threads on here for direct port manipulation on the Due - have
you searched for them?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


There are various threads on here for direct port manipulation on the Due - have
you searched for them?
One such thread is http://forum.arduino.cc/index.php?topic=129868.15.
Essentially you just have a pointer to a structure, which has to long members, one that clears and one that turns on the pin. Here is the quote of the post that shows how this works.
The other way of doing direct port manipulation looks like this:
Code: [Select]
long r = PIOx->register;

where x is A,B,C or D and register is as in the data sheet

For example:
Code: [Select]

PIOB->PIO_SODR=1<<27; //lights the LED
PIOB->PIO_CODR=1<<27; //clears it
boolean p12=!!(PIOD->PIO_PDSR & (1<<8)); //read pin 12

I had a go at writing some digitalReadDirect/digitalWriteDirect functions but you might want to give them some more thorough testing as I only checked if it worked with the LED   :)

Code: [Select]

inline void digitalWriteDirect(int pin, boolean val){
  if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;

inline int digitalReadDirect(int pin){
  return !!(g_APinDescription[pin].pPort -> PIO_PDSR & g_APinDescription[pin].ulPin);

void setup() {               
  pinMode(13, OUTPUT);     
  pinMode(12, INPUT);     

void loop() {
  digitalWriteDirect(13, HIGH);   
  digitalWriteDirect(13, LOW);   

Go Up