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

Committer:
mkelly10
Date:
Fri Oct 20 11:41:22 2017 +0000
Revision:
9:d5fcdcb3c89d
Child:
10:085ab7328054
Tested 10/19/17 Folders

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mkelly10 9:d5fcdcb3c89d 1 #include "ltc1298.hpp"
mkelly10 9:d5fcdcb3c89d 2
mkelly10 9:d5fcdcb3c89d 3 SpiADC::SpiADC(): _spi(p5,p6,p7),adcLed(LED2),cs(p8) //maybe this should configurable on construction
mkelly10 9:d5fcdcb3c89d 4 {
mkelly10 9:d5fcdcb3c89d 5
mkelly10 9:d5fcdcb3c89d 6 }
mkelly10 9:d5fcdcb3c89d 7
mkelly10 9:d5fcdcb3c89d 8 void SpiADC::initialize()
mkelly10 9:d5fcdcb3c89d 9 {
mkelly10 9:d5fcdcb3c89d 10 //set up the spi bus and frequency
mkelly10 9:d5fcdcb3c89d 11 _spi.format(13,0);
mkelly10 9:d5fcdcb3c89d 12 _spi.frequency(1000000);
mkelly10 9:d5fcdcb3c89d 13 //chip select high puts ltc1298 in standby
mkelly10 9:d5fcdcb3c89d 14 adcLed = 1;
mkelly10 9:d5fcdcb3c89d 15 ch0_filt = 0;
mkelly10 9:d5fcdcb3c89d 16 ch1_filt = 0;
mkelly10 9:d5fcdcb3c89d 17 cs = 1;
mkelly10 9:d5fcdcb3c89d 18
mkelly10 9:d5fcdcb3c89d 19 }
mkelly10 9:d5fcdcb3c89d 20
mkelly10 9:d5fcdcb3c89d 21 //This starts an interupt driven trigger of the external ADC
mkelly10 9:d5fcdcb3c89d 22 void SpiADC::start()
mkelly10 9:d5fcdcb3c89d 23 {
mkelly10 9:d5fcdcb3c89d 24 interval.attach_us(this, &SpiADC::update, 100); //this should be a 10 kHz sample rate
mkelly10 9:d5fcdcb3c89d 25 }
mkelly10 9:d5fcdcb3c89d 26
mkelly10 9:d5fcdcb3c89d 27 //This stops it
mkelly10 9:d5fcdcb3c89d 28 void SpiADC::stop()
mkelly10 9:d5fcdcb3c89d 29 {
mkelly10 9:d5fcdcb3c89d 30 interval.detach();
mkelly10 9:d5fcdcb3c89d 31 }
mkelly10 9:d5fcdcb3c89d 32
mkelly10 9:d5fcdcb3c89d 33
mkelly10 9:d5fcdcb3c89d 34 void SpiADC::update()
mkelly10 9:d5fcdcb3c89d 35 {
mkelly10 9:d5fcdcb3c89d 36 //flash the LED
mkelly10 9:d5fcdcb3c89d 37 adcLed = !adcLed;
mkelly10 9:d5fcdcb3c89d 38
mkelly10 9:d5fcdcb3c89d 39 //chip select low starts data conversion
mkelly10 9:d5fcdcb3c89d 40 cs = 0;
mkelly10 9:d5fcdcb3c89d 41 //the next thing is the input data word
mkelly10 9:d5fcdcb3c89d 42 //it is 4 bits and looks like this
mkelly10 9:d5fcdcb3c89d 43 // | start | single/diff | odd/sign | MSB first/LSB first |
mkelly10 9:d5fcdcb3c89d 44 // if you want single ended on channel 0 MSB first then input 0xD
mkelly10 9:d5fcdcb3c89d 45 // if you want single ended on channel 1 MSB first then input 0xF
mkelly10 9:d5fcdcb3c89d 46
mkelly10 9:d5fcdcb3c89d 47 // get channel 0
mkelly10 9:d5fcdcb3c89d 48 unsigned int byte = _spi.write(0xD);
mkelly10 9:d5fcdcb3c89d 49 //send a dummy byte to receive the data
mkelly10 9:d5fcdcb3c89d 50 unsigned int byte1 = _spi.write(0x0);
mkelly10 9:d5fcdcb3c89d 51 ch0_raw = byte1;
mkelly10 9:d5fcdcb3c89d 52 ch0_filt += (ch0_raw - ch0_filt)/CH0OVERSAMPLE;
mkelly10 9:d5fcdcb3c89d 53
mkelly10 9:d5fcdcb3c89d 54 cs = 1;
mkelly10 9:d5fcdcb3c89d 55 cs = 0;
mkelly10 9:d5fcdcb3c89d 56
mkelly10 9:d5fcdcb3c89d 57 byte = _spi.write(0xF);
mkelly10 9:d5fcdcb3c89d 58 //send a dummy byte to receive the data
mkelly10 9:d5fcdcb3c89d 59 byte1 = _spi.write(0x0);
mkelly10 9:d5fcdcb3c89d 60 ch1_raw = byte1;
mkelly10 9:d5fcdcb3c89d 61 ch1_filt += (ch1_raw - ch1_filt)/CH1OVERSAMPLE;
mkelly10 9:d5fcdcb3c89d 62
mkelly10 9:d5fcdcb3c89d 63 //switch chip select back to high
mkelly10 9:d5fcdcb3c89d 64 cs = 1;
mkelly10 9:d5fcdcb3c89d 65 return ;
mkelly10 9:d5fcdcb3c89d 66 }
mkelly10 9:d5fcdcb3c89d 67
mkelly10 9:d5fcdcb3c89d 68 int SpiADC::readCh0()
mkelly10 9:d5fcdcb3c89d 69 {
mkelly10 9:d5fcdcb3c89d 70 return ch0_filt;
mkelly10 9:d5fcdcb3c89d 71 }
mkelly10 9:d5fcdcb3c89d 72
mkelly10 9:d5fcdcb3c89d 73 int SpiADC::readCh1()
mkelly10 9:d5fcdcb3c89d 74 {
mkelly10 9:d5fcdcb3c89d 75 return ch1_filt;
mkelly10 9:d5fcdcb3c89d 76 }