воскресенье, 11 сентября 2016 г.

Music on the Arduino

Arduino с TFT дисплеем и SD картой 2ГБ


Видео проигрывания


Еще картинка


Схема подключения


И код программы:
#include
#include
#include // Arduino LCD library

File file;
File root;
File entry;


// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8


// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
//SdFile root;


const int chipSelect = 4;

TFT TFTscreen = TFT(cs, dc, rst);
//char sensorPrintout[13];



ISR(TIMER1_COMPA_vect) {

OCR0A = file.read(); //Записываем в регистр сравнения значение,
// полученное из файла


}


void startPlayback(char file_name[13]) {


pinMode(6, OUTPUT);


file = SD.open(file_name); //Открываем файл


ASSR &= ~(_BV(EXCLK) | _BV(AS2));
TCCR0A |= _BV(WGM01) | _BV(WGM00);
TCCR0B &= ~_BV(WGM02);
TCCR0A = (TCCR0A | _BV(COM0A1)) & ~_BV(COM0A0);
TCCR0A &= ~(_BV(COM0B1) | _BV(COM0B0));
TCCR0B = (TCCR0B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);


cli();
TCCR1B = (TCCR1B & ~_BV(WGM13)) | _BV(WGM12);
TCCR1A = TCCR1A & ~(_BV(WGM11) | _BV(WGM10));
TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
OCR1A = F_CPU / 8000;
TIMSK1 |= _BV(OCIE1A);
sei();


//Крутимся в цикле, пока не кончится файл или в ком порту "n".
while(file.available()) {
if (Serial.read()=='n') {
break;
}
}


}

void stopPlayback() {
TIMSK1 &= ~_BV(OCIE1A);
TCCR1B &= ~_BV(CS10);
TCCR0B &= ~_BV(CS10);
digitalWrite(6, LOW);
}

void setup() {


Serial.begin(9600);


SD.begin(4); //Инициализация карты.


////////////////////////////////////////////////////////////////////////////////////


// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();


// clear the screen with a black background
TFTscreen.background(0, 0, 0);


// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255, 255, 255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("File :\n ", 0, 0);
// ste the font size very large for the loop
TFTscreen.setTextSize(2);


///////////////////////////////////////////////////////////////////////////////////////


pinMode(6, OUTPUT);


if (!card.init(SPI_HALF_SPEED, chipSelect)) {
// Serial.println("initialization failed. Things to check:");
TFTscreen.text("init failed\n ", 0, 40);
// Serial.println("* is a card is inserted?");
//Serial.println("* Is your wiring correct?");
//Serial.println("* did you change the chipSelect pin to match your shield or module?");
//return;
} else {
TFTscreen.text("init yes\n ", 0, 40);
//Serial.println("Wiring is correct and a card is present.");
}


// print the type of card


switch (card.type()) {
case SD_CARD_TYPE_SD1:
TFTscreen.text("SD1", 0, 60);
//Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
TFTscreen.text("SD2", 0, 60);
//Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
TFTscreen.text("SDHC", 0, 60);
// Serial.println("SDHC");
break;
default:
TFTscreen.text("Unknown", 0, 60);
//Serial.println("Unknown");
}
uint32_t razm;
razm=volume.blocksPerCluster();
String vol=String(razm);
char char_vol[13];
vol.toCharArray(char_vol,13);
TFTscreen.text(char_vol, 0, 80);
}


void loop() {
//TFTscreen.background(0,0,0); // очистка экрана
TFTscreen.stroke(255, 255, 255);
root = SD.open("/"); //Открываем root директорию


//Крутимся в цикле, пока не закончатся файлы


while(root) {/////////
entry = root.openNextFile(); //Открываем первый или следующий файл
if (entry) {
TFTscreen.text(entry.name(), 0, 20);
startPlayback(entry.name()); //Начинаем проигрывать ? файл
stopPlayback(); //Выкл таймеры
file.close();
TFTscreen.stroke(0, 0, 0);
TFTscreen.text(entry.name(), 0, 20);
TFTscreen.stroke(255, 255, 255);
}
else
{
//нет больше фалов
TFTscreen.stroke(255, 255, 255);
TFTscreen.text("nomorefiles", 0, 20);
root.close();
break;
}
}///////////


}


Контакты TFT-модуля


И контакты Atmega-168


Комментариев нет:

Отправить комментарий