16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due
#include <Arduino.h>
#define APIN 33
#define BPIN 34
#define CPIN 35
#define LAT 38
#define OE 36
#define CLK 37
#define R1 32
#define G1 29
#define B1 27
#define R2 30
#define G2 28
#define B2 26
#define PWMBITS 3 // 3 = 3 bit color per LED, 9-bit total pixel color
#define PWMMAX ((1 << PWMBITS) - 1)
#define WIDTH 96 // = 32 X number of panels
#define HEIGHT 16 // = height of each panel
#define NUMBYTES (WIDTH * HEIGHT / 2) * 3 // use 1.5 bytes (12 bits) per pixel
#define maxScan 8 // number of sections that are scanned - 8 rows for each RGB-123
uint8_t matrixbuff[NUMBYTES]; // 768 bytes for 16x32
volatile uint8_t pwmcounter=0;
volatile uint8_t scansection=0;
//----------------------------
void matrix_begin() {
pinMode(APIN, OUTPUT);
digitalWrite(APIN, LOW);
pinMode(BPIN, OUTPUT);
digitalWrite(BPIN, LOW);
pinMode(CPIN, OUTPUT);
digitalWrite(CPIN, LOW);
pinMode(LAT, OUTPUT);
digitalWrite(LAT, LOW);
pinMode(CLK, OUTPUT);
digitalWrite(CLK, HIGH);
pinMode(OE, OUTPUT);
digitalWrite(OE, LOW);
pinMode(R1,OUTPUT);
pinMode(R2,OUTPUT);
pinMode(G1,OUTPUT);
pinMode(G2,OUTPUT);
pinMode(B1,OUTPUT);
pinMode(B2,OUTPUT);
digitalWrite(R1,LOW);
digitalWrite(R2,LOW);
digitalWrite(G1,LOW);
digitalWrite(G2,LOW);
digitalWrite(B1,LOW);
digitalWrite(B2,LOW);
}
// construct a color from an r,g,b value
uint16_t matrix_Color444(uint8_t r, uint8_t g, uint8_t b) {
uint16_t c;
c = r;
c <<= 4;
c |= g & 0xF;
c <<= 4;
c |= b & 0xF;
return c;
}
// draw a pixel at the x & y coords with a specific color
void matrix_drawPixel(uint8_t x, uint8_t y, uint16_t c) {
uint16_t index = 0;
uint8_t old;
uint8_t red, green, blue, panel, ysave, ii;
// extract the 12 bits of color
red = (c >> 8) & 0xF;
green = (c >> 4) & 0xF;
blue = c & 0xF;
panel = (y - y%16)/16;
// both top and bottom are stored in same byte
if (y%16 < 8)
index = y%16;
else
index = y%16-8;
// now multiply this y by the # of pixels in a row
index *= WIDTH;
index += 32*panel;
// now, add the x value of the row
index += x;
// then multiply by 3 bytes per color (12 bit * High and Low = 24 bit = 3 byte)
index *= 3;
old = matrixbuff[index];
if (y%16 < 8) {
// we're going to replace the high nybbles only
// red first!
matrixbuff[index] &= ~0xF0; // mask off top 4 bits
matrixbuff[index] |= (red << 4);
index++;
// then green
matrixbuff[index] &= ~0xF0; // mask off top 4 bits
matrixbuff[index] |= (green << 4);
index++;
// finally blue
matrixbuff[index] &= ~0xF0; // mask off top 4 bits
matrixbuff[index] |= (blue << 4);
} else {
// we're going to replace the low nybbles only
// red first!
matrixbuff[index] &= ~0x0F; // mask off bottom 4 bits
matrixbuff[index] |= red;
index++;
// then green
matrixbuff[index] &= ~0x0F; // mask off bottom 4 bits
matrixbuff[index] |= green;
index++;
// finally blue
matrixbuff[index] &= ~0x0F; // mask off bottom 4 bits
matrixbuff[index] |= blue;
}
}
// output your filled matrix
void matrix_writeSection(uint8_t secn, uint8_t *buffptr) {
//digitalWrite(OE, HIGH);
uint16_t portCstatus_nonclk = 0x0080; // CLK = low
uint16_t portCstatus = 0x0080; // OE = HIGH
portCstatus |= 0x0020; // clk is high here too
REG_PIOC_ODSR = portCstatus; // set OE, CLK to high
// set A, B, C pins
if (secn & 0x1){ // Apin
portCstatus |= 0x0002;
portCstatus_nonclk |= 0x0002;
}
if (secn & 0x2){ // Bpin
portCstatus |= 0x0004;
portCstatus_nonclk |= 0x0004;
}
if (secn & 0x4){ // Cpin
portCstatus |= 0x0008;
portCstatus_nonclk |= 0x0008;
}
REG_PIOC_ODSR = portCstatus; // set A, B, C pins
// if (secn & 0x1) {
// digitalWrite(APIN, HIGH);
// } else {
// digitalWrite(APIN, LOW);
// }
// if (secn & 0x2) {
// digitalWrite(BPIN, HIGH);
// } else {
// digitalWrite(BPIN, LOW);
// }
// if (secn & 0x4) {
// digitalWrite(CPIN, HIGH);
// } else {
// digitalWrite(CPIN, LOW);
// }
uint8_t low, high;
uint16_t out = 0x0000;
uint8_t i;
for ( i=0; i<WIDTH; i++) {
out = 0x0000;
// red
low = *buffptr++;
high = low >> 4;
low &= 0x0F;
//if (low > pwmcounter) {
// digitalWrite(R2,HIGH);
//} else {
// digitalWrite(R2,LOW);
//}
if (low > pwmcounter) out |= 0x0200; // R2, pin 30, PD9
//if (high > pwmcounter){
// digitalWrite(R1,HIGH);
//} else {
// digitalWrite(R1,LOW);
//}
if (high > pwmcounter) out |= 0x0400; // R1, pin 32, PD10
// green
low = *buffptr++;
high = low >> 4;
low &= 0x0F;
//if (low > pwmcounter) {
// digitalWrite(G2,HIGH);
//} else {
// digitalWrite(G2,LOW);
//}
if (low > pwmcounter) out |= 0x0008; // G2, pin 28, PD3
//if (high > pwmcounter){
// digitalWrite(G1,HIGH);
//} else {
// digitalWrite(G1,LOW);
//}
if (high > pwmcounter) out |= 0x0040; // G1, pin 29, PD6
// blue
low = *buffptr++;
high = low >> 4;
low &= 0x0F;
//if (low > pwmcounter) {
// digitalWrite(B2,HIGH);
//} else {
// digitalWrite(B2,LOW);
//}
if (low > pwmcounter) out |= 0x0002; // B2, pin 26, PD1
//if (high > pwmcounter){
// digitalWrite(B1,HIGH);
//} else {
// digitalWrite(B1,LOW);
// }
if (high > pwmcounter) out |= 0x0004; // B1, pin 27, PD2
//digitalWrite(CLK, LOW);
REG_PIOC_ODSR = portCstatus_nonclk; // set clock to low, OE, A, B, C stay the same
REG_PIOD_ODSR = out;
//digitalWrite(CLK, HIGH);
REG_PIOC_ODSR = portCstatus; // set clock to high, OE, A, B, C stay the same
}
// latch it!
//digitalWrite(LAT, HIGH);
REG_PIOC_ODSR = 0x0070; // LAT = HIGH, OE = HIGH, CLK = HIGH, A, B, C = LOW
//digitalWrite(LAT, LOW);
REG_PIOC_ODSR = 0x0030; // only OE = HIGH and CLK = HIGH, LAT = LOW, A, B, C = LOW
// digitalWrite(OE, LOW);
REG_PIOC_ODSR = 0x0020; // only clk is high, OE, LAT, A, B, C are low
}
void matrix_updateDisplay(void) {
matrix_writeSection(scansection, matrixbuff + (3*WIDTH*scansection));
scansection++;
if (scansection == maxScan) {
scansection = 0;
pwmcounter++;
if (pwmcounter == PWMMAX) { pwmcounter = 0; }
}
}
//TC1 ch 0
void TC3_Handler()
{
TC_GetStatus(TC1, 0);
matrix_updateDisplay();
}
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_CLOCK1);
uint32_t rc = VARIANT_MCK/2/frequency; //2 because we selected TIMER_CLOCK1 above
//TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
//// VARIANT_MCK = 84000000, I guess timer4 is every 1/4 or something?
//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(){
matrix_begin();
startTimer(TC1, 0, TC3_IRQn, 150000); //TC1 channel 0, the IRQ for that channel and the desired frequency
}
void loop(){
matrix_drawPixel(0, 2, matrix_Color444(2, 0, 0));
matrix_drawPixel(1, 2, matrix_Color444(2, 0, 0));
matrix_drawPixel(2, 2, matrix_Color444(2, 0, 0));
matrix_drawPixel(3, 2, matrix_Color444(2, 0, 0));
matrix_drawPixel(4, 2, matrix_Color444(2, 0, 0));
}
TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1);
uint32_t rc = VARIANT_MCK/2/frequency; //2 because we selected TIMER_CLOCK1 above
startTimer(TC1, 0, TC3_IRQn, 80000); //TC1 channel 0, the IRQ for that channel and the desired frequency
Re: 16x32 RGB LED Matrix, Arduino Due
uint16_t portCstatus_nonclk = 0x0080; // OE high, CLK = low
uint16_t portCstatus = 0x0080; // OE = HIGH
uint16_t portCstatus_nonclk = 0x0010; // OE high, CLK = low
uint16_t portCstatus = 0x0010; // OE = HIGH
Re: 16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due
avriot wrote:Since nobody has responded, I assume nobody has gotten further then my blundering about
Re: 16x32 RGB LED Matrix, Arduino Due
My apologies. We in support usually look for unanswered topics. When you tag on to an old thread, it often goes unnoticed. It usually better to start a new topic than resurrect an old one.
We aren't really doing much work on porting libraries to the Due. A major reason for this is that Arduino 1.5.2 is quite incomplete and unstable, and it hasn't been updated for over 6 months. We have decided to focus our efforts elsewhere until such time as a usable version of Arduino for the Due is released.
Re: 16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due
Re: 16x32 RGB LED Matrix, Arduino Due