如何解决读写问题的SPI问题?

时间:2019-01-28 06:23:44

标签: c stm32 spi

我正在用SPI注册一些值并读取它们。但是我写237,第一次我读230,第二次是235,但是每次都在变化。什么会导致该问题?我在下面添加了代码块。我该如何解决或如何确定问题?

我在下面添加了SPI配置和SPI senddata8并接收数据功能。 (我为CS引脚配置了另一个功能)

void enc28_writeOp(unsigned char op, unsigned char address, unsigned char data)
{
    unsigned char dat = 0;

    GPIO_ResetBits(GPIOA, GPIO_Pin_4);

    dat = (op | (address & ADDR_MASK));

    BSP_SPI1SendByte(dat);
    Delay(100);

    dat = data;

    BSP_SPI1SendByte(dat);
    Delay(100);

    GPIO_SetBits(GPIOA, GPIO_Pin_4);
}

unsigned char enc28_readOp(unsigned char oper, unsigned char addr)
{
    unsigned char dat = 0;
    GPIO_ResetBits(GPIOA, GPIO_Pin_4);
    Delay(2);

    dat = (oper | (addr & ADDR_MASK));
    BSP_SPI1SendByte(dat);
    dat = BSP_SPI1SendByte(0xFF);

    if (addr & 0x80)
    {
        dat = BSP_SPI1SendByte(0xFF);
    }

    GPIO_SetBits(GPIOA, GPIO_Pin_4);

    return dat;
}

unsigned char BSP_SPI1SendByte(unsigned char byte)
{
    uint16_t timeout;
    //It is done in order to prevent the program hesitatng.
    timeout = TIMEOUT_TIME;
    while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) & (timeout != 0))
    {
        timeout--;
    }

    SPI_SendData8(SPI1, byte);
    Delay(100);
    timeout = TIMEOUT_TIME;
    while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) & (timeout != 0))
    {
        timeout--;
    }

}

SPI配置:

#define TIMEOUT_TIME 0x2000
void BSP_ConfigSPI1(void)
{
    SPI_InitTypeDef SPI_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    // /* Enable SPI1 and GPIOA clocks */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);



    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_5); //spi
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_5); //spi
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_5); //spi

    /* SPI1 configuration */
    SPI_I2S_DeInit(SPI1);
    SPI_StructInit(&SPI_InitStructure);
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; // 72Mhz / 4 = 18Mhz --> ENC28J60 SPI OK
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_Init(SPI1, &SPI_InitStructure);
    SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);

    SPI_DataSizeConfig(SPI1, ENABLE);
    /* Enable SPI1  */
    SPI_Cmd(SPI1, ENABLE);
}
void SPI_SendData8(SPI_TypeDef *SPIx, uint8_t Data)
{
  uint32_t spixbase = 0x00;

  /* Check the parameters */
  assert_param(IS_SPI_ALL_PERIPH(SPIx));

  spixbase = (uint32_t)SPIx;
  spixbase += 0x0C;

  *(__IO uint8_t *)spixbase = Data;
}

uint8_t SPI_ReceiveData8(SPI_TypeDef *SPIx)
{
  uint32_t spixbase = 0x00;

  /* Check the parameters */
  assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));

  spixbase = (uint32_t)SPIx;
  spixbase += 0x0C;

  return *(__IO uint8_t *)spixbase;
}

0 个答案:

没有答案