Loading...
Pages: 1 [2]   Go Down
Author Topic: Single pin Port Manipulation  (Read 797 times)
0 Members and 1 Guest are viewing this topic.
Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 319
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Results:
Code:
Time for digitalWrite(): 4060
Time for true c command: 348
Time for true c command using #define: 348
Time for portWrite(): 2740
Logged


Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 319
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems most of the time is going through the switch();. If I take out the pins I'm not using...
Code:
void portWrite(int pin, int state){
  if (state == 1){ //write HIGH
  switch (pin){
    case 13: PORTB = PORTB | 0x20; break;
  }
  } else {// write low
  switch (pin){
    case 13: PORTB = PORTB & 0xDF; break;
    }
  }
}

...the speed results are impressive:
Code:
Time for digitalWrite(): 4060
Time for true c command: 348
Time for true c command using #define: 348
Time for portWrite(): 352

Logged


Austin, TX
Offline Offline
Faraday Member
**
Karma: 41
Posts: 5171
CMiYC
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You might be interested in this work on making a "fastDigitalWrite()"...

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1267553811/all
Logged

www.cmiyc.com - A guide to being an Enginerd

Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 319
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks again James. The digitalWriteFast beats all the other options!

Code:
Time for digitalWrite(): 4060
Time for true c command: 348
Time for true c command using #define: 348
Time for portWrite(): 2744
Time for digitalWriteFast(): 288
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 118
Posts: 10158
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


And, one more...
static void portWrite(int pin, int state){
Logged

Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 319
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What exactly does 'static' do? I ran my little test again, and there was no change in the speed.
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 118
Posts: 10158
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I ran my little test again, and there was no change in the speed.

Odd.  I expected the optimizer to inline portWrite.  Oh well.

Quote
What exactly does 'static' do?

Informs the compiler that the function is only used within the one compilation unit.  In other words, it gives the compiler permission to be more aggressive when optimizing.
Logged

Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 319
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have just one more semi-related question...

If the internal 8MHz clock is used on a 328, can the crystal pins be used for I/O using port manipulation?
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 118
Posts: 10158
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Yes.
Logged

Pages: 1 [2]   Go Up
Print
 
Jump to: