This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

something wrong with SPI commucation of two stm32

Hello everyone.Recently I have had two stm32 chips to communicate by SPI,then I had a difficult problem.My goal was letting the master and the slave exchanging one byte data,just one byte.after I wrote my program into the controller,I used a logic device to observe the wave of each port.I found that the wave of master was correct,but the wave of the slave(MISO) was wrong,there was two bit wrong shift.I don't know why and how to do.I want to explain the polarity of my program and the mode of eath chip are correct!I know I can also use usart to transmit datas which is easire but I want to find out whether or not I can use SPI to exchange datas. I

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_SetBits(GPIOD,GPIO_Pin_2);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE ); 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_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); delay_init() ;

SPI1_ReadWriteByte(0xf1);

delay_ms(1000);

if(SPI1->DR==0x32) {

GPIO_ResetBits(GPIOD,GPIO_Pin_2); delay_ms(10000); GPIO_SetBits(GPIOD,GPIO_Pin_2); delay_ms(10000); GPIO_ResetBits(GPIOD,GPIO_Pin_2); delay_ms(10000); GPIO_SetBits(GPIOD,GPIO_Pin_2); delay_ms(10000); GPIO_ResetBits(GPIOD,GPIO_Pin_2); }

while(1);
what I write above is the master program,here I wrote if(SPI1->DR==0x32),cos 0x32 was the data I gave to the slave,if the communication is well-function,after the master sending 0xf1,it would have received 0x32 from the slave,then one led would have flashed twice.But when I tested it,it didn't light,so I could say the data receiveing from the slave was wrong!!!!

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE ); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; 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_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); SPI1_ReadWriteByte(0x32);
this is the slave program above.I don't want to use interrupt because just one byte transmitting does't need to use interrupt!*****!!!!!I know stm32 have some bugs,I don't know if this is another bug of stm32,so I really want to find out.Thank you for ur attention. best regards

0