www.pudn.com > PT2258-volume-controller.zip > PT2258调音板程序.txt, change:2011-08-12,size:4317b


#include<reg52.h> 
#define uchar unsigned char 
#define uinP0 unsigned int 
sbit sda=P2^3; 
sbit scl=P2^4; 
sbit dula=P2^6; 
sbit wela=P2^7; 
sbit key1=P2^0; 
sbit key2=P2^1; 
sbit key3=P2^2; 
uchar code table[]={ 
0x7e,0x81,0x81,0x7e,//0,0-3 
0x00,0x41,0xff,0x01,//1,4-7 
0x63,0x85,0x89,0x71,//2,8-11 
0x42,0x91,0x91,0x6e,//3,12-15 
0x3c,0x44,0xff,0x04,//4,16-19 
0xf2,0x91,0x91,0x8e,//5,20-23 
0x7e,0x91,0x91,0x4e,//6,24-27 
0xc0,0x80,0x80,0xff,//7,28-31 
0x6e,0x91,0x91,0x6e,//8,32-35 
0x72,0x89,0x89,0x7e//9,36-39 
}; 
uchar code table1[]={//A字节 
0xe9,0xe8,0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0, 
}; 
uchar code table2[]={//B字节 
0xd7,0xd6,0xd5,0xd4,0xd3,0xd2,0xd1,0xd0 
}; 
 
 
void display(uchar); 
uchar shi,ge,flag,temp; 
char num,num1; 
 
void delay() 
{;;} 
void delay1ms(uinP0 z) 
{ 
	uinP0 x,y; 
	for(x=z;x>0;x--) 
		for(y=110;y>0;y--); 
} 
void start() 
{ 
	sda=1; 
	delay(); 
	scl=1; 
	delay(); 
	sda=0; 
	delay(); 
} 
void stop() 
{ 
	sda=0; 
	delay(); 
	scl=1; 
	delay(); 
	sda=1; 
	delay(); 
} 
void ack() 
{ 
	uchar i; 
	scl=1; 
	delay(); 
	while((sda==1)&&(i<250))i++; 
	scl=0; 
	delay(); 
} 
void init() 
{ 
	sda=1; 
	delay(); 
	scl=1; 
	delay(); 
} 
void write_byte(uchar date) 
{ 
	uchar i,tem; 
	tem=date; 
	for(i=0;i<8;i++) 
	{ 
		tem=tem<<1; 
		scl=0; 
		delay(); 
		sda=CY; 
		delay(); 
		scl=1; 
		delay();	 
	} 
	scl=0; 
	delay(); 
	sda=1; 
	delay(); 
} 
void write_audio(uchar ge,uchar shi)//PT2258音量控制 
{ 
	start(); 
	write_byte(0x88);//PT2258片选地址 
	ack(); 
	write_byte(ge); 
	ack(); 
	write_byte(shi); 
	ack(); 
	stop(); 
} 
uchar read_byte() 
{ 
	uchar i,k; 
	scl=0; 
	delay(); 
	sda=1; 
	delay(); 
	for(i=0;i<8;i++) 
	{ 
		scl=1; 
		delay(); 
		k=(k<<1)|sda; 
		scl=0; 
		delay(); 
	} 
	return k; 
} 
void write_add(uchar address,uchar date) 
{ 
	start(); 
	write_byte(0xa0);//AT24C02的片选地址 
	ack(); 
	write_byte(address); 
	ack(); 
	write_byte(date); 
	ack(); 
	stop();	 
} 
uchar read_add(uchar address) 
{ 
	uchar date; 
	start(); 
	write_byte(0xa0); 
	ack(); 
	write_byte(address); 
	ack(); 
	start(); 
	write_byte(0xa1); 
	ack(); 
	date=read_byte(); 
	stop(); 
	return date; 
} 
 
void keyscan() 
{ 
	if(key1==0) 
	{ 
		P0=0;//黑屏指令,避免屏幕显示乱码 
		delay1ms(10); 
		if(key1==0) 
		{	 
			num++; 
			num1=0; 
			if(num==80) 
				num=79; 
			shi=num/10; 
			ge=num%10; 
			write_audio(table1[ge],table2[shi]);//将num数值写入PT2258 
			write_audio(table1[ge],table2[shi]);//将数据写第二遍,解决x9处漏洞 
			write_add(2,num);//将num数值写入AT24C02 
			while(!key1);			 
		} 
 
	} 
	if(key2==0) 
	{ 
		P0=0; 
		delay1ms(10); 
		if(key2==0) 
		{ 
			num--; 
			num1=0; 
			if(num==-1) 
				num=0; 
			shi=num/10; 
			ge=num%10; 
			write_audio(table1[ge],table2[shi]);//将num数值写入PT2258 
			write_audio(table1[ge],table2[shi]);//将数据写第二遍,解决x0处漏洞 
			write_add(2,num);//将num数值写入AT24C02 
			while(!key2); 
		} 
	} 
	if(key3==0)//mute 
	{ 
		P0=0; 
		delay1ms(10); 
		if(key3==0) 
		{ 
			temp=num1; 
			num1=num;	 
			num=temp; 
			shi=num/10; 
			ge=num%10; 
			write_audio(table1[ge],table2[shi]);//将num数值写入PT2258 
			write_audio(table1[ge],table2[shi]);//将数据写第二遍,解决x0处漏洞 
			while(!key3); 
		 
			/*switch(flag) 
			{ 
				case 0x01: 
				write_audio(0xf9,0);//静音开指令 
				break; 
				case 0xfe: 
				write_audio(0xf8,0);//静音关指令 
				break; 
			} 
			while(!key3); 
			flag=~flag;*/ 
		} 
			 
	} 
} 
void init1()//解决开机噪音 
{ 
		shi=num/10; 
		ge=num%10; 
		write_audio(table1[ge],table2[shi]);//将num数值写入PT2258 
		write_audio(table1[ge],table2[shi]);//将数据写第二遍,解决x0处漏洞 
} 
 
void main() 
{ 
	init(); 
	num=read_add(2); 
	init1(); 
	num1=0; 
	flag=0x01; 
	temp=0; 
	 
	while(1) 
	{ 
		keyscan(); 
		display(num); 
		 
	} 
 
 
} 
void display(uchar shi,uchar ge) 
{ 
		uchar disa,disb; 
		shi=num/10; 
		ge=num%10; 
 
		disa=shi*4; 
		disb=ge*4; 
 
		P0=table[disa];//显示十位 
		P1=0x7f; 
		delay1ms(1); 
		 
		P0=table[disa+1]; 
		P1=0xbf; 
		delay1ms(1); 
		 
		P0=table[disa+2]; 
		P1=0xdf; 
		delay1ms(1); 
		 
		P0=table[disa+3]; 
		P1=0xef; 
		delay1ms(1); 
 
		P0=table[disb];//显示个位 
		P1=0xf7; 
		delay1ms(1); 
		 
		P0=table[disb+1]; 
		P1=0xfb; 
		delay1ms(1); 
		 
		P0=table[disb+2]; 
		P1=0xfd; 
		delay1ms(1); 
		 
		P0=table[disb+3]; 
		P1=0xfe; 
		delay1ms(1);				 
 
}