This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] MSP430FR4133:与 DFPlayer Mini 连接

Guru**** 1626620 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/1355488/msp430fr4133-interfacing-with-dfplayer-mini

器件型号:MSP430FR4133

大家好、我正在尝试将 MSP430与外置迷你 MP3音频播放器连接起来。   不过、我不明白为什么该代码未播放我连接的 SD 卡上存储的任何音频文件、 硬件本身的功能确实很好、因为我可以使用按钮等来运行音频文件。但是、每当数据通过 MSP 发送到播放器本身时、就不会有输出。
这里是我的 main.c 文件。  
我还上传了我的 DFP.c 和 DFPlayer.h 代码、该代码来源于 Github。

#include <msp430.h>
#include <intrinsics.h>
#include <stdlib.h>
#include <DFPlayer.h>

void Uart_Init(void) {
    P1SEL0 |= BIT0 | BIT1;  // Set P1.0 and P1.1 to UART function
    UCA0CTLW0 |= UCSWRST;   // Enable software reset
    UCA0CTLW0 |= UCSSEL__SMCLK;  // Use SMCLK
    UCA0BR0 = 52;  // Baud rate setting for 9600 bps with 4MHz SMCLK
    UCA0BR1 = 0x00;
    UCA0MCTLW = 0x4900 | UCOS16 | UCBRF_1;  // Modulation settings
    UCA0CTLW0 &= ~UCSWRST;  // Disable software reset
}

void UART_Send_Data(unsigned char data) {
    while (!(UCA0IFG & UCTXIFG));  // Wait for TX buffer to be ready
    UCA0TXBUF = data;  // Transmit data
}

unsigned char UART_Read_Data(void) {
    if (UCA0IFG & UCRXIFG) { // Check if the receive buffer is full
        return UCA0RXBUF;  // Return received data
    }
    return 0; // Return 0 if no data is available
}

void Send_Command(unsigned char cmd, unsigned char feedback, unsigned char param1, unsigned char param2) {
    unsigned char command[10];
    unsigned int checksum = 0;
    int i;  // Move declaration to the start of the function

    command[0] = 0x7E;
    command[1] = 0xFF;
    command[2] = 0x06;
    command[3] = cmd;
    command[4] = feedback;
    command[5] = (char)(param1 >> 8);
    command[6] = (char)param1;

    for (i = 1; i < 7; i++) {
        checksum += command[i];
    }
    checksum = 0xFFFF - checksum + 1;
    command[7] = (checksum >> 8) & 0xFF;
    command[8] = checksum & 0xFF;
    command[9] = 0xEF;

    for (i = 0; i < 10; i++) {
        UART_Send_Data(command[i]);
    }
}

void Set_Volume(int volume) {
    if (volume < 0) volume = 0;
    if (volume > 30) volume = 30;
    Send_Command(0x06, 0, 0, volume);  // Command to set volume
}

void Play_Audio_File() {
    Set_Volume(25);  // Set volume to a clearly audible level
    Send_Command(0x03, 0x00, 0x00, 0x01);  // Command to play track number 1
}

void UART_Send_String(const char *str) {
    while (*str) {
        UART_Send_Data(*str++);
    }
}

void print_response(unsigned char response) {
    switch (response) {
        case 0x3A:
            UART_Send_String("Init complete\n");
            break;
        case 0x41:
            UART_Send_String("Error\n");
            break;
        case 0x40:
            UART_Send_String("ACK\n");
            break;
        default:
            UART_Send_String("Unknown response\n");
            break;
    }
}

int main(void) {
    WDTCTL = WDTPW | WDTHOLD;  // Stop watchdog timer
    Uart_Init();               // Initialize UART

    __delay_cycles(500000);    // Wait for DFPlayer Mini to initialize

    Play_Audio_File();         // Play the audio file "001.mp3" from the SD card

    while (1) {
        if (UCA0IFG & UCRXIFG) {
            unsigned char response = UART_Read_Data();  // Continuously read data from DFPlayer
            print_response(response);
        }
    }
}
#include "DFPlayer.h"

void DFPlayer_send_cmd(struct DFPlayer *player, char cmd,int16_t parameter)
{
    char buffer[10];
    int16_t CRC;
    CRC = -(0xFF + 0x06 + cmd + (parameter>>8) + parameter);
    buffer[0] = 0x7E;
    buffer[1] = 0xFF;
    buffer[2] = 0x06;
    buffer[3] = cmd;
    buffer[4] = 0x00;
    buffer[5] = (char)(parameter>>8);
    buffer[6] = (char)parameter;
    buffer[7] = (char)(CRC>>8);
    buffer[8] = (char)CRC;
    buffer[9] = 0xEF;

    player->send(buffer, 10);
}
#ifndef DFPLAYER_H
#define DFPLAYER_H

#include <stdint.h>

#define DFPLAYER_CMD_NEXT 0x01
#define DFPLAYER_CMD_PRESVIOUS 0x02
#define DFPLAYER_CMD_TRACK 0x03
#define DFPLAYER_CMD_VOLUME_P 0x04
#define DFPLAYER_CMD_VOLUME_M 0x05
#define DFPLAYER_CMD_VOL_MANU 0x06
#define DFPLAYER_CMD_EQ 0x07
#define DFPLAYER_CMD_MODE 0x08
#define DFPLAYER_CMD_STDBY 0x0A
#define DFPLAYER_CMD_NORMAL 0x0B
#define DFPLAYER_CMD_RESET 0x0C
#define DFPLAYER_CMD_PLAYBACK 0x0D
#define DFPLAYER_CMD_PAUSE 0x0E
#define DFPLAYER_CMD_FOLDER 0x0F
#define DFPLAYER_CMD_VOL_ADJ 0x10
#define DFPLAYER_CMD_REPEAT 0x11

#define DFPLAYER_QRY_PARAM 0x3F
#define DFPLAYER_QRY_ERROR 0x40
#define DFPLAYER_QRY_REPLY 0x41
#define DFPLAYER_QRY_CUR_STAT 0x42
#define DFPLAYER_QRY_CUR_VOL 0x43
#define DFPLAYER_QRY_CUR_EQ 0x44
#define DFPLAYER_QRY_CUR_MODE 0x45
#define DFPLAYER_QRY_CUR_VER 0x46
#define DFPLAYER_QRY_N_TF_FILE 0x47
#define DFPLAYER_QRY_N_USB_FILE 0x48
#define DFPLAYER_QRY_N_FLSH_FILE 0x49
#define DFPLAYER_QRY_KEEP_ON 0x4A
#define DFPLAYER_QRY_TF_TRACK 0x4B
#define DFPLAYER_QRY_USB_TRACK 0x4C
#define DFPLAYER_QRY_FLASH_TRACK 0x4D

#define DFPLAYER_USB_ONLINE                                        \
    {                                                              \
        0x7E, 0xFF, 0x06, 0x3F, 0x00, 0x00, 0x01, 0xFF, 0xC0, 0xEF \
    }
#define DFPLAYER_SD_ONLINE                                         \
    {                                                              \
        0x7E, 0xFF, 0x06, 0x3F, 0x00, 0x00, 0x02, 0xFF, 0xBF, 0xEF \
    }
#define DFPLAYER_USB_SD_ONLINE                                     \
    {                                                              \
        0x7E, 0xFF, 0x06, 0x3F, 0x00, 0x00, 0x03, 0xFF, 0xBE, 0xEF \
    }
#define DFPLAYER_PC_ONLINE                                         \
    {                                                              \
        0x7E, 0xFF, 0x06, 0x3F, 0x00, 0x00, 0x04, 0xFF, 0xBD, 0xEF \
    }
#define DFPLAYER_FLASH_ONLINE                                      \
    {                                                              \
        0x7E, 0xFF, 0x06, 0x3F, 0x00, 0x00, 0x08, 0xFF, 0xB9, 0xEF \
    }

#define DFPLAYER_USB_PUSH_IN                                       \
    {                                                              \
        0x7E, 0xFF, 0x06, 0x3A, 0x00, 0x00, 0x01, 0xFF, 0xC5, 0xEF \
    }
#define DFPLAYER_SD_PUSH_IN                                        \
    {                                                              \
        0x7E, 0xFF, 0x06, 0x3A, 0x00, 0x00, 0x02, 0xFF, 0xC4, 0xEF \
    }
#define DFPLAYER_USB_PUSH_OUT                                      \
    {                                                              \
        0x7E, 0xFF, 0x06, 0x3B, 0x00, 0x00, 0x01, 0xFF, 0xC4, 0xEF \
    }
#define DFPLAYER_SD_PUSH_OUT                                       \
    {                                                              \
        0x7E, 0xFF, 0x06, 0x3B, 0x00, 0x00, 0x02, 0xFF, 0xC3, 0xEF \
    }

typedef uint8_t (*dfplayer_send_fptr)(uint8_t *data, uint32_t length);

struct DFPlayer {
    dfplayer_send_fptr send;
};

void DFPlayer_send_cmd(struct DFPlayer *player, char cmd,int16_t parameter);

#endif /* DFPLAYER_H */