pt2313.c

来自「音频控制芯片PT2313的驱动源程序」· C语言 代码 · 共 330 行

C
330
字号
/*******************************************************************************
**                            (c) Copyright 2004-2005, xujiajun
**                                    All Rights Reserved
**                                        V040723
**--------------文件信息--------------------------------------------------------
**创   建   人: 徐家俊
**创建日期: 2006年1月20日
**描        述:PT2313驱动
**--------------版本修订历史----------------------------------------------------
** 修改人:徐家俊
** 版  本: V
** 日 期: 年月日
** 描 述:
**--------------当前版本修订----------------------------------------------------
** 修改人:徐家俊
** 版  本:
** 日 期:年月日
** 描 述:
**------------------------------------------------------------------------------
*******************************************************************************/
#define IN_PT2313
#include"main.h"

#define PT2313_ADDR       0x88       //I2C写地址
/*-----------------------------------------------------------------------------*/
uchar code VOLUME_TABLE[]={//音量从0到31档
       //VOLUME 0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
                64,58,52,48,44,40,37,34,31,28,26,24,22,20,18,16,
      //VOLUME 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
                15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
/********************************************************************************************************
 Descriptions:          Set the volume
 input parameters:      Volume: 音量从0到31档
 Returned value:        None
 Used global variables: None
 Calling modules:       None
 Created by:            xjj 2006/01/20
-------------------------------------------------------------------------------------------------------
 Modified by:
********************************************************************************************************/
void SetVolume(uchar Volume)
{
    uchar temp;

    if(Volume > VOLUME_MAX)
        //Volume = 15; ==> ASSIGN_WRONG_VAR simon 2006.05.06
        ucVolume = Volume = 15;

    temp = VOLUME_TABLE[Volume];
    temp &= 0x7f;
    ISendByte(PT2313_ADDR, temp);
}
/********************************************************************************************************
 Descriptions:          Set the Bass
 input parameters:      Bass: 从0到15档
 Returned value:        None
 Used global variables: None
 Calling modules:       None
 Created by:            xjj 2006/01/20
-------------------------------------------------------------------------------------------------------
 Modified by:           simon 2006.05.06
********************************************************************************************************/
/*void SetBass(uchar Bass)
{
    if(Bass > 14)
        Bass = 7;
    if(Bass == 0)
    {
        Bass = b01100000B;
        ISendByte(PT2313_ADDR, Bass);
        return;
    }
    if(Bass == 14)
    {
        Bass = b01101000B;
        ISendByte(PT2313_ADDR, Bass);
        return;
    }
    if(Bass == 7)
    {//最佳低音
        Bass = b01100111B;
        ISendByte(PT2313_ADDR, Bass);
        return;
    }

    if(Bass<7)
    {
        Bass |= b01100000B;
    }
    if(Bass>7)
    {
        Bass = 14 - Bass;
        Bass |= b01101000B;
    }
    ISendByte(PT2313_ADDR, Bass);
} */
void SetBass(uchar Bass)
{
    if(Bass > 14)
        ucBass = Bass = 7;

    if(Bass == 0)
    {
        Bass = b01100000B;
    }
    else if(Bass == 14)
    {
        Bass = b01101000B;
    }
    else if(Bass == 7)
    {//最佳低音
        Bass = b01100111B;
    }
    else if(Bass<7)
    {
        Bass |= b01100000B;
    }
    else //Bass>7
    {
        Bass = 14 - Bass;
        Bass |= b01101000B;
    }

    ISendByte(PT2313_ADDR, Bass);
}
/********************************************************************************************************
 Descriptions:          Set the Treble
 input parameters:      Treble: 从0到15档
 Returned value:        None
 Used global variables: None
 Calling modules:       None
 Created by:            xjj 2006/01/20
-------------------------------------------------------------------------------------------------------
 Modified by:           simon 2006.05.06
********************************************************************************************************/
/*void SetTreble(uchar Treble)
{
    if(Treble > 14)
        Treble = 7;
    if(Treble == 0)
    {
        Treble = b01110000B;
        ISendByte(PT2313_ADDR, Treble);
        return;
    }
    if(Treble == 14)
    {
        Treble = b01111000B;
        ISendByte(PT2313_ADDR, Treble);
        return;
    }
    if(Treble == 7)
    {//最佳高音
        Treble = b01110111B;
        ISendByte(PT2313_ADDR, Treble);
        return;
    }

    if(Treble<7)
    {
        Treble |= b01110000B;
    }
    if(Treble>7)
    {
        Treble = 14 - Treble;
        Treble |= b01111000B;
    }
    ISendByte(PT2313_ADDR, Treble);
}*/
void SetTreble(uchar Treble)
{
    if(Treble > 14)
        ucTreble = Treble = 7;

    if(Treble == 0)
    {
        Treble = b01110000B;
    }
    else if(Treble == 14)
    {
        Treble = b01111000B;
    }
    if(Treble == 7)
    {//最佳高音
        Treble = b01110111B;
    }
    else if(Treble<7)
    {
        Treble |= b01110000B;
    }
    else //Treble>7
    {
        Treble = 14 - Treble;
        Treble |= b01111000B;
    }

    ISendByte(PT2313_ADDR, Treble);
}
/********************************************************************************************************
 Descriptions:          Mute
 input parameters:      None
 Returned value:        None
 Used global variables: None
 Calling modules:       None
 Created by:            xjj 2006/01/20
-------------------------------------------------------------------------------------------------------
 Modified by:
********************************************************************************************************/
/*
void PT2313Mute(void)
{
    uchar Buf[4];

    Buf[0] = 0x9f;//MOV 70H,#10011111B          ;LF
    Buf[1] = 0xbf;//MOV 71H,#10111111B          ;RF
    Buf[2] = 0xdf;//MOV 72H,#11011111B          ;LR
    Buf[3] = 0xff;//MOV 73H,#11111111B          ;RR
    ISendStr(PT2313_ADDR,Buf[0],&Buf[1],3);
}
*/
/********************************************************************************************************
 Descriptions:          AudioSwitch and gain
 input parameters:      channel: Stereo 0 to 3; gain 0 to 3;Londness 1 enable
 Returned value:        None
 Used global variables: None
 Calling modules:       None
 Created by:            xjj 2006/01/20
-------------------------------------------------------------------------------------------------------
 Modified by:
********************************************************************************************************/
void AudioSwitchAndGain(uchar channel, uchar gain, bit LondnessEn)
{
    channel &= 0x03;
    gain &= 0x03;
    gain <<= 3;
    channel |= gain;
    if(!LondnessEn)
        channel |= 0x04;
    channel |= 0x40;
    ISendByte(PT2313_ADDR, channel);
}
/********************************************************************************************************
 Descriptions:          Send sound data to PT2313, include volume, bess, treble, balanceRL,
                        balanceFB
 input parameters:      channel: Stereo 1 to 4
 Returned value:        None
 Used global variables: None
 Calling modules:       None
 Created by:            xjj 2006/01/20
-------------------------------------------------------------------------------------------------------
 Modified by:
********************************************************************************************************/
void SendSoundDataPT2313(void)
{
    uchar ucFL,ucFR,ucBL,ucBR;
    uchar ucMax;
    uchar Buf[4];
    uint16 u16Temp; //ADD_VAR_FOR_CALCULATION simon 2006.05.06

    SetVolume(ucVolume);
    SetBass(ucBass);
    SetTreble(ucTreble);
    //set balance
SET_BALANCE:
    ucFL = ucBalanceRL + ucBalanceFB;
    ucFR = 14-ucBalanceRL+ucBalanceFB;
    ucBL = 14-ucBalanceFB+ucBalanceRL;
    ucBR = 28-ucBalanceRL-ucBalanceFB;

    ucMax = ucFL;
    if(ucFR > ucMax)
        ucMax = ucFR;
    if(ucBL > ucMax)
        ucMax = ucBL;
    if(ucBR > ucMax)
        ucMax = ucBR;

    if(ucMax == 0)
    {
        ucBalanceRL = 7;
        ucBalanceFB = 7;
        goto SET_BALANCE;
    }

    //WRONG_CALCULATION_FOR_VARS_OF_DIFFERENT_SIZE simon 2006.05.06
    /*
    ucFL = 28 - (ucFL*28/ucMax);
    ucFR = 28 - (ucFR*28/ucMax);
    ucBL = 28 - (ucBL*28/ucMax);
    ucBR = 28 - (ucBR*28/ucMax);
    */
    u16Temp = ucFL * 28;
    ucFL = 28 - (u16Temp / ucMax);
    u16Temp = ucFR * 28;
    ucFR = 28 - (u16Temp / ucMax);
    u16Temp = ucBL * 28;
    ucBL = 28 - (u16Temp/ucMax);
    u16Temp = ucBR * 28;
    ucBR = 28 - (u16Temp / ucMax);
    //end WRONG_CALCULATION_FOR_VARS_OF_DIFFERENT_SIZE

    if(ucBalanceRL == 14)
    {
        ucFR = b00011111B;
        ucBR = b00011111B;
    }
    else if(ucBalanceRL == 0)
    {
        ucFL = b00011111B;
        ucBL = b00011111B;
    }

    if(ucBalanceFB == 14)
    {
        ucBR = b00011111B;
        ucBL = b00011111B;
    }
    else if(ucBalanceFB == 0)
    {
        ucFR = b00011111B;
        ucFL = b00011111B;
    }

    Buf[0] = ucFL + b10000000B;//LF
    Buf[1] = ucFR + b10100000B;//RF
    Buf[2] = ucBL + b11000000B;//LR
    Buf[3] = ucBR + b11100000B;//RR
    ISendStr(PT2313_ADDR,Buf[0],&Buf[1],3);
}

⌨️ 快捷键说明

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