给个早期的C51的代码你参考,PT2314电源最好用8V,PT2314E可以用5V电源,二者均不支持3V3,上拉电阻拉到PT2314供电上
#include"..\source\audio.h"
//全局变量定义
unsigned char g_Audio_Volume;
unsigned char g_Audio_Treble;
unsigned char g_Audio_Bass;
unsigned char g_Audio_Balance;
unsigned char g_Audio_Channel;
unsigned char code Volume_Buf[] = { 0, 2, 4, 6, 8,10,12,14,16,18,
19,20,21,22,23,24,25,26,27,28,
29,31,33,35,37,39,41,43,45,47,
48,49,50,51,52,53,53 };
/*********************************************************************************************
Function: 此函数把一个字节数据输入到I2C总线上,并确认传输有效
*********************************************************************************************/
bit PT2314_Write_Byte(unsigned char byte)
{
unsigned char temp = 0x00;
bit Acknowledge = 0;
for(temp=0;temp<8;temp++)
{
PT2314_SCL = 0;
NOP;
NOP;
NOP;
NOP;
if(byte & 0x80)
PT2314_SDA = 1;
else
PT2314_SDA = 0;
NOP;
NOP;
NOP;
NOP;
PT2314_SCL = 1;
byte =byte << 1;
}
PT2314_SCL = 0;
PT2314_SDA = 1;
NOP;
NOP;
PT2314_SCL = 1;
NOP;
NOP;
while(!PT2314_SCL);
if(PT2314_SDA)
Acknowledge = 1;
PT2314_SCL = 0;
NOP;
NOP;
return(Acknowledge); //Acknowledge = 1数据写入失败
}
//本函数进行命令写入,进行设置命令传输。
void PT2314_Write(unsigned char temp1,unsigned char temp2)
{
unsigned char j = 5;
PT2314_SCL = 1; //输入设置
NOP;
NOP;
PT2314_SDA = 1;
NOP;
NOP;
while(!PT2314_SCL);
PT2314_SDA = 0;
NOP;
NOP;
PT2314_SCL = 0; //I2C启动
PT2314_Write_Byte(0x88);
while(PT2314_Write_Byte(temp1 + temp2)&&(j--)); //写入失败反复写5次
NOP;
NOP;
PT2314_SDA = 0;
NOP;
NOP;
PT2314_SCL = 1;
NOP;
NOP;
while(!PT2314_SCL); //I2C结束
PT2314_SDA = 1;
}
//本函数设置输入音频通道
void Set_PT2314_CHL(unsigned char temp_ch)
{
unsigned char temp;
if(b_PT2314_Lound)
{
temp = temp_ch | 0x08;
}
else
{
temp = temp_ch |0x0c;
}
PT2314_Write(0x50,temp);
}
//本函数设置音量衰减,音量增大为衰减量减小
void Set_PT2314_VOL(unsigned char vol)
{
if(b_PT2314_Mute || (vol == 0))
{
PT2314_Write(0x00,0x3f);
}
else
{
PT2314_Write(0x00,(63 - Volume_Buf[vol]));
}
}
/*********************************/
#if 0
void Set_PT2314_FL(unsigned char fl)
{
PT2314_Write(0xc0,(0x1f - fl));
}
void Set_PT2314_FR(unsigned char fr)
{
PT2314_Write(0xe0,(0x1f - fr));
}
#endif
/**********************************/
//下列函数设置高低音调
void Set_PT2314_TRE(unsigned char tre)
{
if(tre <= 7)
{
PT2314_Write(0x70,tre);
}
else
{
PT2314_Write(0x70,(23 - tre));
}
}
void Set_PT2314_BAS(unsigned char bas)
{
if(bas <= 7)
{
PT2314_Write(0x60,bas);
}
else
{
PT2314_Write(0x60,(23 - bas));
}
}
//此函数设置平衡
void Set_PT2314_BAL(unsigned char bal)
{
if (bal > 7)
{
PT2314_Write( 0xe0,(2 * (bal - 7)) ); //right
PT2314_Write( 0xc0,0x00 );
}
else if (bal < 7)
{
PT2314_Write( 0xc0,(2 * (7 - bal)) ); //left
PT2314_Write( 0xe0,0x00 );
}
PT2314_Write(0xe0,0x00); //声场平衡恢复居中设置
PT2314_Write(0xc0,0x00);
}
/**************************************************************/
void Audio_Set_ALL(void) //音效设置
{
Set_PT2314_CHL(g_Audio_Channel);
Set_PT2314_TRE(g_Audio_Treble);
Set_PT2314_BAS(g_Audio_Bass);
Set_PT2314_BAL(g_Audio_Balance);
Set_PT2314_VOL(g_Audio_Volume);
}
/**************************************************************/
void Audio_Channel_SELECT(void) //通道选择
{
g_Audio_Channel++;
if(g_Audio_Channel >= 4)
{
g_Audio_Channel = 0;
}
if(g_Audio_Channel == DVD_MODE)
{
DVD_POWER = 1;
}
else
{
DVD_POWER = 0;
}
if(g_Audio_Channel == TUNER_MODE)
{
b_tuner_flag = 1;
}
else
{
b_tuner_flag = 0;
}
Set_PT2314_CHL(g_Audio_Channel);
}
/**************************************************************/
void Audio_Volume_UP(void) //主音量调节
{
if(g_Audio_Volume < 36)
{
g_Audio_Volume++;
}
Set_PT2314_VOL(g_Audio_Volume);
}
void Audio_Volume_DW(void)
{
if(0 < g_Audio_Volume)
{
g_Audio_Volume--;
}
Set_PT2314_VOL(g_Audio_Volume);
}
/**************************************************************/
void Audio_Treble_UP(void) //高音调节
{
if(g_Audio_Treble < 14)
{
g_Audio_Treble++;
}
Set_PT2314_TRE(g_Audio_Treble);
}
void Audio_Treble_DW(void)
{
if(0 < g_Audio_Treble)
{
g_Audio_Treble--;
}
Set_PT2314_TRE(g_Audio_Treble);
}
/**************************************************************/
void Audio_Bass_UP(void) //低音调节
{
if(g_Audio_Bass < 14)
{
g_Audio_Bass++;
}
Set_PT2314_BAS(g_Audio_Bass);
}
void Audio_Bass_DW(void)
{
if(0 < g_Audio_Bass)
{
g_Audio_Bass--;
}
Set_PT2314_BAS(g_Audio_Bass);
}
/**************************************************************/
/*
void Audio_Balance_RIGHT(void) //平衡调节
{
if(g_Audio_Balance < 14)
{
g_Audio_Balance++;
}
Set_PT2314_BAL(g_Audio_Balance);
}
void Audio_Balance_LEFT(void)
{
if(0 < g_Audio_Balance)
{
g_Audio_Balance--;
}
Set_PT2314_BAL(g_Audio_Balance);
}*/
/**************************************************************/
/*void Audio_Lound_SWITCH(void) //等响开关
{
b_PT2314_Lound = ~b_PT2314_Lound;
Set_PT2314_CHL(g_Audio_Channel);
}
/**************************************************************/
void Audio_MUTE(void) //静音
{
AMP_MUTE = ~AMP_MUTE;
b_PT2314_Mute = ~b_PT2314_Mute;
Set_PT2314_VOL(g_Audio_Volume);
}
/**************************************************************/
void Audio_Init(void) //音效初始化
{
g_Audio_Volume = DEFAULT_VOL;
g_Audio_Treble = DEFAULT_TRE;
g_Audio_Bass = DEFAULT_BAS;
g_Audio_Balance = DEFAULT_BAL;
g_Audio_Channel = DVD_MODE;
Audio_Set_ALL();
}
#ifndef _AUDIO_H_
#define _AUDIO_H_
#include "public.h"
#include "configure.h"
////////////////////////////////////////////////////////////////////////////////
//常量定义
#define DEFAULT_BAL 7
#define DEFAULT_TRE 7
#define DEFAULT_BAS 7
#define DEFAULT_VOL 12
#define DVD_MODE 3
#define AUX_MODE 2
#define PC_MODE 1
#define TUNER_MODE 0
//全局函数申明
extern void Audio_Set_ALL(void);
extern void Audio_Channel_SELECT(void);
extern void Audio_Volume_UP(void);
extern void Audio_Volume_DW(void);
extern void Audio_Treble_UP(void);
extern void Audio_Treble_DW(void);
extern void Audio_Bass_UP(void);
extern void Audio_Bass_DW(void);
extern void Audio_MUTE(void);
extern void Audio_Init(void);
#endif
|