sure electronics LED dot matrix HT1632c Energia example

32x8 LED dot matrix display
32×8 LED dot matrix display

This LED array has been sitting in my room for months now so I decided to hook it up to an msp430g2553 and get it displaying stuff. I bought it from ebay from a chinese supplier called sure electronics. The array is controlled by a ht1632c led matrix driver chip and the interface required is clocked serial data which I wrote in software for the msp430. There are arduino libraries out there for interfacing with this driver chip and I used them as a reference when putting this together. Right now all I have working is initialising the driver chip and writing data to its ram to display various patterns. The leds are mapped directly to the ht1632c ram. i.e. if you write a ‘1’ to a certain memory address that led will come on and writing a ‘0’ will turn it off. The chip has some other cool features like 16 level PWM dimming also. Only 4 wires (plus Vcc and GND) are needed to interface with the led array. The four wires are DATA, WR(clock), CS (this pin is pulsed to switch between read,write and command modes on the ht1632c) and the RD pin which is the data out from the ht1632c. This data out could be the contents of the RAM so you could find out which leds are on. I didn’t use this pin at all so it’s really only 3 wires which is pretty handy. These LED array boards can be chained together up to a max of 4. Right now I only have one so the code written is meant to just control one chip. That being said it shouldn’t be too much hassle to get it working with several boards at once.

Here is the code I wrote using the Energia IDE :


#define DATA       P2_2
#define WR         P2_1
#define CS         P2_0
#define RD         P2_3
#define MSB_FIRST  0
#define LSB_FIRST  1
#define READ_ID    B110
#define WRITE_ID   B101
#define RMW_ID     B101
#define CMD_ID     B100
#define LEDS_ON    0b00000011
#define SYS_DIS    B00000000
#define SYS_EN     B00000001
#define MODE_MAST  B00011000
#define COM_MODE3  B00100100
#define COM_MODE2  B00100000
#define COM_MODE4  B00101100
#define COM_MODE1  B00100000
#define BLINK_ON   B00001001
#define BLINK_OFF  B00001000

//variables
uint8_t mask_bit;
uint8_t i,h;
uint8_t IMG_TWITTER[] = {0b01111110, 0b10000001, 0b10111001, 0b10010101, 0b10010101, 0b10000001, 0b01111110};

void setup()
{
  pinMode(WR, OUTPUT);
  pinMode(CS, OUTPUT);
  pinMode(RD, INPUT);  
  pinMode(DATA, OUTPUT);
  initialize();
  clearDisplay();
  
  
}

void loop()
{
  
  for(h=0; h<50; h=h+2) {    //draw twitter logo scrolling across screen
    clearDisplay();
    char2Display(IMG_TWITTER, h);
    delay(100);
  }
   
 
  
  
 
    

}


/* function writeBits can write up to 8 bits either MSB or LSB first. arg1 bits is the data you 
want transferred, num_bits is how many bits out of the byte you want transferrerd. bit_order specifies
whether you want it sent MSB first or LSB first. for ht1632c memory addresses and commands are MSB first,
data is LSB first*/

void writeBits(uint8_t bits, uint8_t num_bits ,uint8_t bit_order) { 
  
 if (bit_order == MSB_FIRST) mask_bit = 1 << (num_bits - 1); 
 else mask_bit = 1;
  
  if (bit_order == MSB_FIRST) {
    while(mask_bit) {
      digitalWrite(WR, LOW);
      if (bits & mask_bit) digitalWrite(DATA, HIGH);
      else digitalWrite(DATA,LOW);
      digitalWrite(WR, HIGH);
      
      mask_bit >>= 1;
    }
  }
    
  else {
      while(mask_bit != (1 << (num_bits  )) ) {
        digitalWrite(WR, LOW);
        if (bits & mask_bit) digitalWrite(DATA, HIGH);
        else digitalWrite(DATA,LOW);
        digitalWrite(WR, HIGH);
        
        
        mask_bit <<= 1;
        
      }
  }    
  
}


void ht1632Write(uint8_t  mem_addr, uint8_t data) {     //write 4 bits of data to a specified address
  
  csPulse();
  writeBits(WRITE_ID, 3, MSB_FIRST);
  writeBits(mem_addr, 7, MSB_FIRST);
  writeBits(data, 4, LSB_FIRST);  
  
}



void csPulse () {          
 
   digitalWrite(CS,HIGH);
   digitalWrite(CS,LOW); 

}


void send_cmd( uint8_t command) {   // send 8 bit cmd followed by one dont care bit
  
  
  writeBits(command, 8, MSB_FIRST);
  writeBits(0,1,MSB_FIRST);
  
}
  
void initialize() {       //setup the ht1632c as outlined in datasheet
  
  csPulse();
  writeBits(CMD_ID, 3, MSB_FIRST);
  send_cmd(SYS_DIS);
  send_cmd(COM_MODE1);
  send_cmd(MODE_MAST);
  send_cmd(SYS_EN);
  send_cmd(LEDS_ON);
 
  
 
  
  
  
}  



void clearDisplay() {         //write zeros to all ram addresses
  csPulse();
  writeBits(WRITE_ID, 3, MSB_FIRST);
  writeBits(B000000, 7, MSB_FIRST);
  
  for(i=0; i<63; i++) writeBits(B000, 4, LSB_FIRST);
  
}


void char2Display(uint8_t chr[], uint8_t mem_addr) {  // takes array of bytes and writes it to led array ram
  
  csPulse();
  writeBits(WRITE_ID, 3, MSB_FIRST);
  writeBits(mem_addr, 7, MSB_FIRST);
  
  for(i=0; i<8; i++)  {
    writeBits(chr[i], 4, LSB_FIRST);
    writeBits(chr[i]>>4, 4, LSB_FIRST);
    
  }
  
}
 
  

If you connect the LED array using the same pins I’ve defined for DATA, WR and CS it should work right away displaying the twitter logo scrolling across the screen. Here is a picture of it displaying a speaker of some type:

led array displaying speaker
led array displaying speaker

If you’re interested in displaying text then check out this really extensive arduino library for the ht1632c.

Leave a Reply

%d bloggers like this: