Beginners: please read this post and this post before posting to the forum.
0 Members and 1 Guest are viewing this topic.
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 programvoid 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);}
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 programvoid 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.
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 programvoid 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);}