Hello.
I hope you guys can help me out with my latest project. I have the code pretty much done (alot of which isn’t my own) except there is one crippling glitch. I’ve uploaded a video for you guys to see what I’m on about. My project uses a plot() function to turn on a particular LED and I have a clear() function that shuts off all the LEDs at once. I’ve put a bit of test code in the main loop to illustrate the problem. It simply lights up the first two columns one LED at a time. The first iteration works fine, but after the first clear the LEDs turn on in sets of 4 rather than one by one. The CGOL code is commented out.
http://s551.photobucket.com/user/Xplorer822/media/VID_20140412_221407_zps328a79f0.mp4.html
Hopefully one of you with more experience with the Sure display can advise me on whats going on, or point me in the direction of a working library.
Any help is appreciated. Thanks
/* Code written by ... in August 2011 */
/* Simple communication with the LED matrix 32x16 of Sure Electronics */
#define HT1632_ID_CMD 4 /* ID = 100 - Commands */
#define HT1632_ID_RD 6 /* ID = 110 - Read RAM */
#define HT1632_ID_WR 5 /* ID = 101 - Write RAM */
#define HT1632_CMD_SYSDIS 0x00 /* CMD= 0000-0000-x Turn off oscil */
#define HT1632_CMD_SYSON 0x01 /* CMD= 0000-0001-x Enable system oscil */
#define HT1632_CMD_LEDOFF 0x02 /* CMD= 0000-0010-x LED duty cycle gen off */
#define HT1632_CMD_LEDON 0x03 /* CMD= 0000-0011-x LEDs ON */
#define HT1632_CMD_BLOFF 0x08 /* CMD= 0000-1000-x Blink ON */
#define HT1632_CMD_BLON 0x09 /* CMD= 0000-1001-x Blink Off */
#define HT1632_CMD_SLVMD 0x10 /* CMD= 0001-0xxx-x Slave Mode */
#define HT1632_CMD_MSTMD 0x18 /* CMD= 0001-10xx-x Use on-chip clock */
#define HT1632_CMD_EXTCLK 0x1C /* CMD= 0001-11xx-x Use external clock */
#define HT1632_CMD_COMS00 0x20 /* CMD= 0010-ABxx-x commons options */
#define HT1632_CMD_COMS01 0x24 /* CMD= 0010-ABxx-x commons options */
#define HT1632_CMD_COMS10 0x28 /* CMD= 0010-ABxx-x commons options */
#define HT1632_CMD_COMS11 0x2C /* CMD= 0010-ABxx-x commons options */
#define HT1632_CMD_PWM 0xA0 /* CMD= 101x-PPPP-x PWM duty cycle */
#define X_MAX 31 // 0 based X
#define Y_MAX 15 // 0 based Y
#define CHIP_MAX 4 // Number of HT1632C Chips
#define H 32 //Define height
#define W 16 //Define width
// * Set these constants to the values of the pins connected to the SureElectronics Module
static const byte data = 6; // Data pin (pin 7)
static const byte clk = 9; // Data pin (pin 2)
static const byte wrclk = 7; // Write clock pin (pin 5)
static const byte cs = 8; // Chip Select (pin 1)
// The should also be a common GND (pins .
// The module with all LEDs like draws about 200mA,
// which makes it PROBABLY powerable via Arduino +5V
const int bits_l = 64;
const int numChip = 4;
byte pattern[bits_l][numChip];
byte address=0;
const int color = 1; // 0 = Green
// 1 = Red
//====================================================
void setup()
{
//digitalWrite(cs, HIGH);
pinMode(cs, OUTPUT);
pinMode(wrclk, OUTPUT);
pinMode(data, OUTPUT);
pinMode(clk, OUTPUT);
for (int i=1; i<=CHIP_MAX; i++) {
sendcmd(i, HT1632_CMD_SYSDIS); // Disable system
sendcmd(i, HT1632_CMD_COMS00); // 16*32, PMOS drivers
sendcmd(i, HT1632_CMD_MSTMD); // MASTER MODE - Use on-chip clock
sendcmd(i, HT1632_CMD_SYSON); // System on - Enable system oscil
sendcmd(i, HT1632_CMD_LEDON); // LEDs on
}
Clear(); // Clear the display
}
void loop ()
{
//
// randomSeed(analogRead(0)); //CGOL algorithm
// bool now[H][W], next[H][W];
// populateMatrix(now);
// clearMatrix(next);
//
// while (1)
// {
//
// clearMatrix(next);
// calculate(now, next);
// swap(now, next);
// printMatrix(now);
// delay(100);
// Clear();
// }
int x,y; // test code
for(x=0; x<2; x++){
for(y=0; y<16; y++){
Plot(x,y,1);
delay(100);
}
}
Clear();
}
//=====================================================
void Plot (int X, int Y, int color)
{
// color:
// 0 -> green
// 1 -> red
// check the inputs value
// in case of error return nothing
if(color > 1 || color < 0)
return;
if(X<0 || X>31 || Y<0 || Y>15)
return;
// variables declaration
int x = 0;
int y = 0;
int nChip = 0;
// transform the X/Y coordinates to the number of the microchip
// that controls the region (1,2,3 or 4) the LED you want to
// light up
nChip = 1 + X/16 + (Y>7?2:0);
chipselect(nChip); // call the function chipselect to send the
// information to the matrix
// after the selection of the chip we need just the coordinate
// for 1 of them, so we need to change the coordinate system
x = X%16; // columns
y = Y%8; // rows
// from X/Y to address
address = ((x*2+y/4)) + color*32;
// from Y to pattern
pattern[address][nChip] = pattern[address][nChip] | ((1<<(3-y%4)));
writebits(HT1632_ID_WR, 2); // send ID: WRITE to RAM
writebits(address, 6); // Send address
writebits(pattern[address][nChip], 3); // send 4 bits of data
}
//**************************************************************************************************
//Function Name: chipselect
//Function Feature: choose the chipset in which work
//Input Argument: number of the chip
//Output Argument: void
//**************************************************************************************************
void chipselect(int chip){
if(chip<0){
digitalWrite(cs, LOW);
for(int tmp=0; tmp<CHIP_MAX; tmp++) {
digitalWrite(clk, HIGH);
digitalWrite(clk, LOW);
}
}
else{
digitalWrite(cs, HIGH);
for(int tmp=0; tmp<CHIP_MAX; tmp++) {
digitalWrite(clk, HIGH);
digitalWrite(clk, LOW);
}
if(chip>0){
digitalWrite(cs, LOW);
digitalWrite(clk, HIGH);
digitalWrite(clk, LOW);
digitalWrite(cs, HIGH);
for(int tmp=1 ; tmp<chip; tmp++) {
digitalWrite(clk, HIGH);
digitalWrite(clk, LOW);
}
}
}
}
//**************************************************************************************************
//Function Name: clear
//Function Feature: clear display
//Input Argument: void
//Output Argument: void
//**************************************************************************************************
void Clear()
{
char i;
for (int j=1;j<=CHIP_MAX;j++) {
chipselect(j);
writebits(HT1632_ID_WR, 2); // send ID: WRITE to RAM
writebits(0, 6); // Send address
for (i=0; i<64/2; i++) // Clear entire display
writebits(0, 7); // send 8 bits of data
chipselect(0);
}
}
//**************************************************************************************************
//Function Name: writebits
//Function Feature: Write bits (up to 8) to h1632 on pins data, wrclk
// Chip is assumed to already be chip-selected
// Bits are shifted out from MSB to LSB
//Input Argument: bits: bits to send
// length: length of the bits to send
//Output Argument: void
//**************************************************************************************************
void writebits (byte bits, int length)
{
for (int i=length; i>=0; i--) {
digitalWrite(wrclk, LOW);
if (bits & 1<<i) {
digitalWrite(data, HIGH);
}
else {
digitalWrite(data, LOW);
}
digitalWrite(wrclk, HIGH);
}
}
//**************************************************************************************************
//Function Name: sendcmd
//Function Feature: Send a command to the ht1632 chip.
// Select 1 0 0 c7 c6 c5 c4 c3 c2 c1 c0 xx Free
//Input Argument: chipNo: the chip you want to send data
// command: consists of a 3-bit "CMD" ID, an 8bit command, and
// one "don't care bit".
//Output Argument: void
//**************************************************************************************************
static void sendcmd (byte chipNo, byte command)
{
chipselect(chipNo);
writebits(HT1632_ID_CMD, 2); // send 3 bits of id: COMMMAND
writebits(command, 7); // send the actual command
writebits(0, 1); /* one extra dont-care bit in commands. */
chipselect(0);
}