ts-7000
[Top] [All Lists]

[ts-7000] Re: MAX197 ADC interfacing

To:
Subject: [ts-7000] Re: MAX197 ADC interfacing
From: "Phil" <>
Date: Sun, 12 Feb 2006 11:59:13 -0000
Hi Anand,

The example that is included with the driver shows exactly to get 
the results of the conversion and do something with it. You can't 
really ask us what format you should put it in because that is 
totally up to your project. However, because I thought it was 
interesting for other reasons, I wrote another couple of examples of 
taking data from the ADC and writing it to .WAV files so that I 
could hear what it sounded like.

Firstly I tried a quick and dirty program 'example2.c' (see below) 
to sample one channel at 22.05kHz, it was able to do it however the 
cpu utilisation was at ~70% and there were glitches in the audio.

So then I changed the way the prog received the data 'example3.c' 
(see below) to do the same sampling. Much better cpu utilisation of 
just 3% and absolutely perfect audio (for 22.05kHz mono). No 
noticable freq shift from an inaccurate sampling clock, no glitches. 
Also, I had the program writing the output over the network via NFS. 
I did try at 44.1kHz but the driver had issues, I guess that's 
getting a bit high but still I will look into it.

Here is the code for the two examples. Note: call these files 
example2.c and example3.c and put them in the examples directory of 
wherever you put the driver. They need to find the 'adc.h' header 
file from the driver source. They don't need anything else out of 
the ordinary to compile though, just '/path/to/arm-linux-gcc -o 
example3 example3.c'

####################################################################

/**********************
 * example2.c
 **********************/

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "../adc.h"


typedef struct
{
        char            Id[4];
        long            Size;
} DataChunk;

typedef struct
{
        char            Id[4];
        long            Size;

        short           Tag;
        unsigned short  Channels;
        unsigned long   SampRate;
        unsigned long   ByteRate;
        unsigned short  BlockSize;
        unsigned short  Bits;
} FormatChunk;

typedef struct
{
        char            Id[4];
        long            Size;
        char            Type[4];
        FormatChunk     Format;
} RIFFFile;


int main(int argc, char **argv)
{
        int loopCntr;
        int fd;
        int numSecs, secCntr;
        int numSamples;
        FILE* file;
        unsigned long long iTemp64;
        unsigned long samprate;
        char buffer[1024];
        signed short *dataBuff;
        RIFFFile riff;
        DataChunk dataChunk;


        numSecs = 20;


        file = fopen("/dev/adc8", "rt");
        if(file == NULL)
        {
                printf("Can't open device\n");
                return -1;
        }

        fd = fileno(file);

        ioctl(fd, ADC_IOC_GET_SAMPLE_FREQ_HZ, &iTemp64);
        samprate = *((unsigned long *) &iTemp64 + 1);


        numSamples = numSecs * samprate;

        strcpy(riff.Id, "RIFF");
        riff.Size = numSamples * 2 + sizeof(DataChunk) + sizeof
(FormatChunk) + 4;
        strcpy(riff.Type, "WAVE");

        strcpy(riff.Format.Id, "fmt ");
        riff.Format.Size = sizeof(FormatChunk) - 8;
        riff.Format.Tag = 1;            // No compression
        riff.Format.Channels = 1;
        riff.Format.SampRate = samprate;
        riff.Format.ByteRate = samprate * 2;
        riff.Format.BlockSize = 2;
        riff.Format.Bits = 12;

        strcpy(dataChunk.Id, "data");
        dataChunk.Size = numSamples * 2;

        dataBuff = (signed short *) malloc(sizeof(signed short) * 
samprate);


        fwrite(&riff, sizeof(riff), 1, stdout);
        fwrite(&dataChunk, sizeof(dataChunk), 1, stdout);

        for(secCntr = 0; secCntr < numSecs; secCntr++)
        {
                for(loopCntr = 0; loopCntr < samprate ; loopCntr++)
                {
                        char buff[256];
                        signed short sampVal;
                        
                        if(fgets(buff, 250, file) == NULL)
                                break;
                        
                        sscanf(buff, "%hd", &sampVal);
                        sampVal = sampVal << 4;
        
                        dataBuff[loopCntr] = sampVal;
                }

                fwrite(dataBuff, sizeof(signed short), samprate, 
stdout);
        }

        free(dataBuff);

        fclose(file);


        return 0;
}


####################################################################

/**********************
 * example3.c
 **********************/

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "../adc.h"


typedef struct
{
        char            Id[4];
        long            Size;
} DataChunk;

typedef struct
{
        char            Id[4];
        long            Size;

        short           Tag;
        unsigned short  Channels;
        unsigned long   SampRate;
        unsigned long   ByteRate;
        unsigned short  BlockSize;
        unsigned short  Bits;
} FormatChunk;

typedef struct
{
        char            Id[4];
        long            Size;
        char            Type[4];
        FormatChunk     Format;
} RIFFFile;


int main(int argc, char **argv)
{
        int loopCntr;
        int fd;
        int numSecs, secCntr;
        int numSamples;
        FILE* file;
        int iTemp;
        unsigned long long iTemp64;
        unsigned long samprate;
        char buffer[1024];
        signed short *dataBuff;
        RIFFFile riff;
        DataChunk dataChunk;


        numSecs = 20;


        file = fopen("/dev/adc8", "rt");
        if(file == NULL)
        {
                printf("Can't open device\n");
                return -1;
        }

        fd = fileno(file);

        ioctl(fd, ADC_IOC_GET_SAMPLE_FREQ_HZ, &iTemp64);
        samprate = *((unsigned long *) &iTemp64 + 1);

        iTemp = 0;
        if(ioctl(fd, ADC_IOC_SET_READ_FORMAT, &iTemp) != 0)
        {
                fclose(file);
                return -1;
        }



        numSamples = numSecs * samprate;

        strcpy(riff.Id, "RIFF");
        riff.Size = numSamples * 2 + sizeof(DataChunk) + sizeof
(FormatChunk) + 4;
        strcpy(riff.Type, "WAVE");

        strcpy(riff.Format.Id, "fmt ");
        riff.Format.Size = sizeof(FormatChunk) - 8;
        riff.Format.Tag = 1;            // No compression
        riff.Format.Channels = 1;
        riff.Format.SampRate = samprate;
        riff.Format.ByteRate = samprate * 2;
        riff.Format.BlockSize = 2;
        riff.Format.Bits = 12;

        strcpy(dataChunk.Id, "data");
        dataChunk.Size = numSamples * 2;

        dataBuff = (signed short *) malloc(sizeof(signed short) * 
samprate);


        fwrite(&riff, sizeof(riff), 1, stdout);
        fwrite(&dataChunk, sizeof(dataChunk), 1, stdout);

        for(secCntr = 0; secCntr < numSecs; secCntr++)
        {
                for(loopCntr = 0; loopCntr < samprate; )
                {
                        int records;
                        int sampCntr;

                        records = fread(&dataBuff[loopCntr], 2, 
samprate - loopCntr, file);

                        for(; records; records--, loopCntr++)
                        {
                                dataBuff[loopCntr] = dataBuff
[loopCntr] << 4;
                        }

                }

                fwrite(dataBuff, sizeof(signed short), samprate, 
stdout);
        }

        free(dataBuff);

        fclose(file);


        return 0;
}

####################################################################



Cheers
Phil

PS. Did the change from 10F00000 to 10C00000 help?



--- In  anand bhavnani <> wrote:
>
>  
>   I have couple of doubts.
>   How actually are you using the the device node adc1 to adc8 for 
extracting the result.
>   After the result of conversion gets redirected to the device 
files,
> how to use that result.
>   Can you let me know the exact process that is required to 
extract the output of adc
> ,how to store it in a raw file( i mean what format should be 
adopted) and what kind of utility to use for converting it to a 
particular format so that it can be played.
>   Kindly reply .
>   Thanking You
> Anand.
> 






 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ts-7000/

<*> To unsubscribe from this group, send an email to:
    

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


<Prev in Thread] Current Thread [Next in Thread>
Admin

Disclaimer: Neither Andrew Taylor nor the University of NSW School of Computer and Engineering take any responsibility for the contents of this archive. It is purely a compilation of material sent by many people to the birding-aus mailing list. It has not been checked for accuracy nor its content verified in any way. If you wish to get material removed from the archive or have other queries about the archive e-mail Andrew Taylor at this address: andrewt@cse.unsw.EDU.AU