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

📄 sound.c

📁 简单的便协式游戏机的实现
💻 C
字号:
// Sound Control for EZ-USB FX2
// programed by takuya matsubara

#include <ezusb.h>
#include <FX2regs.h>
#include "sound.h"

#define SOUNDPORT   IOA
#define SOUNDPORTOE OEA
#define SOUND_BIT    (1 << 6)	// (bit mask)SOUND 

WORD timing;
WORD timer0cnt;
char playbuff[10];
char playcnt;


#define NSEC_PARCNT 250   // (12Cycle * (1Sec/48MHz))

/*---------------------------------------timer0 interrupt*/
void timer0 (void) interrupt 1
{
	TR0 = 0;	// stop Timer0
	TL0 = LSB(timer0cnt);
	TH0 = MSB(timer0cnt);
	TR0 = 1;	// start Timer0

	SOUNDPORT ^= SOUND_BIT;

	timing--;
	if(timing ==0){
		playcnt++;

		if(playbuff[playcnt] == 0){	//end of data
			SOUNDPORT &= ~SOUND_BIT;   // sound off
			TR0 = 0;	// stop Timer0
		}else{
			timing=100;
			timer0cnt = 45500 + (((WORD)playbuff[0]-'A')*750);	//僇僂儞僞0嶼弌
		}
	}
}

void sound_set(char *p2)
{
	char *p1;

	//--------------copy playdata
	p1 = &playbuff[0];
	while(1){
		*p1 = *p2;
		if(*p1 == 0) break;
		p1++;
		p2++;
	}

	playcnt=0;

	timing=100;
	timer0cnt = 45500 + (((WORD)playbuff[0]-'A')*750);	//僇僂儞僞0嶼弌

	TL0 = LSB(timer0cnt);	// reset Timer0
	TH0 = MSB(timer0cnt);

	ET0 = 1;	// enable Timer0 interrupt
	TR0 = 1;	// start Timer0
	EA =  1;	// enable all interrupts
}


//---------------------------------initilize
void sound_init(void)
{
	SOUNDPORTOE |= SOUND_BIT;  // Output Enable
	SOUNDPORT &= ~SOUND_BIT;   // sound off

	TR0 = 0;	// stop Timer0

	CKCON &= ~(1 << 3);	// Timer Rate Control	//bit3: 0=Timer uses CLK/12

	TMOD = 0x31; 	// setup Timer0 16-bit
	//bit7: 0=Timer1 will clock only when TR1=1,regardless of the state of INT1
	//bit6: 0=Timer1 is clocked by CLK/4 or CLK/12
	//bit5-4: 11=Timer 1 stopped(Mode 3)
	//bit3: 0=Timer0 will clock only when TR0=1,regardless of the state of INT0
	//bit2: 0=Timer0 is clocked by CLK/4 or CLK/12
	//bit1-0: 01=16-bit counter(Mode 1)

	PT0 = 0;	// set Timer0 interrupt to low priority
}

⌨️ 快捷键说明

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