Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

4xPROBE 16 bit A/D converter test.

// Built with BFTC Rev. 1.0.0, ven 26 dicembre 2014 10:49:38

/*
    Copyright 2014 Digital Technology Art SRL
    This file is part of Blackfin Toolchain (BFTC) project.

    BFTC is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BFTC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Name-Program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Very simple example that show how to control four available ADCs on 4xPROBE shield

#include "main.h"

#define ST st = *pTIMER0_COUNTER
#define ET et = *pTIMER0_COUNTER

#define TIMEBASE 0xFFFFFFFF

#define NUM_OF_SAMPLES  1024    // Must be power of 2!
#define NUM_OF_SAMPLES_MASK  (NUM_OF_SAMPLES - 1)

typedef unsigned short int U16;
typedef unsigned int U32;

U16 ad[4][NUM_OF_SAMPLES];   // 16bit ADCs output
U16 adTmp0, adTmp1, adTmp2, adTmp3, loopCounter = 0;
unsigned adClk = 44, storePtr = 0;

inline int ADsample()
{
    if(adClk)
    {
        adClk--;
        PPF_TGL = AD_CLK;
        if(adClk <= 31)
        {
            // On rising edge sample data
            if(adClk & 1)
            {
                if(PPF & AD_IN0) adTmp0 |= 1;
                if(PPF & AD_IN1) adTmp1 |= 1;
                if(PPF & AD_IN2) adTmp2 |= 1;
                if(PPF & AD_IN3) adTmp3 |= 1;
                
            }
            else
            {   // On falling edge shift data but not the last!
                if(adClk)
                {
                    adTmp0 <<= 1;
                    adTmp1 <<= 1;
                    adTmp2 <<= 1;
                    adTmp3 <<= 1;
                }
            }
        }
        return 0;
    }
    PPF_SET = AD_CS;
    adClk = 44;
    ad[0][storePtr] = adTmp0;
    ad[1][storePtr] = adTmp1;
    ad[2][storePtr] = adTmp2;
    ad[3][storePtr] = adTmp3;
    //printf("%5d, %5d, %5d, %5d\n", adTmp0, adTmp1, adTmp2, adTmp3);
    adTmp0 = adTmp1 = adTmp2 = adTmp3 = 0;
    storePtr++;
    storePtr &= NUM_OF_SAMPLES_MASK;
    DelayUs(2);
    PPF_CLR = AD_CS;
    if(!storePtr)
        return 1;
    return 0;
}

inline void Process()
{
    U32     ads, i, acc;
    U16     mn, mx, v;
    int     vl, avg;
    double  std;

    for(ads = 0; ads < 4; ads++)
    {
        acc = 0;
        mn = 65535;
        mx = 0;
        printf("%5d: AD[%d] = ", loopCounter, ads);
        // Find min, max and average
        for(i = 0; i < NUM_OF_SAMPLES; i++)
        {
            v = ad[ads][i];
            acc += v;
            if(v > mx) mx = v;
            if(v < mn) mn = v;
        }
        avg = acc / NUM_OF_SAMPLES;
        acc = 0;
        // Calc STD
        for(i = 0; i < NUM_OF_SAMPLES; i++)
        {
            vl = ad[ads][i];
            vl = avg - vl;
            acc += (vl * vl);
        }
        std = sqrt(acc / NUM_OF_SAMPLES_MASK);
        printf("Min: %5d, Max: %5d, Avg: %5d, mV: %4d Std: %.1f\n", mn, mx, avg, avg * 5000 / 65536, std);
    }
    loopCounter++;
}

int main(void)
{
    bool        done;
    unsigned    st, et, tx;
    Set_PLL(16, 4);             // CORE: 25MHz * 16 = 400MHz, SCLK: 400MHz / 4 = 100MHz
    Set_Port();                 // Set the port according to project set
    Set_Uart0(115200);  // printf is redirected to UART0
    Set_TimerCounter(TIMER_0);
    Set_TimerEnable(TIMER_0, true);

    PPF_CLR = AD_CS;
    while(1)    // Main loop
    {
        // Get A/D data for all channels
        ST;
        done = ADsample();
        if(done) Process();
        // 250 ns delay, this is the semi-period for a SPI clock of 2MHz
        do
        {
            ET;
            if(st > et) tx = TIMEBASE - st + et;
            else tx = et - st;
        }while(tx < 25);
    }
    return 0;
}

Generated on Tue Apr 7 20:07:44 2015 for BF592A Library by doxygen 1.3.1