dotmatrixLED animation with ht1632 LED driver.

Dependencies:   SDFileSystem mbed

ht1632.cpp

Committer:
kohacraft
Date:
2015-08-23
Revision:
2:f28ec2298aa6
Parent:
0:62dc51c731b4

File content as of revision 2:f28ec2298aa6:

#include "mbed.h"
#include "ht1632.h"

DigitalOut ht1632_cs(dp9);
DigitalOut ht1632_wr(dp10);
DigitalOut ht1632_data(dp11);

void initHt1632()
{
        ht1632_cs = 1;
        ht1632_wr = 1;
        ht1632_data = 0;
        
        sendCommand(SYS_EN);
        sendCommand(LED_ON);
        sendCommand(BLINK_OFF);
        sendCommand(RC_MASTER_MODE);
        sendCommand(Pmos8Common);
        sendCommand(PWM16DUTY);
}


void sendCommand( unsigned char orderNum )
{
    unsigned short command = 0x8000; //0b1000 0000 0000 0000
    command = command | ( ( (unsigned short)orderNum) <<5 );   //0b100x xxxx xxx0 0000
    unsigned short mask = 0x8000;

    ht1632_wr = 1;
    ht1632_cs = 0;

    for(int i=0 ; i<12 ; i++ )
    {
        ht1632_wr = 0;
        ht1632_data = (bool)(command & mask);
        mask = mask >> 1;
        ht1632_wr = 1;
    }
    ht1632_cs = 1;
    ht1632_data = 0;
}

void sendData( unsigned char address , unsigned char *data , int length)
{
    unsigned short command = 0xa000; //0b1010 0000 0000 0000
    address = address & 0x3F; //avilable 7bit
    command = command | (((unsigned short)address)<<6);   //0b101x xxxx xx00 0000
    unsigned short commandMmask = 0x8000;
    unsigned char dataMmask = 0x80;

    ht1632_wr = 1;
    ht1632_cs = 0;

    for(int i=0 ; i<10 ; i++ )
    {
        ht1632_wr = 0;
        ht1632_data = (bool)(command & commandMmask);
        commandMmask = commandMmask >> 1;
        ht1632_wr = 1;
    }

    for(int j=0 ; j<length ; j++ )
    {
        dataMmask = 0x01;
        for(int i=0 ; i<4 ; i++ )
        {
            ht1632_wr = 0;
            ht1632_data = (bool)(~data[j] & dataMmask);
            dataMmask = dataMmask << 1;
            ht1632_wr = 1;
        }
        
        dataMmask = 0x10;
        for(int i=0 ; i<4 ; i++ )
        {
            ht1632_wr = 0;
            ht1632_data = (bool)(~data[j] & dataMmask);
            dataMmask = dataMmask << 1;
            ht1632_wr = 1;
        }
     }

    ht1632_wr = 1;
    ht1632_cs = 1;
    ht1632_data = 0;
}

void convColor( unsigned char *data , int length )
{
    ;
}