Embedded System/ATmega128

[ATmega128] DFPlayer_Mini

전두선 2019. 8. 16. 14:33

DFPlayer_Mini 

DFPlayer+Mini+Manual.pdf
0.41MB
FN-M16P+Embedded+MP3+Audio+Module+Datasheet.pdf
0.79MB

- 이 오디오 코덱 모듈은 Micro SD카드에 있는 MP3 파일(또는 WMV)을 재생시켜주는 모듈입니다.

 

디바이스 마트        9,900  http://www.devicemart.co.kr/1278727

aliexpress            
 5pcs, US $5.59(6,384)  https://fr.aliexpress.com/item/5pcs-DFPlayer-Mini-MP3-Player-Module-MP3-Voice-Module-for-Arduino-DIY-Supporting-TF-Card-and/32769724485.html?spm=2114.01010208.3.1.i6qdJo&ws_ab_test=searchweb0_0%2Csearchweb201602_2_10152_10065_10151_10068_10136_10137_10060_

 

국내와 해외 가격이 많이 차이나니까 구매하실때 참고하세요! 

그리고 모듈만 있어서는 안되고 MP3 파일을 담을 Micro SD도 같이 가지고 계셔야 합니다.

 

 



/* 5V 인가시, Tx, Rx에 필히 1k 저항을 달아주셔야 잡음이 안난다는걸 주의하세요! */



 

저는 UART 통신으로 제어하는걸 원해서 이렇게 사용했지만, DFPlayer는 그밖에도 

ADKey, IO 모드를 제공하니 다른 모드를 원하시는분은 데이터 시트를 참고하세요. 

 

<UART 포맷 : 9600/8/n/1/n>

 

 

시리얼 통신으로 DFPlayer를 제어하기 위해서 데이터 시트에서 요구되는 조건을 충족시켜야 제어가 가능한데,

UART 통신의 포맷설정은 당연히 기본이고, 위 사진의 직렬 통신 포맷 순서도 지켜줘야 합니다.

 

간단하게 설명하면 명령의 시작을 알리는 Start Byte를 시작으로 사용자가 원하는 명령에 대한 데이터 6 Byte를

보내고 그에 따른 데이터 6 Byte의 Checksum을 계산 후 보내고 명령의 끝을 알리는 End Byte를 보내면 통신

은 마무리 됩니다.

 

명령에 대한 정보는 데이터 시트에 자세하게 나와 있으므로 참고 바랍니다!

 

예를 들어, SD Card에 저장된 0001.mp3를 재생하려면

0x7E(Start)/FF/06/12(Play CMD)/00/00/01(0001.mp3 Play)/FE/E8/EF(End) 가 됩니다.

추가적으로 Checksum = -(FF+06+12+00+00+01) = 0xFEE8이 되기 때문에, Checksum high byte는 0xFE,

그리고 Low byte는 0xE8이 됩니다.

 

 

MP3와 WMV에 대한 하드웨어 디코딩을 지원하며, FAT16과 FAT32 파티션

지원으로 최대 32GB까지의 메모리를 인식합니다.

 

- 노래는 반드시 SD 카드의 최상위 폴더에 mp3 폴더를 만들어서 

mp3 폴더 안에 넣어야 합니다. 

그리고 mp3 파일명을 0001 ~ 9999 까지 번호를 원래의 MP3 파일명의 앞에 붙여주면 됩니다.

또는 번호만 네이밍 해도 문제없습니다.

(ex> 0001.mp3 or 0001music.mp3)

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Atmel Studio 6.2 */
/* ATmega128 */
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
//
typedef unsigned char INT8;
typedef unsigned int INT16;
//
#define BAUD 9600
#define U2X_S 2     // Set of U2X --> 1 or 2
#define MYUBRR ((F_CPU*U2X_S)/(16L*BAUD)-1)
#define sbi(reg,bit)    reg |= (1<<(bit))      // Set "bit"th bit of Register "reg"
#define cbi(reg,bit)    reg &= ~(1<<(bit))
//
#define MP3_NEXT                    0x01
#define MP3_PREVIOUS                0x02
#define MP3_TRAKING_NUM                0x03 // 0..2999
#define MP3_INC_VOLUME                0x04
#define MP3_DEC_VOLUME                0x05
#define MP3_VOLUME                    0x06 // 0..30
#define MP3_EQ                        0x07 // 0-Normal / 1-Pop / 2-Rock / 3-Jazz / 4-Classic / 5-Base
#define MP3_PLAYBACK_MODE            0x08 // 0-Repeat / 1-folder repeat / 2-single repeat / 3-random
#define MP3_PLAYBACK_SOURCE            0x09 // 0-U / 1-TF / 2-AUX / 3-SLEEP / 4-FLASH
#define MP3_STANDBY                    0x0A
#define MP3_NORMAL_WORK                0x0B
#define MP3_RESET                    0x0C
#define MP3_PLAYBACK                0x0D
#define MP3_PAUSE                    0x0E
#define MP3_PLAY_FOLDER_FILE        0x0F // 0..10
#define MP3_VOLUME_ADJUST            0x10
#define MP3_REPEAT                    0x11 // 0-stop play / 1-start repeat play
// Query the System Parameters
#define MP3_Q_STAY1                    0x3C
#define MP3_Q_STAY2                    0x3D
#define MP3_Q_STAY3                    0x3E
#define MP3_Q_SEND_PRM                0x3F
#define MP3_Q_ERROR                    0x40
#define MP3_Q_REPLY                    0x41
#define MP3_Q_STATUS                0x42
#define MP3_Q_VALUE                    0x43
#define MP3_Q_EQ                    0x44
#define MP3_Q_PLAYBACK_MODE            0x45
#define MP3_Q_SOFT_VERSION            0x46
#define MP3_Q_TF_CARD_FILES            0x47
#define MP3_Q_U_DISK_CARD_FILES        0x48
#define MP3_Q_FLASH_CARD_FILES        0x49
#define MP3_Q_KEEPON                0x4A
#define MP3_Q_CURRENT_TRACK_TF        0x4B
#define MP3_Q_CURRENT_TRACK_U_DISK    0x4C
#define MP3_Q_CURRENT_TRACK_FLASH    0x4D
////////////////////////////////////////////////////////////////////////////////
//Commands parameters
////////////////////////////////////////////////////////////////////////////////
#define MP3_EQ_Normal                    0
#define MP3_EQ_Pop                        1
#define MP3_EQ_Rock                        2
#define MP3_EQ_Jazz                        3
#define MP3_EQ_Classic                    4
#define MP3_EQ_Base                        5
#define MP3_PLAYBACK_MODE_Repeat        0
#define MP3_PLAYBACK_MODE_folder_repeat    1
#define MP3_PLAYBACK_MODE_single_repeat    2
#define MP3_PLAYBACK_MODE_random        3
#define MP3_PLAYBACK_SOURCE_U            0
#define MP3_PLAYBACK_SOURCE_TF            1
#define MP3_PLAYBACK_SOURCE_AUX            2
#define MP3_PLAYBACK_SOURCE_SLEEP        3
#define MP3_PLAYBACK_SOURCE_FLASH        4
 
INT8 default_buffer[10= {0x7E0xFF0x060x000x000x000x000x000x000xEF}; // Default Buffer
volatile INT8 mp3_cmd_buf[10= {0x7E0xFF0x060x000x000x000x000x000x000xEF};
    
void USART0_Init( INT16 ubrr )
{
    // Set baud rate
    UBRR0H = (INT8)(ubrr>>8);
    UBRR0L = (INT8)ubrr;
    // Enable U2X
    if(U2X_S == 2)
    sbi(UCSR0A, U2X);
    else
    cbi(UCSR0A, U2X);
    // Enable receiver and transmitter
    sbi(UCSR0B, RXEN);
    sbi(UCSR0B, TXEN);
    // Set frame format: 8data, 1stop bit
    cbi(UCSR0C, UMSEL);  // asynch
    cbi(UCSR0C, USBS);   // 1 Stop bit
    cbi(UCSR0C, UPM01);  // No parity
    cbi(UCSR0C, UPM00);
    cbi(UCSR0B, UCSZ02); // 8-bit
    sbi(UCSR0C, UCSZ01);
    sbi(UCSR0C, UCSZ00);
}
void USART0_Transmit( char data )
{
    // Wait for empty transmit buffer
    while ( !( UCSR0A & 0x20 ) )   // (1<<UDRE) 
    ;
    // Put data into buffer, sends the data
    UDR0 = data;
}
 
INT16 MP3_checksum (void)
{
    INT16 sum = 0;
    INT8 i;
    for (i=1; i<7; i++) {
        sum += mp3_cmd_buf[i];
    }
    return -sum;
}
void MP3_send_cmd (INT8 cmd, INT16 high_arg, INT16 low_arg)
{
    INT8 i;
    INT16 checksum;
    mp3_cmd_buf[3= cmd;
    mp3_cmd_buf[5= high_arg;
    mp3_cmd_buf[6= low_arg;
    checksum = MP3_checksum();
    mp3_cmd_buf[7= (INT8) ((checksum >> 8& 0x00FF);
    mp3_cmd_buf[8= (INT16) (checksum & 0x00FF);
    for( i=0; i< 10; i++){
        USART0_Transmit(mp3_cmd_buf[i]);
        //putchar(mp3_cmd_buf[i]);
        mp3_cmd_buf[i] = default_buffer[i];
    }
}
void dfplayer_init(void)
{
    MP3_send_cmd(MP3_PLAYBACK_SOURCE,0,MP3_PLAYBACK_SOURCE_TF);_delay_ms(10);
    MP3_send_cmd(MP3_VOLUME, 030); _delay_ms(10);
}
 
int main(void)
{
    
    USART0_Init(MYUBRR); // 9600/8/n/1/n
    
    dfplayer_init();
    
    while(1)
    {
        MP3_send_cmd(0x12,0,1); _delay_ms(5000); // 0001.mp3 Play
    }
}
 
cs

 

BUSY 체크 핀을 이용해서 최소 딜레이만 주고 MP3 재생도 가능합니다. (데이터 시트 참고)