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

📄 main.c

📁 WAV 数字输出播放器
💻 C
字号:
// main routine for Ultimate Source

#include <string.h>
#include <stdio.h>
#include "lcd.h"	//LCD character output routines
#include "delay.h"	//utility delay routines
#include "button.h"	//button initialize, read
#include "CPU.h"	//Oscilator
#include "Timer.h"	//Timer initialize and interrupt
#include "SPI_SD.h"	//SD_mmc routines
#include "Player.h"	//Player Module routines

//Current operation mode
#define MODE_STOP 1
#define MODE_PAUSE 2
#define MODE_PLAY 3

//Timer routine interrupt flag: not used now
BYTE Timer_Flag;
//DMA Page sent interrupt flag.
BYTE DMA_Flag_A;

BYTE PlayMode;

//Total musics in SD card
int maxMusics;

char musicname[10];
char PlayMinSec[10];

//Initialize IO
BYTE IoInit(void)
{
	BYTE ret;
	Delay_Us(500);
	init_cpu();					//init Clock system
	Delay_Us(1);
	init_lcd();					//init LCD
	Delay_Us(1);
	init_button();				//button
	Delay_Us(1);

	lcd_str("KDA-US01", 1);		//banner
	Delay_Us(10);
	lcd_str("STARTUP", 2);

	Delay_Us(15000);
	
	initI2sBuff();				//clear buffer to 0
	dma0_Init();				//init DMA
	i2s_Init();					//init DCI/I2S
	Delay_Us(10);

	init_SPI();					//SPI initialize
	Delay_Us(10);
	Timer_Init();				//timer interrupt
	Delay_Us(10);
	
	ret = SD_InitializeCard();		//card initialize
	if ( ret != 0) return 1;
	
	maxMusics = Player_Init();		//search wav files
	if (maxMusics == 0) return 2;
	
	PlayMode = MODE_STOP;
	Player_GotoMusicHead(1);
	
	dma0_start();				//run DMA
	return 0;
}

void PrintInfo(void)
{
	DWORD FileSize;
	memset(musicname, 0, sizeof(musicname));
	memset(PlayMinSec, 0, sizeof(PlayMinSec));
	//Get File name
	Player_NusicName(musicname);
	//Print File Name at row 1
	lcd_str(musicname, 1);
	Delay_Us(200);	
	//Print 00:00 at row 2
	Player_TotalMinSec(PlayMinSec);
	lcd_str(PlayMinSec, 2);
}	

void PrintTime(void)
{
	//Print 00:00 at row 2
	Player_MinSec(PlayMinSec);
	lcd_str(PlayMinSec, 2);
}	

int main ( void )
{
	BYTE ret;
	char lcdbuffer[10];
	BYTE BtnState;
	BYTE BtnStatePrev;
	BYTE ContinueMode; //1: play continue, 0:music end

	ULONG ViewCounter;
	BYTE MusicNum;

	ViewCounter = 0;
	MusicNum = 1;
	BtnState = 0x00;
	BtnStatePrev = 0x00;
	Timer_Flag = 0;
	DMA_Flag_A = 0;
	ContinueMode = 1;
	
	ret = IoInit();
	if (ret == 1)
	{
		lcd_str("NO CARD", 2);
		while(1);
	}
	if (ret == 2)
	{
		lcd_str("NO MUSIC", 2);
		while(1);
	}
	
	PrintInfo();
	
	while(1)
	{
		Delay8Tcy();
		Delay8Tcy();
		
		if ((ContinueMode == 0) && (PlayMode == MODE_PLAY))
		{
			//Get Music num
			MusicNum = Player_MusicNum();
			//Player played till end of music.
			Player_Pause();
			PlayMode = MODE_PAUSE;

			Delay_Us(200);
			
			if (MusicNum < maxMusics)
			{
				ContinueMode = 1;
				Player_FF();
				PlayMode = MODE_PLAY;
				PrintInfo();
				Delay_Us(200);
				ContinueMode = Player_Play();
			}
			else
			{
				PlayMode = MODE_STOP;
				Player_Stop();
				Delay_Us(200);
				PrintInfo();
			}
			//Do nothing if DMA happend at this process area
			DMA_Flag_A = 0;
		}

		
		//Music Stream processing.
		if (DMA_Flag_A == 1)
		{
			//DMA interrupt occured
			//Do nothing at PAUSE mode (0x0000 will be sent)
			if (PlayMode == MODE_PLAY)
			{
				ContinueMode = Player_Play();
				//Update 00:00 every 0.5sec: too slow. needs improved
				//ViewCounter += 1;
				//if (ViewCounter > 170)
				//{
				//	PrintTime();
				//	ViewCounter = 0;
				//}
			}
			DMA_Flag_A = 0;
		}			

		//Timer routine, _T1Interrupt
		if (Timer_Flag)
		{
			//0.25ms Timer Expired,
			//This is Test Pin, for Timer
			PORTFbits.RF3 = 0;
			
			Timer_Flag = 0;
		}

		//Button and State control .. Human interface
		BtnState = Read_Command();
		if (BtnState != BtnStatePrev)
		{
			switch(BtnState)
			{
				case BTN_STOP:
					switch (PlayMode)
					{
						case MODE_STOP:
							break;
						case MODE_PLAY:
							PlayMode = MODE_STOP;
							Player_Init();
							Player_Stop();
							PrintInfo();
						case MODE_PAUSE:
							PlayMode = MODE_STOP;
							Player_Init();
							Player_Stop();
							PrintInfo();
							break;
					}
					break;
				case BTN_PAUSE:
					switch (PlayMode)
					{
						case MODE_STOP:
							PlayMode = MODE_PAUSE;
							lcd_str("PAUSE", 2);
							Player_Pause();
							Player_Start();
							break;
						case MODE_PLAY:
							PlayMode = MODE_PAUSE;
							Player_Pause();
							lcd_str("PAUSE", 2);
							break;
						case MODE_PAUSE:
							break;
					}
					break;
				case BTN_PLAY:
					switch (PlayMode)
					{
						case MODE_STOP:
							PlayMode = MODE_PLAY;
							PrintInfo();
							Player_Pause();
							Player_Start();
							ContinueMode = 1;
							//ContinueMode = Player_Play();
							break;
						case MODE_PLAY:
							break;
						case MODE_PAUSE:
							PlayMode = MODE_PLAY;
							PrintInfo();
							Player_Pause();
							Player_Start();
							ContinueMode = 1;
							//ContinueMode = Player_Play();
							break;
					}
					break;
				case BTN_FF:
					Player_Pause();	//clear buffer
					Player_FF();
					PrintInfo();
					break;
				case BTN_PREV:
					Player_Pause();	//clear buffer
					Player_Prev();
					PrintInfo();
					break;
				case BTN_NONE:
				default:
					break;	
			}//switch
			BtnStatePrev = BtnState;
		}//if (btnState
	}//while()
}

⌨️ 快捷键说明

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