Code for controlling servos using the TLC5940 pwm led driver.
Embed:
(wiki syntax)
Show/hide line numbers
TLC5940.cpp
00001 #include "TLC5940.h" 00002 00003 TLC5940::TLC5940(PinName mosi, PinName miso, PinName sck, PinName xlat, PinName blank, PinName gsclk) 00004 { 00005 _spi = new SPI(mosi, miso, sck); 00006 _xlat = new DigitalOut(xlat); 00007 _blank = new DigitalOut(blank); 00008 _gsclk = new PwmOut(gsclk); 00009 _t = new Ticker(); 00010 00011 // Setup SPI 00012 _spi->format(12,0); 00013 _spi->frequency(5000000); 00014 00015 //Turn off GSCLK 00016 _gsclk->write(0.0f); 00017 _gsclk->period_us(5); 00018 00019 // Reset to 0 00020 for(int i = 0; i < 16; i++) 00021 { 00022 gs_data[i] = 4095; 00023 int whoami = _spi->write(4095); 00024 } 00025 00026 _xlat->write(1); 00027 _xlat->write(0); 00028 } 00029 00030 void TLC5940::setServo(int ch, int val) 00031 { 00032 gs_data[15-ch] = val; 00033 } 00034 00035 void TLC5940::flush() 00036 { 00037 for(int i = 0; i < 16; i++){ 00038 _spi->write(gs_data[i]); 00039 } 00040 00041 _xlat->write(1); 00042 _xlat->write(0); 00043 } 00044 00045 void TLC5940::run() 00046 { 00047 _gsclk->write(0.5f); 00048 _t->attach_us(this, &TLC5940::refresh, 5*4096); 00049 } 00050 00051 void TLC5940::refresh() 00052 { 00053 _blank->write(1); 00054 _blank->write(0); 00055 }
Generated on Tue Jul 19 2022 00:52:38 by
1.7.2