audio player using the pic32, mcp4822, microsd card and the mddfs library

Upload: tahmidmc

Post on 02-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Audio player using the PIC32, MCP4822, microSD card and the MDDFS library

    1/9

    SYED TAHMID MAHBUB PIC32 based audio player wit !i"roSD "ard a#d $2%bit audio out

    PIC32 BASED AUDI& P'AYE( )ITH MIC(&SD ST&(A*E

    SYED TAHMID MAHBUB

    +A,UA(Y 2-$.

    http://tahmidmc.blogspot.com/2015/01/audio-player-using-pic32-microsd-card.html

    INTR!"#TIN

    The PIC32 series of microcontrollers come with a powerful processor, lots of memory and a

    very rich set of peripherals that make it ideal for a wide range of applications. One such application that

    I wanted to have fun with is audio playack. Initially I made a simple audio player where I hard!coded

    the audio in a header file and played it ack. Oviously this had its drawacks, specifically with

    storage, and so I wanted something more fle"ile. That led me to e"ploring the use of a micro#$ card

    in the pro%ect. I had an &'( micro#$ card lying around that I could use for this pro%ect. #o, I decided

    to uild a simple )*+ player using that.

    I had used the internal P) module for audio playack ut this time I wanted etter audio

    output and so decided to use the CP-&22 2!it dual $*C that I had with me. I have previously

    talked aout it in my articles/

    PIC32 #PI/ 0sing the CP-&22 2!it serial $*C

    PIC32 $*1#PI1$*C/ *nalog output synthesis with ero CP0 overhead

    The )*+ player Ive made supports PC 4pulse code modulation5 format )*+ files. Ive tested

    with 6!it --,778 audio files and the output sounds great. It works with &!it audio files as well ut

    getting that to work has 4oddly5 een particularly difficult. )hile I still havent een ale to figure out

    what e"actly I was doing wrong with the &!it part, I managed to find a solution to the prolem and that

    is what I have for my )*+ player.

    9or now, the user interface is minimalistic and is done through the serial port. I didnt intend this

    pro%ect to e a full!fledged music player pro%ect ut %ust a learning e"perience and so did not enhance

    the 0I much further than necessary to select a song and play it ack.

    http://tahmidmc.blogspot.com/2015/01/audio-player-using-pic32-microsd-card.htmlhttp://tahmidmc.blogspot.com/2014/10/pic32-spi-using-mcp4822-12-bit-serial.htmlhttp://tahmidmc.blogspot.com/2014/10/pic32-dmaspidac-analog-output-synthesis.htmlhttp://tahmidmc.blogspot.com/2014/10/pic32-dmaspidac-analog-output-synthesis.htmlhttp://tahmidmc.blogspot.com/2015/01/audio-player-using-pic32-microsd-card.htmlhttp://tahmidmc.blogspot.com/2014/10/pic32-spi-using-mcp4822-12-bit-serial.html
  • 8/10/2019 Audio player using the PIC32, MCP4822, microSD card and the MDDFS library

    2/9

    SYED TAHMID MAHBUB PIC32 based audio player wit !i"roSD "ard a#d $2%bit audio out

    $%RT&

    The circuit itself is very simple and consists of the following sections/

    PIC32M/2.-0$21B: This is the heart of the pro%ect and is the central microcontroller

    that interfaces with all the different sections. The PIC is operating off of the internal oscillator

    40(CP''configuration5 at -78, with the peripheral us clock also at -78.

    Mi"roSDMDD0S: This is the storage section of the pro%ect. The interface is done

    using a PIC #PI module : #PI specifically. * critical part in the pro%ect is icrochips open!source

    file!system lirary $$9#. This is part of the icrochip ;iraries for *pplications 4 M'A5. I have

    found it very useful and fairly easy to use : it comes with good documentation and demo code, ut %ust

    takes a little it of effort to get going. I used 2-$3%-4%$.4part of the legacy ;*5.

    Audio se"tio# : The $*C I used is the MCP5122. It is a dual 2!it $*Ccommunicating with the PIC using #PI : #PI2 for the $*C. The #PI clock fre 25 ? > $4 ? > 1 ?

  • 8/10/2019 Audio player using the PIC32, MCP4822, microSD card and the MDDFS library

    3/9

    SYED TAHMID MAHBUB PIC32 based audio player wit !i"roSD "ard a#d $2%bit audio out

    Bow on to the actual data. That depends on the numer of channels and the its per sample. The

    first key thing to rememer is that for 6!it PC files, the data is stored as 6!it SI*,EDsamples,

    whereas for &!it PC files, the data is stored as &!it U,SI*,ED samples.

    The way the audio data is organied is illustrated elow for &!it and 6!it mono and stereo

    audio files 4each lock is a yte@ the leftmost lock is the lowest!numered yte5/

    1%bit M&,& 6iles

  • 8/10/2019 Audio player using the PIC32, MCP4822, microSD card and the MDDFS library

    4/9

    SYED TAHMID MAHBUB PIC32 based audio player wit !i"roSD "ard a#d $2%bit audio out

    T& is used as the ETop &f StackF to indicate the position from where to retrieve the audio value to

    push to the $*C.,& is used as the EBottom &f StackF in the section where the audio data values are

    read@ ,& indicates the position onto which a new value is eing pushed into the uffer. It was

    determined, y testing, that 2&!point stacksAuffers are enough to ensure smooth continuous audio

    playack.

    &RI%* "&R INTR%#

    The PIC32 0*=T module 40*=T2 since, y default, thats the one for pri#t65 was used for the

    serial port interface for the user interface. The used aud rate is $$.2--. The s"a#6 function does not

    seem to work y default, so instead I %ust wrote simple functions to get user input when re?-5. The white wire

    on the converter 4=G in5 connects to the TG out from the PIC. The green wire 4TG out5 connects to the

    =G in to the PIC. The lack wire 4+##5 connects to ground. The red wire 4+supply5 is left

    unconnected.

    ;irary functions from the PIC32 peripheral lirary 4plib5 are used to configure the module.

    The user interface is minimalistic. Initially it prompts the user to enter the re

  • 8/10/2019 Audio player using the PIC32, MCP4822, microSD card and the MDDFS library

    5/9

    SYED TAHMID MAHBUB PIC32 based audio player wit !i"roSD "ard a#d $2%bit audio out

    contents of the 3 folders are/

    MDDi#"ludes8 9#$efs.h, 9#IO.h, #$!#PI.h

    MDDsour"es8 9#IO.c , #$!#PI.c

    PIC328 Compiler.h, 9#config.h, 8ardwareProfile.h

    Bow on to the function e"planations.

    The first function I used is!!edia!etect4. This is used to detect if the memory device

    4micro#$ card in my case5 is present in the micro#$ card connector. #everal

    holdersAconnectors provide a pin which can e tested to see if a card is inserted. 8owever, mine

    didnt have such a pin. #o I went into the source file where this function was written and editedthe function so that it always returned . I kept it in place in case I was to use a connector that

    had the "ard dete"t pin 4in which case I can %ust go ack to the source file and undo my

    changes5.

    The ne"t function I used is&Init4. This initialies the file system. It returns 0A'SE 4defined

    as 75 if the initialiation fails, and T(UE 4defined as 0A'SE5 otherwise.

    To search for all )*+ files in the root directory 4which is the default directory upon

    initialiation5 I used two functions. The first one is indirst6.'%( attributes 7rec4. The

    function has three arguments : the first one eing the filename@ the second is attriutes and the

    third is a pointer to a #earch=ec structure 4defined within the lirary5. The function searches for

    the first file with the specified filename and attriutes, and returns 7 if a file is found@ it returns

    ! otherwise. If a file is found, the resulting find results are stored in the #earch=ec structure rec

    which contains properties such as filename, filesie, attriutes, timestamp, etc. In my pro%ect I

    make use only of the filesie parameter to display the filesie of the found file. EattriutesF is a

    0IBT& type variale that can specify one of several attriutes : in my program I specified

    %TTR%&, which says that the file can have any attriute. The way I have filename

    specified, the function searches for the first )*+ type file. If the filename was EH.HF, the

    function would search for the first file 4any 6ile#a!e, any 6iletype5. If the filename was

    E#OB'H.)*+F, the function would search for the first )*+ type file starting with the letters

    #OB'.

    Once the first file is found, it is possile to search for other files using the indNe8t7rec4

  • 8/10/2019 Audio player using the PIC32, MCP4822, microSD card and the MDDFS library

    6/9

    SYED TAHMID MAHBUB PIC32 based audio player wit !i"roSD "ard a#d $2%bit audio out

    function. This function searches for other files with the same filename and attriutes as used for

    theindirst function. The resulting information is, as withindirst, stored in the #earch=ec

    structure rec. The function returns 7 if a file is found@ ! otherwise.

    To open a file for reading, the function &9open9ilename r;4 is used. ErF can e replaced

    with EwF to open the file for writing. 'iven a specified 6ile#a!e, the function returns ,U''if

    opening failed@ otherwise it returns a poi#ter4type 0S0I'E defined in the lirary5 used later in

    my code for the&9read function mentioned elow.

    To actually read data from the opened file, the function used was &9readbu99er s

  • 8/10/2019 Audio player using the PIC32, MCP4822, microSD card and the MDDFS library

    7/9

    SYED TAHMID MAHBUB PIC32 based audio player wit !i"roSD "ard a#d $2%bit audio out

    "&R "N#TIN&

    #everal user functions were utilied in this pro%ect.

    The functions init!%#andsetup"%RTare self e"planatory/ init!%# sets up the #PI2 module

    for use with the $*C@setup"%RT sets up 0*=T2 for use for the serial port interface.

    The function in9inite,linB is used at the end of program e"ecution. *ll it does is toggle the

    ;$ infinitely 4as the name implies5 with a small software delay so that the linking is visile

    y the naked eye.

    The function setup%udio$' essentially clears the timer 2 control register and sets the

    corresponding interrupt priority.

    The functionsetup&ystem#locBconfigures Timer 3 so that its period is ms. This is a system

    clock of sorts used for higher level time!keeping 4since its resolution is ms5. The

    corresponding timer interrupt and priority are set.

    The function Coid getilenamechar 6 bu99er4fills the character array 4string5 uffer with the

    filename. This is called when the user is prompted to type in the filename of the song to play

    ack. The parameter passed is the Epointer to the stringF.

    The function Coid con9igure?ardDare"INT32 sampleRate4 sets up the period register for

    timer 2 4P=25 so that the period is e

  • 8/10/2019 Audio player using the PIC32, MCP4822, microSD card and the MDDFS library

    8/9

    SYED TAHMID MAHBUB PIC32 based audio player wit !i"roSD "ard a#d $2%bit audio out

    The function?-5.

    #ince the current re

  • 8/10/2019 Audio player using the PIC32, MCP4822, microSD card and the MDDFS library

    9/9

    SYED TAHMID MAHBUB PIC32 based audio player wit !i"roSD "ard a#d $2%bit audio out

    !'N*%!&

    ntire pro%ect folder, including all source files, header files, and schematic/

    https/AAdrive.google.comAfileAdA7(-#oP9P=Bi8='7+BN+>u+%AviewQuspDsharing

    "&"* *IN&

    icrochip ;iraries for *pplications

    *B7-?/ Implementing 9ile IAO 9unctions 0sing icrochips emory $isk $rive 9ile #ystem

    ;irary

    https://drive.google.com/file/d/0B4SoPFPRNziHRGE0bVNZYV9uVjQ/view?usp=sharinghttp://www.microchip.com/pagehandler/en-us/devtools/mla/home.htmlhttp://ww1.microchip.com/downloads/en/AppNotes/01045b.pdfhttp://ww1.microchip.com/downloads/en/AppNotes/01045b.pdfhttps://drive.google.com/file/d/0B4SoPFPRNziHRGE0bVNZYV9uVjQ/view?usp=sharinghttp://www.microchip.com/pagehandler/en-us/devtools/mla/home.htmlhttp://ww1.microchip.com/downloads/en/AppNotes/01045b.pdfhttp://ww1.microchip.com/downloads/en/AppNotes/01045b.pdf