donderdag 8 december 2011

Arduino performance with digitalWrite alternative

Arduino language is no more than a high level, java like wrap around the C language. Still you can simply include C libraries or use even C code in your arduino sketch. For some operations this is very useful!. 

Ever checked how much cycles digitalWrites actually take? You would be surprised!

digitalWrite(9,HIGH); // 57 clock cycles
digitalWrite(9,LOW); // 57 clock cycles
digitalWrite(8,value); // 57 clock cycles

Solution is to write these instructions in C code directly. It's the exact same result but about 25 times as fast. An example:

bitSet(PORTB,1); // 2 clock cycles
bitClear(PORTB,1); // 2 clock cycle
bitWrite(PORTB,0,value); // 3 clock cycles

Geen opmerkingen:

Een reactie plaatsen