问题描述
我正在尝试通过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)
- 由于SPIBUSY从未被清除,因此程序陷入了while循环
因此,在仿真中,它似乎可以正常工作,可能是因为SPI1BUSY位从不改变(就像其他STATUS位一样)。
对于目标设备,状态位似乎可以正常工作,但永远不会清除的SPI1BUSY除外。
知道为什么会这样吗? 谢谢
解决方法
再次检查Pic32 SPI的FRM。在音频模式下使用时,有一些特殊情况。