为了节省成本,暂时采用AD7792调试系统,成功后换成AD7793。 经过2天的努力搞通了STM32F103x的SPI,关键代码如下: /* SPI1 Master */ 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_High; 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); //AD7792 Reset SPIRx1 = 0; SPIRx0 = 0; GPIO_ResetBits(GPIOA, GPIO_Pin_4); Delay(0x1000);//180ms while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); // Send SPI1 ReadId for read Status register SPI_I2S_SendData(SPI1,0xFF); SPI_I2S_SendData(SPI1,0xFF); SPI_I2S_SendData(SPI1,0xFF); SPI_I2S_SendData(SPI1,0xFF); Delay(0x1000); while( SPIRx0 != 0x80) { // Wait for SPI1 Tx buffer empty while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); // Send SPI1 ReadId for read Status register SPI_I2S_SendData(SPI1,ReadStatus); Delay(500); // SCK while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, Dummy); //Wait for SPI1 Rx buffer full while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); SPIRx0 = SPI_I2S_ReceiveData(SPI1); Delay(0x1000); }
while( (SPIRx1 & 0x0F) != 0x0A) { // Wait for SPI1 Tx buffer empty while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); // Send SPI1 ReadId for read ID register SPI_I2S_SendData(SPI1,ReadID); Delay(500); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); // Send SPI1 Dummy for SCK SPI_I2S_SendData(SPI1, 0xF0); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); SPIRx1 = SPI_I2S_ReceiveData(SPI1); Delay(0x1000); } GPIO_SetBits(GPIOA, GPIO_Pin_4);
相关链接:http://www.**.com/myspace/blog/index_89343.html |