Forum: Mikrocontroller und Digitale Elektronik Arduino Uno + USB-Shield + TLC5940 Problem


von Joe (Gast)


Lesenswert?

Hallo,

für meine Studienarbeit soll ich eine Software zur Ansteuerung einer 
LED-Matrix überlegen. Dazu verwende ich den Arduino UNO mit passendem 
USB-Shield, um über einen XBox360-Controller PWM-Treiber des Typs 
TLC5940 über SPI anzusteuern.

Es ist mir vorgeschrieben, den TLC5940 über SPI anzusteuern und dafür 
ein vorgefertigtes Shield vom Vorgänger des am Projekt beschäftigten 
Studenten zu benutzen.

Da sowohl USB-Shield als auch TLC5940 über SPI mit dem Arduino 
kommunizieren, habe ich entsprechend der Anleitung auf:
http://www.circuitsathome.com/usb-host-shield-hardware-manual

das USB-Shield sowie die dazugehörige Library angepasst, sodass die 
ursprüngliche SS-Leitung und INT-Leitung durchtrennt und auf einen 
anderen Pin geroutet wurden.


Die Verbindungen sehen nun wie folgt aus:

USB-Shield:

SS-Pin  -> Arduino Uno Digital Pin 7
INT-Pin  -> Arduino Uno Digital Pin 6


TLC5940:

GSCLK -> Arduino Uno Digital Pin 3
XLAT -> Arduino Uno Digital Pin 9
BLANK -> Arduino Uno Digital Pin 10
SIN -> Arduino Uno Digital Pin 11
SCLK -> Arduino Uno Digital Pin 13




Mein Sketch-Code:
//this project uses multiple RGB-LEDs

//the LEDs illuminate plexiglas-plates through the edges; each 
plexiglas-plate represents a pixel

//there are 4 pixels in total, 11 RGB-LEDs per pixel

//Pixel 1:
//channel0 -> red of RGB-LED1
//channel1 -> green of RGB-LED1
//channel2 -> blue of RGB-LED1
//channel3 -> red of RGB-LED2
//channel4 -> green of RGB-LED2
//channel5 -> blue of RGB-LED2
//channel6 -> red of RGB-LED3
//channel7 -> green of RGB-LED3
//channel8 -> blue of RGB-LED3
//channel9 -> red of RGB-LED4
//channel10 -> green of RGB-LED4
//channel11 -> blue of RGB-LED4


//channel 12 to channel 47 are used for the other 3 pixels
// in this example only pixel 1 is controlled

//TLC5940
#include "Tlc5940.h"


#define TLC_BLANK 10    //BLANK pin of TLC5940
#define TLC_XLAT 9      //XLAT pin of TLC5940
#define XBOX_SS 7

int color = 0;        //value of color: 1 -> red, 2 -> green, 3 -> blue
int commandSent = 0;  //is set to "1" when a button was clicked


//USB-Shield
#include "XBOXRECV.h"
// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif

USB Usb;
XBOXRECV Xbox(&Usb);
int i = 0;    //Xbox360-Controller Number 0





void setup()
{
  pinMode(TLC_XLAT, OUTPUT);
  pinMode(TLC_BLANK, OUTPUT);
  pinMode(XBOX_SS, OUTPUT);

  //disconnect USB-Shield from the SPI-Bus before the Tlc.Init()
  digitalWrite(XBOX_SS, HIGH);

  //Setup-Init TLC5940-PWM driver
  Tlc.init();
  Tlc.clear();

  //Setup Init USB-Shield
  Serial.begin(115200);
  #if !defined(_MIPSEL_)
  while (!Serial); // Wait for serial port to connect - used on 
Leonardo, Teensy and other boards with built-in USB CDC serial 
connection
  #endif

  //before USB-Shield communication make sure that TLC5940's state is 
not changed
  Tlc.clear();
  digitalWrite(TLC_BLANK, HIGH);
  digitalWrite(TLC_XLAT, LOW);
  //----------------------------------------------------------//

  if (Usb.Init() == -1)
  {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
}

void xbox_state()
{
  Tlc.clear();
  digitalWrite(TLC_XLAT, LOW);    //When XLAT = low, the data in GS or 
DC register is held constant.
  digitalWrite(XBOX_SS, LOW);


  //----------------------------------------------------------//

  Usb.Task();
  if(Xbox.XboxReceiverConnected)
  {
    if(Xbox.Xbox360Connected[i])
    {
      if(Xbox.getButtonClick(A, i))
      {
        color = 2;  //2 -> green
        Serial.println("In xbox_state");
        Serial.print("Farbe ");
        Serial.println(color);
        //setMatrix(color);
        commandSent = 1;
      }
      else if(Xbox.getButtonClick(B, i))
      {
        color = 1;  //1 -> red
        Serial.println("In xbox_state");
        Serial.print("Farbe ");
        Serial.println(color);
        //setMatrix(color);
        commandSent = 1;
      }
      else if(Xbox.getButtonClick(X, i))
      {
        color = 3;  //3 -> blue
        Serial.println("In xbox_state");
        Serial.print("Farbe ");
        Serial.println(color);
        //setMatrix(color);
        commandSent = 1;
      }
    }
  }
}

void setMatrix(int col)
{
  //delay to try to avoid white flashing of the LEDs
  //delay(100);


  //before communication with TLC5940 disconnect USB-Shield from the 
SPI-Bus
  //SS-Pin of USB-Shields = HIGH -> SLAVE (USB-Shield) ignores the 
master(Arduino)
  digitalWrite(XBOX_SS, HIGH);
  //digitalWrite(TLC_XLAT, HIGH);


  Serial.println("In setMatrix");
  Serial.print("Farbe ");
  Serial.println(color);

  //depending on value of "color" the pixel is illuminated red(1), 
green(2) or blue(3)
  switch(color)
  {
    case 1:
            Serial.println("case 1");
            Tlc.clear();
            for(int count = 0; count <10; count += 3)
            {
              Tlc.set(count, 4095);
            }
            digitalWrite(TLC_XLAT, HIGH);
            Tlc.update();
            Tlc.clear();
            break;

    case 2:
            Serial.println("case 2");
            Tlc.clear();
            for(int count = 1; count <11; count += 3)
            {
              Tlc.set(count, 4095);
            }
            digitalWrite(TLC_XLAT, HIGH);
            Tlc.update();
            Tlc.clear();
            break;

    case 3:
            Serial.println("case 3");
            Tlc.clear();
            for(int count = 2; count <12; count += 3)
            {
              Tlc.set(count, 4095);
            }
            digitalWrite(TLC_XLAT, HIGH);
            Tlc.update();
            Tlc.clear();
            break;

    default:
            Serial.println(" ");
  }
}

void loop()
{
  if(commandSent)
  {
    setMatrix(color);
    commandSent = 0;
  }
  xbox_state();

}


Mein Problem ist, dass hin und wieder nach Betätigen eins Buttons - A, B 
oder X, zunächst alle LEDs ganz kurz weiß aufblitzen und dann nur ein 
Pixel, wie auch gewünscht, in der entsprechenden Farbe aufleuchtet.

Meine Vermutung war, dass vielleicht an irgendeiner Stelle lauter 1'en 
in das GS-Daten-Shiftregister reingeschoben werden und mir ein Pin am 
Arduino einfach das Kommando zum weiterreichen in das 
Latch-Daten-Register erfolgt.

Mein ersters Lösungsansatz bestand darin erst kurz vor dem Aufruf der 
"TLC.update" Funktion XLAT auf HIGH zu setzen.
Auch die delay() Funktion an diversen Stellen einzufügen hat nichts 
genützt.

Hat jemand vielleicht eine Idee?


Vielen Dank für jede Hilfe!

Gruß Joe

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.