-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduinoAtmega8WAVplayer.ino
112 lines (106 loc) · 3.28 KB
/
arduinoAtmega8WAVplayer.ino
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
// https://t.me/ArduinoKaraneJavan
//written by Ali Najafian
//arduino 1.8.10
//sucsess
#include "PetitFS.h"
#define TRIGGER1 215//it is very important number if you want to change the sample rate
#define TRIGGER2 40//it is very important number if you want to change the sample rate
FATFS fs; /* File system object */
volatile uint16_t i = 0;
volatile bool i_need_data1 = true;
volatile bool i_need_data2 = true;
volatile uint8_t buf1[256];
volatile uint8_t buf2[256];
volatile bool one_or_two=1;
ISR(TIMER2_COMP_vect)
{
if(one_or_two)OCR1AL = buf1[i];
else OCR1AL = buf2[i];
i++;
if (i >= sizeof(buf1))
{
i = 0;
one_or_two = !one_or_two;
}
if (i == TRIGGER1&&one_or_two)i_need_data2 = true;
if (i == TRIGGER2&&!one_or_two)i_need_data1 = true;
}
void errorHalt(char* msg) {
Serial.print(F("Error: "));
Serial.println(msg);
while (1);
}
void beginSD()
{
if (pf_mount(&fs)) errorHalt("pf_mount");
else Serial.println(F("sucsess mount"));
_delay_ms(1000);
if (pf_open("TITLE16.WAV")) errorHalt("pf_open");//WARNING: very important it MUST be an upper case string
else Serial.println(F("sucsess open"));
}
void setup()
{
Serial.begin(9600);//initialize Serial for handle errors
//timer 2 initialize
ASSR = 0 << AS2; //disable the async timer 2 clock source
TCCR2 = 0b00001010;//set timer 2 in CTC mode
TCNT2 = 0x00; //it is time 2 register. it automaticly counts up until it reach OCR2 value and it goes back to zero
OCR2 = 0x7C; //this is our time 2 value you change it if you want to use lower quality wav file
pinMode(9, OUTPUT);//very important if you want to use PWM
//begin time 1 in fast PWM mode
//in fact we use this timer as an software DAC.
TCCR1A = (1 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0) | (0 << WGM11) | (1 << WGM10);
TCCR1B = (0 << ICNC1) | (0 << ICES1) | (0 << WGM13) | (1 << WGM12) | (0 << CS12) | (0 << CS11) | (1 << CS10);
TCNT1H = 0x00;
TCNT1L = 0x00;
ICR1H = 0x00;
ICR1L = 0x00;
OCR1AH = 0x00;
OCR1AL = 0xFF;
OCR1BH = 0x00;
OCR1BL = 0x00;
TIMSK = 0x00;
TIMSK = (1 << OCIE2);
OCR1AL = 0xff;
//ok it has begun
//PWM testing section
_delay_ms(1000);
OCR1AL = 0x7f;
_delay_ms(1000);
OCR1AL = 0x00;
_delay_ms(1000);
pinMode(SS, OUTPUT);//very important if you want to use SPI hardware
Serial.println(F("hello world"));//say hello to every one
beginSD();// ...
Serial.println(F("\nDone!"));
}
void loop()
{
if(Serial.available())//Serial handling
{
switch(Serial.read())
{
case 'p':TCCR2 = 0;Serial.println(F("Paused!"));break;//disable timer 2
case 'c':TCCR2 = 0b00001010;Serial.println(F("Countinue"));break;//re-enable timer 2
default :Serial.println(F("Unknown command"));// :(
}
}
unsigned int nr;// store available bytes
if (i_need_data1)
{
i_need_data1 = false;
pf_read((void*)(buf1), sizeof(buf1), &nr);//read SD and store it to buffer number 1
}
if (i_need_data2)
{
i_need_data2=false;
pf_read((void*)(buf2),sizeof(buf2),&nr);//read SD data an store it to buffer number 2
}
if (nr == 0)// if we don't have any data
{
//dasable timer2 and timer 1
TCCR2 = 0;
TCCR1A = 0;
TCCR1B = 0;
}
}