ADC for threaded program
Dependents: LEX_Threaded_Programming MEMSPCR_Control_Program_AD_Logging_Demo_PCB MEMSPCR_Control_Program_AD_Logging_Sync MEMSPCR_Control_Program_AD_Logging_PWM ... more
ADS8568_ADC.cpp@1:cedf75de6aec, 2019-07-16 (annotated)
- Committer:
- AlexStokoe
- Date:
- Tue Jul 16 14:50:51 2019 +0000
- Revision:
- 1:cedf75de6aec
- Parent:
- 0:d59f808e1719
fix adc value accumulation bug;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AlexStokoe | 0:d59f808e1719 | 1 | /*------------------------------------------------------------------------------ |
AlexStokoe | 0:d59f808e1719 | 2 | Library code file for interface to ADS8568 TI 16 bit 8 channel sampling ADC |
AlexStokoe | 0:d59f808e1719 | 3 | Date: 15/07/2018 |
AlexStokoe | 0:d59f808e1719 | 4 | Author: AS7 |
AlexStokoe | 0:d59f808e1719 | 5 | |
AlexStokoe | 0:d59f808e1719 | 6 | |
AlexStokoe | 0:d59f808e1719 | 7 | ------------------------------------------------------------------------------*/ |
AlexStokoe | 0:d59f808e1719 | 8 | |
AlexStokoe | 0:d59f808e1719 | 9 | #include "ADS8568_ADC.h" |
AlexStokoe | 0:d59f808e1719 | 10 | |
AlexStokoe | 0:d59f808e1719 | 11 | |
AlexStokoe | 0:d59f808e1719 | 12 | |
AlexStokoe | 0:d59f808e1719 | 13 | ADS8568_ADC::ADS8568_ADC(PinName mosi, PinName miso, PinName sclk, PinName ncs,PinName reset, PinName convA, PinName convB, PinName convC, PinName convD) |
AlexStokoe | 0:d59f808e1719 | 14 | : bus(mosi, miso, sclk), adc_nselect(ncs), adc_reset(reset), converts(convA, convB, convC, convD) |
AlexStokoe | 0:d59f808e1719 | 15 | { |
AlexStokoe | 0:d59f808e1719 | 16 | /* |
AlexStokoe | 0:d59f808e1719 | 17 | bus = new SPI(mosi, miso, sclk); |
AlexStokoe | 0:d59f808e1719 | 18 | adc_nselect = new DigitalOut(ncs); |
AlexStokoe | 0:d59f808e1719 | 19 | adc_reset = new DigitalOut(reset); |
AlexStokoe | 0:d59f808e1719 | 20 | converts = new BusOut(convA, convB, convC, convD); |
AlexStokoe | 0:d59f808e1719 | 21 | */ |
AlexStokoe | 0:d59f808e1719 | 22 | |
AlexStokoe | 0:d59f808e1719 | 23 | //constants are defined in protected and initialised here |
AlexStokoe | 0:d59f808e1719 | 24 | |
AlexStokoe | 0:d59f808e1719 | 25 | channels_read = 0; |
AlexStokoe | 0:d59f808e1719 | 26 | OSR = 1; |
AlexStokoe | 0:d59f808e1719 | 27 | dummy = 0; |
AlexStokoe | 0:d59f808e1719 | 28 | |
AlexStokoe | 0:d59f808e1719 | 29 | } |
AlexStokoe | 0:d59f808e1719 | 30 | |
AlexStokoe | 0:d59f808e1719 | 31 | void ADS8568_ADC::init() |
AlexStokoe | 0:d59f808e1719 | 32 | { |
AlexStokoe | 0:d59f808e1719 | 33 | //set SPI serial to 2MHz, 16 bit data transfer, mode 2 (clock normally high, data preceeding clock cycle) |
AlexStokoe | 0:d59f808e1719 | 34 | bus.format(8,2); |
AlexStokoe | 0:d59f808e1719 | 35 | bus.frequency(2000000); |
AlexStokoe | 0:d59f808e1719 | 36 | bus.set_default_write_value(0x00); |
AlexStokoe | 0:d59f808e1719 | 37 | adc_nselect = 1; |
AlexStokoe | 0:d59f808e1719 | 38 | |
AlexStokoe | 0:d59f808e1719 | 39 | //Reset ADC sequence |
AlexStokoe | 0:d59f808e1719 | 40 | adc_reset = 1; |
AlexStokoe | 0:d59f808e1719 | 41 | wait_ms(1); |
AlexStokoe | 0:d59f808e1719 | 42 | adc_reset = 0; |
AlexStokoe | 0:d59f808e1719 | 43 | } |
AlexStokoe | 0:d59f808e1719 | 44 | |
AlexStokoe | 0:d59f808e1719 | 45 | void ADS8568_ADC::start_conversion(int channels) |
AlexStokoe | 0:d59f808e1719 | 46 | { |
AlexStokoe | 0:d59f808e1719 | 47 | //send convert signal to all channels |
AlexStokoe | 0:d59f808e1719 | 48 | converts = channels; |
AlexStokoe | 0:d59f808e1719 | 49 | wait_us(1); |
AlexStokoe | 0:d59f808e1719 | 50 | converts = 0; |
AlexStokoe | 0:d59f808e1719 | 51 | |
AlexStokoe | 0:d59f808e1719 | 52 | //save which channels have been converted |
AlexStokoe | 0:d59f808e1719 | 53 | channels_read = channels; |
AlexStokoe | 0:d59f808e1719 | 54 | } |
AlexStokoe | 0:d59f808e1719 | 55 | |
AlexStokoe | 0:d59f808e1719 | 56 | int ADS8568_ADC::read_channels() |
AlexStokoe | 0:d59f808e1719 | 57 | { |
AlexStokoe | 0:d59f808e1719 | 58 | |
AlexStokoe | 0:d59f808e1719 | 59 | //SPI(like) data transfer |
AlexStokoe | 0:d59f808e1719 | 60 | adc_nselect = 0; |
AlexStokoe | 0:d59f808e1719 | 61 | bus.write(&dummy, 1, buffer, 16); |
AlexStokoe | 0:d59f808e1719 | 62 | adc_nselect=1; |
AlexStokoe | 0:d59f808e1719 | 63 | |
AlexStokoe | 0:d59f808e1719 | 64 | //loop over bytes to add channel voltage values |
AlexStokoe | 0:d59f808e1719 | 65 | for (int x=0; x<8; x++){ |
AlexStokoe | 1:cedf75de6aec | 66 | val_array[x]=0; |
AlexStokoe | 1:cedf75de6aec | 67 | |
AlexStokoe | 0:d59f808e1719 | 68 | val_array[x] = buffer[2*x]<<8 | buffer[(2*x) + 1]; |
AlexStokoe | 1:cedf75de6aec | 69 | values [x] = val_array[x]; |
AlexStokoe | 0:d59f808e1719 | 70 | } |
AlexStokoe | 0:d59f808e1719 | 71 | |
AlexStokoe | 0:d59f808e1719 | 72 | |
AlexStokoe | 0:d59f808e1719 | 73 | return 0; |
AlexStokoe | 0:d59f808e1719 | 74 | } |
AlexStokoe | 0:d59f808e1719 | 75 | |
AlexStokoe | 0:d59f808e1719 | 76 | int ADS8568_ADC::read_channel_result(int channel) |
AlexStokoe | 0:d59f808e1719 | 77 | { |
AlexStokoe | 0:d59f808e1719 | 78 | //make sure channel exists to prevent memory error |
AlexStokoe | 0:d59f808e1719 | 79 | if(channel >= 0 && channel < 8) |
AlexStokoe | 0:d59f808e1719 | 80 | { |
AlexStokoe | 0:d59f808e1719 | 81 | //update channels_read to indicate which data has been converted but not read into application |
AlexStokoe | 0:d59f808e1719 | 82 | channels_read = channels_read - (channels_read & channel); |
AlexStokoe | 0:d59f808e1719 | 83 | |
AlexStokoe | 0:d59f808e1719 | 84 | //return ADC channel value |
AlexStokoe | 0:d59f808e1719 | 85 | return values[channel]; |
AlexStokoe | 0:d59f808e1719 | 86 | } |
AlexStokoe | 0:d59f808e1719 | 87 | |
AlexStokoe | 0:d59f808e1719 | 88 | //if error return -1 |
AlexStokoe | 0:d59f808e1719 | 89 | return -1; |
AlexStokoe | 0:d59f808e1719 | 90 | } |
AlexStokoe | 0:d59f808e1719 | 91 | |
AlexStokoe | 0:d59f808e1719 | 92 | int ADS8568_ADC::new_data_channels() |
AlexStokoe | 0:d59f808e1719 | 93 | { |
AlexStokoe | 0:d59f808e1719 | 94 | //check which channels have been converted but not read yet |
AlexStokoe | 0:d59f808e1719 | 95 | return(channels_read); |
AlexStokoe | 0:d59f808e1719 | 96 | } |