1nnocent 发表于 2021-4-21 16:09

【HC32F460开发板测评】06 wm8731驱动TPA3116双声道数字功放

本帖最后由 1nnocent 于 2021-4-23 09:38 编辑

<p>之前申请测评的时候就想把之前做的TPA3116数字功放用上,</p>

<p>上次做了wm8731单独播放音乐的功能,</p>

<p>这次将之前做的功放用上,功放刚好是双声道的,</p>

<p>直接用开发板上的耳机接口输出的语音信号给到功放。</p>

<p>这次主要实现以下功能:</p>

<table style="border-collapse:collapse; width:270pt; border:none" width="360">
        <colgroup>
                <col span="5" style="width:54pt" width="72" />
        </colgroup>
        <tbody>
                <tr height="19" style="height:14.25pt">
                        <td class="xl63" height="19" style="border:0.5pt solid windowtext; height:14.25pt; width:54pt; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle; white-space:nowrap" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线"> </span></span></span></span></span></span></td>
                        <td class="xl64" style="border:0.5pt solid windowtext; border-left:none; width:54pt; text-align:center; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle; white-space:nowrap" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">SW2</span></span></span></span></span></span></td>
                        <td class="xl64" style="border:0.5pt solid windowtext; border-left:none; width:54pt; text-align:center; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle; white-space:nowrap" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">SW3</span></span></span></span></span></span></td>
                        <td class="xl64" style="border:0.5pt solid windowtext; border-left:none; width:54pt; text-align:center; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle; white-space:nowrap" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">SW4</span></span></span></span></span></span></td>
                        <td class="xl64" style="border:0.5pt solid windowtext; border-left:none; width:54pt; text-align:center; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle; white-space:nowrap" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">SW5&nbsp;</span></span></span></span></span></span></td>
                </tr>
                <tr height="57" style="height:42.75pt">
                        <td class="xl65" height="57" style="border:0.5pt solid windowtext; height:42.75pt; border-top:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">TPA3116的现象</span></span></span></span></span></span></td>
                        <td class="xl65" style="border:0.5pt solid windowtext; border-top:none; border-left:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">左右声道音量同时增大</span></span></span></span></span></span></td>
                        <td class="xl65" style="border:0.5pt solid windowtext; border-top:none; border-left:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">左右声道音量同时减小</span></span></span></span></span></span></td>
                        <td class="xl65" style="border:0.5pt solid windowtext; border-top:none; border-left:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">左声道音量增大</span></span></span></span></span></span></td>
                        <td class="xl65" style="border:0.5pt solid windowtext; border-top:none; border-left:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">右声道音量增大</span></span></span></span></span></span></td>
                </tr>
                <tr height="38" style="height:28.5pt">
                        <td class="xl65" height="38" style="border:0.5pt solid windowtext; height:28.5pt; border-top:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">开发板指示灯的现象</span></span></span></span></span></span></td>
                        <td class="xl65" style="border:0.5pt solid windowtext; border-top:none; border-left:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">红灯亮起</span></span></span></span></span></span></td>
                        <td class="xl65" style="border:0.5pt solid windowtext; border-top:none; border-left:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">绿灯亮起</span></span></span></span></span></span></td>
                        <td class="xl65" style="border:0.5pt solid windowtext; border-top:none; border-left:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">黄灯亮起</span></span></span></span></span></span></td>
                        <td class="xl65" style="border:0.5pt solid windowtext; border-top:none; border-left:none; width:54pt; text-align:center; white-space:normal; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:middle" width="72"><span style="font-size:11pt"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:等线">蓝灯亮起</span></span></span></span></span></span></td>
                </tr>
        </tbody>
</table>

<p>&nbsp;这次的实验是用到了wm8731和按键,刚好是前两个实验的结合,</p>

<p>篇幅关系这里只给出主要的几个文件的代码,具体代码可以自行下载文件查看。</p>

<p>代码主要需要对按键值进行读取,并根据键值更改wm8731输入信号的大小,0x00-0x17个音量梯度。改完后重新初始化一下就可以更新音量大小。</p>

<p>关于实验功能的测试,这里使用电脑播放歌曲《完美重合》该歌曲左右声道独立播放两首歌曲,左声道播放歌曲《发如雪》右声道播放歌曲《我不配》;</p>

<p>按键SW4和SW5控制左或右声道的音量大小,最小可以是静音,效果可以查看下面的视频,</p>

<p>或者可以自己下载代码,使用耳机听,效果更加明显,视频里的听起来可能不是很明显因为是录制视频两个喇叭距离也较近所以听不出哪个喇叭声音较小或者是哪个喇叭在发声哪个静音。</p>

<p>视频里可能只可以听出音量大小的变化。</p>

<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=16A411L7f8&amp;page=1" style="background:#eee;margin-bottom:10px;" width="750"></iframe><br />
这里发现一个问题,板子接下载器播放音乐时会引入干扰,下载程序时也会有干扰的声音,可能是数字地和模拟地没有做好隔离,</p>

<p>原理图没有发现有进行合理,板子布线的时候不知道是否有进行相应的隔离处理。</p>

<p></p>

<p>以下是具体实现代码:</p>

<p>key.c</p>

<pre>
<code>/******************************************************************************/
/** \file key.c
**
** \brief Key functions
**
**   - 2021-04-151.0First version for Key
**
******************************************************************************/

/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32_ddl.h"
#include "wm8731.h"
#include "audio.h"
#include "key.h"
#include "led.h"
#include "i2c.h"

/*******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/

/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/

/*******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
extern uint16_t Lin_Volume;                        //0dB--0x17u
extern uint16_t Rin_Volume;                        //0dB--0x17u
extern uint16_t Lout_Volume;                //0x30(-73dB) ~ 0x7F(+6dB), 0 ~ 0x2F: mute
extern uint16_t Lout_Volume;                //0x30(-73dB) ~ 0x7F(+6dB), 0 ~ 0x2F: mute
extern stc_wm8731_reg_t stcWm8731Reg;

/*******************************************************************************
* Local function prototypes ('static')
******************************************************************************/

/**
*******************************************************************************
** \briefInitialize keys
**
** \paramNone
**
** \retval None
**
******************************************************************************/
void IniKey(void)
{
    stc_port_init_t stcPortInit;
    /*initiallize KEY port*/
    MEM_ZERO_STRUCT(stcPortInit);

    /* KEY0 Port/Pin initialization */
    PORT_Init(KEY2_PORT, KEY2_PIN, &amp;stcPortInit);
    /* KEY1 Port/Pin initialization */
    PORT_Init(KEY3_PORT, KEY3_PIN, &amp;stcPortInit);
    /* KEY2 Port/Pin initialization */
    PORT_Init(KEY4_PORT, KEY4_PIN, &amp;stcPortInit);
    /* KEY3 Port/Pin initialization */
    PORT_Init(KEY5_PORT, KEY5_PIN, &amp;stcPortInit);
}

/**
*******************************************************************************
** \briefkeyscan
**
** \paramNone
**
** \retval None
**
******************************************************************************/
void KeyScan(void)
{
        if(KEY2 == 0)
        {
                Ddl_Delay1ms(100);
                if(Rin_Volume == 0x18u)
                        Rin_Volume = 0x00u;
                else
                        Rin_Volume ++;
                Lin_Volume = Rin_Volume;
                WM8731_CodecConfigRecord();
                LED0_TOGGLE();
                Ddl_Delay1ms(10);
                LED0_TOGGLE();
        }
        if(KEY3 == 0)
        {
                Ddl_Delay1ms(100);
                if(Rin_Volume == 0x00u)
                        Rin_Volume = 0x17u;
                else
                        Rin_Volume --;
                Lin_Volume = Rin_Volume;
                WM8731_CodecConfigRecord();
                LED1_TOGGLE();
                Ddl_Delay1ms(10);
                LED1_TOGGLE();
        }
        if(KEY4 == 0)
        {
                Ddl_Delay1ms(100);
                if(Lin_Volume == 0x18u)
                        Lin_Volume = 0x00u;
                else
                        Lin_Volume ++;
                WM8731_CodecConfigRecord();
                LED2_TOGGLE();
                Ddl_Delay1ms(10);
                LED2_TOGGLE();
        }
        if(KEY5 == 0)
        {
                Ddl_Delay1ms(100);
                if(Rin_Volume == 0x18u)
                        Rin_Volume = 0x00u;
                else
                        Rin_Volume ++;
                WM8731_CodecConfigRecord();
                LED3_TOGGLE();
                Ddl_Delay1ms(10);
                LED3_TOGGLE();
        }
}

/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/

/*******************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/

/*******************************************************************************
* EOF (not truncated)
******************************************************************************/


</code></pre>

<p>key.h</p>

<pre>
<code>/******************************************************************************/
/** \file key.h
**
** A detailed description is available at
** @link key Series description @endlink
**
**   - 2021-4-151.0First version for Key Functions description
**   Library.
**
******************************************************************************/
#ifndef __KEY_H__
#define __KEY_H__

/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32_ddl.h"

/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/* KEY0 Port/Pin definition *///SW2
#defineKEY2_PORT          (PortD)
#defineKEY2_PIN         (Pin03)
#defineKEY2                                                                PORT_GetBit(PortD,Pin03)
/* KEY2 Port/Pin definition *///SW3
#defineKEY3_PORT          PortD
#defineKEY3_PIN         Pin05
#defineKEY3                                                                PORT_GetBit(PortD,Pin05)
/* KEY1 Port/Pin definition *///SW4
#defineKEY4_PORT          (PortD)
#defineKEY4_PIN         (Pin04)
#defineKEY4                                                                PORT_GetBit(PortD,Pin04)
/* KEY3 Port/Pin definition *///SW5
#defineKEY5_PORT          PortD
#defineKEY5_PIN         Pin06
#defineKEY5                                                                PORT_GetBit(PortD,Pin06)

void IniKey(void);
void KeyScan(void);

#endif /* __KEY_H__ */

/*******************************************************************************
* End of file
******************************************************************************/




</code></pre>

<p>audio.c</p>

<pre>
<code>/******************************************************************************/
/** \file audio.c
**
** \brief Audio functions
**
**   - 2021-04-151.0First version for Audio
**
******************************************************************************/

/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32_ddl.h"
#include "wm8731.h"
#include "audio.h"
#include "i2c.h"

/*******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/

/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
#define RECORDER_WAVFILELEN         (15u*1024u)

/*******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
extern uint16_t *pu16SoundData;
extern uint16_t au16RecorderSoundI2s;
uint16_t Lin_Volume = 0x17u;                        //0dB--0x17u
uint16_t Rin_Volume = 0x17u;                        //0dB--0x17u
uint16_t Lout_Volume = 0x5Fu;                //0x30(-73dB) ~ 0x7F(+6dB), 0 ~ 0x2F: mute
uint16_t Rout_Volume = 0x5Fu;                //0x30(-73dB) ~ 0x7F(+6dB), 0 ~ 0x2F: mute
stc_wm8731_reg_t stcWm8731Reg;

/*******************************************************************************
* Local function prototypes ('static')
******************************************************************************/

/**
*******************************************************************************
** \briefConfiguration Codec to record wav file
**
** \paramNone
**
** \retval None
**
******************************************************************************/
void WM8731_CodecConfigRecord(void)
{
    MEM_ZERO_STRUCT(stcWm8731Reg);
    /* Config codec */
    /* Reset register */
    stcWm8731Reg.RESET            = 0x00u;    // Reset WM8731
    /* Left &amp; right line input */
    stcWm8731Reg.LLIN_f.LINVOL      = Lin_Volume;    // Left channel line input volume: 0dB--0x17u
    stcWm8731Reg.LLIN_f.LINMUTE   = 0u;       // Enable left channel line input mute
    stcWm8731Reg.LLIN_f.LRINBOTH    = 0u;       // Disable simultaneous input volume and mute load from left to right
    stcWm8731Reg.RLIN_f.RINVOL      = Rin_Volume;    // Right channel line input volume 0dB
    stcWm8731Reg.RLIN_f.RINMUTE   = 0u;       // Enable right channel line input mute
    stcWm8731Reg.RLIN_f.RINBOTH   = 0u;       // Disable simultaneous input volume and mute load from right to left
    /* Left &amp; right headphone output */
    stcWm8731Reg.LHOUT_f.LHPVOL   = 0x5Fu;    // Set volume of left headphone to 0dB. 0x30(-73dB) ~ 0x7F(+6dB), 0 ~ 0x2F: mute
    stcWm8731Reg.LHOUT_f.LZCEN      = 0u;       // Disable left channel zero cross detect
    stcWm8731Reg.LHOUT_f.LRHPBOTH   = 0u;       // Disable simultaneous output volume and mute load from left to right
    stcWm8731Reg.RHOUT_f.RHPVOL   = 0x5Fu;    // Set volume of right headphone to 0dB. 0x30(-73dB) ~ 0x7F(+6dB), 0 ~ 0x2F: mute
    stcWm8731Reg.RHOUT_f.RZCEN      = 0u;       // Enable right channel zero cross detect
    stcWm8731Reg.RHOUT_f.RLHPBOTH   = 0u;       // Disable simultaneous output volume and mute load from right to left
    /* Analog audio path control */
#if(RECORD_MIC)
    stcWm8731Reg.AAPC_f.MICBOOST    = 1u;       // Disable boost, 0: disable 1: enable
    stcWm8731Reg.AAPC_f.MUTEMIC   = 0u;       // Enable mute to ADC
    stcWm8731Reg.AAPC_f.INSEL       = 1u;       // Line input select to ADC, 0: linein1: mic
    stcWm8731Reg.AAPC_f.BYPASS      = 1u;       // Enbale bypass 0: disable 1:enable
    stcWm8731Reg.AAPC_f.DACSEL      = 0u;       // Select DAC
    stcWm8731Reg.AAPC_f.SIDETONE    = 0u;       // Disable side tone 0: disable 1:enable
    stcWm8731Reg.AAPC_f.SIDEATT   = 0u;       // 0: -6dB, 1: -12dB, 2: -9dB, 3: -15dB.
#else
    stcWm8731Reg.AAPC_f.MICBOOST    = 0u;       // Disable boost, 0: disable 1: enable
    stcWm8731Reg.AAPC_f.MUTEMIC   = 1u;       // Enable mute to ADC
    stcWm8731Reg.AAPC_f.INSEL       = 0u;       // Line input select to ADC, 0: linein1: mic
    stcWm8731Reg.AAPC_f.BYPASS      = 1u;       // Enbale bypass 0: disable 1:enable
    stcWm8731Reg.AAPC_f.DACSEL      = 0u;       // Select DAC
    stcWm8731Reg.AAPC_f.SIDETONE    = 0u;       // Disable side tone 0: disable 1:enable
    stcWm8731Reg.AAPC_f.SIDEATT   = 0u;       // 0: -6dB, 1: -12dB, 2: -9dB, 3: -15dB.
#endif
    /* Digital audio path control */
    stcWm8731Reg.DAPC_f.ADCHPD      = 0u;       // Enable high pass filter
    stcWm8731Reg.DAPC_f.DEEMP       = 0u;       // De-emphasis contrl. 0: disable, 1: 32kHz, 2: 44.1kHz, 3: 48kHz
    stcWm8731Reg.DAPC_f.DACMU       = 0u;       // 0:Disable soft mute   1: Enable soft mute
    stcWm8731Reg.DAPC_f.HPOR      = 0u;       // Clear offset when high pass
    /* Power down control */
    stcWm8731Reg.PDC_f.LINEINPD   = 0u;       // Disable line input power down
    stcWm8731Reg.PDC_f.MICPD      = 0u;       // Disable microphone input power down
    stcWm8731Reg.PDC_f.ADCPD      = 0u;       // Disable ADC power down
    stcWm8731Reg.PDC_f.DACPD      = 0u;       // Disable DAC power down
    stcWm8731Reg.PDC_f.OUTPD      = 0u;       // Disable output power down
    stcWm8731Reg.PDC_f.OSCPD      = 0u;       // Disable oscillator power down
    stcWm8731Reg.PDC_f.CLKOUTPD   = 0u;       // Disable CLKOUT power down
    stcWm8731Reg.PDC_f.POWEROFF   = 0u;       // Disable power off mode
    /* Digital audio interface format */
    stcWm8731Reg.DAIF_f.FORMAT      = 2u;       // 0: MSB-First, right justified, 1: MSB-first, left justified, 2: I2S-format, 3: DSP mode
    stcWm8731Reg.DAIF_f.IWL         = 0u;       // 0: 16 bits, 1: 20 bits, 2: 24 bits, 3: 32 bits
    stcWm8731Reg.DAIF_f.LRP         = 0u;       // 1: right channel DAC data when DACLRC (WS) is high,0: right channel DAC data when DACLRC (WS) is low
    stcWm8731Reg.DAIF_f.LRSWAP      = 0u;       // 1: swap left channel and right channel, 0: don't swap
    stcWm8731Reg.DAIF_f.MS          = 0u;       // 1: Enable master mode, 0: Enable slave mode
    stcWm8731Reg.DAIF_f.BCLKINV   = 0u;       // Don't invert BCLK
    /* Sampling control */
    stcWm8731Reg.SC_f.NORMAL_USB    = 0u;       // 0: normal mode, 1: USB mode
    stcWm8731Reg.SC_f.BOSR          = 0u;       // Nomrmal mode: 0: 256fs, 1: 384fs
                                                // USB mode: 0: 250fs, 1:272fs
    stcWm8731Reg.SC_f.SR            = 2u;       // Sample rate setting
    stcWm8731Reg.SC_f.CLKDIV2       = 0u;       // 0: core clock is MCLK, 1: core clock is MCLK divided by 2
    stcWm8731Reg.SC_f.CLKODIV2      = 0u;       // 0: output clock is core clock, 1: core clock is core clock/2
    // Active control
    stcWm8731Reg.AC_f.ACTIVE      = 1u;       // 0: inactive, 1: active

    WM8731_Init(I2C_CH, &amp;stcWm8731Reg);

    WM8731_SetHpVolume(I2C_CH, 0x7Fu,0x7Fu);    //0x2F-MUTE ~ 0x7F Maximum
}

/**
*******************************************************************************
** \briefConfiguration Codec to play wav file
**
** \paramNone
**
** \retval None
**
******************************************************************************/
void WM8731_CodecConfigPlay(void)
{
    stc_wm8731_reg_t stcWm8731Reg;
    MEM_ZERO_STRUCT(stcWm8731Reg);
    /* Config codec */
    /* Reset register */
    stcWm8731Reg.RESET            = 0x00u;    // Reset WM8731
    /* Left &amp; right line input */
    stcWm8731Reg.LLIN_f.LINVOL      = 0x00u;    // Left channel line input volume: 0dB--0x17u
    stcWm8731Reg.LLIN_f.LINMUTE   = 1u;       // Enable left channel line input mute
    stcWm8731Reg.LLIN_f.LRINBOTH    = 0u;       // Disable simultaneous input volume and mute load from left to right
    stcWm8731Reg.RLIN_f.RINVOL      = 0x00u;    // Right channel line input volume 0dB
    stcWm8731Reg.RLIN_f.RINMUTE   = 1u;       // Enable right channel line input mute
    stcWm8731Reg.RLIN_f.RINBOTH   = 0u;       // Disable simultaneous input volume and mute load from right to left
    /* Left &amp; right headphone output */
    stcWm8731Reg.LHOUT_f.LHPVOL   = 0x5Fu;    // Set volume of left headphone to 0dB. 0x30(-73dB) ~ 0x7F(+6dB), 0 ~ 0x2F: mute
    stcWm8731Reg.LHOUT_f.LZCEN      = 0u;       // Disable left channel zero cross detect
    stcWm8731Reg.LHOUT_f.LRHPBOTH   = 0u;       // Disable simultaneous output volume and mute load from left to right
    stcWm8731Reg.RHOUT_f.RHPVOL   = 0x5Fu;    // Set volume of right headphone to 0dB. 0x30(-73dB) ~ 0x7F(+6dB), 0 ~ 0x2F: mute
    stcWm8731Reg.RHOUT_f.RZCEN      = 0u;       // Enable right channel zero cross detect
    stcWm8731Reg.RHOUT_f.RLHPBOTH   = 0u;       // Disable simultaneous output volume and mute load from right to left
    /* Analog audio path control */
    stcWm8731Reg.AAPC_f.MICBOOST    = 0u;       // Disable boost
    stcWm8731Reg.AAPC_f.MUTEMIC   = 1u;       // Enable mute to ADC
    stcWm8731Reg.AAPC_f.INSEL       = 0u;       // Line input select to ADC
    stcWm8731Reg.AAPC_f.BYPASS      = 0u;       // Enbale bypass
    stcWm8731Reg.AAPC_f.DACSEL      = 1u;       // Select DAC
    stcWm8731Reg.AAPC_f.SIDETONE    = 0u;       // Disable side tone
    stcWm8731Reg.AAPC_f.SIDEATT   = 0u;       // 0: -6dB, 1: -12dB, 2: -9dB, 3: -15dB.
    /* Digital audio path control */
    stcWm8731Reg.DAPC_f.ADCHPD      = 0u;       // Enable high pass filter
    stcWm8731Reg.DAPC_f.DEEMP       = 3u;       // De-emphasis contrl. 0: disable, 1: 32kHz, 2: 44.1kHz, 3: 48kHz
    stcWm8731Reg.DAPC_f.DACMU       = 0u;       // 0:Disable soft mute   1: Enable soft mute
    stcWm8731Reg.DAPC_f.HPOR      = 0u;       // Clear offset when high pass
    /* Power down control */
    stcWm8731Reg.PDC_f.LINEINPD   = 0u;       // Disable line input power down
    stcWm8731Reg.PDC_f.MICPD      = 0u;       // Disable microphone input power down
    stcWm8731Reg.PDC_f.ADCPD      = 0u;       // Disable ADC power down
    stcWm8731Reg.PDC_f.DACPD      = 0u;       // Disable DAC power down
    stcWm8731Reg.PDC_f.OUTPD      = 0u;       // Disable output power down
    stcWm8731Reg.PDC_f.OSCPD      = 0u;       // Disable oscillator power down
    stcWm8731Reg.PDC_f.CLKOUTPD   = 0u;       // Disable CLKOUT power down
    stcWm8731Reg.PDC_f.POWEROFF   = 0u;       // Disable power off mode
    /* Digital audio interface format */
    stcWm8731Reg.DAIF_f.FORMAT      = 2u;       // 0: MSB-First, right justified, 1: MSB-first, left justified, 2: I2S-format, 3: DSP mode
    stcWm8731Reg.DAIF_f.IWL         = 0u;       // 0: 16 bits, 1: 20 bits, 2: 24 bits, 3: 32 bits
    stcWm8731Reg.DAIF_f.LRP         = 0u;       // 1: right channel DAC data when DACLRC (WS) is high,0: right channel DAC data when DACLRC (WS) is low
    stcWm8731Reg.DAIF_f.LRSWAP      = 0u;       // 1: swap left channel and right channel, 0: don't swap
    stcWm8731Reg.DAIF_f.MS          = 0u;       // 1: Enable master mode, 0: Enable slave mode
    stcWm8731Reg.DAIF_f.BCLKINV   = 0u;       // Don't invert BCLK
    /* Sampling control */
    stcWm8731Reg.SC_f.NORMAL_USB    = 0u;       // 0: normal mode, 1: USB mode
    stcWm8731Reg.SC_f.BOSR          = 0u;       // Nomrmal mode: 0: 256fs, 1: 384fs
                                                // USB mode: 0: 250fs, 1:272fs
    stcWm8731Reg.SC_f.SR            = 1u;       // Sample rate setting
    stcWm8731Reg.SC_f.CLKDIV2       = 0u;       // 0: core clock is MCLK, 1: core clock is MCLK divided by 2
    stcWm8731Reg.SC_f.CLKODIV2      = 0u;       // 0: output clock is core clock, 1: core clock is core clock/2
    // Active control
    stcWm8731Reg.AC_f.ACTIVE      = 1u;       // 0: inactive, 1: active

    WM8731_Init(I2C_CH, &amp;stcWm8731Reg);

    WM8731_SetHpVolume(I2C_CH, 0x7Fu,0x7Fu);    //0x2F-MUTE ~ 0x7F Maximum
}

/**
*******************************************************************************
** \briefConfiguration Codec to play wav file
**
** \paramNone
**
** \retval None
**
******************************************************************************/
void SpeakerInit(void)
{
        stc_port_init_t stcPortIni;
#if(SPEAKER_ON)
        /* Initialize SPK_EN port for speaker */
        MEM_ZERO_STRUCT(stcPortIni);
        stcPortIni.enPinMode = Pin_Mode_Out;
        PORT_Init(SPK_EN_PORT, SPK_EN_PIN, &amp;stcPortIni);
        SPEAKER_EN();
#endif
}

/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/

/*******************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/

/*******************************************************************************
* EOF (not truncated)
******************************************************************************/
</code></pre>

<p>audio.h</p>

<pre>
<code>/******************************************************************************/
/** \file audio.h
**
** A detailed description is available at
** @link audio Series description @endlink
**
**   - 2021-4-151.0First version for Audio Series Functions description
**   Library.
**
******************************************************************************/
#ifndef __AUDIO_H__
#define __AUDIO_H__

/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32_common.h"

/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/* Define if need play by speaker*/
#define SPEAKER_ON                  (true)
       
/* Select Record source */
//#define RECORD_MIC                  (true)

#define SPK_EN_PORT               (PortB)
#define SPK_EN_PIN                  (Pin00)

#define SPEAKER_EN()                (PORT_SetBits(SPK_EN_PORT, SPK_EN_PIN))
#define SPEAKER_DISEN()             (PORT_ResetBits(SPK_EN_PORT, SPK_EN_PIN))
       
//#define RECORDER_WAVFILELEN         (15u*1024u)

///*******************************************************************************
// * Local variable definitions ('static')
// ******************************************************************************/



void WM8731_CodecConfigRecord(void);
void WM8731_CodecConfigPlay(void);
void SpeakerInit(void);

#endif /* __AUDIO_H__ */

/*******************************************************************************
* End of file
******************************************************************************/

</code></pre>

<p>main.c</p>

<pre>
<code>/**
*******************************************************************************
* @filemain.c
* @brief Main program template.
@verbatim
   Change Logs:
   Date             Author          Notes
   2020-06-30       Zhangxl         First version
@endverbatim
*******************************************************************************
* Copyright (C) 2016, Huada Semiconductor Co., Ltd. All rights reserved.
*
* This software is owned and published by:
* Huada Semiconductor Co., Ltd. ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC
* components. This software is licensed by HDSC to be adapted only
* for use in systems utilizing HDSC components. HDSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. HDSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACCOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*******************************************************************************
*/

/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32_ddl.h"
#include "wm8731.h"
#include "audio.h"
#include "i2c.h"
#include "key.h"
#include "led.h"

/*******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/

/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/

/*******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/

/*******************************************************************************
* Local function prototypes ('static')
******************************************************************************/

/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/

/*******************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/

/**
* @briefMain function of template project
* @paramNone
* @retval int32_t return value, if needed
*/
int32_t main(void)
{
        /* Initialize i2c for audio */
        i2cInit();
        IniKey();
        IniLedPort();

        /* WM8731 configuration for recorder */
        WM8731_CodecConfigRecord();
       
        while(1)
        {
                KeyScan();
        }
}

/*******************************************************************************
* EOF (not truncated)
******************************************************************************/
</code></pre>

<p>&nbsp;</p>

1nnocent 发表于 2021-5-14 18:41

<p>这个帖子是分好几天写的,写完存草稿了,最后一天发布的时候好像在论坛显示的发布时间是刚创建帖子的日期,不是发布当天的,所以好像沉底了。</p>

<p>所以帖子虽然是当天发的,论坛里却要翻到好几天前才能找到。</p>

<p>之前还有一些帖子也是这个情况,可能是个bug</p>

nmg 发表于 2021-5-17 11:53

<div class="quote">
<blockquote><font size="2"><a href="forum.php?mod=redirect&amp;goto=findpost&amp;pid=3062556&amp;ptid=1163360" target="_blank"><font color="#999999">1nnocent 发表于 2021-5-14 18:41</font></a></font> 这个帖子是分好几天写的,写完存草稿了,最后一天发布的时候好像在论坛显示的发布时间是刚创建帖子的日期, ...</blockquote>
</div>

<p>收到,已经和内部讨论下,会以发帖时间排序,避免沉帖子,大概这周技术会调整、修复过来</p>
页: [1]
查看完整版本: 【HC32F460开发板测评】06 wm8731驱动TPA3116双声道数字功放