I am very close to finishing my little project. I had a proper pcb made for an mp3 player circuit, and mounted all the components yesterday. I programmed the chip with an mp3 palyer program i found on avrfreaks and it works
I am now trying to get it so that i can take four timing readings from a user input of clicking a button, add the number of seconds together and then play the track that has that index number (or name, but i think thats a bit more difficult). Below is the main function, that initialises the mp3 chip, and under that is a while loop that takes in the two sets of timings (each stores two times, 4 in total) stored as time_ms1, time_ms2, time_ms3 and time_ms4.
It them calls the function “equation” which does a little sum, adding all four times together, then the bit i am having trouble with is making the mp3 player play the track with that index number
I think it sounds more complex than it is, because the timers allready work, i just am struggling with the equation function and getting it to play the right song
Please can someone help me
Thanks:
unsigned int time_ms1 = 0;
unsigned int time_ms2 = 0;
unsigned int time_ms3 = 0;
unsigned int time_ms4 = 0;
volatile unsigned int timer_tick; //global timer variable MUST BE DECLARED VOLATILE
volatile unsigned int timer_tick_2; //global timer variable MUST BE DECLARED VOLATILE
//main function
int main()
{
uint8 retry = 0;
DDRD &= 0x03;//³õʼ»¯¶Ë¿Ú
PORTD |= 0xfc;
LED1_CON();
LED1_OFF();
LED2_CON();
LED2_OFF();
OSCCAL = 0x00;//×îСRCÕñµ´ÆµÂÊ //in order to operate some low speed card the initialization should run at lowest speed
Delay(0xffff);
MMC_SD_Init();//³õʼ»¯spi¿Ú //SPI initialize
Delay(0xffff);
if(VS1003B_Init())
{
//LED1_ON();//ÅäÖÃVS1003 //config vs1003
while(1)
{
LED1_ON();
Delay(0xffff);
LED1_OFF();
Delay(0xffff);
}
}
Delay(0xffff);//Ìṩ×ã¹»µÄÑÓʱ //supply enough delay
Delay(0xffff);
Delay(0xffff);
Delay(0xffff);
Delay(0xffff);
while(MMC_SD_Reset())//³õʼ»¯SD¿¨ //sd card initialize
{
retry++;
if(retry>20)
{
// LED2_ON();
while(1)
{
LED2_ON();
Delay(0xffff);
LED2_OFF();
Delay(0xffff);
}
}
}
OSCCAL = 0xff;//×î´óRCÕñµ´ÆµÂÊ //normal operation maximum the frequency
Delay(0xffff); //wait for stable
if(FAT_Init())//³õʼ»¯Îļþϵͳ Ö§³ÖFAT16ºÍFAT32 //initialize file system FAT16 and FAT32 are supported
{
// LED1_ON();
// LED2_ON();
// while(1);
while(1)
{
LED1_ON();LED2_ON();
Delay(0xffff);
LED1_OFF();LED2_OFF();
Delay(0xffff);
}
}
Search(PATH,&MusicInfo,&totalsongs,&type);
//search the songs in the root directery on the SD card
//You can also specify the directery where the songs are placed
//eg: Search("\\new\\mp3",&MusicInfo,&totalsongs,&type);
// means search the file in the foler C:\new\mp3
DDRD &= ~(1 << PD6);
PORTD|=(1<<PD6);
DDRD &= ~(1 << PD7);
PORTD|=(1<<PD7);
DDRD &= ~(1 << PD5);
PORTD|=(1<<PD5);
//initialize timer 0
TCNT0=~(124);//preload counter to overflow after 1 ms (125 counts)
TIMSK0=(1<<TOIE0);//enable interrupt on timer0 overflow
TCCR0B=(1<<CS01)|(1<<CS00);//set timer to normal mode with CPU clock/64 (125 kHz)
sei();//enable all interrupts
// time an event:
timer_tick=0;
timer_tick_2=0; //start timer, clear global millisecond counter
TCNT0 = ~(124);
while(1){
//first timing
time_ms1 = 0;
time_ms2 = 0;
while (PIND&(1<<PD6)); // loop here until first button press
TCNT0 = 0;
timer_tick=0;// here is where we start timing
_delay_ms(1); // delay for 1 ms to debounce button press
/* LED1_ON();LED2_ON();
Delay(0xffff);
LED1_OFF();LED2_OFF();
Delay(0xffff);*/
while (!(PIND&(1<<PD6))); // loop here until button is released
_delay_ms(10); // delay for 10 ms to debounce button release
while (PIND&(1<<PD6)); // loop here until second button press
time_ms1 = timer_tick; //get the value of the millisecond counter
timer_tick=0;// here is where reset the timer
_delay_ms(1); // delay for 1 ms to debounce button press
while (!(PIND&(1<<PD7))); // loop here until button is released
_delay_ms(10); // delay for 10 ms to debounce button release
while (PIND&(1<<PD6)); // loop here until 3rd button press
time_ms2 = timer_tick; //get the value of the millisecond counter
// lcd_line1();// display the times
// lcd_time(time_ms1);
// lcd_line2();
// lcd_time(time_ms2);
_delay_ms(10); // delay for 10 ms to debounce button release
// lcd_clear();
}
//second timing
{
time_ms3 = 0;
time_ms3 = 0;
while (PIND&(1<<PD6)); // loop here until first button press
TCNT0 = 0;
timer_tick_2=0;// here is where we start timing
_delay_ms(1); // delay for 1 ms to debounce button press
while (!(PIND&(1<<PD6))); // loop here until button is released
_delay_ms(10); // delay for 10 ms to debounce button release
while (PIND&(1<<PD6)); // loop here until second button press
time_ms3 = timer_tick_2; //get the value of the millisecond counter
timer_tick_2=0;// here is where reset the timer
_delay_ms(1); // delay for 1 ms to debounce button press
while (!(PIND&(1<<PD6))); // loop here until button is released
_delay_ms(10); // delay for 10 ms to debounce button release
while (PIND&(1<<PD6)); // loop here until 3rd button press
time_ms4 = timer_tick_2; //get the value of the millisecond counter
// lcd_line1();// display the times
// lcd_time(time_ms3);
//lcd_line2();
//lcd_time(time_ms4);
_delay_ms(1000); //for button debounce
//lcd_clear();
equation(); //call equation to add four times together
PlaySongs();
while(1)
{
while(1)
{
LED1_ON();LED2_OFF();
Delay(0xffff);
LED2_ON();LED1_OFF();
Delay(0xffff);
}
}
return 0;
}
}
//-----------------------------------
// Function
//----------------------------------
int equation()
{
//little eqn adding the two four timings together
return(0);
}
//-----------------------------------
// End of Function detmining ball position
//-----------------------------------
/*
TIMER 0 overflow interrupt service routine
This routine is called every millisecond
Operation: increment global variable timer_tick, reload TCNT0 to count 125 clock cycles
*/
ISR(TIMER0_OVF_vect)
{
timer_tick++;
timer_tick_2++;
TCNT0 = ~(125); //preload counter to overflow after 1 ms (125 counts)
}
Below is the function that gets a song to play:
void PlaySong(uint16 songs){
uint16 keylen; //ÓÃÓÚ¼ü´¦Àí //for key processing
uint16 count; //Êý¾Ý¼ÆÊý //data counting
uint8 i; //Ñ»·±äÁ¿ //loop variable
uint16 j; //Ñ»·±äÁ¿ //loop variable
uint32 p; //´Øָʾֵ //cluster
uint32 totalsect; //ÎļþÓµÓеÄÉÈÇøÊý //cotain the total sector number of a file
uint16 leftbytes; //Ê£Óà×Ö½Ú //cotain the left bytes number of a file //the last cluster usually not fully occupied by the file
uint8 *buffer; //»º³å //buffer
uint32 sector; //ÉÈÇø //recor the current sector to judge the end sector
uint8 flag; //²¥·Å/ÔÝÍ£±êÖ¾ //flag of pause
// uint16 vol=DefaultVolume;//³õʼÒôÁ¿£¬Ó¦Óëvs1003º¯ÊýÀïµÄ³õʼÒôÁ¿Ïàͬ //default volume
uint8 mode=DEFAULT_MODE;//µ¥ÇúÖظ´ //repet all by default
uint16 songs_cnt = 0; //how many songs have been played
if(totalsongs==0)return;//Èç¹ûûÓиèÇúÔòÒì³£Í˳ö //if no music file return
uint32 rand_val;
Timer1_Initial();//Æô¶¯¶¨Ê±Æ÷£¬ÓÃÓÚ²úÉúËæ»úº¯ÊýµÄÖÖ×Ó //initialize the timer
ClearTrackInfo();
//next://ÏÂÒ»Ê׸èµÄÆðʼµØ·½ //label for "goto"
if(mode==RANDOM)//Ëæ»ú²¥·Å¸èÇú //if the mode is shuffle the songs
{
songs_cnt++;
if(songs_cnt == totalsongs && totalsongs<1025)
{
ClearTrackInfo();
songs_cnt = 0;
}
rand_val = TCNT1;
Delay((random() && 0x00ff));
rand_val <<= 16;
rand_val += TCNT1;
srandom(rand_val);
if(totalsongs>1024)
{
songs = (uint16)(((random()/214749)*(uint32)totalsongs)/10000)+1;//Ëæ»ú²úÉú¸èÇúÐòºÅ //create random song number
}
while(totalsongs<1025)
{
songs = (uint16)(((random()/214749)*(uint32)totalsongs)/10000)+1;//Ëæ»ú²úÉú¸èÇúÐòºÅ //create random song number
if(SetTrack(songs) == 0)break;
}
}
count=0;//Çå»ùÊý //clear count
flag=1;
while(count<2048 && (type != MID))//recommand 2048 zeros honoring DREQ befor soft reset
{ //ÓÃÓÚ´ÓwmaÌø³öµ½ÏÂÒ»Ê׸裬ºÍÒ»Ê׸è½áÊøÌî³äÊý¾Ý//midi¸ñʽ²»ÐèÒª
if((VS1003B_PIN & _BV(VS1003B_DREQ))!=0)
{
for(j=0;j<32;j++)
{
VS1003B_WriteDAT(0x00);//Ìî³ä0 //fill 0
count++;
}
if(count == 2047)break;
}
}
VS1003B_SoftReset();//soft reset //in case of playing wma files//Èí¼þ¸´Î»
Search(PATH,&MusicInfo,&songs,&type); //ÕÒµ½ÏàÓ¦µÄÎļþ //find the file
p = MusicInfo.deStartCluster+(((uint32)MusicInfo.deHighClust)<<16);//¶ÁÎļþÊ×´Ø //the first cluster of the file
totalsect = MusicInfo.deFileSize/512; //¼ÆËãÉÈÇøÊý //calculate the total sectors
leftbytes = MusicInfo.deFileSize%512; //¼ÆËãÊ£ÓàµÄ×Ö½ÚÊý //calculate the left bytes
i=0;
sector=0;
while(1)
{
keylen=0;
for(;i<SectorsPerClust;i++) //Ò»¸ö´Ø //a cluster
{
buffer=malloc(512);
FAT_LoadPartCluster(p,i,buffer);//¶ÁÒ»¸öÉÈÇø //read a sector
count=0;
while(count<512)
{
if(flag==0){if(keylen){Delay(100);keylen--;}}
else if(type == MID){if(keylen){Delay(100);keylen--;}}
if((VS1003B_PIN & _BV(VS1003B_DREQ))!=0 && flag) //¸ù¾ÝÐèÒªËÍÊý¾Ý //send data honoring DREQ
{
for(j=0;j<32;j++) //ÿ´ÎËÍ32¸öÊý¾Ý //32 Bytes each time
{
VS1003B_WriteDAT(buffer[count]);
count++;
}
if(keylen)keylen--; //ÓÃÓÚ¼ü´¦Àí //for key processing
if(sector == totalsect && count >= leftbytes) //Èç¹ûÎļþÒѽáÊø //if this is the end of the file
{
if(type == MID)//waiting the midi file was decoded
{//¶ÔÓÚmidÒôÀÖÒª½Ó×ÅËÍ2048¸öÁã
count=0;
while(count<2048)//recommand 2048 zeros honoring DREQ goto next songs
{
if((VS1003B_PIN & _BV(VS1003B_DREQ))!=0 )
{
for(j=0;j<32;j++)
{
VS1003B_WriteDAT(0x00);
count++;
}
if(count == 2047)break;
}
}
}
i=SectorsPerClust;
break;
}//Îļþ½áÊø //file ended
if(count == 511){break;}//512×Ö½ÚËÍÍêÌø³ö //break if a sector was sent
}
if((PIND&STOP)==0 /*&& keylen==0*/)//²¥·ÅÔÝÍ£¼ü //key PLAY/PAUSE
{
Delay(100);
if(!(PIND&STOP))
{
while(!(PIND&STOP));
if(flag)flag=0;
else flag=1;
Delay(1000);
}
}
}
sector++;
free(buffer);
}
}
}