Author Topic: Slow arduino core?  (Read 2936 times)

0 Members and 1 Guest are viewing this topic.

Offline Razor ConceptsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Slow arduino core?
« on: May 25, 2009, 05:14:52 PM »
I am shifting out 288 bits into a TLC5947 pwm driver. There are 12 bits of resolution (24 outputs x 12 bits  = 288). 12 bits so 4096 possibilities. I am using Arduino for the sake of fast prototyping... but the code itself seems to be very slow. To test it out I am shifting out 288 bits 4096 times, code shown below:

Code: [Select]
int latchPin = 7;
int clockPin = 5;
int dataPin = 6;
void setup() {
  pinMode(latchPin, OUTPUT);
   pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
void loop()
{
  long a;
  for(a = 0; a < 4096; a++)
  {
    digitalWrite(latchPin,LOW);
    shiftOut(a);
    digitalWrite(latchPin,HIGH);  
  }
}



// the heart of the program
void shiftOut(long myDataOut)
{
  int pinState;
  digitalWrite(dataPin, 0);
  digitalWrite(clockPin, 0);
  int outPut;
  for (outPut=0; outPut < 24; outPut++)  //24 different outputs
  {
    for(int val = 11; val >= 0; val--)    //12 bit number
    {
digitalWrite(clockPin, 0);
if ( myDataOut & (1<<val) )
{
 pinState = 1;
}
else
{
 pinState= 0;
}

digitalWrite(dataPin, pinState);
digitalWrite(clockPin, 1);
digitalWrite(dataPin, 0);
    }
  }
  //stop shifting
  digitalWrite(clockPin, 0);
}

However, going through the entire for loop takes roughly 25 seconds! Is there something particular that is causing this slowness or is simply turning all those pins high and low that many times slowing everything down. Since this is a fairly simple code I am thinking of switching over to true-c with AVR studio 4, with faster pin switching.

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: Slow arduino core?
« Reply #1 on: May 25, 2009, 06:23:18 PM »
I am shifting out 288 bits into a TLC5947 pwm driver. There are 12 bits of resolution (24 outputs x 12 bits  = 288). 12 bits so 4096 possibilities. I am using Arduino for the sake of fast prototyping... but the code itself seems to be very slow. To test it out I am shifting out 288 bits 4096 times, code shown below:

Code: [Select]
int latchPin = 7;
int clockPin = 5;
int dataPin = 6;
void setup() {
  pinMode(latchPin, OUTPUT);
   pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
void loop()
{
  long a;
  for(a = 0; a < 4096; a++)
  {
    digitalWrite(latchPin,LOW);
    shiftOut(a);
    digitalWrite(latchPin,HIGH);  
  }
}



// the heart of the program
void shiftOut(long myDataOut)
{
  int pinState;
  digitalWrite(dataPin, 0);
  digitalWrite(clockPin, 0);
  int outPut;
  for (outPut=0; outPut < 24; outPut++)  //24 different outputs
  {
    for(int val = 11; val >= 0; val--)    //12 bit number
    {
digitalWrite(clockPin, 0);
if ( myDataOut & (1<<val) )
{
 pinState = 1;
}
else
{
 pinState= 0;
}

digitalWrite(dataPin, pinState);
digitalWrite(clockPin, 1);
digitalWrite(dataPin, 0);
    }
  }
  //stop shifting
  digitalWrite(clockPin, 0);
}

However, going through the entire for loop takes roughly 25 seconds! Is there something particular that is causing this slowness or is simply turning all those pins high and low that many times slowing everything down. Since this is a fairly simple code I am thinking of switching over to true-c with AVR studio 4, with faster pin switching.


The digitalWrite function is known to be slow (I read it multiple times on the arduino.cc forum itself). You can use the tlc5940 library, it is quite fast and uses bitbang or SPI to shift data in the register.

http://code.google.com/p/tlc5940arduino/

Offline Razor ConceptsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: Slow arduino core?
« Reply #2 on: May 25, 2009, 08:15:21 PM »
Thanks. I just switched to "true c" statements and it works great now.
Code: [Select]
int latchPin = 7;
int clockPin = 5;
int dataPin = 6;
void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
void loop()
{
  long a;
  for(a = 0; a < 4096; a++)
  {
    PORTD &= ~(1<<7);//digitalWrite(latchPin,LOW);
    shiftOut(a);
    PORTD |= (1<<7);//digitalWrite(latchPin,HIGH); 
    //delayMicroseconds(600);
  }
}



// the heart of the program
void shiftOut(long myDataOut)
{
  int pinState;
  //PORTD &= ~(1<<6);//digitalWrite(dataPin, 0);
  //PORTD &= ~(1<<5);//digitalWrite(clockPin, 0);
  int outPut;
  for (outPut=24; outPut >= 0; outPut--)  //24 different outputs
  {
    for(int val = 11; val >= 0; val--)    //12 bit number
    {
      PORTD &= ~(1<<5);//digitalWrite(clockPin, 0);
      if ( myDataOut & (1<<val) )
      {
        pinState = 1;
      }
      else
      {
        pinState= 0;
      }

      if(pinState == 0)
        PORTD &= ~(1<<6);//digitalWrite(dataPin, pinState);
      else
      {
        if(outPut == 0)
        PORTD |= (1<<6);
        else
        PORTD &= ~(1<<6);
      }
      PORTD |= (1<<5);//digitalWrite(clockPin, 1);
      PORTD &= ~(1<<6);//digitalWrite(dataPin, 0);
    }
  }
  //stop shifting
  PORTD &= ~(1<<5);//digitalWrite(clockPin, 0);
}

 


Get Your Ad Here

data_list