TLC5940 library which supports SWSPI, has API to specify grayscale PWM period and has API like Arduino library.

Fork of TLC5940 by Stefan Nielsen

Committer:
deton
Date:
Tue Oct 13 12:20:23 2015 +0000
Revision:
2:b411648dfe54
Parent:
1:b188393f5b49
Child:
3:2c0af5f5fa13
Remove unused setAll().; ; Comment out sclk pulse before grayscale data,; becaue it works without this pulse.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Julepalme 0:bdf7a64b89a7 1 #include "TLC5940.h"
deton 1:b188393f5b49 2 #define GSCLK_PERIOD 5
Julepalme 0:bdf7a64b89a7 3
deton 1:b188393f5b49 4 TLC5940::TLC5940(PinName mosi, PinName miso, PinName sck, PinName xlat, PinName blank, PinName gsclk):
deton 1:b188393f5b49 5 _spi(SWSPI(mosi, miso, sck)),
deton 1:b188393f5b49 6 _gsclk(PwmOut(gsclk)),
deton 1:b188393f5b49 7 _sclk(DigitalOut(sck)),
deton 1:b188393f5b49 8 _xlat(DigitalOut(xlat)),
deton 1:b188393f5b49 9 _blank(DigitalOut(blank)),
deton 1:b188393f5b49 10 _t(Ticker())
Julepalme 0:bdf7a64b89a7 11 {
deton 1:b188393f5b49 12 _spi.format(12,0);
deton 1:b188393f5b49 13 _spi.frequency(30000000);
deton 1:b188393f5b49 14
deton 1:b188393f5b49 15 // Turn off GSCLK
deton 1:b188393f5b49 16 _gsclk.write(0.0f);
deton 1:b188393f5b49 17 _gsclk.period_us(GSCLK_PERIOD);
deton 1:b188393f5b49 18
Julepalme 0:bdf7a64b89a7 19 // Reset to 0
Julepalme 0:bdf7a64b89a7 20 for(int i = 0; i < 16; i++)
Julepalme 0:bdf7a64b89a7 21 {
deton 1:b188393f5b49 22 gs_data[i] = 0;
deton 1:b188393f5b49 23 int whoami = _spi.write(0);
Julepalme 0:bdf7a64b89a7 24 }
deton 1:b188393f5b49 25
deton 1:b188393f5b49 26 _xlat.write(1);
deton 1:b188393f5b49 27 _xlat.write(0);
Julepalme 0:bdf7a64b89a7 28 }
Julepalme 0:bdf7a64b89a7 29
deton 1:b188393f5b49 30 void TLC5940::set(int channel, uint16_t brightness)
Julepalme 0:bdf7a64b89a7 31 {
deton 1:b188393f5b49 32 gs_data[15-channel] = brightness;
deton 1:b188393f5b49 33 }
deton 1:b188393f5b49 34
Julepalme 0:bdf7a64b89a7 35
Julepalme 0:bdf7a64b89a7 36 void TLC5940::flush()
Julepalme 0:bdf7a64b89a7 37 {
deton 2:b411648dfe54 38 //_sclk.write(1);
deton 2:b411648dfe54 39 //_sclk.write(0);
deton 1:b188393f5b49 40
Julepalme 0:bdf7a64b89a7 41 for(int i = 0; i < 16; i++){
deton 1:b188393f5b49 42 _spi.write(gs_data[i]);
Julepalme 0:bdf7a64b89a7 43 }
deton 1:b188393f5b49 44
deton 1:b188393f5b49 45 _xlat.write(1);
deton 1:b188393f5b49 46 _xlat.write(0);
Julepalme 0:bdf7a64b89a7 47 }
Julepalme 0:bdf7a64b89a7 48
Julepalme 0:bdf7a64b89a7 49 void TLC5940::run()
Julepalme 0:bdf7a64b89a7 50 {
deton 1:b188393f5b49 51 _gsclk.write(0.5f);
deton 1:b188393f5b49 52 _t.attach_us(this, &TLC5940::refresh, GSCLK_PERIOD*4096);
Julepalme 0:bdf7a64b89a7 53 }
Julepalme 0:bdf7a64b89a7 54
Julepalme 0:bdf7a64b89a7 55 void TLC5940::refresh()
Julepalme 0:bdf7a64b89a7 56 {
deton 1:b188393f5b49 57 _blank.write(1);
deton 1:b188393f5b49 58 _blank.write(0);
deton 1:b188393f5b49 59 }