Calculating the Sampling Frequency on ADC

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
b4rzan
Posts: 60
Joined: 12 Nov 2011 21:39

Calculating the Sampling Frequency on ADC

#1 Post by b4rzan » 27 Feb 2012 18:42

Hi all,

I am trying to write 8 bits out of the 10 bit offered by the pic16f877a microcontroller on to an mmc card, so basically I am just throwing away the other two bits.

I am hoping to store a wave sound on to the mmc, but I am not quite sure how to determine the sampling rate (i.e. how often i should take a sample). I am using a 20MHZ xtal.

Your help is much appreciated...

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Calculating the Sampling Frequency on ADC

#2 Post by janko.kaljevic » 28 Feb 2012 17:27

Hello,

Please reconsider using PIC16F for this project.
You will need at least PIC18 to use MMC_Fat libraries.

The best is to choose sampling rate according to total speed that you are able to achieve with your controller.
Especially if you are going to write some data on SD card.

Best regards.

MR2010
Posts: 109
Joined: 06 Jun 2010 13:28

Re: Calculating the Sampling Frequency on ADC

#3 Post by MR2010 » 28 Feb 2012 22:02

b4rzan wrote:Hi all,

I am trying to write 8 bits out of the 10 bit offered by the pic16f877a microcontroller on to an mmc card, so basically I am just throwing away the other two bits.

I am hoping to store a wave sound on to the mmc, but I am not quite sure how to determine the sampling rate (i.e. how often i should take a sample). I am using a 20MHZ xtal.

Your help is much appreciated...

Try this it will create RAW audio file on your Sd card and it will destroy your SD card bit by bit so you need to use fat library to create files and you need create special library for recording and look after sectors the best way is to record into RAM ( BIG RAM) and write the file to your SD at once if im right ( maybe you will need CPU and ex ram todo pure job for recording and huge libraries for encoding to wave or watever you want todo,anyway everythings possible but needs time and practics, enjoy

Code: Select all

//########################### difines & includes ##############################################
#include "built_in.h"
#include "MMC_INIT.c"
#include "stdarg.h"

#define RecLED PORTB.F5
#define errorLED PORTB.F4
#define Stop PORTB.F3       //stop pushbutton
#define Play PORTB.F1        //play PB
#define Rec PORTB.F2          //record PB
#define adcc PORTB.F0        //pause PB

 static unsigned long loc;
 static unsigned long  Curr_File;
 static unsigned long  rootdir_sector;
 static unsigned long  BPB_FileSize; // file size  bytes  123 > 127      in root dir   in 127 byts


//**********************************************************************************
//############################### write sound to mmc ##############################################
void WriteSD(void)
{
        unsigned int r,i;

        CS = 0;
        ADCON1 = 0B11001110;                // Fosc/16, ch1, ref=Vdd, right just.
        ADCON0 = 0B011000000;        // Fosc/16.
        RecLED = 0;
        r = Command(WRITE_MULTIPLE_BLOCKS,loc,0xFF);        //read multi-sector
        if(r != 0){errorLED = 1;
        }else{
        errorLED = 0;}
        ADON_bit=1;

        SPI(0xFF);
        SPI(0xFF);
        SPI(0xFF);

        while(Stop !=1)
        {
                SPI(0xFC);       //multi sector token byte

                 RecLED = 1;

                for(i=0;i<512;i++)
                {

                                ADCON0.B2 = 1;


                                  while(!TMR2IF_bit){}        //20KHz clock


                                SPI(ADRESL);


                                //play while record
                       CCP1X_bit = (ADRESL & 1);        //shift byte to get the required PWM duty cycle
                       CCPR1L = (ADRESL>>1);

                        TMR2IF_bit = 0;



                }
                 SPI(0xFF);        // CRC
                SPI(0xFF);        // CRC


        if((r=SPI(0xFF) & 0x0F) == 0x05){        //data accepted        = 0101
                for(i=10000;i>0;i--){
                        if(r=SPI(0xFF))        break;
                }
        }
        else{
                errorLED = 1;
        }
        while(SPI(0xFF) != 0xFF){}        // while busy
        loc += 512;
        }
        SPI(0xFD);        //stop transfer        token byte

        SPI(0xFF);
        SPI(0xFF);
        while(SPI(0xFF) != 0xFF)        // while busy

        CS = 1;
         ADON_bit=0;
        RecLED = 0;
}
//#################################### play music from mmc/sd ####################################
void PAUSE(){

delay_ms(1000);
 while(Play!=1){

 }
 delay_ms(1000);
 }
void ReadSD(void)
{       

        unsigned int i,count;
       unsigned char datt;
           delay_ms(500);
                     READ_Mlti(Curr_File);

        while(Stop !=1)
        {
                while(SPI(0xFF) != 0xFE) {}       // wait for first byte
                for(i=0;i<512;i++)
              {


                        while(!TMR2IF_bit){}
                        //datt = SPI(0xFF);
                       // CCP2X_bit = ((datt>>2) & 1);        //shift byte to get the required PWM duty cycle
                       // CCPR2L = (datt &0xFF);
                       datt = SPI(0xFF);
                        CCP1X_bit = ((datt>>2) & 1);        //shift byte to get the required PWM duty cycle
                        CCPR1L = (datt &0xFF);
                        TMR2IF_bit = 0;

                    if(Play) PAUSE();

                if (count < 23000)RecLED = 1;
                if (count > 23000)RecLED = 0;

                count++;
                      if(Rec) {next();}
                       
                       if(Stop==1) 
                       {
                        initcrap();
                       MMC_Close();
                        return ;
                       }


              }
                SPI(0xFF);        //discard of CRC
                SPI(0xFF);
                loc += 512;
               if(loc >= BPB_FileSize)
             {
                MMC_Close();
               loc=(Curr_File);
               READ_Mlti(loc);//read multi
              }
        }
        
        

        Command(STOP_TRANSMISSION ,0x00,0xFF);        //stop transmit
        SPI(0xFF);
        SPI(0xFF);
        CS = 1;
}


main code

Code: Select all

void main(){



     ADCON0=0;
     ADCON1=0x07;
    // INTCON=0B11000000;
    // TMR2IE_bit=1;
TRISC=0b00010000;
TRISB = 0b00001111;
TRISD=0x00;
TRISE=0x00;
TRISA=0b00000001;
PORTA=0x00;
PORTD=0x00;

        CS=1;
          delay_ms(100);
        CCP1CON = 0B00001100;        //PWM1 mode
        //CCP2CON = 0B00001100;        //PWM2 mode
        PR2 = 227;        //20KHz  100
        T2CON = 0B000000100;        //prescale 1, post scale 1, timer2 on

      



intspi();

          RecLED = 0;
        errorLED = 0;

MMC_Init();

 //FAT_init();



while(1)
{

     if(Rec) WriteSD();
        if(Play) ReadSD();

}
}
its little bit compilcated but i am sure you will get round it
Regards


Mustapha;;;;;;;

b4rzan
Posts: 60
Joined: 12 Nov 2011 21:39

Re: Calculating the Sampling Frequency on ADC

#4 Post by b4rzan » 05 Mar 2012 10:31

thank you Mustapha for your kind replies.

I have already achieved the read and write using pic16. My problem is that I dont know how to calculate the
maximum sampling frequency on PIC16 ADC. I looked at the data sheet, there is a formula there but I could quite get it.

Post Reply

Return to “mikroC PRO for PIC General”