Teensy 3.2 SPI Problem or my lousy code?

Status
Not open for further replies.

Tharkun

New member
Differences between an Arduino and a Teensy 3.2


Complete code below

I just bought 2 old el cheapo 7 x 7 Segment displays from an old gaming machine at a local Hamfest. They sat in my junk box until I though I’d reverse engineer them and find out how to display something on them. Each board has 2 595 8 bit buffers daisy chained together. The first 595 feeds the first 4 led digits, and the second feeds the remaining 3. The 595 data out latch pins are tied together. You clock in an entire 2 byte word then latch the data out. There are four additional enabling lines that enable pairs of 7 segment displays line 1 enables displays 1 and 5, line 2 enables 2 and 6, line 3 enables 3 and 7 and line 4 enables display 4 only. You multiplex the 4 control lines and clock out the respective word, to feed the corresponding pair of digits.

So far so good. The sketch below uses the SPI interface to clock out the data, as the enable lines and multiplexed. With a delay of 5ms between displaying the data and then clocking all 0s to clear it for the next clock in, you get a nice flicker free display on an Arduino Pro Mini. Nice to work with ye olde 74 series electronics where you can see and trace everything.

Now for the interesting part. I loaded the sketch into a Teensy 3.2. What happened? Well the first thing was the LED displays were inverted, I had to bit wise negate the 7 segment encoding values (see numbers array in initialise section).

Then although the data was successfully displayed, I couldn’t help noticing that there was slight flicker, or glitching on a couple of the most significant digits, and sometimes others. Not all the time but enough to be annoying, the display still functions as expected: the noise or whatever it is is cleared away son the next refresh cycle.

I tried dividing down the SPI clock but to no effect.

How do I sort this out? Are there any know problems or differences with the Arduino SPI implementation on the Teensy 3.2 ? Same code on the Arduino (pro micro) perfect, on the Teensy 3.2 noise etc.

I tried the following experiment that yielded interesting results. Consider this code. Latch low, clock in the word, ten latch high to display on whatever pair of displays has been selected.

I I stick a 2 milli second delay between the data word being clocked out, and the data being latched, then the displays fill with garbage. What happens between data clock out and latch up of the 595s? The same experiment on the Arduino was fine: no garbage, just a bit of extra flicker because of the extra delay.

I'd appreciate some help, the code is probably lousy to begin with, any suggestions as to how to make it cleaner? What might be causing my Teensy problem, and how can I cure it? Why the inverted logic to drive the 7 segment displays?

Cheers,

Tim


pinPairSelect(toEnable);
digitalWrite(latchPin, LOW);

//Shift out the data into the shift registers
//
SPI.transfer(firstByte);
SPI.transfer(secondByte);
digitalWrite(latchPin, HIGH);


Complete Code Follows


// inslude the SPI library:
#include <SPI.h>

//Pin connected to ST_CP of 74HC595s
int latchPin = 10;

const char enable4 = 5;
const char enable3_7 = 3;
const char enable2_6 = 4;
const char enable1_5 = 2;

//other
int del = 5;
int loopcount = 0;

//char numbers[10] = {252,96,218,242,102,182,62,224,254,246};
char numbers[10] = {63,6,91,79,102,109,124,7,127,111};

char theContent[8] = "1234567";

void setup()
{
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(2);
SPI.begin();

Serial.begin(9600);

pinMode(latchPin, OUTPUT);

pinMode(enable4, OUTPUT);
pinMode(enable3_7, OUTPUT);
pinMode(enable2_6, OUTPUT);
pinMode(enable1_5, OUTPUT);
}

void loop()
{
unsigned long j = 0;

for (j=9999999; j>=0; j--)
{
//sprintf(theContent,"%07lu", millis());
sprintf(theContent,"%07lu", j);
displayWrite(theContent);
}

}

void displayWrite(char toDisplay[])
{
writeDigits(enable1_5,asciiToSegNumber(toDisplay[0]),asciiToSegNumber(toDisplay[4]));
writeDigits(enable2_6,asciiToSegNumber(toDisplay[1]),asciiToSegNumber(toDisplay[5]));
writeDigits(enable3_7,asciiToSegNumber(toDisplay[2]),asciiToSegNumber(toDisplay[6]));
writeDigits(enable4,0,asciiToSegNumber(toDisplay[3]));
}

char asciiToSegNumber(char theChar)
{
return(numbers[theChar -'0']);
}

void writeDigits(char toEnable, char firstByte, char secondByte)
{
// Write delay then clear
//
writeDigitPair(toEnable, firstByte, secondByte);
delay(del);
writeDigitPair(toEnable, 0, 0);
}

void writeDigitPair(char toEnable, char firstByte, char secondByte)
{
pinPairSelect(toEnable);
digitalWrite(latchPin, LOW);

//Shift out the data into the shift registers
//
SPI.transfer(firstByte);
SPI.transfer(secondByte);
digitalWrite(latchPin, HIGH);
}

void pinPairSelect(char thePin)
{
switch (thePin)
{
case enable4 :
digitalWrite(enable4, HIGH);
digitalWrite(enable3_7, LOW);
digitalWrite(enable2_6, LOW);
digitalWrite(enable1_5, LOW);
break;

case enable3_7 :
digitalWrite(enable4, LOW);
digitalWrite(enable3_7, HIGH);
digitalWrite(enable2_6, LOW);
digitalWrite(enable1_5, LOW);
break;

case enable2_6 :
digitalWrite(enable4, LOW);
digitalWrite(enable3_7, LOW);
digitalWrite(enable2_6, HIGH);
digitalWrite(enable1_5, LOW);
break;

case enable1_5 :
digitalWrite(enable4, LOW);
digitalWrite(enable3_7, LOW);
digitalWrite(enable2_6, LOW);
digitalWrite(enable1_5, HIGH);
}
}
 
This code looks like it ought to work.

My gut feeling is some sort of hardware problem, perhaps a combination of the 3.3V signals barely meeting the 5V logic circuit's thresholds, and perhaps issues with ringing, crosstalk or other signal quality problems.

For a visual idea, take a look at the OctoWS2811 page. Scroll down to "Signal Quality".
 
Yeah, thank's for that Paul, I'll try a 3.3v to 5v level converter between the Teensy and the display. I guess I need to be careful exactly what "5 volt tolerant" means...

Cheers,

Tim




This code looks like it ought to work.

My gut feeling is some sort of hardware problem, perhaps a combination of the 3.3V signals barely meeting the 5V logic circuit's thresholds, and perhaps issues with ringing, crosstalk or other signal quality problems.

For a visual idea, take a look at the OctoWS2811 page. Scroll down to "Signal Quality".
 
Status
Not open for further replies.
Back
Top