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

Committer:
omatthews
Date:
Mon Aug 19 07:55:44 2019 +0000
Revision:
3:7c4fd26c696e
Parent:
2:52a40eb59b06
10/08/2019

Who changed what in which revision?

UserRevisionLine numberNew 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"
omatthews 3:7c4fd26c696e 10 extern DigitalOut led_0;
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
omatthews 3:7c4fd26c696e 48
AlexStokoe 0:d59f808e1719 49 converts = channels;
AlexStokoe 0:d59f808e1719 50 wait_us(1);
AlexStokoe 0:d59f808e1719 51 converts = 0;
AlexStokoe 0:d59f808e1719 52
AlexStokoe 0:d59f808e1719 53 //save which channels have been converted
AlexStokoe 0:d59f808e1719 54 channels_read = channels;
AlexStokoe 0:d59f808e1719 55 }
AlexStokoe 0:d59f808e1719 56
AlexStokoe 0:d59f808e1719 57 int ADS8568_ADC::read_channels()
AlexStokoe 0:d59f808e1719 58 {
AlexStokoe 0:d59f808e1719 59
AlexStokoe 0:d59f808e1719 60 //SPI(like) data transfer
AlexStokoe 0:d59f808e1719 61 adc_nselect = 0;
AlexStokoe 0:d59f808e1719 62 bus.write(&dummy, 1, buffer, 16);
AlexStokoe 0:d59f808e1719 63 adc_nselect=1;
AlexStokoe 0:d59f808e1719 64
AlexStokoe 0:d59f808e1719 65 //loop over bytes to add channel voltage values
AlexStokoe 0:d59f808e1719 66 for (int x=0; x<8; x++){
AlexStokoe 0:d59f808e1719 67 val_array[x] = buffer[2*x]<<8 | buffer[(2*x) + 1];
omatthews 2:52a40eb59b06 68 values [x] = val_array[x];
AlexStokoe 0:d59f808e1719 69 }
AlexStokoe 0:d59f808e1719 70
AlexStokoe 0:d59f808e1719 71
AlexStokoe 0:d59f808e1719 72 return 0;
AlexStokoe 0:d59f808e1719 73 }
AlexStokoe 0:d59f808e1719 74
AlexStokoe 0:d59f808e1719 75 int ADS8568_ADC::read_channel_result(int channel)
AlexStokoe 0:d59f808e1719 76 {
AlexStokoe 0:d59f808e1719 77 //make sure channel exists to prevent memory error
AlexStokoe 0:d59f808e1719 78 if(channel >= 0 && channel < 8)
AlexStokoe 0:d59f808e1719 79 {
AlexStokoe 0:d59f808e1719 80 //update channels_read to indicate which data has been converted but not read into application
AlexStokoe 0:d59f808e1719 81 channels_read = channels_read - (channels_read & channel);
AlexStokoe 0:d59f808e1719 82
AlexStokoe 0:d59f808e1719 83 //return ADC channel value
AlexStokoe 0:d59f808e1719 84 return values[channel];
AlexStokoe 0:d59f808e1719 85 }
AlexStokoe 0:d59f808e1719 86
AlexStokoe 0:d59f808e1719 87 //if error return -1
AlexStokoe 0:d59f808e1719 88 return -1;
AlexStokoe 0:d59f808e1719 89 }
AlexStokoe 0:d59f808e1719 90
AlexStokoe 0:d59f808e1719 91 int ADS8568_ADC::new_data_channels()
AlexStokoe 0:d59f808e1719 92 {
AlexStokoe 0:d59f808e1719 93 //check which channels have been converted but not read yet
AlexStokoe 0:d59f808e1719 94 return(channels_read);
AlexStokoe 0:d59f808e1719 95 }