www.pudn.com > EFA.rar > ch452.c


/******************************************************************* 
 CH452 2 线接口 
*******************************************************************/ 
#include  
#include "define.h" 
#include "Global.h" 
 
#define DELAY_1US       _NOP();_NOP();_NOP();_NOP();_NOP() 
#define CHIP1           0x40 
#define CHIP2           0x60 
#define CHIP3           0x40            
 
/******************************************************************* 
函数名称:对第一片CH452进行写操作 
command 命令字 
*******************************************************************/ 
void CH452_Write_1(unsigned int command)	           
{ 
    Start_I2c_1();                                        // 启动总线 
    Send_Byte_1(((unsigned char)(command>>7)&0x1E)|CHIP1);// 发送高位数据 
    Send_Byte_1((unsigned char)command);                  // 发送低位数据 
    Stop_I2c_1();                                         // 结束总线  
} 
 
/******************************************************************* 
函数名称:对第二片CH452进行写操作 
command 命令字 
*******************************************************************/ 
void CH452_Write_2(unsigned int command)	           
{ 
    Start_I2c_1();                                        // 启动总线 
    Send_Byte_1(((unsigned char)(command>>7)&0x1E)|CHIP2);// 发送高位数据 
    Send_Byte_1((unsigned char)command);                  // 发送低位数据 
    Stop_I2c_1();                                         // 结束总线  
} 
 
/******************************************************************* 
函数名称:对第三片CH452进行写操作 
command 命令字 
*******************************************************************/ 
void CH452_Write_3(unsigned int command)	           
{ 
    Start_I2c_3();                                        // 启动总线 
    Send_Byte_3(((unsigned char)(command>>7)&0x1E)|CHIP3);// 发送高位数据 
    Send_Byte_3((unsigned char)command);                  // 发送低位数据 
    Stop_I2c_3();                                         // 结束总线  
} 
 
/*******************************************************************               
函数名称: Start_I2c_1   
功能描述: 启动总线  
********************************************************************/ 
void Start_I2c_1(void) 
{ 
    P2OUT |= SDA1; 
    DELAY_1US;      
    P2OUT |= SCL1; 
    DELAY_1US;      
    P2OUT &= ~SDA1;                                	 
    DELAY_1US;      
    P2OUT &= ~SCL1; 
    DELAY_1US;      
} 
 
/*******************************************************************               
函数名称: Stop_I2c_1   
功能描述: 停止总线     
********************************************************************/ 
void Stop_I2c_1(void) 
{ 
    P2OUT &= ~SDA1; 
    DELAY_1US;      
    P2OUT |= SCL1; 
    DELAY_1US;      
    P2OUT |= SDA1;                                	 
    DELAY_1US; 
    P2OUT &= ~SCL1; 
    DELAY_1US;          
} 
 
/*******************************************************************               
函数名称: Send_Byte_1   
功能描述: 发送字节 
输    入: 要发送的字     
********************************************************************/ 
void Send_Byte_1(unsigned char dat) 
{ 
    unsigned char i; 
    for (i=0; i<8; i++) 
    { 
        if ((dat & 0x80) == 0x80) P2OUT |= SDA1; 
        else P2OUT &= ~SDA1; 
        dat <<= 1; 
        DELAY_1US;      
        P2OUT |= SCL1; 
        DELAY_1US;      
        P2OUT &= ~SCL1; 
        DELAY_1US;      
    } 
    P2OUT |= SDA1;     	 
    DELAY_1US;                    					 
    P2OUT |= SCL1; 
    DELAY_1US;                              		 
    P2OUT &= ~SCL1; 
    DELAY_1US;     	 
} 
 
/*******************************************************************               
函数名称: Start_I2c_3   
功能描述: 启动总线  
********************************************************************/ 
void Start_I2c_3(void) 
{ 
    P2OUT |= SDA2; 
    DELAY_1US;      
    P2OUT |= SCL2; 
    DELAY_1US;      
    P2OUT &= ~SDA2;                                	 
    DELAY_1US;      
    P2OUT &= ~SCL2; 
    DELAY_1US;      
} 
 
/*******************************************************************               
函数名称: Stop_I2c_3   
功能描述: 停止总线     
********************************************************************/ 
void Stop_I2c_3(void) 
{ 
    P2OUT &= ~SDA2; 
    DELAY_1US;      
    P2OUT |= SCL2; 
    DELAY_1US;      
    P2OUT |= SDA2;                                	 
    DELAY_1US; 
    P2OUT &= ~SCL2; 
    DELAY_1US;      
} 
 
/*******************************************************************               
函数名称: Send_Byte_3   
功能描述: 发送字节 
输    入: 要发送的字     
********************************************************************/ 
void Send_Byte_3(unsigned char dat) 
{ 
    unsigned char i; 
    for (i=0; i<8; i++) 
    { 
        if ((dat & 0x80) == 0x80) P2OUT |= SDA2; 
        else P2OUT &= ~SDA2; 
        dat <<= 1; 
        DELAY_1US;      
        P2OUT |= SCL2; 
        DELAY_1US;      
        P2OUT &= ~SCL2; 
        DELAY_1US;      
    }                    					 
    P2OUT |= SDA2;     	 
    DELAY_1US; 
    P2OUT |= SCL2; 
    DELAY_1US;                              		 
    P2OUT &= ~SCL2; 
    DELAY_1US;     	 
}