上電后讀 寄存器的值也不對(duì),讀函數(shù)如下,有什么問題嘛? u8 read_ch432data(u8 addr,u8 spi_scs) { u8 dat; addr<<=2; addr&=0xfd; start_spi(spi_scs); //選擇相應(yīng)片選 SPIx_ReadWriteByte(addr); dat=SPIx_ReadWriteByte(0xff); stop_scs(spi_scs); return dat; }
u8 SPIx_ReadWriteByte(u8 TxData) { u8 retry = 0; // while((SPI1->SR&(1<<1)) == 0) //等待發(fā)送區(qū)空 while(!(SPI1->SR & 0X0002)) { retry++; if(retry > 200) { return 0; } } SPI1->DR = TxData; //發(fā)送一個(gè)byte retry = 0; // while((SPI1->SR&1<<0) == 0) //等待接收完一個(gè)byte while(!(SPI1->SR & 0X0001)) { retry++; if(retry >200) { return 0; }
} return SPI1->DR; //返回接收到的數(shù)據(jù) }