打印

我正在用adxl345,是IIc,示波器上证指数有应答,但信号始终ff

[复制链接]
2014|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
fenglongxu8|  楼主 | 2011-1-26 12:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCL=p1^1;
sbit SDA=p1^2;
uchar buf[20];
void Delayus(unsigned char us)//delay 1 us
{
unsigned char uscnt;
uscnt=us;            
while(--uscnt);
}
void I2CStart(void)
      {
      SDA=1;Delayus(100);
      SCL=1;Delayus(100);
      SDA=0;Delayus(100);
      SCL=0;Delayus(100);//数据线保持高,时钟线从高到低一次跳变,I2C通信开始
      }
void I2CStop(void)
      {
       Delayus(100);SDA=0;
    Delayus(100);SCL=1;
    Delayus(100); SDA=1;//数据线保持低,时钟线从低到高一次跳变,I2C通信停止
   }
  void ack()   //发送应答信号
  {
  Delayus(100);
  SDA=0;
  Delayus(100);
  SCL=1;
  Delayus(100);
  SCL=0;
  Delayus(100);
}
void noack()//发送非应答信号
  {
  Delayus(100);
  SDA=1
  Delayus(100);
  SCL=1;
  Delayus(100);
  SCL=0;
  Delayus(100);
}
uchar read_8bit() //读1字节
{
uchar  j=0;
uchar  i=8;
SDA=1;
while(i--)
{
  j<<=1;
  Delayus(100);
  SCL=1;
  if(SDA)
  {
   j|=0x01;
   }
   Delayus(100);
   SCL=0;
  }
return(j);
}
void write_8bit(uchar ch) //写1字节
{
  uchar data i=8;
  while(i--)
  {
   Delayus(100);
   SDA=(bit)(ch&0x80);
   ch= ch<<1;
   Delay1us(100);
   SCL=1;
   Delay1us(100);
   SCL=0;
   Delayus(100);
   }
   SDA=1;
   Delayus(100);
   SCL=1;
   Delayus(100);
   SCL=0;
   Delayus(100);
   }
uchar read_byte(unsigned int address) //读指定地址字节
   {
   uchar  x;
   I2CStart();
   write_8bit(0XA6);
   Delayus(5);
   write_8bit(address);
   Delayus(5);
   I2CStart();
   write_8bit(0XA7);
   Delayus(5);
   x=read_8bit();
   Delayus(5);
   noack();
   Delayus(5);
   I2CStop();
   return(x);
  }
    void  init() //系统初始化
   {
    SCON=0x50;//允许串口接受数据,工作方式为1,无奇偶校验位
    TMOD=0x20;//定时器1,定时方式2
    TL1=0xfd; //计算定时器1的初值,装载TL1,TH1
       TH1=0Xfd;//允许串口接受数据,工作方式为1,无奇偶校验位
    PCON=0x00;//串口波特率不加倍(9600)
    TR1=1; //启动定时器1计数,TR1是TCON.6
    }
     void main()
    {
    init();
       while(1)
    {
          for(i=0;i<6;i++)//把读到的数据发送出去
        {
         buf[0]=read_byte(0x32+i);
         Delayus(5);
         SBUF=buf[0];
         while(TI==0);
          TI=0;
         for(j=0;j<1000;j++);
         }
     }
  }

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

3

帖子

0

粉丝