亿加合和智能车制作

标题: xs128IIC功能函数哪里有问题啊,无法读取数据~ [打印本页]

作者: 夏日之恋    时间: 2015-4-8 16:42
标题: xs128IIC功能函数哪里有问题啊,无法读取数据~
#include <hidef.h>
#include "MC9S12XS128.h"
#include "derivative.h"
#include "iic.h"

#define   uchar unsigned char
#define   uint unsigned int

uchar IIC_ERROR=0;/*应答标志位*/
// uint reg,Data;

#define SDA PORTA_PA6
#define SCL PORTA_PA7

void IIC_Init()
{
  DDRA=0XFF;
}

/**************************************
延时n微秒
**************************************/
void Delay5us(int c)
{
int cnt;
for(cnt=0;cnt<c;cnt++)
{
_asm NOP;_asm NOP;_asm NOP;
_asm NOP;_asm NOP;_asm NOP;
_asm NOP;_asm NOP;_asm NOP;
_asm NOP;_asm NOP;_asm NOP;

_asm NOP;_asm NOP;
_asm NOP;_asm NOP;_asm NOP;
_asm NOP;_asm NOP;_asm NOP;
}

}
/**************************************
延时5毫秒
**************************************/
void Delay5ms(void)
{
   uint n;
   n = 560;
  while (n--);
  }

/**************************************
起始信号
**************************************/
void IIC_Start()
{
                        //拉高数据线

    SCL = 1;
    SDA = 1;                    //拉高时钟线
    Delay5us(5);                 //延时
    SDA = 0;                    //产生下降沿
    Delay5us(5);                 //延时
    SCL = 0;                    //拉低时钟线
    Delay5us(2);                    
}
/**************************************
停止信号
**************************************/
void IIC_Stop()
{
                       //拉低数据线
    SCL = 1;
    SDA = 0;                     //拉高时钟线
    Delay5us(5);                 //延时
    SDA = 1;                    //产生上升沿
    Delay5us(4);                 //延时
}

/**************************************
发送应答信号
入口参数:ack (0:ACK 1:NAK)
**************************************/
void IIC_SendACK(byte ack)
{
    SDA = ack;                  //写应答信号
    Delay5us(1);
    SCL = 1;                    //拉高时钟线
    Delay5us(5);                 //延时
    SCL = 0;                    //拉低时钟线
    Delay5us(2);                 //延时
}

/**************************************
接收应答信号
**************************************/
void IIC_RecvACK()
{
    Delay5us(3);
    SCL = 0;                    //拉高时钟线
    Delay5us(3);
    DDRA_DDRA6=0;                 //延时
    SDA=1;
    Delay5us(3);                   //读应答信号
    SCL = 1;                    //拉低时钟线
    Delay5us(5);                 //延时
    if(SDA==1)
      IIC_ERROR=1;
    else
      IIC_ERROR=0;
    SCL=0;
    Delay5us(3);
    DDRA_DDRA6=1;


}

/**************************************
向IIC总线发送一个字节数据
**************************************/
void IIC_SendByte(uchar Data)
{
    uint i=8;
    uchar m=Data;
    uchar temp;
    for(i=8;i>0;i--)
    {
      temp=m&0x80;
      if(temp==0x80)
        SDA=1;
      else
        SDA=0;
      Delay5us(5);
      SCL=1;
      Delay5us(5);
      SCL=0;
      m=m<<1;
    }
}

/**************************************
从IIC总线接收一个字节数据
**************************************/
uchar IIC_RecvByte()
{
    int i;
    uchar j = 0;
    DDRA_DDRA6=0;
    SDA = 1;                    //使能内部上拉,准备读取数据,
    for (i=8; i>0; i--)         //8位计数器
    {
        SDA=1;
        Delay5us(3);
        SCL = 1;                //拉高时钟线
        Delay5us(5);
        if(SDA==1)             //延时
          j=j|0x01;
        else j=j;//读数据               
          SCL = 0;                //拉低时钟线
        if(i!=1)
         {
          j=j<<1;
         }
          //延时
    }
    DDRA_DDRA6=1;
    return j;
}

/*****************************************/
void i2c_write_reg(uint L3G4200_ADRESS,uint reg,uint Data)  //L3G4200 写寄存器
{

    IIC_Start();                  //起始信号
    IIC_SendByte(L3G4200_ADRESS);   //发送设备地址+写信号?
    IIC_RecvACK();
    IIC_SendByte(reg);    //内部寄存器地址
    IIC_RecvACK();
    IIC_SendByte(Data);       //内部寄存器数据
    IIC_RecvACK();
    IIC_Stop();                   //发送停止信号
}

uint i2c_read_reg(uint L3G4200_ADRESS ,uint reg)             //L3G4200 读寄存器
{   
     uchar BUF;
    IIC_Start();                          //起始信号
    IIC_SendByte(L3G4200_ADRESS);           //发送设备地址+写信号
    IIC_RecvACK();
    IIC_SendByte(reg);                   //发送存储单元地址,从0x01开始*************存在哪?
作者: hujianhao    时间: 2015-4-8 16:42
程序本身没问题,检查下寄存器地址,寄存器地址可能有问题!
作者: 低调的张扬    时间: 2016-1-7 21:16
IIC_ERROR后来用在哪了?




欢迎光临 亿加合和智能车制作 (http://111.231.132.190/) Powered by Discuz! X3.2