[英]Havig trouble with SPI in PIC24FJ256GB412
I am trying to configure a PIC24FJ256GB412 to use the SPI interface with an ADC module(ADS114S08).我正在尝试将 PIC24FJ256GB412 配置为将 SPI 接口与 ADC 模块 (ADS114S08) 一起使用。 And now, I can view data by oscilloscope(by measuring the SDI pin).现在,我可以通过示波器查看数据(通过测量 SDI 引脚)。 But the SPI1BUFL didn't receive correct data(always received 0x01).但是 SPI1BUFL 没有收到正确的数据(总是收到 0x01)。 Below is the relevant code that I am using to configure the SPI.下面是我用来配置 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.如上所述,我可以通过示波器看到 SDI 引脚上的数据。 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. ADC 模块可能以正确的方式工作。
But... Let's see the MPLAB IDE side.但是...让我们看看 MPLAB IDE 方面。 enter image description here I can't get the right data(SPI1BUF = 0x0001).在此处输入图像描述我无法获得正确的数据(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----------------------------------------------- ------------------------------------------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 IDE:在此处输入图像描述
enter image description here在此处输入图像描述
The SPI should clear SPI1BUFL every time as you transmit data.每次传输数据时,SPI 都应清除 SPI1BUFL。 So I try the code like this.所以我尝试这样的代码。
uint8_t SPI_EXCH(uint8_t data)
{
SPI1BUFL = data; // write to buffer for TX
while(!SPI1STATLbits.SPIRBF); // wait for transfer to complete
return SPI1BUFL; // read the received value
}
And every time you try to do an SPI exchange, use the function above.并且每次尝试进行 SPI 交换时,请使用上面的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.