AD7680 Library

Committer:
wacomg
Date:
Wed Apr 19 09:58:17 2017 +0000
Revision:
0:91b4ea0c12f8
AD7680 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wacomg 0:91b4ea0c12f8 1 /***************************************************************************
wacomg 0:91b4ea0c12f8 2 * @author Wacomg
wacomg 0:91b4ea0c12f8 3 *
wacomg 0:91b4ea0c12f8 4 * @section LICENSE
wacomg 0:91b4ea0c12f8 5 *
wacomg 0:91b4ea0c12f8 6 * Copyright (c) 2017 Wacomg
wacomg 0:91b4ea0c12f8 7 *
wacomg 0:91b4ea0c12f8 8 * @section DESCRIPTION
wacomg 0:91b4ea0c12f8 9 *
wacomg 0:91b4ea0c12f8 10 * AD7680.CPP
wacomg 0:91b4ea0c12f8 11 * Source file for AD7680 class library
wacomg 0:91b4ea0c12f8 12 * The AD7680 is a 16-bits, 1-channel, SPI-interfaced ADC from Analog Devices
wacomg 0:91b4ea0c12f8 13 *
wacomg 0:91b4ea0c12f8 14 *****************************************************************************/
wacomg 0:91b4ea0c12f8 15
wacomg 0:91b4ea0c12f8 16 #include "ad7680.h"
wacomg 0:91b4ea0c12f8 17
wacomg 0:91b4ea0c12f8 18
wacomg 0:91b4ea0c12f8 19 // Constructor
wacomg 0:91b4ea0c12f8 20 AD7680::AD7680(PinName MISO, PinName SCLK, PinName CS, int frequency) : _spi(NC, MISO, SCLK), _cs(CS) {
wacomg 0:91b4ea0c12f8 21 _spi.frequency(frequency);
wacomg 0:91b4ea0c12f8 22 _spi.format(24, 0);
wacomg 0:91b4ea0c12f8 23 _cs = 1;
wacomg 0:91b4ea0c12f8 24 _q = (double) 5.0/65535.0;;
wacomg 0:91b4ea0c12f8 25 };
wacomg 0:91b4ea0c12f8 26
wacomg 0:91b4ea0c12f8 27
wacomg 0:91b4ea0c12f8 28
wacomg 0:91b4ea0c12f8 29 void AD7680::readRAW(int16_t *rawDataBuffer) {
wacomg 0:91b4ea0c12f8 30 uint8_t lowByte, medByte, highByte;
wacomg 0:91b4ea0c12f8 31 _cs = 0;
wacomg 0:91b4ea0c12f8 32 highByte = _spi.write(0x00);
wacomg 0:91b4ea0c12f8 33 medByte = _spi.write(0x00);
wacomg 0:91b4ea0c12f8 34 lowByte = _spi.write(0x00);
wacomg 0:91b4ea0c12f8 35
wacomg 0:91b4ea0c12f8 36 *(rawDataBuffer + 0) = (int16_t) ((highByte << 12) + (medByte<<4) + (lowByte>>4));
wacomg 0:91b4ea0c12f8 37 _cs = 1;
wacomg 0:91b4ea0c12f8 38 }
wacomg 0:91b4ea0c12f8 39
wacomg 0:91b4ea0c12f8 40 void AD7680::readAnalog(double *analogDataBuffer) {
wacomg 0:91b4ea0c12f8 41 uint8_t lowByte, medByte, highByte;
wacomg 0:91b4ea0c12f8 42 _cs = 0;
wacomg 0:91b4ea0c12f8 43 highByte = _spi.write(0x00);
wacomg 0:91b4ea0c12f8 44 medByte = _spi.write(0x00);
wacomg 0:91b4ea0c12f8 45 lowByte = _spi.write(0x00);
wacomg 0:91b4ea0c12f8 46
wacomg 0:91b4ea0c12f8 47 *(analogDataBuffer +0 ) = (double) (uint16_t)((highByte << 12) + (medByte<<4) + (lowByte>>4))*_q;
wacomg 0:91b4ea0c12f8 48 _cs = 1;
wacomg 0:91b4ea0c12f8 49 }