OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 8817|回复: 6

STM32通过SPI1与AD7190通信问题求助

[复制链接]

1

主题

3

帖子

0

精华

新手入门

积分
27
金钱
27
注册时间
2014-9-29
在线时间
0 小时
发表于 2014-11-19 21:33:23 | 显示全部楼层 |阅读模式
5金钱
PA7连接  AD7190的DIN引脚
PA8连接AD7190的CS
PA5 连接AD7190的SCK
PA6连接AD7190的DOUT
SPI写入正常,但是读出是数据整体向右移了一位,如AD7190配置寄存器实际值0,80,88,串口输出的数据为128,40,44。补进的最高为DOUT的值
AD7190的时序图




int main(void)
{  

  SystemInit();
  NVIC_Configuration(); 
  bsp_InitUart(); //配置串口
  bsp_InitLed();   //配置LED
  bsp_InitButton();  //配置GPIO

  AD7190_Init() ;
  while(1)
  {
  Start_AD7190();
  }

}


void  AD7190_Init(void)
{  
      GPIO_Configuration();

      SPI_Tx(); //设置flag_spi的初始值
      CLR_CS; //工作在三线模式
      SPI_SendByte(T,RESET);
      SPI_SendByte(T,RESET);
      SPI_SendByte(T,RESET);
      SPI_SendByte(T,RESET);
      SPI_SendByte(T,RESET);
      Delay(50);  //复位后,至少等待500us

      SPI_SendByte(T,STATUS_REG|READ);
      buf[0]=SPI_SendByte(R,DUMMY);
      //_______写配置,斩波禁用,选用外部基准电压,单极性模式_______
      //_______使能缓冲器,电压增益1(输入范围 +5v),选择AIN1,AINCOM通道______
     SPI_SendByte(T,CONFIGURATION_REG|WRITE);  
     SPI_SendByte(T,REF1);

    SPI_SendByte(T,AIN2|AIN3);
    SPI_SendByte(T,GAIN_1_5|SINGLE_POLARITY);
    SPI_SendByte(T,CONFIGURATION_REG|READ);

    buf[2]=SPI_SendByte(R,DUMMY);
    buf[1]=SPI_SendByte(R,DUMMY);
    buf[0]=SPI_SendByte(R,DUMMY);
    printf("配置寄存器 %d  \r\n", buf[2]);
    printf(" %d  \r\n", buf[1]);
    printf(" %d \r\n", buf[0]);


   SPI_SendByte(T,CONFIGURATION_REG|WRITE);  
   SPI_SendByte(T,REF1);

   SPI_SendByte(T,AIN2|AIN3);
   SPI_SendByte(T,GAIN_1_5|SINGLE_POLARITY);
   SPI_SendByte(T,CONFIGURATION_REG|READ);

    buf[2]=SPI_SendByte(R,DUMMY);
    buf[1]=SPI_SendByte(R,DUMMY);
    buf[0]=SPI_SendByte(R,DUMMY);
         printf("配置寄存器 %d  \r\n", buf[2]);
          printf(" %d  \r\n", buf[1]);
        printf(" %d \r\n", buf[0]);

      SPI_SendByte(T,STATUS_REG|CONTINUOUS_READ);
     buf[0]=SPI_SendByte(R,DUMMY);
     //_______写模式,启动连续转换,输出数据速率为50Hz______
     //_______启用内部4.92MHz晶振_______
        SPI_SendByte(T,MODE_REG|WRITE);  
      SPI_SendByte(T,CONTINUOUS_CONVERT|IN_OSC);
    SPI_SendByte(T,0x00);
     SPI_SendByte(T,SPEED_1600);


        SPI_SendByte(T,MODE_REG|READ);
      buf[2]=SPI_SendByte(R,DUMMY);
      buf[1]=SPI_SendByte(R,DUMMY);
       buf[0]=SPI_SendByte(R,DUMMY);
      printf("模式寄存器 %d  \r\n", buf[2]);
        printf("%d  \r\n", buf[1]);
     printf("%d  \r\n", buf[0]);
 }



u8 SPI_SendByte(unsigned char flag,u8 byte) //因为时钟信号只有在发的时候才产生,所以收数据的时候必须发数据(无效的数据)
{
       if(flag==T_flag)
       {
              if(flag_spi==Tx)
              {
                 while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);
                 SPI_I2S_SendData(SPIy, byte);
                 return 0;
             }
             else//SPI由接收状态转成发送状态
            {
              SPI_Tx();
              while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);
              SPI_I2S_SendData(SPIy, byte);
              return 0;
               }
         }
        if(flag==R_flag)
        {
                   if(flag_spi==Full)
                 {
                     while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);

                        SPI_I2S_SendData(SPIy, byte);
                     while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET);
 
                   return SPI_I2S_ReceiveData(SPIy);
                }
                else//SPI由发送状态转成接收状态
       {
                SPI_Full();
                while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);
                SPI_I2S_SendData(SPIy, byte);
                 buf[4]=SPI_I2S_ReceiveData(SPIy);
                while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);

                 SPI_I2S_SendData(SPIy, byte);
                while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET);
               return SPI_I2S_ReceiveData(SPIy);
           }
      }
}

vu32 Read_Data_From_AD7190(unsigned char n)
{
          vu32 buf_dat,buf_dat1,buf_dat2,buf_dat3;
            if(n==1)//处理一个字节
              {
           buf_dat=SPI_SendByte(R,DUMMY);
            return buf_dat;
           }
          else if(n==4)//处理4个字节
             {
             buf[3]=SPI_SendByte(R,DUMMY);
           buf[2]=SPI_SendByte(R,DUMMY);
            buf[1]=SPI_SendByte(R,DUMMY);
             buf[0]=SPI_SendByte(R,DUMMY);

          printf("shuju %d  \r\n",buf[3]);
             printf("%d  \r\n", buf[2]);
           printf("%d  \r\n", buf[1]);
           printf("%d  \r\n", buf[0]);
           printf("%d  \r\n", buf[4]);
              buf_dat3=buf[3]<<24;
             buf_dat2=buf[2]<<16;
                buf_dat1=buf[1]<<8;
             buf_dat=buf_dat3+buf_dat2+buf_dat1+buf[0];
              return buf_dat;
        }
}


void Start_AD7190(void)
{
         uint32_t i;
          SPI_SendByte(T,DATA_REG|CONTINUOUS_READ);
           if(WAIT_AD7190==0)
          {
             for(i=0;i<310;i++)
            {
//      printf("等待 \r\n");
 
 /* Check the parameters */
   
             while(WAIT_AD7190);

              SPI_SendByte(T,0x58);

               buffer=Read_Data_From_AD7190(4);


            } 
         }  
}

void SPI_Full(void)
{

SPI_InitTypeDef   SPI_InitStructure;
/* SPI2 configuration ------------------------------------------------------*/
SPI_Cmd(SPIy, DISABLE); 
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_8; 
SPI_InitStructure.SPI_FirstBit =  SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIy , &SPI_InitStructure);

SPI_Cmd(SPIy, ENABLE);
flag_spi=Full;

}
void SPI_Tx(void)
{
SPI_InitTypeDef   SPI_InitStructure;
/* SPI2 configuration ------------------------------------------------------*/
SPI_Cmd(SPIy, DISABLE); 
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
//SPI_BiDirectionalLineConfig(SPI_Direction_Tx);
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_8; 
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIy , &SPI_InitStructure);
SPI_Cmd(SPIy, ENABLE);
flag_spi=Tx;
}




正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2014-11-20 00:16:47 | 显示全部楼层
回复

使用道具 举报

1

主题

3

帖子

0

精华

新手入门

积分
27
金钱
27
注册时间
2014-9-29
在线时间
0 小时
 楼主| 发表于 2014-11-21 09:17:58 | 显示全部楼层
回复【2楼】正点原子:
---------------------------------
原子哥,SPI全双工通信时前8个时钟只发送不接收,后8个时钟只接收不发送,而AD7190是接收到时钟根据写入通信寄存器的指令进行发送和接收的。
回复

使用道具 举报

15

主题

37

帖子

0

精华

初级会员

Rank: 2

积分
178
金钱
178
注册时间
2013-9-24
在线时间
20 小时
发表于 2014-11-22 17:12:54 | 显示全部楼层
你上升沿读数试试、这一位的偏移是片子的时序原因啊。就最后一位,不影响的。1/65535、、
回复

使用道具 举报

1

主题

3

帖子

0

精华

新手入门

积分
27
金钱
27
注册时间
2014-9-29
在线时间
0 小时
 楼主| 发表于 2014-12-3 09:45:30 | 显示全部楼层
回复【4楼】kqh1120:
---------------------------------
4种读取方式都试了,还是不行。
回复

使用道具 举报

0

主题

1

帖子

0

精华

新手上路

积分
20
金钱
20
注册时间
2016-3-25
在线时间
4 小时
发表于 2016-10-28 10:19:46 | 显示全部楼层
您好!您的问题解决了吗?我也遇到了相同的问题,能否告知是如何解决的,谢谢!
回复

使用道具 举报

16

主题

147

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
7140
金钱
7140
注册时间
2015-1-23
在线时间
106 小时
发表于 2018-9-27 09:47:45 | 显示全部楼层
  读出的数据右移一位  是因为SPI 配置有问题   注意 CPOL=1  CPHA=1

SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;

  SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-20 06:13

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表