loading
7 Comments

Tell us about yourself!

Complete Your Profile
  • DebojitK commented on amandaghassaei's instructable Arduino Audio Output4 months ago
    Arduino Audio Output

    I did a test on the timings of direct port write. I used the following pins: PB1,PB0, PD7,PD6,PD5,PD4,PD3,PD2 ()leaving PD1 and PD0 for rx/tx). PORTD = (PORTD & B00000011)|((input<<2)&B11111100); PORTB = (PORTB & B11111100)|((input>>6)&B00000011);These two lines set the input on the aforementioned pins with direct bit-banging write method described. The result is astonishing- It only takes ~1.6 usec to execute these two lines. So for interrupt service routine you get ample time to do other processing.Here is the code:#include "Arduino.h"//The setup function is called once at startup of the sketchuint8_t input=100;String inputString="";void setup(){// Add your initialization code here Serial.begin(115200);}// The loop function is call...see more »I did a test on the timings of direct port write. I used the following pins: PB1,PB0, PD7,PD6,PD5,PD4,PD3,PD2 ()leaving PD1 and PD0 for rx/tx). PORTD = (PORTD & B00000011)|((input<<2)&B11111100); PORTB = (PORTB & B11111100)|((input>>6)&B00000011);These two lines set the input on the aforementioned pins with direct bit-banging write method described. The result is astonishing- It only takes ~1.6 usec to execute these two lines. So for interrupt service routine you get ample time to do other processing.Here is the code:#include "Arduino.h"//The setup function is called once at startup of the sketchuint8_t input=100;String inputString="";void setup(){// Add your initialization code here Serial.begin(115200);}// The loop function is called in an endless loopvoid loop(){//Add your repeated code here testSerialEvent(); long sTime=millis(); for(long i=0;i<100000;i++){ PORTD = (PORTD & B00000011)|((input<<2)&B11111100); PORTB = (PORTB & B11111100)|((input>>6)&B00000011); } long eTime=millis()-sTime; Serial.println(eTime); Serial.print("["); for(int i=1;i>=0;i--){ Serial.print(((PORTB&(1<<i))>>i));Serial.print(" "); } for(int i=7;i>=2;i--){ Serial.print(((PORTD&(1<<i))>>i));Serial.print(" "); } Serial.println("]"); delay(1000); //output-every loop takes 170 msec that mean one iteration takes ~1.7 usec. Its too good.}void testSerialEvent(){ while(Serial.available()){ char c=(char)Serial.read(); inputString += c; if (c == '\n') { input=inputString.toInt(); inputString=""; Serial.print("Input->");Serial.println(input); break; } }}

    View Instructable »