rgbyteclock-code/led1642gw.c

199 lines
4.8 KiB
C
Raw Normal View History

2014-03-18 01:04:18 +01:00
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <struppi@struppi.name> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
* (c) 2014 Stefan Rupp
* ----------------------------------------------------------------------------
*/
#include <string.h>
#include "led1642gw.h"
#include "led1642gw_config.h"
#define NUM_LED1642GW_CHANNELS (16) // number of LED channels per IC
//total numer of channels. needed to calculate the buffer size.
2014-03-18 01:04:18 +01:00
#define NUM_LED_CHANNELS (NUM_LED1642GW_CHANNELS*NUM_LED1642GW_ICs)
/* The buffer to hold the LED values.
* The data in this buffer can be manipulated with
* e.g. led1642gw_set().
* calling led1642gw_flush() sends the data in this buffer
* the data registers of the LED1642 ICs.
*/
2014-03-18 01:04:18 +01:00
static uint16_t ledbuffer[NUM_LED_CHANNELS];
/*
* Write 16 bits of \data, with LE set high
* for the number of clock cycles specified in \le_clocks.
* MSB comes first, LSB is last.
*/
2014-03-18 01:04:18 +01:00
static void write_data(uint16_t data, uint8_t le_clocks)
{
uint16_t mask = 0x8000;
int8_t bit;
SET_LE_L();
for (bit=15; bit>=le_clocks; bit--) {
SET_CLK_L();
if(data&mask) { SET_SDI_H(); }
else { SET_SDI_L(); }
SET_CLK_H();
mask >>= 1;
}
SET_LE_H();
for (/*noting to initialize*/; bit>=0; bit--) {
SET_CLK_L();
if(data&mask) { SET_SDI_H(); }
else { SET_SDI_L(); }
SET_CLK_H();
mask >>= 1;
}
// set all pins to low after transmission
2014-03-18 01:04:18 +01:00
SET_CLK_L();
SET_LE_L();
SET_SDI_L();
}
/*
* Write data to BRIGHTNESS DATA LATCH register.
* that means setting LE high for 3 or 4 clock cycles
*/
2014-03-18 01:04:18 +01:00
static void write_data_latch(uint16_t data)
{
write_data(data, 4);
}
/*
* Write data to BRIGHTNESS GLOBAL LATCH register.
* that means setting LE high for 5 or 6 clock cycles
*/
2014-03-18 01:04:18 +01:00
static void write_global_latch(uint16_t data)
{
write_data(data, 6);
}
/*
* This function shifts data through the 16bit shift
* register of the LED1642GW, without writing the data
* to any internal register of the IC.
* This way, we can daisy chain an bunch of LED1642GW ICs,
* and still get data through to any of those.
*/
2014-03-18 01:04:18 +01:00
static void write_no_command(uint16_t data)
{
write_data(data, 0);
}
/*
* Turn all channels on, so the data in the DATA LATCH
* register affects the LEDs attached to the IC.
*/
2014-03-18 01:04:18 +01:00
void led1642gw_turn_all_on(void)
{
write_data(0xffff, 2);
write_data(0xffff, 2);
write_data(0xffff, 2);
}
/*
* Turn all channels off,
*/
2014-03-18 01:04:18 +01:00
void led1642gw_turn_all_off(void)
{
write_data(0x0000, 2);
write_data(0x0000, 2);
write_data(0x0000, 2);
}
/*
* Initialize the pins of the ATMega processor
* to drive the data signals to the ICs
* and initialize the LED buffer to zero.
*/
2014-03-18 01:04:18 +01:00
void led1642gw_init(void)
{
SET_CLK_L();
SET_SDI_L();
SET_LE_L();
DDR_CLK |= (1<<PIN_CLK);
DDR_SDI |= (1<<PIN_SDI);
DDR_LE |= (1<<PIN_LE);
memset(ledbuffer, 0x00, sizeof(ledbuffer));
led1642gw_flush();
}
/*
* Transmit data from the ledbuffer to the BRIGHTNESS latches of
* the LED driver ICs.
* Let's assume, we have n LED1642GW ICs daisy chained. Then
* we write n-1 times with write_no_command, to shift all
* data through the 16bit shift registers of each of the ICs.
* Then we once write with write_data_latch to store the data
* in the BRIGHTNESS DATA registers of the respective ICs.
* We do this for all but the last set of brightness data,
* where we don't write to the DATA LATCH, but to the GLOBAL DATA LATCH.
*/
2014-03-18 01:04:18 +01:00
void led1642gw_flush(void)
{
uint8_t channel;
uint8_t ic;
// for each of the first 15 channels, do the following:
2014-03-18 01:04:18 +01:00
for (channel=0; channel<NUM_LED1642GW_CHANNELS-1; channel++) {
// shift data throught the first n-1 ICs with write_no_command
2014-03-18 01:04:18 +01:00
for (ic=0; ic<(NUM_LED1642GW_ICs-1); ic++) {
write_no_command(ledbuffer[channel+(NUM_LED1642GW_CHANNELS*ic)]);
}
// then, when the brightness data has propagated through the
// shift registers, write all data into the DATA LATCH of
// all of the ICs.
2014-03-18 01:04:18 +01:00
write_data_latch(ledbuffer[channel+(ic*NUM_LED1642GW_CHANNELS)]);
}
// for the 16th channel, we don't write to the DATA LATCH, but
// to the CLOBAL data latch.
// once more, we do the trick with write_no_command, to
// shift data through all the ICs
2014-03-18 01:04:18 +01:00
for (ic=1; ic<NUM_LED1642GW_ICs; ic++) {
write_no_command(ledbuffer[(ic*NUM_LED1642GW_CHANNELS)-1]);
}
// than, at last, write data to the global latch, to force
// the ICs to update their brightness data from the DATA LATCHES.
2014-03-18 01:04:18 +01:00
write_global_latch(ledbuffer[(ic*NUM_LED1642GW_CHANNELS)-1]);
}
void led1642gw_set_channel(uint8_t channel, uint16_t value)
{
if (channel < NUM_LED_CHANNELS) {
ledbuffer[channel] = value;
}
}
void led1642gw_clear(void)
{
memset(ledbuffer, 0x00, sizeof(ledbuffer));
}