STM32 SPI传输

时间:2018-06-03 19:20:33

标签: c stm32 spi cubemx truestudio

我一直在使用STM32F103C8,尝试了一段时间来获得SPI接口。一直在使用Atollic TrueStudio。我还是新手,所以如果这是一个愚蠢的问题,我希望你能原谅我。

无论我尝试什么,我都无法在SPI接口上传输任何信息。我查看了手册,我认为我做的一切都正确,但显然没有。从我读到的,似乎是:

  • 在RCC APB2ENR中启用SPI时钟
  • 配置SPI CR1寄存器
  • 使用SPE位
  • 启用SPI
  • 通过将数据加载到SPI中来传输> DR注册
  • 通过从SPI读取数据接收> DR寄存器,因为存在与SPI-> DR
  • 链接的发送和接收缓冲器

我尝试将MOSI引脚回送到MISO引脚并进行写操作,但什么也没得到。然后我连接了逻辑分析仪,SPI时钟引脚甚至没有做任何事情。以下相关代码,如果有人可以提供帮助,那就太棒了:

void PrintStrToUART(char str[])
{
char *pointertostr;
for (pointertostr = &str[0]; *pointertostr != '\0'; pointertostr++){
    USART1 -> DR = (*pointertostr & USART_DR_DR);
    while ((USART1 -> SR & USART_SR_TXE) == 0){
        ; 
        }
    }
}

void PrintCharArrayToUART(unsigned char str[], int arraysize){
int j;
unsigned char buffer[((5*arraysize)-2)];
unsigned char *positionpointer = &buffer[0];

for(j=0; j <= (arraysize-1); j++){
    if(j){
        positionpointer += sprintf(positionpointer, ", ");
    }
    positionpointer += sprintf(positionpointer, "%d", str[j]);
}

int newarrayend;
newarrayend = (positionpointer - &buffer[0]);

PrintStrToUART("[");
for(j = 0; j <= newarrayend; j++){ 
    USART1 -> DR = (buffer[j] & USART_DR_DR);
    while ((USART1 -> SR & USART_SR_TXE) == 0){
        ;
    }
}
PrintStrToUART("]\n\r");
}

void SelfSPIInit(void){ //because the Mx one is not working
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; //Enable SPI clock

//Set baud prescaler
SPI1->CR1 = SPI_CR1_BR; //Slowest SPI I can have

//CPHA CPOL
SPI1-> CR1 &= ~(SPI_CR1_CPOL | SPI_CR1_CPHA);

//8 bit data format
SPI1->CR1 &= ~(SPI_CR1_DFF);

//Full duplex mode
SPI1->CR1 &= ~(SPI_CR1_BIDIMODE);
SPI1->CR1 &= ~(SPI_CR1_RXONLY);

//MSB first
SPI1->CR1 &= ~(SPI_CR1_LSBFIRST);

//Master mode
SPI1->CR1 |= SPI_CR1_MSTR;

//Software NSS mode off
SPI1->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI;

//Enable
SPI1->CR1 |= SPI_CR1_SPE;

//Enable the SPI GPIO pins

}

int main(void)
{
int i;
HAL_Init();

/* Configure the system clock */
SystemClock_Config(); //config to run at the correct speed

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
MX_TIM1_Init();
MX_USART1_UART_Init();
MX_TIM2_Init();
MX_CRC_Init();
MX_USART2_UART_Init();
MX_ADC1_Init();
MX_TIM3_Init();
//MX_SPI1_Init();
SelfSPIInit();

TIM1 -> CR1 |= TIM_CR1_CEN;
TIM2 -> CR1 |= TIM_CR1_CEN;
I2C1 -> CR1 |= I2C_CR1_PE; //peripheral enable
SPI1 -> CR1 |= SPI_CR1_SPE;

unsigned char newchararray[21] = {0x41};
unsigned char *pnewchararray = &newchararray[0];

unsigned char recarray[7] = {127};
unsigned char *precarray = &recarray[0];

while (1)
{

  GPIOC -> BSRR |= GPIO_BSRR_BR13;
  GPIOA -> BSRR |= GPIO_BSRR_BR6;
  MilliSecondDelay(500);
  GPIOC -> BSRR |= GPIO_BSRR_BS13;
  GPIOA -> BSRR |= GPIO_BSRR_BS6;
  MilliSecondDelay(500);
  SPI1->DR = *pnewchararray;
  while((SPI1->SR & SPI_SR_TXE) == 0){
      //while transmit buffer not empty, wait
      ;
  }
  while((SPI1->SR & SPI_SR_RXNE) == 0){
      ;//while receive buffer not empty
  }
  *precarray = SPI1->DR;
  PrintCharArrayToUART(precarray, 7);
 }
}

对于缩进感到抱歉,因为我说我还是新手,但这是编译和运行的代码。感谢任何帮助,谢谢!

编辑:不确定我是否应该包含USART功能,这只是打印到我的笔记本电脑,所以我可以看到SPI数据寄存器有什么。但我想如果把它包括在内,程序会更有意义。

2 个答案:

答案 0 :(得分:1)

请查看http://www.handsonembedded.com/stm32f103-spl-tutorial-5/。 stm32f103有一个完整的spi示例。

答案 1 :(得分:0)

另一个是:看起来你忘记了引脚多路复用。