⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp3init.c

📁 Frank s MP3 Player Source Files
💻 C
📖 第 1 页 / 共 4 页
字号:
            EEPROM_Write(eepromaddr++, (u08)firstfile);		
        	EEPROM_Write(eepromaddr++, (u08)(firstfile>>8));    // first filenumber in current cluster
            EEPROM_Write(eepromaddr++, (u08)cluster);
            EEPROM_Write(eepromaddr++, (u08)(cluster>>8));
            EEPROM_Write(eepromaddr++, (u08)(cluster>>16));
            EEPROM_Write(eepromaddr++, (u08)(cluster>>24));     // cluster number
            filetarget += filestep;                             // set target filenumber for next table entry
            entries--;                                          // one less table entry to do
        }
    }

    // ensure StreamFileC is idle before returning
    STREAMFILE_CMD = StreamFile_Cmd_Stop;                       // tell StreamFileC to stop, then
    while (STREAMFILE_ST!=StreamFile_St_Idle) StreamFileC();    // wait until it's idle               
    return;
}



void InitRandomVariables(void)
// This routine is called when Random mode is switched on. It determines the values for two global variables:
// FileNumber and StepSize. Random mode works by stepping through the list of files with an incremement 
// of StepSize. StepSize must be carefully, but semi-randomly, chosen, to ensure that when the stepping through of
// the files occurs, no files are skipped. The majority of this routine is dedicated to calculatiing a value of StepSize.
// At the beginning it also quickly determines a random value for FileNumber, which will be the initial file to start playing.
{
	u16		numberelements, remainder;
	
	// get the number of files (elements) in the current directory
	numberelements = (((u16)EEPROM_ReadC(StoreNumFilesH))<<8) + (u16)EEPROM_ReadC(StoreNumFilesL);
	
	// First thing is to just find a random initial value for FileNumber. Then the rest
	// of this routine is dedicated to determining a suitable value for stepsize.
	FileNumber = GetRandomNumber(numberelements);

	// For a really low number of files, set stepsize to 1 (not random at all, but who cares)
	if (numberelements < 10)
		StepSize = 1;

	// Define an initial value for StepSize (an estimate, to start testing against the criteria with)
	if (numberelements < 2*SPACING) {
		// execute here if we only have a few elements. We'll try stepsizes of 7 or below
		StepSize = 7;
	}
	else {
		// execute here for a decent number of elements
		// find an initial stepsize to start testing at
		StepSize = numberelements / SPACING;
		if (StepSize < SPACING)
			StepSize = SPACING;
	// NOTE: THERE NEEDS TO BE A RANDOM ELEMENT WHEN FINDING AN INITIAL STEPSIZE !!!
	// Once you have this initial stepsize, choose a new initial stepsize
	// as a random int between stepsize and 3*stepsize.
		StepSize += GetRandomNumber(StepSize<<1);	// add a random number in the range 1...2*stepsize
		if (StepSize >= numberelements)
			StepSize = numberelements-1;		// never permit stepsize to exceed number of files
	}

	// Then test sample stepsizes until one meets the criteria. The criteria is:
	//    That the remainder of #elements/StepSize is not zero, and that this remainder also
	//    is not exactly divisable into #elements.
	while (StepSize != 1) {
		remainder = numberelements % StepSize;
		if (remainder && (numberelements%remainder))
			return;					// if we've found a good StepSize then exit, we're done
		StepSize--;
	}
}



void MenusInit (void)
// This routine performs any initializations required for any of the menu functions.
{
    u08     temp, err;

    // Init for OutputLevel. Read eeprom for sta013 attenuation level. If eeprom value is
    // 0xff then it's been erased; write 0x00 in it's place (0xff would be maximum attenuation;
    // no music would be heard.) Tell STA013 what the attenuation level is.
    temp = eeprom_read_byte(STattenuation);
    if (temp == 0xff) {                                 // if eeprom thinks maximum attenuation...
        temp = 0;
        EEPROM_Write(STattenuation, temp);              // then change it to minimum attenuation
    }
    err = sta013_writeTWI (0x46, temp);				    /* set attenuation for the STA013 left output channel */
	if (err != TWI_write_OK) StopOnError(err + 60);	    /* report error if it occurs */
	Delayms(1);					                        /* waste a little time */
	err = sta013_writeTWI (0x48, temp);					/* set attenuation for the right output channel */
	if (err != TWI_write_OK) StopOnError(err + 70);		/* report error if it occurs */

    // Perform initializations for any other menu functions here. (None currently)
//    EEPROM_Write(STattenuation, 0);
}




//////////////////////////////////////////////////////////////////////////////////////////////
//
//  The following routines are used for testing (diagnostics) of the IDE bus. They are
//  called from the debug port menu. Note that the IDE bus MUST BE UNPLUGGED for these
//  routines to work. The main routine is called TestPortBits, as it includes the LED 
//  port bit in its test plus the unused port bits (ie more than just the IDE bus, although 
//  in reality it is mostly IDE bus pins.)
//
//////////////////////////////////////////////////////////////////////////////////////////////



void TestPortBits(void)
// This is the routine that performs the testing of the AVR ports bits. It involves the following
// ports bits in its tests:
//		Port A all bits				(mask 0xff)
//		Port B bits 4 - 7			(mask 0xf0)
//		Port C all bits				(mask 0xff)
//		Port D bits 4 - 7			(mask 0xf0)
//		Port E bit 4				(mask 0x10)
//		Port F bits 0 - 4, 6 & 7	(mask 0xdf)
//		Port G bits 0 - 4			(mask 0x1f)
// For a bit to be usable, it (a) cannot have an external pulldown of any type, and (b) be OK to
// make it an input, an output high, or output low.
// Because no test jig is used, this routine cannot detect broken traces (open circuits). It
// can only detect short-circuits between the pins listed above. It can also detect if a pin is
// shorted to GND. If a pin is shorted to VCC it may or may not detect that.
// A mask bit of '1' means the pin is tested, a mask bit of '0' means it is ignored.
{
	#define		TESTMASKA	0xff
	#define		TESTMASKB	0xf0
	#define		TESTMASKC	0xff
	#define		TESTMASKD	0xf0
	#define		TESTMASKE	0x10
	#define		TESTMASKF	0xdf	/* binary 1101 1111 */
	#define		TESTMASKG	0x1f	/* binary 0001 1111 */
	
	u08		errorc, bitpattern;
	
	uart0_putwaitPROGstr(PSTR("\r\nCommencing AVR Ports Bits Tests.\r\n"));

	// First test. Set all the bits to inputs, turn their pull-up resistors on, and then
	// read them all to ensure they all read high. If anything reads low we know we have
	// a problem.
	*AVR_DDRA  &= ~TESTMASKA;		// make port bits inputs (0=input, 1=output)
	*AVR_PORTA |= TESTMASKA;		// pull-up resistors enabled for those input pins
	*AVR_DDRB  &= ~TESTMASKB;		// make port bits inputs (0=input, 1=output)
	*AVR_PORTB |= TESTMASKB;		// pull-up resistors enabled for those input pins
	*AVR_DDRC  &= ~TESTMASKC;		// make port bits inputs (0=input, 1=output)
	*AVR_PORTC |= TESTMASKC;		// pull-up resistors enabled for those input pins
	*AVR_DDRD  &= ~TESTMASKD;		// make port bits inputs (0=input, 1=output)
	*AVR_PORTD |= TESTMASKD;		// pull-up resistors enabled for those input pins
	*AVR_DDRE  &= ~TESTMASKE;		// make port bits inputs (0=input, 1=output)
	*AVR_PORTE |= TESTMASKE;		// pull-up resistors enabled for those input pins
	*AVR_DDRF  &= ~TESTMASKF;		// make port bits inputs (0=input, 1=output)
	*AVR_PORTF |= TESTMASKF;		// pull-up resistors enabled for those input pins
	*AVR_DDRG  &= ~TESTMASKG;		// make port bits inputs (0=input, 1=output)
	*AVR_PORTG |= TESTMASKG;		// pull-up resistors enabled for those input pins
	errorc = ReadPortsExpectHigh (TESTMASKA, TESTMASKB, TESTMASKC, TESTMASKD, TESTMASKE, TESTMASKF, TESTMASKG);
	if (errorc) {
		uart0_putwaitPROGstr(PSTR("ERROR - pin tied low. "));
		PrintTestPortError(errorc);
		return;
	}


	// Next test. For port A, set each bit to a low output, one at a time, and ensure none of
	// the other port bits go low. IE, we're looking for shorts between pins.
	bitpattern = 0x01;					// start with the low bit, bit 0
	while (bitpattern != 0) {
		*AVR_DDRA = bitpattern;			// one bit output, all others input
		*AVR_PORTA = ~bitpattern;		// output bit low, input bits "high" - actually pullups enabled
		errorc = ReadPortsExpectHigh (~bitpattern, TESTMASKB, TESTMASKC, TESTMASKD, TESTMASKE, TESTMASKF, TESTMASKG);
		if (errorc) {
			uart0_putwaitPROGstr(PSTR("ERROR - Apparent pin short. Port A output value was 0x"));
			UART_PutHexWaitC(~bitpattern);
			uart0_putwaitPROGstr(PSTR(". Unexpected pin low found on "));
			PrintTestPortError(errorc);
			return;
		}
		bitpattern <<= 1;				// test the next bit; next one to the left
	}
	*AVR_DDRA  &= ~TESTMASKA;			// wrapup:  make port bits inputs (0=input, 1=output)
	*AVR_PORTA |= TESTMASKA;			// wrapup:  pull-up resistors enabled for those input pins


	// Next test. For port B, set each testable bit to a low output, one at a time, and ensure none of
	// the other port bits go low. IE, we're looking for shorts between pins.
	bitpattern = 0x10;					// start with bit 4, as bits 0-3 are not testable
	while (bitpattern != 0) {
		*AVR_DDRB &= ~TESTMASKB;		// all portB testable bits set to input
		*AVR_DDRB |= bitpattern;		// then the one bit of interest set to output
		*AVR_PORTB |= TESTMASKB;		// all portB testable bits set high (for inputs their pullups enabled)
		*AVR_PORTB &= ~bitpattern;		// then the one (output) bit of interest is driven low
		errorc = ReadPortsExpectHigh (TESTMASKA, (~bitpattern)&TESTMASKB, TESTMASKC, TESTMASKD, TESTMASKE, TESTMASKF, TESTMASKG);
		if (errorc) {
			uart0_putwaitPROGstr(PSTR("ERROR - Apparent pin short. Port B output value was 0x"));
			UART_PutHexWaitC(~bitpattern);
			uart0_putwaitPROGstr(PSTR(". Unexpected pin low found on "));
			PrintTestPortError(errorc);
			return;
		}
		bitpattern <<= 1;				// test the next bit; next one to the left
	}
	*AVR_DDRB  &= ~TESTMASKB;			// wrapup:  make testable port bits all inputs (0=input, 1=output)
	*AVR_PORTB |= TESTMASKB;			// wrapup:  pull-up resistors enabled for those input pins



	// Next test. For port C, set each bit to a low output, one at a time, and ensure none of
	// the other port bits go low. IE, we're looking for shorts between pins.
	bitpattern = 0x01;					// start with the low bit, bit 0
	while (bitpattern != 0) {
		*AVR_DDRC = bitpattern;			// one bit output, all others input
		*AVR_PORTC = ~bitpattern;		// output bit low, input bits "high" - actually pullups enabled
		errorc = ReadPortsExpectHigh (TESTMASKA, TESTMASKB, ~bitpattern, TESTMASKD, TESTMASKE, TESTMASKF, TESTMASKG);
		if (errorc) {
			uart0_putwaitPROGstr(PSTR("ERROR - Apparent pin short. Port C output value was 0x"));
			UART_PutHexWaitC(~bitpattern);
			uart0_putwaitPROGstr(PSTR(". Unexpected pin low found on "));
			PrintTestPortError(errorc);
			return;
		}
		bitpattern <<= 1;				// test the next bit; next one to the left
	}
	*AVR_DDRC  &= ~TESTMASKC;			// wrapup:  make port bits inputs (0=input, 1=output)
	*AVR_PORTC |= TESTMASKC;			// wrapup:  pull-up resistors enabled for those input pins


	// Next test. For port D, set each testable bit to a low output, one at a time, and ensure none of
	// the other port bits go low. IE, we're looking for shorts between pins.
	bitpattern = 0x10;					// start with bit 4, as bits 0-3 are not testable
	while (bitpattern != 0) {
		*AVR_DDRD &= ~TESTMASKD;		// all portD testable bits set to input
		*AVR_DDRD |= bitpattern;		// then the one bit of interest set to output
		*AVR_PORTD |= TESTMASKD;		// all portD testable bits set high (for inputs their pullups enabled)
		*AVR_PORTD &= ~bitpattern;		// then the one (output) bit of interest is driven low
		errorc = ReadPortsExpectHigh (TESTMASKA, TESTMASKB, TESTMASKC, (~bitpattern)&TESTMASKD, TESTMASKE, TESTMASKF, TESTMASKG);
		if (errorc) {
			uart0_putwaitPROGstr(PSTR("ERROR - Apparent pin short. Port D output value was 0x"));
			UART_PutHexWaitC(~bitpattern);
			uart0_putwaitPROGstr(PSTR(". Unexpected pin low found on "));
			PrintTestPortError(errorc);
			return;
		}
		bitpattern <<= 1;				// test the next bit; next one to the left
	}
	*AVR_DDRD  &= ~TESTMASKD;			// wrapup:  make testable port bits all inputs (0=input, 1=output)
	*AVR_PORTD |= TESTMASKD;			// wrapup:  pull-up resistors enabled for those input pins


	// Next test. For port E, set each testable bit to a low output, one at a time, and ensure none of
	// the other port bits go low. IE, we're looking for shorts between pins.
	bitpattern = 0x10;					// start with bit 4, as bits 0-3 are not testable
	while (bitpattern == 0x10) {		// only test this one bit (bit 4)!
		*AVR_DDRE &= ~TESTMASKE;		// all portE testable bits set to input
		*AVR_DDRE |= bitpattern;		// then the one bit of interest set to output
		*AVR_PORTE |= TESTMASKE;		// all portE testable bits set high (for inputs their pullups enabled)
		*AVR_PORTE &= ~bitpattern;		// then the one (output) bit of interest is driven low
		errorc = ReadPortsExpectHigh (TESTMASKA, TESTMASKB, TESTMASKC, TESTMASKD, (~bitpattern)&TESTMASKE, TESTMASKF, TESTMASKG);
		if (errorc) {
			uart0_putwaitPROGstr(PSTR("ERROR - Apparent pin short. Port E output value was 0x"));
			UART_PutHexWaitC(~bitpattern);
			uart0_putwaitPROGstr(PSTR(". Unexpected pin low found on "));
			PrintTestPortError(errorc);
			return;
		}
		bitpattern <<= 1;				// test the next bit; next one to the left
	}
	*AVR_DDRE  &= ~TESTMASKE;			// wrapup:  make testable port bits all inputs (0=input, 1=output)
	*AVR_PORTE |= TESTMASKE;			// wrapup:  pull-up resistors enabled for those input pins


	// Next test. For port F, set each bit to a low output, one at a time, and ensure none of
	// the other port bits go low. IE, we're looking for shorts between pins. Note the "special case" of
	// Port F bit 5. This pin drives the LCD backlight control, and is effectively pulled low. This means
	// we have to mask it out, in TESTMASKF, because when it's read it reads low. However there's no problem
	// driving it high or low (no damage will occur) so we do. So this routine drives each of the 8 bits
	// of PortF low, but when they're read bit 5 is ignored (ignored by masking it out in TESTMASKF).
	bitpattern = 0x01;					// start with the low bit, bit 0
	while (bitpattern != 0) {
		*AVR_DDRF = bitpattern;			// one bit output, all others input
		*AVR_PORTF = ~bitpattern;		// output bit low, input bits "high" - actually pullups enabled
		errorc = ReadPortsExpectHigh (TESTMASKA, TESTMASKB, TESTMASKC, TESTMASKD, TESTMASKE, (~bitpattern)&TESTMASKF, TESTMASKG);
		if (errorc) {
			uart0_putwaitPROGstr(PSTR("ERROR - Apparent pin short. Port F output value was 0x"));
			UART_PutHexWaitC(~bitpattern);
			uart0_putwaitPROGstr(PSTR(". Unexpected pin low found on "));
			PrintTestPortError(errorc);
			return;
		}
		bitpattern <<= 1;				// test the next bit; next one to the left
	}
	*AVR_DDRF  &= ~TESTMASKF;			// wrapup:  make port bits inputs (0=input, 1=output)
	*AVR_PORTF |= TESTMASKF;			// wrapup:  pull-up resistors enabled for those input pins


	// Next test. For port G, set each testable bit to a low output, one at a time, and ensure none of
	// the other port bits go low. IE, we're looking for shorts between pins.
	bitpattern = 0x10;					// start with bit 4, as bits 5-7 are not testable
	while (bitpattern != 0) {
		*AVR_DDRG &= ~TESTMASKG;		// all portG testable bits set to input

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -