S35390驱动程序
/**********************************************************
Function: S35390A_START(void)
Description: I2C通讯启动标志
Input: none
output: none
Return: none
**********************************************************/
void S35390A_START(void)
{
S35390A_SDA_HIGH(); //高
S35390A_SCL_HIGH(); //高
DELAYthreeNOP();
S35390A_SDA_LOW(); //低
DELAYthreeNOP();
S35390A_SCL_LOW(); //低
DELAYeightNOP();
}
/**********************************************************
Function: S35390A_STOP(void)
Description: I2C通讯结束标志
Input: none
output: none
Return: none
**********************************************************/
void S35390A_STOP(void)
{
S35390A_SDA_LOW(); //低
S35390A_SCL_HIGH(); //高
DELAYthreeNOP();
S35390A_SDA_HIGH(); //高
DELAYeightNOP();
//S35390A_SCL_LOW(); //低
//DELAYeightNOP();
}
/**********************************************************
Function: S35390A_GETACK(void)
Description: 获得ACK信号
Input: none
output: none
Return: temp
**********************************************************/
unsigned char S35390A_GETACK(void)
{
unsigned char w=0;
unsigned char z=100;
_NOP();
_NOP();
S35390A_SCL_LOW();
S35390A_SDA_DIR&=~S35390A_SDA; //SDA input
S35390A_SCL_OUT|=S35390A_SCL;
_NOP();
wait:
w=((S35390A_SDA_IN&S35390A_SDA));
if((w!=0)&&((z--)!=0))
{
goto wait;
}
S35390A_SCL_LOW();
DELAYeightNOP();
return(w);
}
/**********************************************************
Function: S35390A_SETACK(void)
Description: 输出ACK信号
Input: none
output: none
Return: none
**********************************************************/
void S35390A_SETACK(void)
{
S35390A_SCL_LOW();
DELAYeightNOP();
S35390A_SDA_LOW();
DELAYthreeNOP();
S35390A_SCL_HIGH();
DELAYthreeNOP();
S35390A_SCL_LOW();
DELAYeightNOP();
}
/**********************************************************
Function: S35390A_SETNCK(void)
Description: 输出NCK信号
Input: none
output: none
Return: none
**********************************************************/
void S35390A_SETNCK(void)
{
S35390A_SCL_LOW();
DELAYeightNOP();
S35390A_SDA_HIGH();
DELAYthreeNOP();
S35390A_SCL_HIGH();
DELAYthreeNOP();
S35390A_SCL_LOW();
DELAYeightNOP();
}
/**********************************************************
Function: S35390A_WRITE(unsigned char opt,unsigned char adr,unsigned char count)
Description: 向S35390A写数据
Input: opt:装置命今,count:写数据的字节个数
output: none
Return: 0或1
**********************************************************/
unsigned char S35390A_WRITE(unsigned char opt,unsigned char count)
{
unsigned char s_temp=0;
unsigned char ws;
S35390A_START(); //启动总线
S35390A_SEND_BYTE(opt); //发送装置命今字节
s_temp=S35390A_GETACK(); //接收应答信号
if((s_temp & S35390A_SDA))
{
S35390A_STOP();
return(0);
}
for(ws=0;ws
s_temp=s35390a[ws];
S35390A_SEND_BYTE(s_temp);
s_temp=S35390A_GETACK(); //接收应答信号
if((s_temp & S35390A_SDA))
{
S35390A_STOP();
return(0);
}
}
S35390A_STOP(); //停止总线
return(1);
}
/**********************************************************
Function: S35390A_READ(unsigned char opt,unsigned char adr,unsigned char count)
Description: 从S35390A读数据
Input: none
output: none
Return: none
**********************************************************/
unsigned char S35390A_READ(unsigned char opt,unsigned char count)
{
unsigned char s_temp=0;
unsigned char ws;
S35390A_START(); //启动总线
S35390A_SEND_BYTE(opt); //发送装置命今字节
s_temp=S35390A_GETACK(); //接收应答信号
if((s_temp & S35390A_SDA))
{
S35390A_STOP();
return(0);
}
for(ws=0;ws
s35390a[ws] = S35390A_RECEIVE_BYTE();
if(ws==(count-1))
{
S35390A_SETNCK(); //不发回应
}
else
{
S35390A_SETACK(); //发回应
}
}
S35390A_STOP(); //停止总线
return(1);
}
/**********************************************************
Function: S35390A_SWAP_BYTE(unsigned char swdata)
Description: 字节首尾位交换
Input: none
output: none
Return: none
**********************************************************/
unsigned char S35390A_SWAP_BYTE(unsigned char swdata)
{
unsigned char swtemp = swdata;
swtemp = ((swtemp & 0x55) < 1) | ((swtemp & 0xaa) >> 1); //相邻两位对换
swtemp = ((swtemp & 0x33) < 2) | ((swtemp & 0xcc) >> 2); //
swtemp = ((swtemp & 0x0f) < 4) | ((swtemp & 0xf0) >> 4);
return(swtemp);
}
S35390驱动程 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)