[求助] stm32 使用SPI读取AD5422

490353119   2014-8-21 10:33 楼主
STM32 使用IO模拟读取AD5422完全没问题,但是使用SPI读取不行:
SPI_I2S_DeInit(SPI_Master1);
  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_1Edge;
  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(SPI_Master1, &SPI_InitStructure);

u8 SPI_Master_RW(u8 date)
{
u8 retry=0;
while(SPI_I2S_GetFlagStatus(SPI_Master1,SPI_I2S_FLAG_TXE)==RESET)
{
retry++;
if(retry>200)
return 0;
}

SPI_I2S_SendData(SPI_Master1,date);
while(SPI_I2S_GetFlagStatus(SPI_Master1,SPI_I2S_FLAG_RXNE)==RESET)
{
retry++;
if(retry>200)
return 0;
}
return SPI_I2S_ReceiveData(SPI_Master1);
}
void AD5422_Read(u8 num,u8 *pbuf)
{
u8 i=0;
Select_DAC1();
for(i=num;i>0;i--)
{
*(pbuf+i-1)=SPI_Master_RW(AD5422_NOP);
}
NotSelect_DAC1();
}

void AD5422_Write(u8 num,u8 *pbuf)
{
u8 i=0;
Select_DAC1();
for(i=num;i>0;i--)
{
SPI_Master_RW(*(pbuf+i-1));
}
NotSelect_DAC1();
}

SPI四种模式都试过了,示波器有波形输出(CLK,CS,MOSI波形都有),但是AD5422完全没反应。也不能读取数据。这是为什么?难道只能IO模拟不能使用STM32的SPI?

回复评论 (4)

注意SPI模式,一共有4种,看看你的芯片支持哪一种。
我的博客
点赞  2014-8-21 10:56
不能只配置 SPI 外设这部分的参数,在这之前要配置 SPI 所用的4个管脚的 GPIO 的工作模式。
点赞  2014-8-21 11:00
AD5422时序图 :使用
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
void SPI_Master_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
//  DMA_InitTypeDef DMA_InitStructure;
  SPI_InitTypeDef  SPI_InitStructure;
  /* Peripheral Clock Enable -------------------------------------------------*/
  /* Enable the SPI clock */
  SPI_Master1_CLK_INIT(SPI_Master1_CLK, ENABLE);
  
  /* Enable GPIO clocks */
  RCC_AHB1PeriphClockCmd(SPI_Master1_SCK_GPIO_CLK | SPI_Master1_MISO_GPIO_CLK | SPI_Master1_MOSI_GPIO_CLK, ENABLE);
  
//  /* Enable DMA clock */
//  RCC_AHB1PeriphClockCmd(SPI_Master1_DMA_CLK, ENABLE);

  /* SPI GPIO Configuration --------------------------------------------------*/
  /* GPIO Deinitialisation */
  GPIO_DeInit(SPI_Master1_SCK_GPIO_PORT);
  GPIO_DeInit(SPI_Master1_MISO_GPIO_PORT);
  GPIO_DeInit(SPI_Master1_MOSI_GPIO_PORT);
  
  /* Connect SPI pins to AF5 */  
  GPIO_PinAFConfig(SPI_Master1_SCK_GPIO_PORT, SPI_Master1_SCK_SOURCE, SPI_Master1_SCK_AF);
  GPIO_PinAFConfig(SPI_Master1_MISO_GPIO_PORT, SPI_Master1_MISO_SOURCE, SPI_Master1_MISO_AF);   
  GPIO_PinAFConfig(SPI_Master1_MOSI_GPIO_PORT, SPI_Master1_MOSI_SOURCE, SPI_Master1_MOSI_AF);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

  /* SPI SCK pin configuration */
  GPIO_InitStructure.GPIO_Pin = SPI_Master1_SCK_PIN;
  GPIO_Init(SPI_Master1_SCK_GPIO_PORT, &GPIO_InitStructure);
  
  /* SPI  MISO pin configuration */
  GPIO_InitStructure.GPIO_Pin =  SPI_Master1_MISO_PIN;
  GPIO_Init(SPI_Master1_MISO_GPIO_PORT, &GPIO_InitStructure);  

  /* SPI  MOSI pin configuration */
  GPIO_InitStructure.GPIO_Pin =  SPI_Master1_MOSI_PIN;
  GPIO_Init(SPI_Master1_MOSI_GPIO_PORT, &GPIO_InitStructure);

  /* SPI configuration -------------------------------------------------------*/
  SPI_I2S_DeInit(SPI_Master1);
  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_1Edge;
  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(SPI_Master1, &SPI_InitStructure);

SPI_Cmd(SPI_Master1, ENABLE);

}
  • QQ图片20140821140306.jpg
点赞  2014-8-21 14:06

麻烦问一下, 读取寄存器的值正确,  但是无电压输出什么原因啊

点赞  2019-6-13 17:33
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复