havig在PIC24FJ256GB412中与SPI的麻烦
我正在尝试配置PIC24FJ256GB412将SPI接口与ADC模块(ADS114S08)一起使用。现在,我可以通过示波器查看数据(通过测量SDI引脚)。但是SPI1BUFL没有收到正确的数据(始终接收到0x01)。以下是我用来配置SPI的相关代码。
uint8_t DATA_S[1000]; // SPI data buffer for Receiving
uint8_t DATA1; // SPI data buffer for Receiving
uint16_t DATA_counter=0;
void System_Initial(void)
{
//Setup IO
TRISDbits.TRISD0 = 0; //SCLK
TRISDbits.TRISD5 = 0; //ADC RESET
TRISDbits.TRISD10 = 0; //ADC START
TRISDbits.TRISD11 = 1; //ADC DRDY
TRISFbits.TRISF3 = 1; //SDI
TRISFbits.TRISF4 = 0; //SDO
ANSELDbits.ANSELD0 = 0; //Digital pins
ANSELDbits.ANSELD5 = 0;
ANSELDbits.ANSELD10 = 0;
ANSELDbits.ANSELD11 = 0;
ANSELFbits.ANSELF3 = 0;
ANSELFbits.ANSELF4 = 0;
//Setup SPI
//MCU operates in 16MHz, ADC internal clk 4MHz
SPI1BRGL = 1; //Baud rate = 4MHz
SPI1CON1L = 0x8020; //8bit mode //SCLK enable //Master Mode
SPI1CON1H = 0x2000; //AUDEN=0
SPI1CON2L = 0x0007; //8bit data
//Setup PPS
__builtin_write_OSCCONL(OSCCON & 0xBF);
_RP11R = _RPOUT_SCK1OUT; // RP11 -> SCK1.
RPINR20bits.SDI1R = 16; // RP16 -> SDI1.
_RP10R = _RPOUT_SDO1; // RP10 -> SDO1.
__builtin_write_OSCCONL(OSCCON | 0x40);
}
//SPI1 - Set ADS114S08
void SPI1_SetADC()
{
//set PGA
//Input MUX Register, address = 02h
//Gain setting Register, address = 03h
SPI1BUFL = 0x42; //WREG at 02h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //02h //AINP = AIN0, AINN = AIN1
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x0B; //03h //PGA enabled, Gain = 011 =8
while( SPI1STATLbits.SPIBUSY==1);
//set Mode
//Data Rate Register, address = 04h
//Reference Control Register, address = 05h
SPI1BUFL = 0x44; //WREG at 04h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x14; //04h //Continuous conversion mode //0100 : 20 SPS
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x30; //05h
while( SPI1STATLbits.SPIBUSY==1);
//set Excitation Current Sources
//Excitation Current Register1(IDACMAG), address = 06h
//Excitation Current Register2(IDACMUX), address = 07h
SPI1BUFL = 0x46; //WREG at 06h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x05; //06h //IDAC = 0101 = 500uA
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0xF0; //07h //use IDAC1 => AIN0
while( SPI1STATLbits.SPIBUSY==1);
}
//SPI1 - RREG
void SPI1_RREG()
{
SPI1BUFL = 0x20; //send RREG Command //read 00h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x00; //1 byte
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x0000; //send DUMMY data
while( SPI1STATLbits.SPIBUSY==1); //Wait Data
uint8_t register1= SPI1BUFL; //get data
DATA1 = register1;
DATA_S[DATA_counter%1000] = register1;
}
int main(void)
{
System_Initial();
LATDbits.LATD10 = 0; //ADC START=0
LATDbits.LATD5 = 0; //ADC RESET=0 //Enter RESET Status
uint32_t loop = 0;
loop = 4*4; //MCU 16Mhz, ADC internal clk 4Mhz
while (loop--); //wait 4clk => ADS114S08 tw(RSL)
LATDbits.LATD5 = 1; //ADC RESET low -> high //Enter Standby Mode
loop=4096*4;
while (loop--); //wait 4096clk => ADS114S08 td(RSSC)
SPI1_SetADC(); //set ADC Configuration
LATDbits.LATD10 = 1; //START high -> Start Conversion
//main while
while(1)
{
DATA_counter++;
SPI1_RREG();
}
}
粗体 斜体
如上所述,我可以通过示波器看到有关SDI PIN的数据。 在此处输入图像描述 和另一个数据测试。 在此处输入图像描述
在示波器上可以看到的波形。 ADC模块可能以正确的方式工作。
但是...让我们看看Mplab Ide方面。 在此处输入图像描述 我无法获得正确的数据(SPI1BUF = 0x0001)。我试过较低的福音。但是结果没有什么不同。我缺少有参数吗?感谢您的帮助。
--------------------------------------------------------------------------------------- -----------------------------------------------------------
嗨,克雷格,我尝试了代码。
SPI1BUFL = 0x20; //send RREG Command //read 00h
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty
SPI1BUFL = 0x00; //set 1 byte
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty
uint8_t register1=0;
SPI1BUFL = 0x00; //send DUMMY data
while(!SPI1STATLbits.SPIRBF); //hang until data is received
register1 = SPI1BUFL; //get data
结果没有差异。我录制了。
示波器: 在此处输入图像描述
I am trying to configure a PIC24FJ256GB412 to use the SPI interface with an ADC module(ADS114S08). And now, I can view data by oscilloscope(by measuring the SDI pin). But the SPI1BUFL didn't receive correct data(always received 0x01). Below is the relevant code that I am using to configure the SPI.
uint8_t DATA_S[1000]; // SPI data buffer for Receiving
uint8_t DATA1; // SPI data buffer for Receiving
uint16_t DATA_counter=0;
void System_Initial(void)
{
//Setup IO
TRISDbits.TRISD0 = 0; //SCLK
TRISDbits.TRISD5 = 0; //ADC RESET
TRISDbits.TRISD10 = 0; //ADC START
TRISDbits.TRISD11 = 1; //ADC DRDY
TRISFbits.TRISF3 = 1; //SDI
TRISFbits.TRISF4 = 0; //SDO
ANSELDbits.ANSELD0 = 0; //Digital pins
ANSELDbits.ANSELD5 = 0;
ANSELDbits.ANSELD10 = 0;
ANSELDbits.ANSELD11 = 0;
ANSELFbits.ANSELF3 = 0;
ANSELFbits.ANSELF4 = 0;
//Setup SPI
//MCU operates in 16MHz, ADC internal clk 4MHz
SPI1BRGL = 1; //Baud rate = 4MHz
SPI1CON1L = 0x8020; //8bit mode //SCLK enable //Master Mode
SPI1CON1H = 0x2000; //AUDEN=0
SPI1CON2L = 0x0007; //8bit data
//Setup PPS
__builtin_write_OSCCONL(OSCCON & 0xBF);
_RP11R = _RPOUT_SCK1OUT; // RP11 -> SCK1.
RPINR20bits.SDI1R = 16; // RP16 -> SDI1.
_RP10R = _RPOUT_SDO1; // RP10 -> SDO1.
__builtin_write_OSCCONL(OSCCON | 0x40);
}
//SPI1 - Set ADS114S08
void SPI1_SetADC()
{
//set PGA
//Input MUX Register, address = 02h
//Gain setting Register, address = 03h
SPI1BUFL = 0x42; //WREG at 02h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //02h //AINP = AIN0, AINN = AIN1
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x0B; //03h //PGA enabled, Gain = 011 =8
while( SPI1STATLbits.SPIBUSY==1);
//set Mode
//Data Rate Register, address = 04h
//Reference Control Register, address = 05h
SPI1BUFL = 0x44; //WREG at 04h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x14; //04h //Continuous conversion mode //0100 : 20 SPS
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x30; //05h
while( SPI1STATLbits.SPIBUSY==1);
//set Excitation Current Sources
//Excitation Current Register1(IDACMAG), address = 06h
//Excitation Current Register2(IDACMUX), address = 07h
SPI1BUFL = 0x46; //WREG at 06h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x05; //06h //IDAC = 0101 = 500uA
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0xF0; //07h //use IDAC1 => AIN0
while( SPI1STATLbits.SPIBUSY==1);
}
//SPI1 - RREG
void SPI1_RREG()
{
SPI1BUFL = 0x20; //send RREG Command //read 00h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x00; //1 byte
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x0000; //send DUMMY data
while( SPI1STATLbits.SPIBUSY==1); //Wait Data
uint8_t register1= SPI1BUFL; //get data
DATA1 = register1;
DATA_S[DATA_counter%1000] = register1;
}
int main(void)
{
System_Initial();
LATDbits.LATD10 = 0; //ADC START=0
LATDbits.LATD5 = 0; //ADC RESET=0 //Enter RESET Status
uint32_t loop = 0;
loop = 4*4; //MCU 16Mhz, ADC internal clk 4Mhz
while (loop--); //wait 4clk => ADS114S08 tw(RSL)
LATDbits.LATD5 = 1; //ADC RESET low -> high //Enter Standby Mode
loop=4096*4;
while (loop--); //wait 4096clk => ADS114S08 td(RSSC)
SPI1_SetADC(); //set ADC Configuration
LATDbits.LATD10 = 1; //START high -> Start Conversion
//main while
while(1)
{
DATA_counter++;
SPI1_RREG();
}
}
bold
italic
As mentioned above, I can see data on SDI pin by oscilloscope.
enter image description here
And another data test.
enter image description here
By the waveform that we can see on the oscilloscope. The ADC module probably works the right way.
But... Let's see the MPLAB IDE side.
enter image description here
I can't get the right data(SPI1BUF = 0x0001). I have tried changing baudrate lower. But the result is no different. Is there a parameter I'm missing? Thanks for your help.
------------------------------------------0530-----------------------------------------------
Hi Craig, I have tried the code.
SPI1BUFL = 0x20; //send RREG Command //read 00h
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty
SPI1BUFL = 0x00; //set 1 byte
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty
uint8_t register1=0;
SPI1BUFL = 0x00; //send DUMMY data
while(!SPI1STATLbits.SPIRBF); //hang until data is received
register1 = SPI1BUFL; //get data
The results were no difference. And I recorded it.
Oscilloscope:
enter image description here
IDE:
enter image description here
enter image description here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
传输数据时,SPI每次都应清除SPI1BUFL。
因此,我尝试这样的代码。
每当您尝试进行SPI交换时,都会使用上面的功能。
The SPI should clear SPI1BUFL every time as you transmit data.
So I try the code like this.
And every time you try to do an SPI exchange, use the function above.