most functionality to splashdwon, find neutral and start mission. short timeouts still in code for testing, will adjust to go directly to sit_idle after splashdown
Dependencies: mbed MODSERIAL FATFileSystem
ltc1298.cpp
00001 #include "ltc1298.hpp" 00002 00003 SpiADC::SpiADC(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName led) : 00004 _spi(mosi, miso, sclk), // mosi, miso, sclk 00005 adcLed(led), // status led 00006 cs(csel) // chip select 00007 { 00008 } 00009 00010 void SpiADC::initialize() { 00011 //set up the spi bus and frequency 00012 _spi.format(12,0); 00013 _spi.frequency(1000000); 00014 00015 //chip select high puts ltc1298 in standby 00016 cs = 1; 00017 00018 //zero the initial ch0 and ch1 oversampled readings 00019 ch0_filt = 0; 00020 ch1_filt = 0; 00021 ch2_filt = 0; 00022 ch3_filt = 0; 00023 ch4_filt = 0; 00024 ch5_filt = 0; 00025 ch6_filt = 0; 00026 ch7_filt = 0; 00027 00028 //led on to say hello 00029 adcLed = 0; //debug turned off 00030 } 00031 00032 // start an interupt driven trigger of the external ADC 00033 void SpiADC::start() { 00034 interval.attach_us(this, &SpiADC::update, 10000); //this should be a 100 Hz sample rate 00035 } 00036 00037 // stop the interupt driven trigger 00038 void SpiADC::stop() { 00039 interval.detach(); 00040 } 00041 00042 void SpiADC::update() { 00043 //flash the LED 00044 //adcLed = !adcLed; 00045 00046 //chip select low starts data conversion 00047 cs = 0; 00048 00049 //the next thing is the input data word 00050 //it is 4 bits and looks like this 00051 // | start | single/diff | odd/sign | MSB first/LSB first | 00052 // if you want single ended on channel 0 MSB first then input 0xD 00053 // if you want single ended on channel 1 MSB first then input 0xF 00054 00055 // get channel 0 00056 unsigned int byte = _spi.write((0x18)<<2); 00057 //send a dummy byte to receive the data 00058 unsigned int byte1 = _spi.write(0x0); 00059 ch0_raw = byte1; 00060 ch0_filt += (ch0_raw - ch0_filt)/CH0OVERSAMPLE; 00061 00062 cs = 1; 00063 cs = 0; 00064 00065 byte = _spi.write((0x19)<<2); 00066 //send a dummy byte to receive the data 00067 byte1 = _spi.write(0x0); 00068 ch1_raw = byte1; 00069 ch1_filt += (ch1_raw - ch1_filt)/CH1OVERSAMPLE; 00070 00071 //switch chip select back to high 00072 cs = 1; 00073 cs = 0; 00074 00075 byte = _spi.write((0x1A)<<2); 00076 //send a dummy byte to receive the data 00077 byte1 = _spi.write(0x0); 00078 ch2_raw = byte1; 00079 ch2_filt += (ch2_raw - ch2_filt)/CH2OVERSAMPLE; 00080 00081 //switch chip select back to high 00082 cs = 1; 00083 cs = 0; 00084 00085 byte = _spi.write((0x1B)<<2); 00086 //send a dummy byte to receive the data 00087 byte1 = _spi.write(0x0); 00088 ch3_raw = byte1; 00089 ch3_filt += (ch3_raw - ch3_filt)/CH3OVERSAMPLE; 00090 00091 //switch chip select back to high 00092 cs = 1; 00093 00094 cs = 0; 00095 00096 byte = _spi.write((0x1C)<<2); 00097 //send a dummy byte to receive the data 00098 byte1 = _spi.write(0x0); 00099 ch4_raw = byte1; 00100 ch4_filt += (ch4_raw - ch4_filt)/CH4OVERSAMPLE; 00101 00102 //switch chip select back to high 00103 cs = 1; 00104 00105 cs = 0; 00106 00107 byte = _spi.write((0x1D)<<2); 00108 //send a dummy byte to receive the data 00109 byte1 = _spi.write(0x0); 00110 ch5_raw = byte1; 00111 ch5_filt += (ch5_raw - ch5_filt)/CH5OVERSAMPLE; 00112 00113 //switch chip select back to high 00114 cs = 1; 00115 00116 cs = 0; 00117 00118 byte = _spi.write((0x1E)<<2); 00119 //send a dummy byte to receive the data 00120 byte1 = _spi.write(0x0); 00121 ch6_raw = byte1; 00122 ch6_filt += (ch6_raw - ch6_filt)/CH6OVERSAMPLE; 00123 00124 //switch chip select back to high 00125 cs = 1; 00126 00127 cs = 0; 00128 00129 byte = _spi.write((0x1F)<<2); 00130 //send a dummy byte to receive the data 00131 byte1 = _spi.write(0x0); 00132 ch7_raw = byte1; 00133 ch7_filt += (ch7_raw - ch7_filt)/CH7OVERSAMPLE; 00134 00135 //switch chip select back to high 00136 cs = 1; 00137 00138 return ; 00139 } 00140 00141 int SpiADC::readCh0() { 00142 return ch0_filt; 00143 } 00144 00145 int SpiADC::readCh1() { 00146 return ch1_filt; 00147 } 00148 00149 int SpiADC::readCh2() { 00150 return ch2_filt; 00151 } 00152 00153 int SpiADC::readCh3() { 00154 return ch3_filt; 00155 } 00156 00157 int SpiADC::readCh4() { 00158 return ch4_filt; 00159 } 00160 00161 int SpiADC::readCh5() { 00162 return ch5_filt; 00163 } 00164 00165 int SpiADC::readCh6() { 00166 return ch6_filt; 00167 } 00168 00169 int SpiADC::readCh7() { 00170 return ch7_filt; 00171 } 00172 00173 int SpiADC::readRawCh0() { 00174 return ch0_raw; 00175 } 00176 00177 int SpiADC::readRawCh1() { 00178 return ch1_raw; 00179 } 00180 00181 int SpiADC::readRawCh2() { 00182 return ch2_raw; 00183 } 00184 00185 int SpiADC::readRawCh3() { 00186 return ch3_raw; 00187 } 00188 00189 int SpiADC::readRawCh4() { 00190 return ch4_raw; 00191 } 00192 00193 int SpiADC::readRawCh5() { 00194 return ch5_raw; 00195 } 00196 00197 int SpiADC::readRawCh6() { 00198 return ch6_raw; 00199 } 00200 00201 int SpiADC::readRawCh7() { 00202 return ch7_raw; 00203 }
Generated on Thu Jul 14 2022 10:54:35 by 1.7.2