|
目前这样测试:主机发数据给从机stm32时,从机stm32总回复0xa5.
但很多时候从机发的并不是0xa5,可能是其他数据。
例如主机可能收到以下错误的数据:
[插入] 我使用示波器确认过从机发出的的确是这样的数据
- #include "stm32f10x_lib.h"
void spi_to_arm11_clock(void) { GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOB, &GPIO_InitStructure); }
void spi_to_arm11_cfg(void) { SPI_InitTypeDef SPI_InitStructure; SPI_Cmd(SPI2, DISABLE);//必须先禁能,才能改变MODE
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure);
SPI_NSSInternalSoftwareConfig(SPI2, SPI_NSSInternalSoft_Reset);//设置内部NSS为低电平
SPI_Cmd(SPI2, ENABLE); }
int spi_to_arm11_read8(u8 * value) { if (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) != RESET) { *value = SPI_I2S_ReceiveData(SPI2); return 0; }
return -1; }
int spi_to_arm11_write8(u8 value) { while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI2, value); return 0; return -1; }
int main() { ............... ............... while(1) spi_to_arm11_write8(0xa5); }
复制代码
|
|