arielw 发表于 2011-11-22 10:07:18

L3G4200D的SPI读写问题

想用L3G4200D这款芯片获取角度信息,初次使用。。。我用C8051F310单片机,SPI接口通讯。首先想用串口看是否能正常显示,但串口接收不到数据。用示波器测试SDO是有波形产生的,但不太稳定。我想请问一下问题大概是出在了什么地方,谢谢~
//Read data in
uiTemp = 0; // storage the data received
for (i=0;i<16;i++)
{
ldi =0;       
DELAY(20);               
lck=0;
DELAY(20);               
lck=1;
DELAY(20);               
if (ldo)
   {
    uiTemp = uiTemp | 0x0001;       
   }
else
{
   uiTemp = uiTemp & 0xfffe;       
}
if (i<15)
{
   uiTemp = (uiTemp <<1)&0xfffe;
}
DELAY(20);               
}
DELAY(50);               
lcs=1;
return (uiTemp);
}
SDO输出波形ourdev_697842L1L08U.jpg(文件大小:1.58M,只有400K以内的图片才能直接显示) (原文件名:2011-11-22_09-56-02_186.jpg)
请各位指教哈~~

young-ive 发表于 2011-12-22 17:25:07

回复【楼主位】arielw
-----------------------------------------------------------------------

我使用F040模拟SPI,你的串口程序有问题吧。。。
另外,看你的时序,不知道你对datasheet中的说明怎么理解:
CS is the serial port enable and is controlled by the SPI master. It goes low at the start of the
transmission and returns to high at the end. SPC is the serial port clock and is controlled by
the SPI master. It is stopped high when CS is high (no transmission). SDI and SDO are,
respectively, the serial port data input and output. These lines are driven at the falling edge
of SPC and should be captured at the rising edge of SPC.
共同探讨!

MrLCL 发表于 2012-1-7 23:26:27

回复【楼主位】arielw
-----------------------------------------------------------------------

/*******************************************************************/
/*
/*L3G4200D读寄存器
/*
/*******************************************************************/
uchar L3G_R(uchar add)
{
        uchar k,dat=0xFF;
        add |=0x80;       //写寄存器;地址不自动增加;
        CS=0;
          for(k=0;k<8;k++)   //将寄存器的地址写入L3G4200D中
                   {
                          SCK=0;
                          if((add << k) & 0x80)
                                  {
                                        SDI = 1;
                                  }
                           else
                                  {
                                        SDI= 0;
                                  }                                                                                                                    
                           SCK=1;
                   }
                   SDI=1;
          for(k=0;k<8;k++)//将目标寄存器的数据读出来
                      {
                          SCK=0;
                          dat=dat<<1;
                          if(SDO==1)dat++;
                          SCK=1;
                   }
   CS =1;
       SDI=1;
       SDI=1;
       return(dat);       
}
/*******************************************************************/
/*
/*L3G4200D写寄存器
/*
/*******************************************************************/
void L3G_W(uchar add,uchar dat)
{
        uchar k;
        add &=0x3F;       //写寄存器;地址不自动增加;
        CS   =0;
          for(k=0;k<8;k++)   //将寄存器的地址写入L3G4200D中
                   {
                          SCK=0;
                          if((add << k) & 0x80)
                                  {
                                        SDI = 1;
                                  }
                           else
                                  {
                                        SDI= 0;
                                  }                                                                                                                    
                           SCK=1;
                   }

          for(k=0;k<8;k++)   //将配置数据写入L3G4200D寄存器中
                   {
                          SCK=0;
                          if((dat << k) & 0x80)
                                  {
                                        SDI = 1;
                                  }
                           else
                                  {
                                        SDI= 0;
                                  }                                                                                                                    
                           SCK=1;
                   }
                   SDI=1;
                  
      CS =1;
          SDI=1;
          SDO=1;       
}

试试这个,我测试过的程序;可能麻烦了点。

oner 发表于 2013-1-22 21:22:57

我现在也是遇到这个问题了。纠结中。{:mad:}
页: [1]
查看完整版本: L3G4200D的SPI读写问题