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

4xPROBE 12 bit D/A converter test.

// Built with BFTC Rev. 1.0.0, ven 26 dicembre 2014 09:02:22

/*
    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 DACs on 4xPROBE shield

#include "main.h"

// Output swing 0-5V
#define BIT 12              // Possible selection 12 or 16

#if(BIT == 12)
    #define DAC_ 0x10       // x AD5624, 12 bit DAC
    #define By_mV(mv) (4095L * ((unsigned) mv)) / 5000L
#endif

#if(BIT == 16)
    #define DAC_ 0x01       // x AD5664, 16 bit DAC
    #define By_mV(mv) (65535L * ((unsigned) mv)) / 5000L
#endif

void DAC(unsigned channel, unsigned value, bool update)
{
    unsigned    n, mk = 0x800000;
#if(BIT == 12)
    value <<= 4;
#endif
    // Prepare the output data
    value |= (channel << 16);
    if(update)
        value |= 0x100000;  // Load & update
    PPG_CLR = DA_CS;        // Set DAC CS active
    for(n = 0; n < 24; n++)
    {
        PPG_SET = DA_CLK;
        if(value & mk)  PPG_SET = DA_DATA;
        else PPG_CLR = DA_DATA;
        PPG_CLR = DA_CLK;
        mk >>= 1;
    }
    PPG_SET = DA_CS;        // Set DAC CS disabled
}

void Menu()
{
    printf("*** M E N U ***************\n");
    printf("0 - Set a voltage on output 0\n");
    printf("1 - Set a voltage on output 1\n");
    printf("2 - Set a voltage on output 2\n");
    printf("3 - Set a voltage on output 3\n");
    printf("4 - Sawtooth wave on all outputs\n");
    printf("5 - Sine wave on all outputs\n");
    printf("6 - Square wave on all outputs\n");
    printf("7 - Triangle wave on all outputs\n");
    printf("> ");
}

int main(void)
{
    int         k;
    int         da, dal, dah, n, i;
    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
    
    // Wait a character before to start
    GetChar_Uart0();
    while(1)                    // Main loop
    {
        Menu();
        switch(k = GetChar_Uart0())
        {
            case '0':   // Levels
            case '1':
            case '2':
            case '3':
                    printf("\n");
                    printf("Out[%d] (0-5000 mV) = ", k - '0');
                    _scanf("%d", &da);
                    DAC(k - '0', By_mV(da), true);
                    break;
            case '4':   // Sawtooth
                    printf("\nLow level  (0-5000 mV) = ");
                    _scanf("%d", &dal);
                    printf("\nHigh level (0-5000 mV) = ");
                    _scanf("%d", &dah);
                    n = By_mV(dah) - By_mV(dal);
                    da = By_mV(dal);
                    printf("\nPress any key to exit\n");
                    do
                    {
                        for(i = 0; i < n; i++)
                        {
                            DAC(0, da + i, false);
                            DAC(1, da + i, false);
                            DAC(2, da + i, false);
                            DAC(3, da + i, true);  // Load last DAC and update all
                            DelayUs(5);
                        }
                    }while(!Signal_Uart0());
                    GetChar_Uart0();
                    break;
            case '5':   // Sine
                    printf("\nLow level  (0-5000 mV) = ");
                    _scanf("%d", &dal);
                    printf("\nHigh level (0-5000 mV) = ");
                    _scanf("%d", &dah);
                    dal = By_mV(dal);
                    dah = By_mV(dah);
                    n = (dah - dal) / 2;
                    dal += n;
                    printf("\nPress any key to exit\n");
                    do
                    {
                        for(i = 0; i < 360; i++)
                        {
                            DAC(0, dal + n * isin(i) / 1000, false);
                            DAC(1, dal + n * isin(i) / 1000, false);
                            DAC(2, dal + n * isin(i) / 1000, false);
                            DAC(3, dal + n * isin(i) / 1000, true);  // Load last DAC and update all
                            DelayUs(5);
                        }
                    }while(!Signal_Uart0());
                    GetChar_Uart0();
                    break;
            case '6':   // Square-wave
                    printf("\nLow level  (0-5000 mV) = ");
                    _scanf("%d", &dal);
                    printf("\nHigh level (0-5000 mV) = ");
                    _scanf("%d", &dah);
                    dal = By_mV(dal);
                    dah = By_mV(dah);
                    printf("\nPress any key to exit\n");
                    do
                    {
                        // 0 - 5V sawtooth on all channels
                        DAC(0, dal, false);
                        DAC(1, dal, false);
                        DAC(2, dal, false);
                        DAC(3, dal, true);  // Load last DAC and update all
                        DelayUs(5);
                        DAC(0, dah, false);
                        DAC(1, dah, false);
                        DAC(2, dah, false);
                        DAC(3, dah, true);  // Load last DAC and update all
                        DelayUs(5);
                    }while(!Signal_Uart0());
                    GetChar_Uart0();
                    break;
            case '7':   // Triangle
                    printf("\nLow level  (0-5000 mV) = ");
                    _scanf("%d", &dal);
                    printf("\nHigh level (0-5000 mV) = ");
                    _scanf("%d", &dah);
                    dal = By_mV(dal);
                    dah = By_mV(dah);
                    n = dah - dal;
                    printf("\nPress any key to exit\n");
                    do
                    {
                        for(i = 0; i < n; i++)
                        {
                            DAC(0, dal + i, false);
                            DAC(1, dal + i, false);
                            DAC(2, dal + i, false);
                            DAC(3, dal + i, true);  // Load last DAC and update all
                            DelayUs(5);
                        }
                        for(i = 0; i < n; i++)
                        {
                            DAC(0, dah - i, false);
                            DAC(1, dah - i, false);
                            DAC(2, dah - i, false);
                            DAC(3, dah - i, true);  // Load last DAC and update all
                            DelayUs(5);
                        }
                    }while(!Signal_Uart0());
                    GetChar_Uart0();
                    break;
        }
        printf("\n\n");
    }
    return 0;
}

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