如何使用LPC1788使用SPI传输和接收数据

时间:2013-07-15 13:37:37

标签: c microcontroller spi lpc

我正在尝试使用LPC1788微控制器编写程序,我需要使用SPI协议将数据发送和传输到UFDC(通用频率 - 数字转换器)。可以找到here的数据表。我的微控制器的SSP控制器被设置为主设备,UFDC可能成为从设备。我能够沿MOSI线传输信息,并在示波器上看到数据。但是,当我尝试在MISO线路上接收数据时,我只收到我发送给MOSI的数据。

Here is a picture of the schematic.

我写的代码的相关部分如下:

SSP_CFG_Type sspConfig;
SSP_DATA_SETUP_Type sspData;
LPC_SSP_TypeDef *SSPx = NULL;

UFDC_RESULT_T result_SSP0_FX1, result_SSP0_FX2;
UFDC_RESULT_T result_SSP1_FX1, result_SSP1_FX2;
UFDC_RESULT_T *result, *resultFX1, *resultFX2 = NULL;

uint8_t resultSign;
uint64_t resultInt, resultFract;

uint8_t SSP0resultFX1sign, SSP0resultFX2sign;
uint8_t SSP1resultFX1sign, SSP1resultFX2sign;

uint64_t SSP0resultFX1int, SSP0resultFX2int;
uint64_t SSP1resultFX1int, SSP1resultFX2int;

uint64_t SSP0resultFX1fract, SSP0resultFX2fract;
uint64_t SSP1resultFX1fract, SSP1resultFX2fract;

uint16_t getAccInstr = 0x01FF;
uint16_t setAccInstr = 0x0204;
uint16_t checkStatusInstr = 0x03FF;
uint16_t setMeasureModeInstr1 = 0x0600;
uint16_t setMeasureModeInstr2 = 0x060E;
uint16_t getBCDResultInstr = 0x07FF;
uint8_t startMeasureInstr = 0x09;

uint32_t measureInstr;

uint8_t txData[2];
uint8_t rxData[2];

uint16_t data;

sspConfig.CPHA = SSP_CPHA_FIRST;
sspConfig.CPOL = SSP_CPOL_HI;
sspConfig.ClockRate = 100000;
sspConfig.Databit = SSP_DATABIT_8;
sspConfig.Mode = SSP_MASTER_MODE;
sspConfig.FrameFormat = SSP_FRAME_SPI;

sspData.tx_data = txData;
sspData.rx_data = rxData;
sspData.length = 2;

printf("Initialising SSP0 and SSP1...\n\n");

PINSEL_ConfigPin(0, 15, 2); // SSP0_SCK
PINSEL_ConfigPin(0, 16, 2); // SSP0_SSEL
PINSEL_ConfigPin(0, 17, 2); // SSP0_MISO
PINSEL_ConfigPin(0, 18, 2); // SSP0_MOSI
PINSEL_ConfigPin(0, 6, 2); // SSP1_SCK
PINSEL_ConfigPin(0, 7, 2); // SSP1_SSEL
PINSEL_ConfigPin(0, 8, 2); // SSP1_MISO
PINSEL_ConfigPin(0, 9, 2); // SSP1_MOSI

PINSEL_SetFilter(0, 7, DISABLE);
PINSEL_SetFilter(0, 8, DISABLE);
PINSEL_SetFilter(0, 9, DISABLE);

SSP_Init(LPC_SSP0, &sspConfig);
SSP_Init(LPC_SSP1, &sspConfig);
SSP_Cmd(LPC_SSP0, ENABLE);
SSP_Cmd(LPC_SSP1, ENABLE);

printf("Reading UDFC frequency values...\n\n");
for(int i=0; i < 2; i++)
{
  if(i == 0)
  {
    SSPx = LPC_SSP0;
    resultFX1 = &result_SSP0_FX1;
    resultFX2 = &result_SSP0_FX2;
  }
  else
  {
    SSPx = LPC_SSP1;
    resultFX1 = &result_SSP1_FX1;
    resultFX2 = &result_SSP1_FX2;
  }

  // Set UFDC accuracy to 0.05%.
  SSP_SendData(SSPx, (uint8_t)(setAccInstr >> 8));
  SSP_SendData(SSPx, (uint8_t)(setAccInstr & 0xFF));

  // Check accuracy.
  while(1)
  {
    SSP_SendData(SSPx, (uint8_t)(getAccInstr >> 8));
    SSP_SendData(SSPx, (uint8_t)(getAccInstr & 0xFF));

    data = SSP_ReceiveData(SSPx);
    printf("Accuracy check 1: %i\n", data >> 8);
    printf("Accuracy check 2: %i\n", data & 0xFF);
  }
// More code follows.

我从中获得的输出如下:

Accuracy check 1: 0
Accuracy check 2: 0
Accuracy check 1: 0
Accuracy check 2: 255
Accuracy check 1: 0
Accuracy check 2: 2
Accuracy check 1: 0
Accuracy check 2: 4
Accuracy check 1: 0
Accuracy check 2: 1
Accuracy check 1: 0
Accuracy check 2: 255
Accuracy check 1: 0
Accuracy check 2: 1
Accuracy check 1: 0
Accuracy check 2: 255
Accuracy check 1: 0
Accuracy check 2: 1
Accuracy check 1: 0
Accuracy check 2: 255

编辑:可以使用SPI控制器设置“回送启用”,以便从MISO进行MOSI读取输入,反之亦然。但是,我检查了相关的寄存器,这是禁用的。

2 个答案:

答案 0 :(得分:0)

通过SPI发送一个字节后,必须再次清除数据寄存器(DR)中的一个字节。

在SPI中,您总是发送和接收一个字节您不能只发送或接收一个字节,它始终是AND。技术解释是只有一个时钟线和两条数据线(MISO和MOSI)

答案 1 :(得分:0)

我认为你应该在用这行代码读取数据之前检查接收FIFO而不是空标志:

while(!(LPC_SSP0-&gt; SR&amp; SSP_SR_RNE)); //轮询接收FIFO非空标志

在SPI初始化中,您应该使用以下代码行清除接收FIFO:

while (LPC_SSP0->SR & SSP_SR_RNE){
    tmp = (uint32_t) SSP_ReceiveData(LPC_SSP0);
}

希望它能奏效!