极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 24690|回复: 2

arduino+SD+syn6288文件朗读器

[复制链接]
发表于 2019-12-13 14:17:43 | 显示全部楼层 |阅读模式


使用方法: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[16];
byte yuyin[200];

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[206];
  byte yihuo = 0;

  //yuyin组装前缀控制符
  yuyindata[0] = 0xFD;
  yuyindata[1] = 0x00;
  yuyindata[2] = len + 3;
  yuyindata[3] = 0x01;
  yuyindata[4] = 0x01;
  //yuyin组装正文
  for (int i = 0; i < len; i++)
  {
    yuyindata[i + 5] = 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[count++] = c;
  } while ((c != 0x0d) && (c != 0x0a) && (count < 14));
  FileName[count++] = 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[count++] = 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);
    }
  }
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2020-2-16 19:56:29 | 显示全部楼层
效果刚刚的,学习好好用
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-1 13:14:42 | 显示全部楼层
方恨少 发表于 2020-2-16 19:56
效果刚刚的,学习好好用

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-26 13:35 , Processed in 0.045012 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表