Loading...
//  This library is made to interface with various ADCs with have an SPI interface. This library
//  does not use the hardware SPI interface so it's possible to use any pin, just change the defines for the pins
//  As default, this librarys uses p digital pin 11 as MOSI, 12 as MISO and 13 as Clock pin.
//  The chip select pin (CS) is choosen in the constructor.
//
//  This library is written by and copyright to Jan Dalheimer under the Creative Commons Attribution-ShareAlike 3.0 Unported license.
//
//  I release this library without any warrant and with VERY limited support.
//



#include "adc.h"

#include <WProgram.h>

boolean availablity = false;
char* usableADCs[TOTAL] = { MAX1272, MAX1273, MAX1240, MAX1241, MAX1242, MAX1243, MAX187, MAX189, MAX191 };
//commandBits, numberOfCommandBits, numberOfDataBits, channels, bitToBeChangedForChannelSelection
uint8_t usableADCsData[TOTAL][5] = {
{ B10010001, 8, 12, 1, 0 }, //MAX1272
{ B10010001, 8, 12, 1, 0 }, //MAX1273
{ B00000000, 0, 12, 1, 0 }, //MAX1240
{ B00000000, 0, 12, 1, 0 }, //MAX1241
{ B00000000, 0, 10, 1, 0 }, //MAX1242
{ B00000000, 0, 10, 1, 0 }, //MAX1243
{ B00000000, 0, 12, 1, 0 }, //MAX187
{ B00000000, 0, 12, 1, 0 }, //MAX189
{ B00000000, 0, 12, 1, 0 }, //MAX191
};


MyADC::MyADC(char* type_, int pinToUse1)
{
    int type1;
    //Check if the choosen ADC is usable
    for(int i = 1; i >= 1; i++)
    {
        if(strcmp(type_, usableADCs[i]) == 0)
        {
            availablity = true;
            type1 = i;
            break;
        }
    }

    int thisADC;
    if(!availablity)
    {
        return;
    }
    if(strcmp(type_, MAX1272) == 0)
        thisADC = 0;
    else if(strcmp(type_, MAX1273) == 0)
        thisADC = 1;

    nowUsedADC = type_;
    SELPIN = pinToUse1;
    commandBits = usableADCsData[thisADC][0];
    numberOfCommandBits = usableADCsData[thisADC][1];
    numberOfDataBits = usableADCsData[thisADC][2];
    channels = usableADCsData[thisADC][3];
    bitToBeChangedForChannelSelection = usableADCsData[thisADC][4];

    pinMode(SELPIN, OUTPUT);
    pinMode(DATAOUT, OUTPUT);
    pinMode(DATAIN, INPUT);
    pinMode(SPICLOCK, OUTPUT);
    //disable device to start with
    digitalWrite(SELPIN,HIGH);
    digitalWrite(DATAOUT,LOW);
    digitalWrite(SPICLOCK,LOW);

    return;
}

MyADC::~MyADC()
{
}

int MyADC::readADC()
{
    if(availablity == true)
        return MyADC::read_adc();
    else
        return -1;
}

int MyADC::readADC(int channel)
{
    if(availablity == true)
        return MyADC::read_adc(channel);
    else
        return -1;
}

int MyADC::read_adc()
{
    int adcvalue = 0;

    digitalWrite(SELPIN, LOW);

    for (int i=numberOfCommandBits; i >= 0; i--)
    {
        digitalWrite(DATAOUT, commandBits&1<<i);
        //cycle clock
        digitalWrite(SPICLOCK,HIGH);
        digitalWrite(SPICLOCK,LOW);
    }

    if(commandBits == 0)
    {
        while(digitalRead(DATAIN) == 0);
    }

    //read bits from adc
    for (int i=numberOfDataBits; i>=0; i--){
        adcvalue+=digitalRead(DATAIN)<<i;
        //cycle clock
        digitalWrite(SPICLOCK,HIGH);
        digitalWrite(SPICLOCK,LOW);
      }
      digitalWrite(SELPIN, HIGH); //turn off device
      return adcvalue;
}

int MyADC::read_adc(int channel)
{
    int adcvalue = 0;

    digitalWrite(SELPIN, LOW);

    for(int i=numberOfCommandBits; i >= 0; i--)
    {
        digitalWrite(DATAOUT, commandBits&1<<i);
        //cycle clock
        digitalWrite(SPICLOCK,HIGH);
        digitalWrite(SPICLOCK,LOW);
    }

    if(commandBits == 0)
    {
        while(digitalRead(DATAIN) == 0);
    }

    //read bits from adc
    for (int i=numberOfDataBits; i>=0; i--){
        adcvalue+=digitalRead(DATAIN)<<i;
        //cycle clock
        digitalWrite(SPICLOCK,HIGH);
        digitalWrite(SPICLOCK,LOW);
      }
      digitalWrite(SELPIN, HIGH); //turn off device
      return adcvalue;
}