Advertisement
Feldon

DFPlayer Command List from Banggood

Oct 24th, 2021
1,160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.82 KB | None | 0 0
  1. /*
  2. DF Player mini command discovery
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Program is mend to discover all the possibilities of the command
  5. structure of the DFPlayer mini. No special libraries are needed.
  6. In general, there are 3 ways to address a MP3 or WAV file:
  7. (1) Track order:
  8.     All songs are stored a certain order on the card. The order is however
  9.     not garanteed and very depending on the order how the files were written to the card. This
  10.     method is not suitable when it is absolutly neccessary that a specific track is played. Commands
  11.     using this written order are (0x01, 0x02, 0x03, 0x08, 0x11, 0x3C, 0x3D, 0x3E, 0x4B, 0x4C, 0x4D).
  12.     The name of a song is arbitrary.
  13. (2) Folder number and song number:
  14.     Folders are named 01~99 and songs 001~255.mp3 or 001~255.WAV. It's possible to exactly address
  15.     a specific song with command 0x0F. Command 0x17 is related.  When adressing a specific file,
  16.     the file is always internaly converted to the stored track number. This number can be requested by
  17.     commands  0x4B, 0x4C or 0x4D.
  18. (3) Folder named "mp3" and song number:
  19.     A folder is named "mp3" and songs with a name of exact a 4-digit number (0001~2999) e.g. 0235.mp3.
  20.     It's possible to exactly address a specific song with command 0x12.The according track number can
  21.     be requested by commands 0x4B, 0x4C or 0x4D.
  22.  
  23. This program is simple and can be the basis for your own mp3 player sketch.
  24. Note: The DF Player commands are not always correct described in the manual. I tried to fix it, but
  25. there is still a something to do. The commands recoverd so far are listed below.
  26. How to use this sketch:
  27. Enter three (separated) DECIMAL numbers in the Serial Monitor with no end of line character.
  28. First number : Command
  29. Second number: First (High Byte) parameter
  30. Third number : Second (Low Byte) parameter
  31. E.g.: 3,0,1 will play the first track on the TF card
  32. VERY IMPORTANT: Use serial 1K resistors or a level shifter between module RX and TX
  33.   and Arduino to suppress audio noise
  34. Connect Sound module board RX to Arduino pin 11 (via 1K resistor)
  35. Connect Sound module board TX to Arduino pin 10 (via 1K resistor)
  36. Connect Sound module board Vcc to Arduino Vin when powered via USB (preferably 3.0)
  37.   else use seperate 5V power supply
  38. Connect Sound module board GND to Arduino GND
  39.  
  40. General DF Player mini command structure (only byte 3, 5 and 6 to be entered in the serial monitor):
  41. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. Byte Function         Value
  43. ==== ================ ====
  44. (0)  Start Byte       0x7E
  45. (1)  Version Info     0xFF  (don't know why it's called Version Info)
  46. (2)  Number of bytes  0x06  (Always 6 bytes)
  47. (3)  Command          0x__
  48. (4)  Command feedback 0x__  If enabled returns info with command 0x41 [0x01: info, 0x00: no info]
  49. (5)  Parameter 1 [DH] 0x__
  50. (6)  Parameter 2 [DL] 0x__
  51. (7)  Checksum high    0x__ See explanation below. Is calculated in function: execute_CMD
  52. (8)  Checksum low     0x__ See explanation below. Is calculated in function: execute_CMD
  53. (9)  End command      0xEF
  54. Checksum calculation.
  55. ~~~~~~~~~~~~~~~~~~~~
  56. Checksum =  -Sum(byte(1..6)) (2 bytes, notice minus sign!)
  57. DF Player mini Commands without returned parameters (*=Confirmed command ?=Unknown, not clear or not validated)
  58. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. CMD  CMD
  60. HEX  DEC Function Description                  Parameters(2 x 8 bit)
  61. ==== === =================================== = ======================================================================
  62. 0x01   1 Next                                * [DH]=X, [DL]=X Next track in current folder.Loops when last file played
  63. 0x02   2 Previous                            * [DH]=X, [DL]=X Previous track in current folder.Loops when last file played
  64. 0x03   3 Specify track(NUM)                  * [DH]=highByte(NUM), [DL]=lowByte(NUM)
  65.                                                1~2999 Playing order is order in which the numbers are stored.
  66.                                                Filename and foldername are arbitrary, but when named starting with
  67.                                                an increasing number and then placed in one folder, files are (often)
  68.                                                played in that order and with correct track number.
  69.                                                e.g. 0001-Joe Jackson.mp3...0348-Lets dance.mp3)
  70. 0x04   4 Increase volume                     * [DH]=X, [DL]=X Increase volume by 1
  71. 0x05   5 Decrease volume                     * [DH]=X, [DL]=X Decrease volume by 1
  72. 0x06   6 Specify volume                      * [DH]=X, [DL]= Volume (0-0x30) Default=0x30
  73. 0x07   7 Specify Equalizer                   * [DH]=X, [DL]= EQ(0/1/2/3/4/5) [Normal/Pop/Rock/Jazz/Classic/Base]
  74. 0x08   8 Specify repeat(NUM)                 * [DH]=highByte(NUM), [DL]=lowByte(NUM).Repeat the specified track number
  75. 0x09   9 Specify playback source (Datasheet) ? [DH]=X, [DL]= (0/1/2/3/4)Unknown. Seems to be overrided by automatic detection
  76.                                                (Datasheet: U/TF/AUX/SLEEP/FLASH)
  77. 0x0A  10 Enter into standby – low power loss * [DH]=X, [DL]=X Works, but no command found yet to end standby
  78.                                                (insert TF-card again will end standby mode)
  79. 0x0B  11 Normal working (Datasheet)          ? Unknown. No error code, but no function found
  80. 0x0C  12 Reset module                        * [DH]=X, [DL]=X Resets all (Track = 0x01, Volume = 0x30)
  81.                                                Will return 0x3F initialization parameter (0x02 for TF-card)
  82.                                                "Clap" sound after excecuting command (no solution found)
  83. 0x0D  13 Play                                * [DH]=X, [DL]=X Play pointered track
  84. 0x0E  14 Pause                               * [DH]=X, [DL]=X Pause track
  85. 0x0F  15 Specify folder and file to playback * [DH]=Folder, [DL]=File
  86.                                                Important: Folders must be named 01~99, files must be named 001~255
  87. 0x10  16 Volume adjust set (Datasheet)       ? Unknown. No error code. Does not change the volume gain.
  88. 0x11  17 Loop play                           * [DH]=X, [DL]=(0x01:play, 0x00:stop play)
  89.                                                Loop play all the tracks. Start at track 1.
  90. 0x12  18 Play mp3 file [NUM] in mp3 folder   * [DH]=highByte(NUM), [DL]=lowByte(NUM)
  91.                                                Play mp3 file in folder named mp3 in your TF-card. File format exact
  92.                                                4-digit number (0001~2999) e.g. 0235.mp3
  93. 0x13  19 Unknown                             ? Unknown: Returns error code 0x07
  94. 0x14  20 Unknown                             ? Unknown: Returns error code 0x06
  95. 0x15  21 Unknown                             ? Unknown: Returns no error code, but no function found                                              
  96. 0x16  22 Stop                                * [DH]=X, [DL]=X, Stop playing current track
  97. 0x17  23 Loop Folder "01"                    * [DH]=x, [DL]=1~255, Loops all files in folder named "01"
  98. 0x18  24 Random play                         * [DH]=X, [DL]=X Random all tracks, always starts at track 1
  99. 0x19  25 Single loop                         * [DH]=0, [DL]=0 Loops the track that is playing
  100. 0x1A  26 Pause                               * [DH]=X, [DL]=(0x01:pause, 0x00:stop pause)
  101. Commands with returned parameters  (*=Confirmed command ?=Unknown, not clear or not validated)
  102. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  103. CMD  CMD
  104. HEX  DEC Function Description                  Parameters(2 x 8 bit)
  105. ==== === =================================== = ==========================================================================
  106. 0x3A  58 Medium inserted                     * [DH]=0, [DL]=(1:U-disk, 2:TF-card)
  107. 0x3B  59 Medium ejected                      * [DH]=0, [DL]=(1:U-disk, 2:TF-card)
  108. 0x3C  60 Finished track on U-disk            * [DH]=highByte(NUM), [DL]=lowByte(NUM)
  109.                                                Not validated. Returns track number when song is finished on U-Disk
  110. 0x3D  61 Finished track on TF-card           * [DH]=highByte(NUM), [DL]=lowByte(NUM)
  111.                                                Returns track number when song is finished on TF
  112. 0x3E  62 Finished track on Flash             * [DH]=highByte(NUM), [DL]=lowByte(NUM)
  113.                                                Not validated. Returns track number when song is finished on Flash
  114. 0x3F  63 Initialization parameters           * [DH]=0, [DL]= 0 ~ 0x0F. Returned code when Reset (0x12) is used.
  115.                                                (each bit represent one device of the low-four bits)
  116.                                                See Datasheet. 0x02 is TF-card. Error 0x01 when no medium is inserted.
  117. 0x40  64 Error                               ? [DH]=0, [DL]= 0~7 Error code(Returned codes not yet analyzed)
  118. 0x41  65 Reply                               ? [DH]=0, [DL]= 0~? Return code when command feedback is high
  119.                                                0x00 no Error (Other returned code not known)
  120. 0x42  66 The current status                  * [DH] = Device number [DL] = 0 no play, 1 play
  121. 0x43  67 The current volume                  * [DH]=0, [DL]= Volume (0x00-0x30)
  122. 0x44  68 The current EQ                      * [DH]=0, [DL]= EQ(0/1/2/3/4/5) [Normal/Pop/Rock/Jazz/Classic/Base]
  123. 0x45  69 The current playback mode           * [DH]=0, [DL]= (0x00: no CMD 0x08 used, 0x02: CMD 0x08 used, not usefull)
  124. 0x46  70 The current software version        * [DH]=0, [DL]= Software version. (My version is 5)
  125. 0x47  71 The total number of U-disk files    * [DH]=highByte(NUM), [DL]=lowByte(NUM). Not validated
  126. 0x48  72 The total number of TF-card files   * [DH]=highByte(NUM), [DL]=lowByte(NUM)
  127. 0x49  73 The total number of flash files     * [DH]=highByte(NUM), [DL]=lowByte(NUM). Not validated
  128. 0x4A  74 Keep on (Datasheet)                 ? Unknown. No returned parameter
  129. 0x4B  75 The current track of U-Disk         * [DH]=highByte(NUM), [DL]=lowByte(NUM), Current track on all media
  130. 0x4C  76 The current track of TF card        * [DH]=highByte(NUM), [DL]=lowByte(NUM), Current track on all media
  131. 0x4D  77 The current track of Flash          * [DH]=highByte(NUM), [DL]=lowByte(NUM), Current track on all media
  132. 0x4E  78 Folder "01" [DH]=x, [DL]=1          * [DH]=0, [DL]=(NUM) Change to first track in folder "01"
  133.                                                Returns number of files in folder "01"
  134. 0x4F  79 The total number of folders         * [DH]=0, [DL]=(NUM), Total number of folders, including root directory
  135. This software is free to use and free to share
  136. Additional info can be found on DFRobot site, but is not very reliable
  137. Additional info:http://www.dfrobot.com/index.php?route=product/product&product_id=1121
  138. Ype Brada 2015-04-06
  139. */
  140.  
  141. #include "SoftwareSerial.h"
  142. # define Start_Byte     0x7E
  143. # define Version_Byte   0xFF
  144. # define Command_Length 0x06
  145. # define End_Byte       0xEF
  146. # define Acknowledge    0x00        //Returns info with command 0x41 [0x01: info, 0x00: no info]
  147. SoftwareSerial mySerial(10, 11);
  148.  
  149. void setup () {
  150.    Serial.begin(9600);
  151.    mySerial.begin (9600);
  152.    execute_CMD(0x3F, 0x00, 0x00);   // Send request for initialization parameters
  153.    while (mySerial.available()<10)  // Wait until initialization parameters are received (10 bytes)
  154.    delay(30);                       // Pretty long delays between succesive commands needed
  155.    
  156.    // Set sound (0x06) to very low volume (0x05). Adept according used speaker and required volume
  157.    execute_CMD(0x06, 0x00, 0x05);
  158. }
  159.  
  160. void loop () {
  161.   if (Serial.available())
  162.   {
  163.     // Input Serial monitor: Command and the two parameters in DECIMAL numbers (NOT HEX)
  164.     // E.g. 3,0,1 (or 3 0 1 or 3;0;1) will play first track on the TF-card
  165.     byte Command    = Serial.parseInt();
  166.     byte Parameter1 = Serial.parseInt();
  167.     byte Parameter2 = Serial.parseInt();
  168.    
  169.     // Write your input at the screen
  170.     Serial.print("Command : 0x");if (Command < 16) Serial.print("0"); Serial.print(Command, HEX);
  171.     Serial.print("("); Serial.print(Command, DEC);
  172.     Serial.print("); Parameter: 0x");if (Parameter1 < 16) Serial.print("0");Serial.print(Parameter1, HEX);
  173.     Serial.print("("); Serial.print(Parameter1, DEC);
  174.     Serial.print("), 0x");if (Parameter2 < 16) Serial.print("0");Serial.print(Parameter2, HEX);
  175.     Serial.print("("); Serial.print(Parameter2, DEC);Serial.println(")");
  176.    
  177.     // Excecute the entered command and parameters
  178.     execute_CMD(Command, Parameter1, Parameter2);
  179.   }
  180.  
  181.   if (mySerial.available()>=10)
  182.   {
  183.     // There is at least 1 returned message (10 bytes each)  
  184.     // Read the returned code
  185.     byte Returned[10];
  186.     for (byte k=0; k<10; k++)
  187.       Returned[k] = mySerial.read();
  188.    
  189.     // Write the returned code to the screen
  190.     Serial.print("Returned: 0x"); if (Returned[3] < 16) Serial.print("0"); Serial.print(Returned[3],HEX);
  191.     Serial.print("("); Serial.print(Returned[3], DEC);
  192.     Serial.print("); Parameter: 0x"); if (Returned[5] < 16) Serial.print("0"); Serial.print(Returned[5],HEX);
  193.     Serial.print("("); Serial.print(Returned[5], DEC);
  194.     Serial.print("), 0x"); if (Returned[6] < 16) Serial.print("0"); Serial.print(Returned[6],HEX);
  195.     Serial.print("("); Serial.print(Returned[6], DEC); Serial.println(")");
  196.   }
  197. }
  198. void execute_CMD(byte CMD, byte Par1, byte Par2)
  199. // Excecute the command and parameters
  200. {
  201.   // Calculate the checksum (2 bytes)
  202.   word checksum =  -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
  203.   // Build the command line
  204.   byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
  205.                Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
  206.   //Send the command line to the module
  207.   for (byte k=0; k<10; k++)
  208.   {
  209.     mySerial.write( Command_line[k]);
  210.   }
  211. }
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement