jh800215 发表于 2019-12-13 14:17:43

arduino+SD+syn6288文件朗读器



使用方法:SD卡中放入多个.txt文件,程序上传后在串口监视器(不要用arduino自己的串口监视器,用sscom)中能看到文件列表,然后串口输入播放的文件名(全名带扩展名),即可全文播放。要点:syn6288最多一次播放200字节内容,所以需要分段播放。

代码:

// include the SD library:
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
SoftwareSerial yySerial(6, 7); // RX, TX//6288 TXD-->6 ,RXD-->7

byte text1[] = {0xC7, 0xEB, 0xB0, 0xB4, 0xBC, 0xFC, 0xB2, 0xA5, 0xB7, 0xC5}; //请按键播放
byte text2[] = {0xCF, 0xB5, 0xCD, 0xB3, 0xC6, 0xF4, 0xB6, 0xAF}; //系统启动
byte text3[] = {0xC7, 0xEB, 0xCA, 0xE4, 0xC8, 0xEB, 0xD2, 0xAA, 0xB2, 0xA5, 0xB7, 0xC5, 0xB5, 0xC4, 0xCE, 0xC4, 0xBC, 0xFE, 0xC3, 0xFB}; //请输入要播放的文件名
byte text4[] = {0x53, 0x44, 0xBF, 0xA8, 0xB4, 0xED, 0xCE, 0xF3}; //SD卡错误
byte text5[] = {0xB0, 0xB4, 0xBC, 0xFC, 0xB4, 0xED, 0xCE, 0xF3}; //
byte text6[] = {0xB3, 0xAC, 0xCA, 0xB1, 0xCD, 0xCB, 0xB3, 0xF6}; //超时退出

int pin = 3; //接中断信号的脚
int length = 0;//语音内容长度
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 4;

char FileName;
byte yuyin;

void setup() {
// put your setup code here, to run once:
pinMode(pin, INPUT_PULLUP);
Serial.begin(9600);
yySerial.begin(9600);
SD.begin(chipSelect);

length = sizeof(text2) / sizeof(byte);
synout(text2, length);//系统启动

Serial.print("\nInit SD card...");
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("init failed. You need to check !");

    length = sizeof(text4) / sizeof(byte);
    synout(text4, length);//卡错误

    while (1);
} else {
    Serial.println("card is OK.");
}

if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    while (1);
}
Serial.println("\nFiles found on the card : ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);

length = sizeof(text3) / sizeof(byte);
synout(text3, length);//输入文件名播放

}


////读一段文字&print函数
void synout(byte yyd[] , int len)
{
int i = 0;
byte yuyindata;
byte yihuo = 0;

//yuyin组装前缀控制符
yuyindata = 0xFD;
yuyindata = 0x00;
yuyindata = len + 3;
yuyindata = 0x01;
yuyindata = 0x01;
//yuyin组装正文
for (int i = 0; i < len; i++)
{
    yuyindata = yyd;
}
//循环输出
for (int i = 0; i < len + 5; i++)
{
    yihuo = yihuo ^ yuyindata;
    // Serial.println();
    Serial.write(yuyindata);
    yySerial.write(yuyindata);
}
//输出校验码
    Serial.write(yihuo);
yySerial.write(yihuo);
delay(100);
}


void loop() {
// put your main code here, to run repeatedly:
uint8_t count = 0, c , n;

ReEnter:
count = 0;

Serial.println(F("\r\nEnter filename (send newline after input):"));
do {
    while (!Serial.available()) ;
    c = Serial.read();
    if (c > ' ') FileName = c;
} while ((c != 0x0d) && (c != 0x0a) && (count < 14));
FileName = 0;


Serial.println(F("-->Looking for file... "));
if (!SD.exists(FileName)) {
    Serial.print("-->");
    Serial.print(FileName);
    Serial.println(F("- not found on card! "));
    goto ReEnter;
} else {
    Serial.print("-->");
    Serial.print(FileName);
    Serial.println(F("---found!"));
    File dataFile = SD.open(FileName);
    if (dataFile) {
      //n = 0;
      while (dataFile.available()) {
      c = dataFile.read();
      yuyin = c;
      //Serial.write(c);
      //n = n + 1;
      if ((c < 0x10) || (count >196)) //找到0D或长度=196强行断句
      {
          if ((dataFile.read() < 0x10) || (count >196)) //继续找0A ,或最大到200,一行结束
          {
          //Serial.println(F("\r\nPress SW :"));
         Serial.println();Serial.print("[");Serial.print(count);Serial.print("]");
            length = count;
            synout(yuyin, length);//播放内容

            while (1)
            {
            int k = digitalRead(3);//等待信号
      //      delay(100);
            if (!k)
            {
                // n = 0;
                count = 0;
                break;
            }
            }
          }
      }
      }
      dataFile.close();
    }
    else {
      Serial.print("-->");
      Serial.print("error opening file :");
      Serial.println(FileName);
    }
}
}

方恨少 发表于 2020-2-16 19:56:29

效果刚刚的,学习好好用

jh800215 发表于 2020-3-1 13:14:42

方恨少 发表于 2020-2-16 19:56
效果刚刚的,学习好好用

:handshake
页: [1]
查看完整版本: arduino+SD+syn6288文件朗读器