我正在尝试使用 PIC32MX250F128B 通过 I2S 与 DAC+放大器从模块 (MAX98257) 通信来生成简单的正弦波声音。最终目标是为学校项目制作带键盘的合成器。
我用 MPLABX (v.5.40) 创建了一个基本项目我现在没有使用 Harmony 或任何其他框架,我只是想拥有一个基本的声音生成、工作代码。
这是代码:
微控制器配置位:
// DEVCFG3
#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config PMDL1WAY = ON // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow only one reconfiguration)
#pragma config FUSBIDIO = ON // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO = ON // USB VBUS ON Selection (Controlled by USB Module)
// DEVCFG2
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_18 // PLL Multiplier (18x Multiplier)
#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 1)
// DEVCFG1
#pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
#pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = XT // Primary Oscillator Configuration (XT osc mode)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_2 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/2)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)
// DEVCFG0
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
SPI/I2S 配置 + 代码:
int sineTable[4096]= { ... };
int i = 0;
void main(void) {
RPB5R = 0b0011; //pin 14 : SDO1 (data out)
RPA0R = 0b0011; //pin 2 : SS1 (word select)
SPI1CON2bits.AUDEN = 1; //audio protocol enabled
SPI1CON2bits.AUDMONO = 1; //MONO mode
SPI1CON2bits.AUDMOD = 0b00; //I2S mode
SPI1CON2bits.IGNROV = 1; // Ignore Receive Overflow bit (for Audio Data Transmissions)
SPI1CON2bits.IGNTUR = 1; // Ignore Transmit Underrun bit (for Audio Data Transmissions) 1 = A TUR is not a critical error and zeros are transmitted until thSPIxTXB is not empty 0 = A TUR is a critical error which stop SPI operation
SPI1BRG = 140;
SPI1CONbits.MSTEN = 1;
SPI1CONbits.MCLKSEL = 0; //Clock = Fpb
SPI1CONbits.CKP = 1; //Idle state high, active state low
SPI1CONbits.MODE32 = 1;
SPI1CONbits.MODE16 = 0; //32 bit
//SPI1CONbits.STXISEL = 0b11; //quand générer l'interruption?
SPI1CONbits.DISSDI = 1; // disable serial data in
SPI1CONbits.ENHBUF = 1; // enable enhanced buffer
SPI1CONbits.ON = 0; //ON
SPI1CONbits.ON = 1; //ON
while(1)
{
SPI1BUF = sineTable[i];
while( SPI1STATbits.SPIBUSY); // wait for transfer complete
if(i >= 4095){
i = 0;
}
i++;
} // main loop
}
模拟行为: 在模拟中,代码似乎工作正常,但是状态寄存器(SPI1STAT)中的所有位都为 0,并且始终保持为 0(包括 SPIBUSY),这很奇怪。
目标行为: 我正在用面包板上的 Pickit3 对 PIC32 进行编程(ICSP,没有开发板)。我遵循数据表中指定的 PIC32MX 的基本连接,并连接了 DAC
我注意到:
- 在我用 sinetable 中的第一个值填充 SPI1BUF 后设置 SPIBUSY。
- 我没有在手表窗口中看到缓冲区(SPI1BUF)的值发生变化,而在模拟中我看到了(但这可能是正常的 idk)
- 程序卡在 while 循环中,因为 SPIBUSY 永远不会被清除
所以在模拟中,它似乎工作正常,可能是因为 SPI1BUSY 位永远不会改变(就像其他状态位一样)。
对于目标,状态位似乎工作,除了永远不会被清除的 SPI1BUSY。
知道为什么会这样吗?谢谢