Timer

It seemed to survive phew!
I programmed it and added it to my circuit.

I have started my circuit again though.

If you have two different pins going to vcc, and vcc always has a capacitor also going to ground, can you have only 1 capacitor for both pins going to vcc - i.e can you save on capacitors, if the values are the same?

Capacitors are funny things. They’re not always absolutely necessary (in the same sense as, say, the reset pull-up resistor), but sometimes you see odd behavior when you don’t have them, and sometimes things just won’t work without them.

Voltage regulator manufacturers specify capacitors you should have between the input and output lines to the regulator and ground, with values usually in the 1uF to 100uF range. These are probably the most important, since they dampen out large voltage spikes and fluctuations across your entire power bus.

Aside from that, it sounds like you’re talking about the (lots and lots of) 0.1uF capacitors placed all over, at the power pins of the various chips, I/O lines and such. In your schematic there are so many of these listed on the same VCC line because you’re supposed to put them as close to the device pins they connect to as possible. These are sort of extra insurance, and generally less important in low-power projects like yours (again, except for when things start going wrong without them).

Since no lives depend on your mp3 player working perfectly every time, you can leave out as many capacitors as you want, but if you start having unexplainable inconsistent behavior, put in some capacitors and see if they help!

-Adam

cheers for that, and what about leds, if three leds, on three different pins all have the same resistor value they are in series with then going to ground, can they all use the same resistor?

secondly, i think i have found my error. when i dismantled my breadboard with the WORKING circuit on, i wrote it out on paper, but i got muddled up please can you check the following for me, because i am working off the schematic which uses the cs1003 and i am using my simplified vs1oo2 breakout board,
PIN | Description
1 | RESET
7 | VCC
8 | GND
15 | LED
16 | CS???
17 | SI
18 | SO
19 | CLK
20 | VCC
22 | GND
23 | RST
24 | DREQ
25 | BSYNC
26 | CS ???
28 | LED

I have left the buttons out to simplify things, I am mostly worried about a) the CS. does it go to 16 or 26, and is everything else correct, I think i wrote down SI as pin 16 when i copied the breadboard, which meant that SO CLK and VCC of 17,18,19 and 20 all got out of line. If i follow the above is that correct?
also in the scematic there is XCS - does the refer to me at all, i think i can leave it out cant I?
thanks

alex

right my situation.

i think i cooked my 3.3v reg so changed to a 2v. Its code on it is TS1086. I think that in pin order it goes 1. GND 2.OUT . IN But cant find a datasheet.

When i turn my circuit on - which i have now completely rebuilt. I get led 1 flashing for a couple of flashes and then it stops. LED 1 is supposed to say that it cant find VS1002. The only worry though is the mega16 gets VERY hot? so does the regulator?

I have the capacitors in the circuit that are part of the power supply, but no others?

I have the CS from the vs1002 going to pin 26 because in the schematic from the mp3, it is XCS which goes to pin 26, and in the schematic for the vs1002 breakout it is XCS. wheras the CS from the SD card goes to pin 16. Is this right?

i have checked all the connections and they are making it to the pins. What could cause the heat?

oh and by the way. There is a 0.1uf cap between out of v reg and gnd, can this not account for in the schematic where it says there should be one between every vcc pin and gnd?

I have checked every connection and they seem to be going to the right places. if you check ksd-design.co.uk/photos/photos.html you can see a plan

Somehow (i havent got a clue how, but somehow) i olved the problem o not being able to connect to the vs1002 board, and now I am trying to work out why the SD card cant be read. According to multimeter, CS, DI, VCC, CLK, GND and SO are all connected to their respective pins.

One question though. From the schematic, do you think the CS of the vs1002 and the CS of the SD card should be connected to the same pin?

If you have two LEDS in parallel, and turn one on, then the other, will the brightness of the first drop?

Sorry for the delay, I was away for a long weekend. Lets see now…

Putting LEDs in parallel going through the same resistor to ground is a dicey proposition. The multiple LEDs won’t light up properly, and when you have only some on you risk damaging the LEDs that are off by exceeding their maximum reverse voltage. In general if you want to switch an LED on separately from others, it needs its own resistor. If you want several LEDs to light up together, you can put several of them in series with a single resistor. I find this website to be extremely helpful in designing such LED arrays. Until your LEDs are together drawing more current than your voltage regulator (or other power source) can supply, you should be able to put as many LED+resistor pairs in parallel as you want, and turning on one shouldn’t noticeably dim the others.

Yikes, and I hope you figured out your regulator problem, because no component on your board should be getting more than the very slightest bit warm to the touch! If anything is getting hot it’s a sign of something seriously, damagingly wrong, and you should disconnect the power and try to figure out what it is. A voltage regulator wired up improperly can let the source voltage straight through while getting very hot itself, can you measure the output voltage of your new regulator?

Finally, the schematic is a little confusing because every pin has two names (in addition to the pin number). The name on the inside of the gray box identifies the function of that pin for that particular device, and for the most part while you’re laying out your wiring, you can disregard these names. The names you want to pay attention to are the ones outside the gray boxes, which are the names of the ‘nets’. Every pin (two or more) on the same net should be wired together, but if all these wires were drawn out on the schematic diagram it would be messy and difficult to read. The confusing part is that these nets can have very similar names (CS and XCS for example), and even have the same name as the internal function names of pins on the devices, which they may or may not be connected to. It sounds like you’ve figured this out tough, and yes, The CS pin on the SD card header should be connected to pin 16 on the AVR because they are both on the CS net, and the XCS pin on the mp3 chip should be connected to pin 26 on the AVR because they are both on the XCS net.

I hope that helps, and that none of your chips have burned out!

-Adam

Well somehow thanks to I dont know what, I think everything has survived.

I have changed my voltage regulator back to 3.3 (a new one) and we are one step further.
According tto the LEDS my vs1002 i OK but my SD is “failing”. Its not the card - thats new today (i killed the old one somehow).

I dont really have room for any more resistors, so i think for now i will take the risk of the LEDS blowing - if i increase/decrease the resistor, can this increase the chances of my LEDs surviving? Or what about that resistor theory, when three resistors are in parallel, the inverse total is equal to the sum of the inverses? or something?

I think if i crack the sd card error, everything will work properly! - because the microcontroller got very hot, even though it is telling me an error, and lighting up the leds could it still be damaged - which is actually the error and not the SD card?

What methods are there for casing a circuit? I know you can buy enclosures, but is the a silicon gel or something that i can mold over the pcb - i know that would prevent any further modifications, but i want to se if i can have the SMALLEST possible casing! - maybe a liquid fibreglass moulded over it, protecting everything??? odd question i know!
Could the error b other parts of the circuitry, because when i re built the power supply set up (voltage reg etc), it fixed the vs1002 error!?
Hope you had good weekend

alex

I don’t really have any good guesses as to what is causing your errors. In terms of theory, LEDs are essentially diodes that happen to output light, and your LEDs really should have their own resistors! This could be part of your other problems, but in general it’s also just good practice. If you don’t have space on the board, you can solder the resistors in line with the LED pins, like this:

Plastic enclosures are the standard way to go, and they come in all shapes and sizes. You can also encase your board various resins once you’re super-sure you don’t want to make any more changes. I’ve made things like epoxy-encased USB memory sticks, and here’s a neat project where someone encased an Arduino and an LED matrix in clear casting resin. If you do this, make sure to use a good mold release meant to work with your resin, and if you’re using a transparent resin, make sure the walls of your mold are very smooth.

-Adam

Cheers Adam,

Is there anyway I can test (without an expensiv oscilliscope) whether any data is flowing back and forward from the mega168 to the SD card? with a multimeter i mean. There is a voltage across the vcc/gnd pins of 3.28 volts which is promising - telling me it is getting power, so i feel it must be a problem on either SO, SI CLK or CS lines - obvously i suppose - there the only others!

Yeah, you don’t need to buy an oscilloscope just to see if there is data going by or not, only if you need to be able to actually see what the data is.

The simplest method is to use an LED with a very high resistor (~1Kohm) between a signal pin and ground (the high resistance keeps the LED from drawing too much current and messing up the signal). The Orangutan’s have an LED like this built into the serial TX line, so you can see when you’re transmitting serial bytes. The LED will only glow faintly, but you should be able to see it flicker if there are data bits going by. Depending on the serial protocol you’re using the LED will either be normally on and wink off as data goes by, or normally off and flash on as data goes by.

Another neat trick I discovered by accident (to get the most out of an 8-pin chip you really have to multi-purpose the programming pins) is to run a piezoelectric buzzer off of a signal pin. To hook the buzzer up directly to the pin without interfering with the signal you would need a high resistor, like a 1Kohm, and I’m not sure you would still be able to hear the buzzer at such low currents. My board ran one of the signal pins through a resistor to a transistor, which switched power to the buzzer on and off. You could hear it click as little packets of data went by, and it sounded like the matrix when you programmed the AVR!

-Adam

I’ll try the led one, its sounds a little easier!

I changed the microcontroller - only, and now i get no leds on, when before i got one. I’m going to try the old microcontroller back to see if the led comes on again. if it does, i am absolutely stumped. if it doesnt i think my parallel leds have blown. will replace them and try again.

i put a voltmeter across the led and there is nothing, which is worrying, because between vcc and gnd there is 3.3v

there is no common snse solution i can find to all this little bugs. its driving me CRAZY!

its so annoying when you have a breadboard circuit working beautifully, then move it to a permanent setup and the whole thing has to start again.

edit:

yeah, led comes on with old microchip!

EDIT 2:

I repgorammed the microchip that was bringing the led on, with exactly the same code as the second chip - that wasnt bringing it on, and now its also not bringing the led on, which suggests the code does not bring the led on??? (confused?).

THe code is below, and all i can see is it only brings leds on when there is an error - flashing. but then why is it not getting to the function playmusic()?

/*******************************************************************/
/*          Mega8 MP3 Player (mega8 + VS1003 + SD)  V1.8           */
/*                                                                 */
/* Discription: Can play mp3/wma/wav/mid SF0 files in the root     */
/*              directery on the SD card which file system is FAT16*/
/*              or FAT32.                                          */
/*              It has six keys to control this player             */
/*              MODE: switch between repeat all (default),repeat 1 */
/*                    and shuffle                                  */
/*              PLAY/PAUSE:                                        */
/*              PREV: previous songs                               */
/*              NEXT: next songs                                   */
/*              UP  : volume up                                    */
/*              DOWN: volume down                                  */
/* Platform   : AVRStudio4.13 b528 + WinAVR20070525                */
/*              optimize -0s                                       */
/* Author     : bozai(Zhang Qibo)                                  */
/* E-mail     : sudazqb@163.com                                    */
/* MSN        : zhangqibo_1985@hotmail.com                         */
/* Date       : 2007-06-16                                         */
/*******************************************************************/
/*  V1.8                                                           */
/*  2007-06-16                                                     */
/*  Still the bug of FAT, it's not resolved totally                */
/*                                                                 */
/*  V1.7                                                           */
/*  2007-05-21                                                     */
/*  Fix a serious bug of FAT                                       */
/*  Set the default mode to "Shuffle"                              */
/*  Because the mode button is inside the box                      */
/*  So I don't want to open the box when I turn on it              */
/*  If the button is easy to press of your mp3 player              */
/*  just change the macro below to:                                */
/*            0 = repet all    1 = repet one  2 = shuffle          */

#define DEFAULT_MODE   0

/*                                                                 */
/*  V1.6                                                           */
/*  2007-05-09                                                     */
/*  And a track function to make the shuffle a real shuffle        */
/*  Due to the limted RAM space of mega8. Maximum no repetition    */
/*  songs is 1024. And songs more than that will use randomly play */
/*  Special thanks to Ronald Ausloos (Belgium)'s advice            */ 
/*                                                                 */
/*  V1.5                                                           */
/*  2007-05-03                                                     */
/*  Change the behavior of the LEDs:                               */
/*                      VS1003 faild: LED1 blink                   */
/*                      SD faild    : LED2 blink                   */
/*                      FAT faild   : LED1 & LED2 blink alternatly */
/*                      File not found: Both LED blink             */
/*  Add read capacity function, fix the bug of FAT init            */
/*  Add slow start up code of VS1003                               */ 
/*                                                                 */                                                              
/*  V1.4                                                           */
/*  2007-05-02                                                     */
/*  Add enough delay when operate VS1003                           */ 
/*                                                                 */                                                              
/*  V1.3                                                           */
/*  2007-04-21                                                     */
/*  Modify the retry time of sd reset                              */
/*  Enable some code incase that when FAT initialize               */
/*  faild program can't jump out the loop                          */
/*  so the error LED can't light up                                */
/*  And light up both leds when FAT_Ini fails                      */
/*                                                                 */
/*  V1.2:                                                          */
/*  2007-04-04:                                                    */
/*  Add a macro of path                                            */
/*                                                                 */
/*  V1.1:                                                          */
/*  2007-02-25                                                     */
/*  Change the directory to C:\music\                              */
/*  A bug fixed                                                    */
/*  Modify some code to adjust new GCC compiler                    */
/*                                                                 */
/*  V1.0:                                                          */
/*  2006-12-03:                                                    */
/*  Original Version                                               */
/*******************************************************************/

#include<avr/io.h>
#include"MMC_SD/MMC_SD.h" //head files
#include"FAT/FAT.h"
#include"VS1003B/VS1003B.h"
#include<avr/pgmspace.h>

#define uint8 unsigned char
#define uint16 unsigned int
#define uint32 unsigned long


#define PATH (unsigned char *)("\\music")
//It's the path where the file placed
//Change it if you want to placed it to anoter foler
//The path should not have blanks, and each foler name's length should less than 8 with no extention

//diagnose and state indicate leds
//at start up this two led indicate error
//at normal they indicate the state of the MODE 
#define LED1_CON() DDRC|=_BV(PC5)
#define LED1_ON()  PORTC|=_BV(PC5)
#define LED1_OFF() PORTC&=~_BV(PC5)

#define LED2_CON() DDRB|=_BV(PB1)
#define LED2_ON()  PORTB|=_BV(PB1)
#define LED2_OFF() PORTB&=~_BV(PB1)

//keys
#define STOP _BV(PD2)
#define MODE _BV(PD3)
#define NEXT _BV(PD7)
#define UP   _BV(PD4)
#define DOWN _BV(PD6)
#define PREV _BV(PD5)

#define MP3 1
#define WMA 2
#define MID 3

//mode
#define REPET_ALL 0
#define REPET_ONE 1
#define RANDOM    2

extern uint16 SectorsPerClust;//ÿ´ØÉÈÇøÊý
extern uint16 FirstDataSector;//µÚÒ»¸öÊý¾ÝÉÈÇøÊý   //struct of file information
extern uint8  FAT32_Enable;

struct FileInfoStruct FileInfo;//ÎļþÐÅÏ¢

struct direntry MusicInfo;	//Òª²¥·ÅµÄmp3ÎļþÐÅÏ¢		//the mp3 file item whichi will be played
uint16 totalsongs;			//×ܵÄÒôÀÖÎļþÊýÄ¿			//total songs in the root directery on the SD card
uint8 type;					//ÎļþÀàÐÍ					//file type

uint8 track[128];			//stroe the information of songs (bit set indicate songs has been played)

void ClearTrackInfo()		//cleare the array track[128]
{
	uint8 i;
	for(i=0;i<128;i++)track[i] = 0;
}

uint8 SetTrack(uint16 songs)//set the track bit, return 1 means the song has been played
{
	uint8 byte_offset;
	uint8 bit_offset;
	songs--;
	byte_offset = songs/8;
	bit_offset = songs%8;
	if(track[byte_offset] & (1<<bit_offset))return 1;
	else
	{
		track[byte_offset] |= 1<<bit_offset;
		return 0;
	}
}

void Delay(uint16 n)//ÑÓʱ
{
	while(n--)asm("nop");
}

//Timer initialization offer seed of the srandom()
void Timer1_Initial()
{
 TCNT1H=0x00;
 TCNT1L=0x00;
 TCCR1B=0x01;//system clock;
 ICR1H=0xff;
 ICR1L=0xff;
}

void PlayMusic()//²¥·ÅÒôÀÖº¯Êý£¬Ò»µ©Ö´Ðв»»áÍ˳ö
{
	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
	uint16 songs=1;			//ĬÈϷŵÚÒ»Ê׸è	//play the fist songs by default
	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);
					 }
				 }
				else if(!(PIND&DOWN) && keylen==0)  //ÒôÁ¿-			//Volume down
	 			{
				    Delay(100);
	   			 	if(!(PIND&DOWN))  
					{
					keylen=200;
					   vol=vol+((uint16)(1<<8)+1);
					   if(vol>=0xFEFE) vol=0xFEFE; 
	   			       else VS1003B_WriteCMD(0x0b,vol);
					 }
	 			 }
				 else if(!(PIND&UP) && keylen==0) //ÒôÁ¿+			//Volume up
	  			 {
					 Delay(100);
				 	 if(!(PIND&UP)) 
					 {
					 keylen=200;
					   vol=vol-((uint16)(1<<8)+1);
					   if(vol<=0x0101) vol=0x0101;
					   else VS1003B_WriteCMD(0x0b,vol);
	   			     }
				  }
				 else if(!(PIND&NEXT)) //ÏÂÒ»Ê×						next songs
	 			 {
					Delay(0x7fff);		
					if(!(PIND&NEXT))
					{
						Delay(0x7fff);
						if(!(PIND&NEXT))
						{
							while(!(PIND&NEXT));
							songs++;
							if(songs > totalsongs)songs=1;
							{
								free(buffer);
							//	Delay(0xffff);
								goto next;
							}
						}
					}
	  			 }
				 else if(!(PIND&PREV)) //ÉÏÒ»Ê×						previous songs
	 			 {
					Delay(0x7fff);		
					if(!(PIND&PREV))
					{
						Delay(0x7fff);
						if(!(PIND&PREV))
						{
							while(!(PIND&PREV));
							if(songs == 1)songs=totalsongs;
							else songs--;
							{
								free(buffer);
							//	Delay(0xffff);
								goto next;
							}
						}
					}
	  			 }
				 else if((!(PIND&MODE)) && keylen==0) //ģʽ			//mode key
	 			 {
					Delay(100);		
					if(!(PIND&MODE))
					{
						keylen=0xffff;
						if(mode==REPET_ALL)
						{
							mode=REPET_ONE;
							LED1_ON();
							LED2_OFF();
						}
						else if(mode==REPET_ONE)//next mode is shuffle
						{
							mode=RANDOM;
							LED1_OFF();
							LED2_ON();
							srandom(((uint32)TCNT1)<<16);//²úÉúËæ»úÊýµÄÖÖ×Ó
						}
						else 
						{
							mode=REPET_ALL;
							LED1_OFF();
							LED2_OFF();
						}
					}
	  			 }
			}
			sector++;
			free(buffer);
		}
		i=0;
		p=FAT_NextCluster(p);//¶ÁÏÂÒ»´ØÊý¾Ý			//read next cluster
		if(p == 0x0fffffff || p == 0x0ffffff8 || (FAT32_Enable == 0 && p == 0xffff))//Èç¹ûÎÞºóÐø´ØÔò½áÊø£¬	//no more cluster
		{
				if(mode==REPET_ALL)songs++;
				if(songs>totalsongs)songs=1;
				goto next;
		}
	}
}

//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("\\new\\mp3",&MusicInfo,&totalsongs,&type);
	//´ú±íÔÚ¸ùĿ¼ÏµÄnewÎļþ¼ÐϵÄmp3ÎļþϵÄÒôÀÖÎļþ

	//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

	PlayMusic();//²¥·Å¸èÇú		//play songs
	while(1)
	{
		while(1)
		{
			LED1_ON();LED2_OFF();
			Delay(0xffff);
			LED2_ON();LED1_OFF();
			Delay(0xffff);
		}
	}
	return 0;
}

by the way, i have no buttons - would it notice this and sulk?

I posted also above this post, i dont think you’ve read.

I went back to our code we made that took the timing readings - which i modified so that it was all in the main function (and removed the slide pot - if you remember, for the time being). the codes below.

On this one i get a constant clicking in the headphones maybe 10 times a second???, and pressing a button which i have now added between pins 13 and 8(gnd) for pd7, the button does…nothing…

/*******************************************************************/
/*          Mega8 MP3 Player (mega8 + VS1003 + SD)  V1.8           */
/*                                                                 */
/* Discription: Can play mp3/wma/wav/mid SF0 files in the root     */
/*              directery on the SD card which file system is FAT16*/
/*              or FAT32.                                          */
/*              It has six keys to control this player             */
/*              MODE: switch between repeat all (default),repeat 1 */
/*                    and shuffle                                  */
/*              PLAY/PAUSE:                                        */
/*              PREV: previous songs                               */
/*              NEXT: next songs                                   */
/*              UP  : volume up                                    */
/*              DOWN: volume down                                  */
/* Platform   : AVRStudio4.13 b528 + WinAVR20070525                */
/*              optimize -0s                                       */
/* Author     : bozai(Zhang Qibo)                                  */
/* E-mail     : sudazqb@163.com                                    */
/* MSN        : zhangqibo_1985@hotmail.com                         */
/* Date       : 2007-06-16                                         */
/*******************************************************************/
/*  V1.8                                                           */
/*  2007-06-16                                                     */
/*  Still the bug of FAT, it's not resolved totally                */
/*                                                                 */
/*  V1.7                                                           */
/*  2007-05-21                                                     */
/*  Fix a serious bug of FAT                                       */
/*  Set the default mode to "Shuffle"                              */
/*  Because the mode button is inside the box                      */
/*  So I don't want to open the box when I turn on it              */
/*  If the button is easy to press of your mp3 player              */
/*  just change the macro below to:                                */
/*            0 = repet all    1 = repet one  2 = shuffle          */

#define DEFAULT_MODE   0

/*                                                                 */
/*  V1.6                                                           */
/*  2007-05-09                                                     */
/*  And a track function to make the shuffle a real shuffle        */
/*  Due to the limted RAM space of mega8. Maximum no repetition    */
/*  songs is 1024. And songs more than that will use randomly play */
/*  Special thanks to Ronald Ausloos (Belgium)'s advice            */ 
/*                                                                 */
/*  V1.5                                                           */
/*  2007-05-03                                                     */
/*  Change the behavior of the LEDs:                               */
/*                      VS1003 faild: LED1 blink                   */
/*                      SD faild    : LED2 blink                   */
/*                      FAT faild   : LED1 & LED2 blink alternatly */
/*                      File not found: Both LED blink             */
/*  Add read capacity function, fix the bug of FAT init            */
/*  Add slow start up code of VS1003                               */ 
/*                                                                 */                                                              
/*  V1.4                                                           */
/*  2007-05-02                                                     */
/*  Add enough delay when operate VS1003                           */ 
/*                                                                 */                                                              
/*  V1.3                                                           */
/*  2007-04-21                                                     */
/*  Modify the retry time of sd reset                              */
/*  Enable some code incase that when FAT initialize               */
/*  faild program can't jump out the loop                          */
/*  so the error LED can't light up                                */
/*  And light up both leds when FAT_Ini fails                      */
/*                                                                 */
/*  V1.2:                                                          */
/*  2007-04-04:                                                    */
/*  Add a macro of path                                            */
/*                                                                 */
/*  V1.1:                                                          */
/*  2007-02-25                                                     */
/*  Change the directory to C:\music\                              */
/*  A bug fixed                                                    */
/*  Modify some code to adjust new GCC compiler                    */
/*                                                                 */
/*  V1.0:                                                          */
/*  2006-12-03:                                                    */
/*  Original Version                                               */
/*******************************************************************/
// F_CPU tells util/delay.h our clock frequency
       #define F_CPU 8000000UL   // Orangutan frequency (8MHz)

#include<avr/io.h>
#include"MMC_SD/MMC_SD.h" //head files
#include"FAT/FAT.h"
#include"VS1003B/VS1003B.h"
#include<avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>

	  const unsigned char single_zero_wheel[37]={0,32,15,19,4,21,2,25,17,34,6,
                                 27,13,36,11,30,8,23,10,5,24,
                                 16,33,1,20,14,31,9,22,18,29,
                                 7,28,12,35,3,26};

const unsigned char single_zero_order[37]={0,23,6,35,4,19,10,31,16,27,18,
                        14,33,12,25,2,21,8,29,3,24,5,
                        28,17,20,7,36,11,32,30,15,26,
                        1,22,9,34,13};



#define uint8 unsigned char
#define uint16 unsigned int
#define uint32 unsigned long


#define PATH (unsigned char *)("\\music")
//It's the path where the file placed
//Change it if you want to placed it to anoter foler
//The path should not have blanks, and each foler name's length should less than 8 with no extention

//diagnose and state indicate leds
//at start up this two led indicate error
//at normal they indicate the state of the MODE 
#define LED1_CON() DDRC|=_BV(PC5)
#define LED1_ON()  PORTC|=_BV(PC5)
#define LED1_OFF() PORTC&=~_BV(PC5)

#define LED2_CON() DDRB|=_BV(PB1)
#define LED2_ON()  PORTB|=_BV(PB1)
#define LED2_OFF() PORTB&=~_BV(PB1)

//keys
#define STOP _BV(PD2)
#define MODE _BV(PD3)
#define NEXT _BV(PD7)
#define UP   _BV(PD4)
#define DOWN _BV(PD6)
#define PREV _BV(PD5)

#define MP3 1
#define WMA 2
#define MID 3

//mode
#define REPET_ALL 0
#define REPET_ONE 1
#define RANDOM    2

extern uint16 SectorsPerClust;
extern uint16 FirstDataSector;   //struct of file information
extern uint8  FAT32_Enable;

struct FileInfoStruct FileInfo;

struct direntry MusicInfo;			//the mp3 file item whichi will be played
uint16 totalsongs;						//total songs in the root directery on the SD card
uint8 type;									//file type

uint8 track[128];			//stroe the information of songs (bit set indicate songs has been played)

void ClearTrackInfo()		//cleare the array track[128]
{
	uint8 i;
	for(i=0;i<128;i++)track[i] = 0;
}

uint8 SetTrack(uint16 songs)//set the track bit, return 1 means the song has been played
{
	uint8 byte_offset;
	uint8 bit_offset;
	songs--;
	byte_offset = songs/8;
	bit_offset = songs%8;
	if(track[byte_offset] & (1<<bit_offset))return 1;
	else
	{
		track[byte_offset] |= 1<<bit_offset;
		return 0;
	}
}

void Delay(uint16 n)//ÑÓʱ
{
	while(n--)asm("nop");
}

//Timer initialization offer seed of the srandom()
void Timer1_Initial()
{
 TCNT1H=0x00;
 TCNT1L=0x00;
 TCCR1B=0x01;//system clock;
 ICR1H=0xff;
 ICR1L=0xff;
}


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);
      }
   }
}






void PlayMusic()//²¥·ÅÒôÀÖº¯Êý£¬Ò»µ©Ö´Ðв»»áÍ˳ö
{
	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
	uint16 songs=1;			//ĬÈϷŵÚÒ»Ê׸è	//play the fist songs by default
	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);
					 }
				 }
				
				 else if((!(PIND&MODE)) && keylen==0) //ģʽ			//mode key
	 			 {
					Delay(100);		
					if(!(PIND&MODE))
					{
						keylen=0xffff;
						if(mode==REPET_ALL)
						{
							mode=REPET_ONE;
							LED1_ON();
							LED2_OFF();
						}
						else if(mode==REPET_ONE)//next mode is shuffle
						{
							mode=RANDOM;
							LED1_OFF();
							LED2_ON();
							srandom(((uint32)TCNT1)<<16);//²úÉúËæ»úÊýµÄÖÖ×Ó
						}
						else 
						{
							mode=REPET_ALL;
							LED1_OFF();
							LED2_OFF();
						}
					}
	  			 }
			}
			sector++;
			free(buffer);
		}
		i=0;
		p=FAT_NextCluster(p);//¶ÁÏÂÒ»´ØÊý¾Ý			//read next cluster
		if(p == 0x0fffffff || p == 0x0ffffff8 || (FAT32_Enable == 0 && p == 0xffff))//Èç¹ûÎÞºóÐø´ØÔò½áÊø£¬	//no more cluster
		{
				if(mode==REPET_ALL)songs++;
				if(songs>totalsongs)songs=1;
				goto next;
				
		}
	}
}






//-----------------------------------------
        //All to do with ADC......................
        //-----------------------------------------
void analog_init(void){
   ADCSRA|=(6<<ADPS0)|(1<<ADEN);
}
unsigned int analog10(unsigned char channel)
{

ADMUX &= ~0x1F;

ADMUX |= channel;

ADCSRA |= (1 << ADSC);

while (bit_is_set(ADCSRA, ADSC));


return (ADCL | ADCH << 4); // read ADC (full 10 bits);
}

unsigned int analog8(unsigned char channel)
{
return(analog10(channel) >> 2);
}
		//-----------------------------------------
        //Finish - All to do with ADC......................
        //-----------------------------------------

      	unsigned int time_ms1 = 0;
   		unsigned int time_ms2 = 0;
		unsigned int wheel_ms1 = 0;
		unsigned int wheel_ms2 = 0;
     	volatile unsigned int timer_tick;      		//global timer variable MUST BE DECLARED VOLATILE
		volatile unsigned int timer_tick_wheel;     //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();


	// Initialize the ADC:
      analog_init();

	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



DDRC &= ~(1 << PD6);
   PORTD|=(1<<PD6); 
   
   DDRC &= ~(1 << PD7);
   PORTD|=(1<<PD7);

   DDRC &= ~(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_wheel=0;            //start timer, clear global millisecond counter
   TCNT0 = ~(124);
      
   while(1){
     

					//ball speed first
   					time_ms1 = 0; 
   					time_ms2 = 0;

   
   					 while (PIND&(1<<PD7));  // 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

   					 while (!(PIND&(1<<PD7)));  // loop here until button is released
   					 _delay_ms(10);  // delay for 10 ms to debounce button release
   
   
   					 while (PIND&(1<<PD7));  // 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<<PD7));  // 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(1000);  // delay for 10 ms to debounce button release
//  lcd_clear();
   }	
   					//wheel speed second
  					 {
  					 wheel_ms1 = 0; 
  					 wheel_ms1 = 0;

   
  					  while (PIND&(1<<PD7));  // loop here until first button press
   					 TCNT0 = 0;
  					  timer_tick_wheel=0;// here is where we start timing
  					  _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<<PD7));  // loop here until second button press
   					 wheel_ms1 = timer_tick_wheel;      //get the value of the millisecond counter
    				 timer_tick_wheel=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<<PD7));  // loop here until 3rd button press
   					 wheel_ms2 = timer_tick_wheel;      //get the value of the millisecond counter
   
//    lcd_line1();// display the times
  //  lcd_time(wheel_ms1);
    //lcd_line2();
    //lcd_time(wheel_ms2);

					 _delay_ms(1000); //for button debounce 
   //lcd_clear();
	

   

PlayMusic();	
	while(1)
	{
		while(1)
		{
			LED1_ON();LED2_OFF();
			Delay(0xffff);
			LED2_ON();LED1_OFF();
			Delay(0xffff);
		}
	}
	return 0;
}

}


/*
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_wheel++;
   TCNT0 = ~(125);         //preload counter to overflow after 1 ms (125 counts)
}


but that led does come on again with this program…

I know what you mean about it driving you batty. I remember somewhere hearing or reading an interview about very early computer programming, and how everyone immediately came to the unexpected realization that they were going to spend 99% of their time debugging rather than writing code. The same goes for hardware!

Actually, looking at your code, no LEDs flashing could be a very good thing, it would mean that all the startup tests were passed. With no buttons to press though, it looks like your code would get stuck in this block:

	DDRC &= ~(1 << PD6);
	PORTD|=(1<<PD6);

	DDRC &= ~(1 << PD7);
	PORTD|=(1<<PD7);

	DDRC &= ~(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_wheel=0;            //start timer, clear global millisecond counter
	TCNT0 = ~(124);
     
	while(1){
		if (!(PIND&(1<<PD7))){
			LED2_ON();
			Delay(0xffff);
			LED2_OFF();
			Delay(0xffff);
			_delay_ms(10);//delay for 10 ms to debounce the press
			time();
			break;
		}else if(!(PIND&(1<<PD6))){
			LED2_ON();
			Delay(0xffff);
			LED2_OFF();
			Delay(0xffff);
			_delay_ms(10);//delay for 10 ms to debounce the press
			roulette_pot();//run a different function
			break;
		}else if(!(PIND&(1<<PD5))){
			LED2_ON();
			Delay(0xffff);
			LED2_OFF();
			Delay(0xffff);
			_delay_ms(10);//delay for 10 ms to debounce the press
			wheel();//run function to take number ball started on.
			break;
		}else if(!(PIND&(1<<PC2))){
			LED2_ON();
			Delay(0xffff);
			LED2_OFF();
			Delay(0xffff);
			_delay_ms(10);//delay for 10 ms to debounce the press
			break;
		}
	}

You can get rid of your six lines of DDR and PORT statements, they’re a little mixed up with a couple of C’s that I think should be D’s, but anyway the PortD configuration is already taken care of by these two lines of code above:

   DDRD &= 0x03;
   PORTD |= 0xfc;

Other than that you either need buttons to push, or to take whatever function you want to run automatically (PlayMusic(), roulette_pot(), etc…) and call it outside of these if(button pressed) statements.

Any luck?

-Adam

I will work on a routine then in the code, and pass it by you to see whether the code is at fault. after that, it must be hardware…

On one of the two programs i posted though, the dreq led comes on (not flashing) and the other it doesnt.

i dont remember turning that off at any point, so i was worried that it wasnt coming on…

I changed those C’s to D’s, and added a button where the schematic has one called “play”. I am going to try and just get the mp3 player working before i put in any of my own functions. so i wont change any code from the original i got from avr-freaks. And then see what happens…

I tried to run a debug in avrstudio on the original code i got from avr-freaks (below), and a yellow arrow appears at the line with the bracket

//main function
int main()
{

What does that yellow arrow mean - does it mean it is getting stuck there or anything like that, because i am getting nothing through the heaphones in terms of music.

the code i am running is:

/*******************************************************************/
/*          Mega8 MP3 Player (mega8 + VS1003 + SD)  V1.8           */
/*                                                                 */
/* Discription: Can play mp3/wma/wav/mid SF0 files in the root     */
/*              directery on the SD card which file system is FAT16*/
/*              or FAT32.                                          */
/*              It has six keys to control this player             */
/*              MODE: switch between repeat all (default),repeat 1 */
/*                    and shuffle                                  */
/*              PLAY/PAUSE:                                        */
/*              PREV: previous songs                               */
/*              NEXT: next songs                                   */
/*              UP  : volume up                                    */
/*              DOWN: volume down                                  */
/* Platform   : AVRStudio4.13 b528 + WinAVR20070525                */
/*              optimize -0s                                       */
/* Author     : bozai(Zhang Qibo)                                  */
/* E-mail     : sudazqb@163.com                                    */
/* MSN        : zhangqibo_1985@hotmail.com                         */
/* Date       : 2007-06-16                                         */
/*******************************************************************/
/*  V1.8                                                           */
/*  2007-06-16                                                     */
/*  Still the bug of FAT, it's not resolved totally                */
/*                                                                 */
/*  V1.7                                                           */
/*  2007-05-21                                                     */
/*  Fix a serious bug of FAT                                       */
/*  Set the default mode to "Shuffle"                              */
/*  Because the mode button is inside the box                      */
/*  So I don't want to open the box when I turn on it              */
/*  If the button is easy to press of your mp3 player              */
/*  just change the macro below to:                                */
/*            0 = repet all    1 = repet one  2 = shuffle          */

#define DEFAULT_MODE   0

/*                                                                 */
/*  V1.6                                                           */
/*  2007-05-09                                                     */
/*  And a track function to make the shuffle a real shuffle        */
/*  Due to the limted RAM space of mega8. Maximum no repetition    */
/*  songs is 1024. And songs more than that will use randomly play */
/*  Special thanks to Ronald Ausloos (Belgium)'s advice            */ 
/*                                                                 */
/*  V1.5                                                           */
/*  2007-05-03                                                     */
/*  Change the behavior of the LEDs:                               */
/*                      VS1003 faild: LED1 blink                   */
/*                      SD faild    : LED2 blink                   */
/*                      FAT faild   : LED1 & LED2 blink alternatly */
/*                      File not found: Both LED blink             */
/*  Add read capacity function, fix the bug of FAT init            */
/*  Add slow start up code of VS1003                               */ 
/*                                                                 */                                                              
/*  V1.4                                                           */
/*  2007-05-02                                                     */
/*  Add enough delay when operate VS1003                           */ 
/*                                                                 */                                                              
/*  V1.3                                                           */
/*  2007-04-21                                                     */
/*  Modify the retry time of sd reset                              */
/*  Enable some code incase that when FAT initialize               */
/*  faild program can't jump out the loop                          */
/*  so the error LED can't light up                                */
/*  And light up both leds when FAT_Ini fails                      */
/*                                                                 */
/*  V1.2:                                                          */
/*  2007-04-04:                                                    */
/*  Add a macro of path                                            */
/*                                                                 */
/*  V1.1:                                                          */
/*  2007-02-25                                                     */
/*  Change the directory to C:\music\                              */
/*  A bug fixed                                                    */
/*  Modify some code to adjust new GCC compiler                    */
/*                                                                 */
/*  V1.0:                                                          */
/*  2006-12-03:                                                    */
/*  Original Version                                               */
/*******************************************************************/

#include<avr/io.h>
#include"MMC_SD/MMC_SD.h" //head files
#include"FAT/FAT.h"
#include"VS1003B/VS1003B.h"
#include<avr/pgmspace.h>

#define uint8 unsigned char
#define uint16 unsigned int
#define uint32 unsigned long


#define PATH (unsigned char *)("\\music")
//It's the path where the file placed
//Change it if you want to placed it to anoter foler
//The path should not have blanks, and each foler name's length should less than 8 with no extention

//diagnose and state indicate leds
//at start up this two led indicate error
//at normal they indicate the state of the MODE 
#define LED1_CON() DDRC|=_BV(PC5)
#define LED1_ON()  PORTC|=_BV(PC5)
#define LED1_OFF() PORTC&=~_BV(PC5)

#define LED2_CON() DDRB|=_BV(PB1)
#define LED2_ON()  PORTB|=_BV(PB1)
#define LED2_OFF() PORTB&=~_BV(PB1)

//keys
#define STOP _BV(PD2)
#define MODE _BV(PD3)
#define NEXT _BV(PD7)
#define UP   _BV(PD4)
#define DOWN _BV(PD6)
#define PREV _BV(PD5)

#define MP3 1
#define WMA 2
#define MID 3

//mode
#define REPET_ALL 0
#define REPET_ONE 1
#define RANDOM    2

extern uint16 SectorsPerClust;//ÿ´ØÉÈÇøÊý
extern uint16 FirstDataSector;//µÚÒ»¸öÊý¾ÝÉÈÇøÊý   //struct of file information
extern uint8  FAT32_Enable;

struct FileInfoStruct FileInfo;//ÎļþÐÅÏ¢

struct direntry MusicInfo;	//Òª²¥·ÅµÄmp3ÎļþÐÅÏ¢		//the mp3 file item whichi will be played
uint16 totalsongs;			//×ܵÄÒôÀÖÎļþÊýÄ¿			//total songs in the root directery on the SD card
uint8 type;					//ÎļþÀàÐÍ					//file type

uint8 track[128];			//stroe the information of songs (bit set indicate songs has been played)

void ClearTrackInfo()		//cleare the array track[128]
{
	uint8 i;
	for(i=0;i<128;i++)track[i] = 0;
}

uint8 SetTrack(uint16 songs)//set the track bit, return 1 means the song has been played
{
	uint8 byte_offset;
	uint8 bit_offset;
	songs--;
	byte_offset = songs/8;
	bit_offset = songs%8;
	if(track[byte_offset] & (1<<bit_offset))return 1;
	else
	{
		track[byte_offset] |= 1<<bit_offset;
		return 0;
	}
}

void Delay(uint16 n)//ÑÓʱ
{
	while(n--)asm("nop");
}

//Timer initialization offer seed of the srandom()
void Timer1_Initial()
{
 TCNT1H=0x00;
 TCNT1L=0x00;
 TCCR1B=0x01;//system clock;
 ICR1H=0xff;
 ICR1L=0xff;
}

void PlayMusic()//²¥·ÅÒôÀÖº¯Êý£¬Ò»µ©Ö´Ðв»»áÍ˳ö
{
	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
	uint16 songs=1;			//ĬÈϷŵÚÒ»Ê׸è	//play the fist songs by default
	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);
					 }
				 }
				else if(!(PIND&DOWN) && keylen==0)  //ÒôÁ¿-			//Volume down
	 			{
				    Delay(100);
	   			 	if(!(PIND&DOWN))  
					{
					keylen=200;
					   vol=vol+((uint16)(1<<8)+1);
					   if(vol>=0xFEFE) vol=0xFEFE; 
	   			       else VS1003B_WriteCMD(0x0b,vol);
					 }
	 			 }
				 else if(!(PIND&UP) && keylen==0) //ÒôÁ¿+			//Volume up
	  			 {
					 Delay(100);
				 	 if(!(PIND&UP)) 
					 {
					 keylen=200;
					   vol=vol-((uint16)(1<<8)+1);
					   if(vol<=0x0101) vol=0x0101;
					   else VS1003B_WriteCMD(0x0b,vol);
	   			     }
				  }
				 else if(!(PIND&NEXT)) //ÏÂÒ»Ê×						next songs
	 			 {
					Delay(0x7fff);		
					if(!(PIND&NEXT))
					{
						Delay(0x7fff);
						if(!(PIND&NEXT))
						{
							while(!(PIND&NEXT));
							songs++;
							if(songs > totalsongs)songs=1;
							{
								free(buffer);
							//	Delay(0xffff);
								goto next;
							}
						}
					}
	  			 }
				 else if(!(PIND&PREV)) //ÉÏÒ»Ê×						previous songs
	 			 {
					Delay(0x7fff);		
					if(!(PIND&PREV))
					{
						Delay(0x7fff);
						if(!(PIND&PREV))
						{
							while(!(PIND&PREV));
							if(songs == 1)songs=totalsongs;
							else songs--;
							{
								free(buffer);
							//	Delay(0xffff);
								goto next;
							}
						}
					}
	  			 }
				 else if((!(PIND&MODE)) && keylen==0) //ģʽ			//mode key
	 			 {
					Delay(100);		
					if(!(PIND&MODE))
					{
						keylen=0xffff;
						if(mode==REPET_ALL)
						{
							mode=REPET_ONE;
							LED1_ON();
							LED2_OFF();
						}
						else if(mode==REPET_ONE)//next mode is shuffle
						{
							mode=RANDOM;
							LED1_OFF();
							LED2_ON();
							srandom(((uint32)TCNT1)<<16);//²úÉúËæ»úÊýµÄÖÖ×Ó
						}
						else 
						{
							mode=REPET_ALL;
							LED1_OFF();
							LED2_OFF();
						}
					}
	  			 }
			}
			sector++;
			free(buffer);
		}
		i=0;
		p=FAT_NextCluster(p);//¶ÁÏÂÒ»´ØÊý¾Ý			//read next cluster
		if(p == 0x0fffffff || p == 0x0ffffff8 || (FAT32_Enable == 0 && p == 0xffff))//Èç¹ûÎÞºóÐø´ØÔò½áÊø£¬	//no more cluster
		{
				if(mode==REPET_ALL)songs++;
				if(songs>totalsongs)songs=1;
				goto next;
		}
	}
}

//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("\\new\\mp3",&MusicInfo,&totalsongs,&type);
	//´ú±íÔÚ¸ùĿ¼ÏµÄnewÎļþ¼ÐϵÄmp3ÎļþϵÄÒôÀÖÎļþ

	//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

	PlayMusic();//²¥·Å¸èÇú		//play songs
	while(1)
	{
		while(1)
		{
			LED1_ON();LED2_OFF();
			Delay(0xffff);
			LED2_ON();LED1_OFF();
			Delay(0xffff);
		}
	}
	return 0;
}

I remember when the mp3 player was working on the breadboard, the led on the “dreq” pin used to be alight, and then flash whilst it was playing music - probably as data was passing through dreq. is it being told somewhere to do this, because running the above code - no leds are on.

P.S How important is the GBUF line. I only have two ontacts on the .5mm jack, so i have left and GBUF connected, not right. With those two connected there is a constant click, click, clicking. - no tunes! and my play/pause button doesnt affect it at all,

also the vs1002 chip gets much warmer than the mega168 chip - could i have damaged it when i had the CS of the SD and CS of the Vs1002 connected together - it did blow the SD card.

Will the following turn those pins on when the button is pressed?

#include <avr/io.h>
int main( void )
        {
DDRD |= (1<< PD2);
DDRB |= (1<< PB1);
while(1)
{
		while(PIND&(1<<PD2))
		{
		}

		PORTB |= (1<< PB1);
}
   return 1;

        }

hi, i have been away for a week, but now back.

I have replaced the vs1002 breakout board with a new one, and its not getting hot anymore. that seems to be issue 1 solved.

i have a bread board which i use to program the microcontrollers. one capacitor fell out and the light went orange and flashed on the programmer.

in my circuit i have not included 1 capacitor on pin 1 going to ground. is this enough to stop my circuit working. I mean, all the vcc’s are connected, and if another one has a capacitor (say the sd card vcc) then does that not do the job for the pin 1 vcc of the microcontroller???

the capcitors at the voltage regulator get quite hot, and so does the actual voltage regulator. nothing i cant hold but i can definateyl feel it…
also all my leds now have 412k resitors each, but there ground line has a 530k resistor as well (which is parralel and they are all using it. is that too much for a light to come on?

i am getting no signal except a buzzing in the headphones that the circuit even exists. have you any ideas?

EDIT: I think the problem could be in the power supply, or around there. possibly a short. I changed the voltage reg, to a bigger 1 - 800mA, and it is getting very hot. This is either because i twisted the legs for a different configuration and they’re touching or a short somwhere. I will probably build one on a breadboard and wire it into the circuit - that way i can monitor that the legs arent touching…

The point of having so many of these little 0.1uF capacitors is to put them very near pins that are susceptible to interference from electronic noise. Everything will probably work fine without them, but then again everything will definitely work fine with them (assuming nothing else is wrong), so it’s your choice.

Wait, do you actually have 412k and 530k resistors on your LEDs (as in 412,000 Ohms)? If so, that is WAY too much, and you won’t see any light at all. If you meant to say Ohms insted of Kilo-Ohms, then you should still be able to see the LEDs light up dimly. They still shouldn’t be arranged in parallel like that though. If you have 412 ohm resistors handy you should have your I/O pin connected to a single resistor connected to one LED’s anode, and that LED’s cathode connected straight to ground. If it doesn’t lite up with this arrangement you know you’ve got a problem somewhere.

I think you’re probably right about the power supply being the source of your trouble. Your entire circuit should draw very little current, somewhere on the order of 100 mA, so a voltage regulator that large should not be getting hot at all (mildly warm is OK). You should probably solve this problem on the breadboard and check the output voltage before powering your new vs1002 breakout board any more.

Good luck!

-Adam

will confirm those resistor sizes. they are not in parralell. each led goes:

[relevant pin]----[led]—[resistor 470]----[resistor 520]

they are all independant of each other, except the resisotr 520 which they all share because it was awkward to remove, but i will take it out and they then all share the same gnd line.

am going to build the power supply on a breadboard and test it, then wire it into the circuit from the bread board.

the vs1002 board runs off 9 volts anyway.

will try all that

That arrangement should light up, so long as we’re talking about hundreds of Ohms, not hundreds of kiloohms!

-Adam

Right, this is my set up now, on the breadboard is the power supply, connected to the output of the 3.3volt reg is an led and the vcc and of the circuit, connected to the ground is the gnd of the circuit. connected to 9volts is the breakout board for the mp3 decoder.

if i disconnect the circuit the led comes on and is fine. the moment the circuit is connected, the led no longer lights and the regulator gets very very hot…

photos of the circuit are here:

ksd-design.co.uk/photos/photos.html

the circuit is exactly the same as in the pictures, except the power supply is now no longer on the strip board but on the bread board like in the bottom two photos.

the green and red wires joining the circuit to the breadboard, come from pins 7 and 8 of the micrcontroller, but i think you can see in the pics, that is joined also to the rest of the vcc and gnds around the circuit.

hopefully there is a solution!

As I’m sure you’ve figured out, it sounds like there is a short circuit or some similar problem on your perf-board circuit, but it’s hard to spot from the pictures.

With power and the power board disconnected what resistance do you measure between the VCC and ground nets on your perfboard circuit?

With power plugged into the mp3 chip breakout board what voltage do you measure between VCC and ground?

-Adam