文章标题

来源:互联网 发布:快速转换视频格式软件 编辑:程序博客网 时间:2024/06/07 02:46
/******************************************************************************

Module : I2C接口文件..
Description :
Build Date :
others :


Modification History:

#ifndef _I2C_DRIVER_C#define _I2C_DRIVER_C#include "I2C_driver.h"#include "..\MISC\MIS_UAL.h"#include "..\LOG\LOG_UAL.h"

/* ========================================================================= */
/Private Functions ======================================================== /
/* Description: I2C delay procedure ————————————– */
void I2C_delay( void )
{
__NOP();
__NOP();
__NOP();
__NOP();
}

/* Description: startI2C procedure ————————————— */
void I2C_start( void )
{
I2C_SDA_OUTPUT;
I2C_SCL_OUTPUT;
I2C_SCL_L;I2C_delay( ); //#define IO_INPUT (1)
I2C_SDA_H;I2C_delay( ); //#define IO_OUTPUT (0)
I2C_SCL_H;I2C_delay( ); //#define IO_HIGH (1)
I2C_SDA_L;I2C_delay( ); //#define IO_LOW (0)
//#define IO_OUTPUT (0)
I2C_SCL_L;
}
/* Description: restartI2C procedure ————————————— */
void I2C_restart( void )
{
I2C_SCL_L;I2C_delay( );
I2C_SDA_H;I2C_delay( );
I2C_SCL_H;I2C_delay( );
I2C_SDA_L;I2C_delay( );
I2C_SCL_L;
}

//——————————————————————————
//Description: send one byte to the i2c bus
// MSB first
//Input: U8 oneByte - data to be send
//Output: ack signal from the slave
//——————————————————————————
U8 I2C_sendByte( U8 oneByte )
{
U8 k,ack;

for( k=0;k<8;k++ ) //sending eight bit{    I2C_SCL_L;    if( oneByte&0x80 )    {        I2C_SDA_H;    }    else    {        I2C_SDA_L;    }    I2C_delay(  );    oneByte <<= 1;    I2C_SCL_H;I2C_delay(  );}I2C_SCL_L;I2C_delay(  );//0I2C_SDA_INPUT;  // 1I2C_SDA_INPUT_EN; //0I2C_delay(  );I2C_delay(  );I2C_SCL_H;I2C_delay(  );ack = I2C_SDA_PV;                       //#define I2C_SDA_PV                    ( PIN_DATA( P7, I2C_SDA_PIN ) )I2C_SCL_L;I2C_delay(  );I2C_SDA_OUTPUT;return( ack );

}

//——————————————————————————
//Description: receive one byte from the slave
// MSB first
//Input: none
//Output: the data received
//——————————————————————————
U8 I2C_rcvByte( void )
{
U8 k,tmpByte = 0x00;

I2C_SCL_L;I2C_SDA_INPUT;I2C_SDA_INPUT_EN;I2C_delay(  );I2C_delay(  );for( k=0;k<8;k++ ){    I2C_SCL_H;I2C_delay(  );    tmpByte <<= 0x01;    if( I2C_SDA_PV )        tmpByte++;    I2C_SCL_L;I2C_delay(  );}I2C_SDA_OUTPUT;return( tmpByte );

}

//——————————————————————————
//Input: ack signal to be sent to I2C bus
//Output: none
//——————————————————————————
void I2C_ack( U8 ack )
{
//I2C_SCL_L;I2C_delay( );
if( ack==1 )
{
I2C_SDA_H;
}
else
{
I2C_SDA_L;
}
I2C_SCL_H;I2C_delay( );
I2C_SCL_L;I2C_delay( );
}

//——————————————————————————
//Description: stop I2C bus
//Input: none
//Output: none STOP条件:SDA线的低到高跳变(和启动相反),而时钟SCL稳定在高电平状态。 停止条件终止存储设备和

                   //总线主站之间的通信。 读取命令结束后的一个停止条件(在NoAck之后,并且仅在之后)会强制内存设备                    //进入待机状态。 写命令结束时的STOP条件触发内部EEPROM写周期。

//——————————————————————————
void I2C_stop( void )
{
I2C_SCL_L;I2C_delay( );
I2C_SDA_L;I2C_delay( );
I2C_SCL_H;I2C_delay( );
I2C_SDA_H;I2C_delay( );
I2C_SDA_INPUT;
I2C_SDA_INPUT_EN;
I2C_SCL_INPUT;
}

/*********************************************************************
Description: 读取铁电FM24CL64、EE-AT24LC512函数接口;..
Input:
U8 sla: slave address;
U16 iSuba: the addr of the data to be read from the slave module;
U8 *pBuf: the pointer of the buffer to store the data;
U8 num: the number of the data to be read(unit:Byte);
Return: None
*********************************************************************/
void I2C_readData( U8 sla, U16 iSuba,U8 *pBuf, U16 num )
{
U8 tryNum;
U16 k;
if( num==0x00 ) return;
for( tryNum=0;tryNum<2;tryNum++ )
{
if( tryNum!=0 ) I2C_wrBusyWait( sla );//MIS_delay_ms( 5 );
I2C_start( );
if( I2C_sendByte( sla )==0x01 ) continue; //send the slave address
if( I2C_sendByte( iSuba>>0x08 )==0x01 ) continue; //send the MSB address
if( I2C_sendByte( iSuba )==0x01 ) continue; //send the LSB address
I2C_restart( );
if( I2C_sendByte( sla+0x01 )==0x01 )continue; //send the slave address
for( k=0;k

endif

原创粉丝点击