MAKE ARDUINO DIGITAL PINS SWITCH FASTER ANYTHING USING PORT MANUPULATION
This post helps to use port manipulation technique to switch
multiple ports ON or OFF at same time improves
switching speed to Micro seconds.
The Arduino ide default uses the digital Write() function for
ON or OFF the digital pins but if take a look at the backend code of the digital
write function there is lot happening that takes lot of the time nearly 50ms delay to execute.
Since it is slow and sequential in execution we can use PORT MANIPULATION technique to speed up process.
After all c code is faster than c++.
Here the avr dude compiler built in Arduino ide uses register
called DDRX means “Data direction register” the “X” replaces the
PORTS of atmega328pu. The DDRX replaces the pinMode () function on void setup (). In DDRB means data direction register of portB(pin
8 to 13) the direction of ‘0’ means ‘’INPUT’’ ‘1’ means OUTPUT.
The PORTX
used instead of digital Write ()
which is used to turn on or off all port pins same time. Here the “X” means
PORT NAME B, C, D. writing PORTB replace
digitalWrite(), Assigning the value ‘1’ means ‘’HIGH’’ and ‘0’ means ‘’LOW’’.
Check the above picture for pin mapping of which port.
Here is the code for turning pin 8 ON and OFF :
void setup() {
Serial.begin(9600);
DDRB |= B00000001;
/* this replaces pinMode(8,OUTPUT) function and pins 9 to 13 are INPUT here 6th and 7th bits are reserved hence don’t change the values of it */
}
void loop(){
PORTB |= B11000001;
/*6th and //7th bits are reserved hence don’t change the //values of it rest all 0’s are LOW and 1 is HIGH(means 8th pin HIGH). */
delay(3000);
PORTB &= B11000000;
/*6th and //7th bits are reserved hence don’t change the //values of it rest all 0’s are LOW (means 8th pin OFF along with up to 13 pin). */
delay(3000);
Serial.println(PORTB);
}
Comments
Post a Comment