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

📄 yampp2.c

📁 Procyon方案(手工制作的MP3播放器电路及资料)
💻 C
📖 第 1 页 / 共 2 页
字号:

    a1  = (ULONG) i2cdata[11] << 16;
    a1 += (ULONG) i2cdata[8] << 8;
    a1 += (ULONG) i2cdata[9];

    PRINT("Date: ");
    UART_Printfu32(a1);
    EOL();

    for (i=12;i<40;i+=4)
    {
    	UART_SendByte(i2cdata[i]);
    	UART_SendByte(i2cdata[i+1]);
	}
    EOL();
}

#endif





//----------------------------------------------------------------------------
// Main Lupe
//----------------------------------------------------------------------------
int main(void) 
{

 	unsigned int  i;
  	unsigned char c;
 	char *p, *q;
	unsigned int dentry = 0;
 	long filesize = 0;
	unsigned char volume = 0x2C;
	event_e event = EV_IDLE;
	unsigned char mute = 0;
	unsigned char autoplay;

 
 //------------------------------
 // Initialize 
 //------------------------------


//////////////////////////////////////////////////////////////////
// B - Port
//////////////////////////////////////////////////////////////////

	sbi(PORTB, 0);	// DIOW- hi
	sbi(DDRB, 0);	// pin PB0 is output, DIOW- 

	sbi(PORTB, 1);	// DIOR- hi
	sbi(DDRB, 1);	// pin PB1 is output, DIOR- 

	cbi(DDRB, 2);	// pin PB2 is input, DEMAND
	sbi(PORTB, 2);	// activate pullup

	// PB3 and PB4 is used for I2C Bus

	// PB5 (MOSI) is used for SPI
	// PB6 (MISO) is used for SPI
	// PB7 (SCK) is used for SPI

//////////////////////////////////////////////////////////////////
// D - Port
//////////////////////////////////////////////////////////////////

	// PD0 and PD1 are TX and RX signals

	// PD2 is used for the IR receiver
	
	// PD3 is keyboard input

	cbi(DDRD, 3);	// pin PD3 is input
	cbi(PORTD, 3);	// disable pullups


	// PD4 is available
	
	// PD5 is used by LCD module

	// PD6 and PD7 are RD and WR signals
	// but we need to set their default states used
	// when the external memory interface is disabled
    // they should then both be high to avoid disturbing
	// RAM contents
	
	sbi(DDRD, 6);		// as outputs
	sbi(DDRD, 7);
	sbi(PORTD, 6);		// and both hi
	sbi(PORTD, 7);


 //----------------------------------------------------------------------------
 // 
 //----------------------------------------------------------------------------

    lcd_init(0/*(1<<LCD_ON_CURSOR)*/, LCD_FUNCTION_DEFAULT); 	// also sets up EXTRAM pins

	lcd_puts(" yampp-2 alive");


	UART_Init();	// init RS-232 link
	i2c_init();		// initialize I2C bus pins
	init_spi();		// set up SPI interface

/* 	uncomment if you want to use IR
	init_rec80();	// set up IR receiver interface
*/

	// say hello
  	PRINT("\nyampp-2\n");


 //----------------------------------------------------------------------------
 // Let things settle
 //----------------------------------------------------------------------------                        


	// wait for drive

  	PRINT("Wait..\n"); 
  	WriteBYTE(CMD, 6, 0xA0); // Set drive/Head register, ie. select drive 0
   	while (( (c=ReadBYTE(CMD,7)) & 0xC0)!=0x40); // Wait for DRDY and NOT BUSY
  	PRINT("Drive ready!\n");

	// reset drive 
	ATA_SW_Reset();

	// read structures
	init_fat(DRIVE0);

	// setup buffer structure

	buffer1 = (unsigned char *)BUFFERSTART;	 /* S_P_C is normally 8 */
	buffer2 = (unsigned char *)BUFFERSTART + Sectors_Per_Cluster * 512 * 1;
	buffer3 = (unsigned char *)BUFFERSTART + Sectors_Per_Cluster * 512 * 2;
	buffer4 = (unsigned char *)BUFFERSTART + Sectors_Per_Cluster * 512 * 3;

	MAS_init();		// init MAS decoder
	DAC_init();		// init D/A converter

#ifdef MAS_INFO
	mas_info();     // print some MAS chip info
#endif


	// load and set default volume
	volume = eeprom_rb(EEPROM_VOLUME);
	DAC_SetVolume(volume,volume);


	// load autoplay and position settings
	autoplay = eeprom_rb(EEPROM_AUTOPLAY);
	dentry 	 = eeprom_rb(EEPROM_SONGPOS);	

	if (autoplay)
		event = EV_PLAY;

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


//setup timer1

	outp(0, TCCR1A);
	outp(5, TCCR1B);	// prescaler /1024  tPeriod = 128 uS
	outp(TI1_H, TCNT1H);
	outp(TI1_L, TCNT1L);
	sbi(TIMSK, TOIE1);	 	//enable timer1 interrupt

	sei();		// start timer

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


  	while (1) 
  	{
		
		if (UART_HasChar())
			event = get_char_event();

/* uncomment this to use IR
		if (rec80_active)
			event = ir_event();
*/
		if (event == EV_IDLE)
			event = get_key_event();

		switch (event)
		{
			default:
			case EV_IDLE: 
				break;

			case EV_PREV:
				eeprom_wb(EEPROM_AUTOPLAY,0);
				dentry-=2;	
				if ((int)dentry <= -2)
					dentry = (unsigned int)-1;
				// fall through		

			case EV_NEXT:
				eeprom_wb(EEPROM_AUTOPLAY,1);
				dentry++;
				isPlaying = PS_STOPPED;
				// fall through

			case EV_PLAY:
				
				switch (isPlaying)
				{
					case PS_PAUSED :
						isPlaying = PS_PLAYING;
						break;
					case PS_PLAYING :
						isPlaying = PS_PAUSED;
						break;
					case PS_STOPPED :												

						eeprom_wb(EEPROM_SONGPOS,dentry);
					
						// setup for play
						if ( (mycluster = get_dir_entry(dentry,0)) == 0)	// no more
						{	
							event = EV_NEXT;
							dentry = -1;
							isPlaying = PS_STOPPED;
							break;
						}	
	
						filesize = get_filesize();
		
						// start playing
						PRINT("\r\nPlaying #"); UART_Printfu08(dentry); UART_SendByte(' ');
						UART_Puts( get_dirname() ); 
						UART_Putsln( (p = get_filename()) );
						q = scroll_line;
						scroll_pos = 0;
						scroll_length = 0;
						while ( (*q = *p) != 0)
						{	
							scroll_length++;
							q++;
							p++;
						}
		
						*((unsigned long*)q)++ = 0x20202020L;
						*q++ = 0;
		
		
					    // preload buffers
					    cli();
						load_sectors(mycluster,buffer1);
						mycluster = next_cluster(mycluster);
						load_sectors(mycluster,buffer2);
						mycluster = next_cluster(mycluster);
						load_sectors(mycluster,buffer3);
						mycluster = next_cluster(mycluster);
						sei();
						
					    outptr = buffer1;
						updbuf = 0;
						strcpy(time,"000:00");
						time_now = 0;
						isPlaying = PS_PLAYING;
						break;
				}
				event = EV_IDLE;
				break;					

			case EV_STOP:
				isPlaying = PS_STOPPED;
				event = EV_IDLE;
				break;				

			case EV_VOLUP: 
				if (volume < 0x38) volume++; 
				DAC_SetVolume(volume,volume); 
				eeprom_wb(EEPROM_VOLUME,volume);
				event = EV_IDLE;
				break;

			case EV_VOLDN: 
				if (volume > 0) volume--; 
				DAC_SetVolume(volume,volume); 
				eeprom_wb(EEPROM_VOLUME,volume);
				event = EV_IDLE;
				break;

			case EV_NEUTRAL: 
				volume = 0x2C;
				DAC_SetVolume(volume,volume); 
				eeprom_wb(EEPROM_VOLUME,volume);
				event = EV_IDLE;
				break;

			case EV_MUTE:
				mute = 1-mute;
				write_register(0xaa,(unsigned long)mute);				
				event = EV_IDLE;
				break;
		}
	
	
		// time to update a buffer ?
		if (updbuf)
		{
			if (mycluster)
			{
				cli();
				load_sectors(mycluster,updbuf);			// load a clusterfull of data
				mycluster = next_cluster(mycluster);	// follow FAT chain
				sei();
			}
			updbuf = (unsigned char *)0;			// mark as done
			scroll_flag++;
		}

		
		// 
		if (isPlaying == PS_PLAYING)
		{
			while ( bit_is_set(PINB, PB2) )			// while DEMAND is high 
			{
				//send data on SPI bus
				outp(*outptr++, SPDR);

				// buffers are updated approx every 250 mS  ( at 128 kBs datastream )

				// check for buffer wrap
				if (outptr == buffer2)
					updbuf = buffer1;
				else if (outptr == buffer3)
					updbuf = buffer2;
				else if (outptr == buffer4)
				{
					updbuf = buffer3;
					outptr = buffer1;
				}

				// check if we're done
				if (--filesize <= 0)
				{
					isPlaying = PS_STOPPED;				// the end is near 
					// try autoplay next song
					event = EV_NEXT;
					break;
				}

				// wait for data to be sent
				loop_until_bit_is_set(SPSR, SPIF);	
			}
			
			// scroll display on every buffer updates (approx 250 ms)
			if (scroll_flag == 1)
			{
				lcd_gotoxy(0,0);
				for (i=0;i<16;i++)
					lcd_putchar( scroll_line[ (scroll_pos+i) % (scroll_length+4) ] );
				scroll_pos++;
				scroll_flag = 0;
			}    
			
			// print time
			if (time_flag)
			{
				lcd_gotoxy(5,1);

				i = time_now/2;
	
				time[0] = '0' + (i / 6000);
				i %= 6000;
				time[1] = '0' + (i / 600);
				i %= 600;
				time[2] = '0' + (i / 60);
				i %= 60;
				time[4] = '0' + (i / 10);
				i %= 10;
				time[5] = '0' + i;
				
				lcd_puts(time);
				time_flag = 0;
			}
		}
		else if (isPlaying == PS_PAUSED)
		{
			if (time_flag == 1)
			{
				lcd_gotoxy(5,1);				
				lcd_puts("Paused");
			}
			else if (time_flag == 2)
			{
				lcd_gotoxy(5,1);				
				lcd_puts("      ");
				time_flag = 0;
			}
			
		}
  	}    
}



⌨️ 快捷键说明

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