Pages: [1]   Go Down
Author Topic: Arduino Due + Tlc5940  (Read 1454 times)
0 Members and 1 Guest are viewing this topic.
The Netherlands
Offline Offline
Full Member
***
Karma: 1
Posts: 139
MKDS hacker and Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The tlc5940arduino library isn't working due to the differences between arm and avr. So, has someone tried to get the Tlc5940 working with the Arduino due? I want to drive my 8x8 rgb matrix with it.

Edit: I found some documentation and started making my own library. (or at least code) I am going to try it tomorrow.
« Last Edit: December 28, 2012, 01:40:26 pm by Gericom » Logged

Subscribe my youtube channel: http://www.youtube.com/user/MKDS3

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,
I know this topic hasnt been updated but I was wondering if you made a working tlc5940 library, because I would love to use it.

CakeBoss
Logged

The Netherlands
Offline Offline
Full Member
***
Karma: 1
Posts: 139
MKDS hacker and Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,
I know this topic hasnt been updated but I was wondering if you made a working tlc5940 library, because I would love to use it.

CakeBoss
I made something, but it didn't work very well. I could post the source here. Maybe someone could work it out.
Logged

Subscribe my youtube channel: http://www.youtube.com/user/MKDS3

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would be interested to see the code. I have the Due just for interfacing to make and 8x8x8 rbg cube and plan on using the 5940.
Logged

The Netherlands
Offline Offline
Full Member
***
Karma: 1
Posts: 139
MKDS hacker and Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would be interested to see the code. I have the Due just for interfacing to make and 8x8x8 rbg cube and plan on using the 5940.
Okay, I will post it tomorrow.
Logged

Subscribe my youtube channel: http://www.youtube.com/user/MKDS3

The Netherlands
Offline Offline
Full Member
***
Karma: 1
Posts: 139
MKDS hacker and Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here it is:
Code:
//TLC5940
#define GSCLK_PIN 2
#define SIN_PIN 3
#define SCLK_PIN 4
#define BLANK_PIN 5
#define DCPRG_PIN 6
#define VPRG_PIN 7
#define XLAT_PIN 8

#define TLC5940_N 2

//74HC595
#define LATCH_PIN 9
#define CLOCK_PIN 10
#define DATA_PIN 11

//Display
int rowId = 0;

uint16_t rowR[8][8] =
{
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
};

uint16_t rowG[8][8] =
{
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
};

uint16_t rowB[8][8] =
{
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
  ,
  {
    4095,4095,4095,4095,4095,4095,4095,4095  }
};

uint8_t dcData[96 * TLC5940_N] = {
  //MSB          LSB
  //TLC 1
  1, 1, 1, 1, 1, 1, // Channel 15
  1, 1, 1, 1, 1, 1, // Channel 14
  1, 1, 1, 1, 1, 1, // Channel 13
  1, 1, 1, 1, 1, 1, // Channel 12
  1, 1, 1, 1, 1, 1, // Channel 11
  1, 1, 1, 1, 1, 1, // Channel 10
  1, 1, 1, 1, 1, 1, // Channel 9
  1, 1, 1, 1, 1, 1, // Channel 8
  1, 1, 1, 1, 1, 1, // Channel 7
  1, 1, 1, 1, 1, 1, // Channel 6
  1, 1, 1, 1, 1, 1, // Channel 5
  1, 1, 1, 1, 1, 1, // Channel 4
  1, 1, 1, 1, 1, 1, // Channel 3
  1, 1, 1, 1, 1, 1, // Channel 2
  1, 1, 1, 1, 1, 1, // Channel 1
  1, 1, 1, 1, 1, 1, // Channel 0
  //TLC 2
  1, 1, 1, 1, 1, 1, // Channel 15
  1, 1, 1, 1, 1, 1, // Channel 14
  1, 1, 1, 1, 1, 1, // Channel 13
  1, 1, 1, 1, 1, 1, // Channel 12
  1, 1, 1, 1, 1, 1, // Channel 11
  1, 1, 1, 1, 1, 1, // Channel 10
  1, 1, 1, 1, 1, 1, // Channel 9
  1, 1, 1, 1, 1, 1, // Channel 8
  1, 1, 1, 1, 1, 1, // Channel 7
  1, 1, 1, 1, 1, 1, // Channel 6
  1, 1, 1, 1, 1, 1, // Channel 5
  1, 1, 1, 1, 1, 1, // Channel 4
  1, 1, 1, 1, 1, 1, // Channel 3
  1, 1, 1, 1, 1, 1, // Channel 2
  1, 1, 1, 1, 1, 1, // Channel 1
  1, 1, 1, 1, 1, 1, // Channel 0
};

uint8_t gsData[192 * TLC5940_N] = {
  //MSB                            LSB
  //TLC 1
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 15
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 13
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // Channel 12
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, // Channel 11
  0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, // Channel 10
  0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // Channel 9
  0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, // Channel 8
  0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, // Channel 7
  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, // Channel 6
  0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // Channel 5
  0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 4
  0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 3
  0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 2
  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 1
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // Channel 0
  //TLC 2
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 15
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 13
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // Channel 12
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, // Channel 11
  0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, // Channel 10
  0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // Channel 9
  0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, // Channel 8
  0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, // Channel 7
  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, // Channel 6
  0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // Channel 5
  0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 4
  0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 3
  0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 2
  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 1
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // Channel 0
};

void TLC5940_Init()
{
  pinMode(GSCLK_PIN, OUTPUT);
  pinMode(SCLK_PIN, OUTPUT);
  pinMode(DCPRG_PIN, OUTPUT);
  pinMode(VPRG_PIN, OUTPUT);
  pinMode(XLAT_PIN, OUTPUT);
  pinMode(BLANK_PIN, OUTPUT);
  pinMode(SIN_PIN, OUTPUT);

  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  pinMode(DATA_PIN, OUTPUT);

  digitalWrite(GSCLK_PIN, LOW);
  digitalWrite(SCLK_PIN, LOW);
  digitalWrite(DCPRG_PIN, LOW);
  digitalWrite(VPRG_PIN, HIGH);
  digitalWrite(XLAT_PIN, LOW);
  digitalWrite(BLANK_PIN, HIGH);
}

void TLC5940_ClockInDC()
{
  digitalWrite(DCPRG_PIN, HIGH);
  digitalWrite(VPRG_PIN, HIGH);

  uint8_t Counter = 0;

  while(true)
  {
    if (Counter > TLC5940_N * 96 - 1) {
      digitalWrite(XLAT_PIN, HIGH);
      digitalWrite(XLAT_PIN, LOW);
      break;
    }
    else {
      digitalWrite(SIN_PIN, dcData[Counter]);
      digitalWrite(SCLK_PIN, HIGH);
      digitalWrite(SCLK_PIN, LOW);
      Counter++;
    }
  }
}

void TLC5940_SetGS_And_GS_PWM()
{
  uint8_t firstCycleFlag = 0;
  if (digitalRead(VPRG_PIN))
  {
    digitalWrite(VPRG_PIN, LOW);
    firstCycleFlag = 1;
  }
  uint16_t GSCLK_Counter = 0;
  uint8_t Data_Counter = 0;
  digitalWrite(BLANK_PIN, LOW);
  while (true)
  {
    if (GSCLK_Counter > 4095)
    {
      digitalWrite(BLANK_PIN, HIGH);
      digitalWrite(XLAT_PIN, HIGH);
      digitalWrite(XLAT_PIN, LOW);
      if (firstCycleFlag)
      {
        digitalWrite(SCLK_PIN, HIGH);
        digitalWrite(SCLK_PIN, LOW);
        firstCycleFlag = 0;
      }
      break;
    }
    else
    {
      if (!(Data_Counter > TLC5940_N * 192 - 1))
      {
        if (gsData[Data_Counter])
          digitalWrite(SIN_PIN, HIGH);
        else
          digitalWrite(SIN_PIN, LOW);
        digitalWrite(SCLK_PIN, HIGH);
        digitalWrite(SCLK_PIN, LOW);
        Data_Counter++;
      }
    }
    digitalWrite(GSCLK_PIN, HIGH);
    digitalWrite(GSCLK_PIN, LOW);
    GSCLK_Counter++;
  }
}

void setLed(int ledId, int driverId, uint16_t value)
{
  for(int i = 0; i < 12; i++)
  {
    gsData[driverId * 12 * 16 + 12 * ledId + i] = value >> (12 - i - 1);
  }
}

/*void TC3_Handler()
{
  TC_GetStatus(TC1, 0);
  digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, 1 << rowId);
  digitalWrite(LATCH_PIN, HIGH);
  for(int i = 0; i < 8; i++)
  {
   setLed(i, 0, rowR[i][rowId]);
   setLed(i, 1, rowG[i][rowId]);
   setLed(i + 8, 1, rowB[i][rowId]);
  }
  rowId++;
  if(rowId == 8) rowId = 0;
  TLC5940_SetGS_And_GS_PWM();
}*/

void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) {
  pmc_set_writeprotect(false);
  pmc_enable_periph_clk((uint32_t)irq);
  TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
  uint32_t rc = VARIANT_MCK/128/frequency; //128 because we selected TIMER_CLOCK4 above
  TC_SetRA(tc, channel, rc/2); //50% high, 50% low
  TC_SetRC(tc, channel, rc);
  TC_Start(tc, channel);
  tc->TC_CHANNEL[channel].TC_IER=TC_IER_CPCS;
  tc->TC_CHANNEL[channel].TC_IDR=~TC_IER_CPCS;
  NVIC_EnableIRQ(irq);
}

void setup()
{
  TLC5940_Init();
  TLC5940_ClockInDC();
 // startTimer(TC1, 0, TC3_IRQn, 84000000 / 2);
}

void loop()
{
digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, 1 << rowId);
  digitalWrite(LATCH_PIN, HIGH);
  for(int i = 0; i < 8; i++)
  {
   setLed(i, 1, rowR[i][rowId]);
   //setLed(i, 0, rowG[i][rowId]);
   setLed(i + 8, 0, rowB[i][rowId]);
  }
 // rowId++;
 // if(rowId == 8) rowId = 0;
  TLC5940_SetGS_And_GS_PWM();
}
Maybe someone can fix it.
Logged

Subscribe my youtube channel: http://www.youtube.com/user/MKDS3

Pages: [1]   Go Up
Jump to: