TLC5940 library which supports SWSPI, has API to specify grayscale PWM period and has API like Arduino library.
Fork of TLC5940 by
TLC5940.cpp@0:bdf7a64b89a7, 2013-03-24 (annotated)
- Committer:
- Julepalme
- Date:
- Sun Mar 24 16:23:29 2013 +0000
- Revision:
- 0:bdf7a64b89a7
- Child:
- 1:b188393f5b49
s
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Julepalme | 0:bdf7a64b89a7 | 1 | #include "TLC5940.h" |
Julepalme | 0:bdf7a64b89a7 | 2 | |
Julepalme | 0:bdf7a64b89a7 | 3 | TLC5940::TLC5940(PinName mosi, PinName miso, PinName sck, PinName xlat, PinName blank, PinName gsclk) |
Julepalme | 0:bdf7a64b89a7 | 4 | { |
Julepalme | 0:bdf7a64b89a7 | 5 | _spi = new SPI(mosi, miso, sck); |
Julepalme | 0:bdf7a64b89a7 | 6 | _xlat = new DigitalOut(xlat); |
Julepalme | 0:bdf7a64b89a7 | 7 | _blank = new DigitalOut(blank); |
Julepalme | 0:bdf7a64b89a7 | 8 | _gsclk = new PwmOut(gsclk); |
Julepalme | 0:bdf7a64b89a7 | 9 | _t = new Ticker(); |
Julepalme | 0:bdf7a64b89a7 | 10 | |
Julepalme | 0:bdf7a64b89a7 | 11 | // Setup SPI |
Julepalme | 0:bdf7a64b89a7 | 12 | _spi->format(12,0); |
Julepalme | 0:bdf7a64b89a7 | 13 | _spi->frequency(5000000); |
Julepalme | 0:bdf7a64b89a7 | 14 | |
Julepalme | 0:bdf7a64b89a7 | 15 | //Turn off GSCLK |
Julepalme | 0:bdf7a64b89a7 | 16 | _gsclk->write(0.0f); |
Julepalme | 0:bdf7a64b89a7 | 17 | _gsclk->period_us(5); |
Julepalme | 0:bdf7a64b89a7 | 18 | |
Julepalme | 0:bdf7a64b89a7 | 19 | // Reset to 0 |
Julepalme | 0:bdf7a64b89a7 | 20 | for(int i = 0; i < 16; i++) |
Julepalme | 0:bdf7a64b89a7 | 21 | { |
Julepalme | 0:bdf7a64b89a7 | 22 | gs_data[i] = 4095; |
Julepalme | 0:bdf7a64b89a7 | 23 | int whoami = _spi->write(4095); |
Julepalme | 0:bdf7a64b89a7 | 24 | } |
Julepalme | 0:bdf7a64b89a7 | 25 | |
Julepalme | 0:bdf7a64b89a7 | 26 | _xlat->write(1); |
Julepalme | 0:bdf7a64b89a7 | 27 | _xlat->write(0); |
Julepalme | 0:bdf7a64b89a7 | 28 | } |
Julepalme | 0:bdf7a64b89a7 | 29 | |
Julepalme | 0:bdf7a64b89a7 | 30 | void TLC5940::setServo(int ch, int val) |
Julepalme | 0:bdf7a64b89a7 | 31 | { |
Julepalme | 0:bdf7a64b89a7 | 32 | gs_data[15-ch] = val; |
Julepalme | 0:bdf7a64b89a7 | 33 | } |
Julepalme | 0:bdf7a64b89a7 | 34 | |
Julepalme | 0:bdf7a64b89a7 | 35 | void TLC5940::flush() |
Julepalme | 0:bdf7a64b89a7 | 36 | { |
Julepalme | 0:bdf7a64b89a7 | 37 | for(int i = 0; i < 16; i++){ |
Julepalme | 0:bdf7a64b89a7 | 38 | _spi->write(gs_data[i]); |
Julepalme | 0:bdf7a64b89a7 | 39 | } |
Julepalme | 0:bdf7a64b89a7 | 40 | |
Julepalme | 0:bdf7a64b89a7 | 41 | _xlat->write(1); |
Julepalme | 0:bdf7a64b89a7 | 42 | _xlat->write(0); |
Julepalme | 0:bdf7a64b89a7 | 43 | } |
Julepalme | 0:bdf7a64b89a7 | 44 | |
Julepalme | 0:bdf7a64b89a7 | 45 | void TLC5940::run() |
Julepalme | 0:bdf7a64b89a7 | 46 | { |
Julepalme | 0:bdf7a64b89a7 | 47 | _gsclk->write(0.5f); |
Julepalme | 0:bdf7a64b89a7 | 48 | _t->attach_us(this, &TLC5940::refresh, 5*4096); |
Julepalme | 0:bdf7a64b89a7 | 49 | } |
Julepalme | 0:bdf7a64b89a7 | 50 | |
Julepalme | 0:bdf7a64b89a7 | 51 | void TLC5940::refresh() |
Julepalme | 0:bdf7a64b89a7 | 52 | { |
Julepalme | 0:bdf7a64b89a7 | 53 | _blank->write(1); |
Julepalme | 0:bdf7a64b89a7 | 54 | _blank->write(0); |
Julepalme | 0:bdf7a64b89a7 | 55 | } |