操作ADS7961时遇到的一个问题
此次7961的操作的关键就在时序这里,切忌,希望以后能不要再走弯路。
附:最终代码:
uint32_t CBoard::ReadVotage(uint32_t channel) { spi_config_t spi_config; spi_config.mode = SPI_MODE_MASTER; spi_config.pin_option = SPI_4PIN_CS; spi_config.cs_active = SPI_CS_ACTIVE_LOW; spi_config.shift_dir = SPI_SHIFT_MSB; spi_config.polarity = 0; spi_config.phase = 1; spi_config.freq = 2000000; spi_config.chip_number_select = H_SPI1_AD_CSn; uint8_t sendBuf[2]; uint8_t recvBuf[2]; recvBuf[0] = 0x00; recvBuf[1] = 0x00; sendBuf[0] = (3 << 3) | ((channel & 0x0f) >> 1); sendBuf[1] = (channel & 0x01) << 7; CTimer outtime; SPI_init(SPI1,&spi_config); outtime.DelayUs(100); SPI_xfer(SPI1,sendBuf,recvBuf,2,SPI_HOLD_CLR); SPI_init(SPI1,&spi_config); //要重新初始化SPI1,不然下面收到的是0xFF,目前不知道是什么原因 outtime.DelayUs(100); SPI_xfer(SPI1,sendBuf,recvBuf,2,SPI_HOLD_CLR); SPI_init(SPI1,&spi_config); outtime.DelayUs(100); SPI_xfer(SPI1,sendBuf,recvBuf,2,SPI_HOLD_CLR);//第三次才是需要的数据 uint32_t voltage; voltage = ((recvBuf[0] & 0x0f) << 4) | ((recvBuf[1] & 0xf0) >> 4); voltage = voltage/256.0*2.5*1000*11; return voltage; }