📄 iic.txt
字号:
#i nclude
#i nclude
extern struct Parameter code Par[];
extern int Current_Parameter_Value;
extern char Current_Parameter_Attrib;
//************************************************
void I2cWait(void) //延时,I2c大约30k/s
{
unsigned char i;
for(i=10;i>0;i--)
{
i=i;
}
}
//************************************************
void I2cStart(void) // 起始信号
{
SDA = 1;
SCL = 1; I2cWait(); //起始条件建立时间大于4.7us,延时*/
SDA = 0; I2cWait(); //发送起始信号
SCL = 0;
}
//************************************************
void I2cStop(void) //停止信号
{
SDA = 0;
SCL = 1; I2cWait();
SDA = 1; I2cWait();
SCL = 0;
}
//************************************************
void mast_ack(void) //主答函数
{ SDA=0;
SCL=1;I2cWait();
SCL=0;
SDA=1;
}
//************************************************
void mast_nack(void) //主不答函数
{
SDA=1;
SCL=1;I2cWait();
SCL=0;I2cWait();
SDA=0;
}
//************************************************
void ack(void) //应答函数
{ SDA=1;
SCL=1;I2cWait();
// while(SDA){;} //等,直到成功
if(SDA)
E2ROM_ERROR=1; //失败或出错
else
E2ROM_ERROR=0; //成功
SCL=0;I2cWait();
}
//************************************************
void I2cSendByte(unsigned char bytedata) //送一字节数据到slave,成功返回1,失败返回0
{ unsigned char i=8;
// SCL=0;I2cWait();
for(i=0;i>7;
addressh = addressh&0x0e;
I2cStart();
I2cSendByte(E2ROM_WR|addressh); E2ROM_ERROR_CHECK;
I2cSendByte(addressl); E2ROM_ERROR_CHECK;
I2cSendByte(bytedata); E2ROM_ERROR_CHECK;
I2cStop(); I2cWait();
EA=1;
return 1; //成功返回1
}
/*
************************************************************
* 向E2ROM指定地址为首址,连续写n字节数据
* address:指定E2ROM 首地址
* saddress:数据源地址
* number:数据个数,对于24lc16该个数不能大于16,否则卷页
************************************************************
*/
bit E2ROM_PageWrite(unsigned int address,unsigned char *saddress,unsigned char number)
{
unsigned char i;
unsigned char addressh,addressl;
EA=0;
addressl = address&0xFF; //有关地址的计算参考手册Block部分
addressh = address>>7;
addressh =addressh*0x0e;
I2cStart();
I2cSendByte(E2ROM_WR|addressh); //指定页地址
E2ROM_ERROR_CHECK;
I2cSendByte(addressl); E2ROM_ERROR_CHECK;
for(i=number;i>0;i--)
{I2cSendByte(*saddress); E2ROM_ERROR_CHECK;
saddress++;
}
I2cWait();
I2cStop();
I2cWait();
EA=1;
return 1;
}
/*
************************************************************
* 向E2ROM写入参数值及属性
* 因所选器件24lc16B的特殊性,在进行写操作时不能连续写入超过16
* 字节的字符串,且如果在连续写入的地址中,如果包括16的整数倍
* 将卷页,例如将1,2,3写入15,16,17地址中,那么3将写到1地址中
* 如果以后升级将修改该函数
************************************************************
*/
void Write_Parameter(unsigned char which)
{unsigned int address,temp;
unsigned char Err_Count,str[3];
str[0]=Current_Parameter_Value>>8;
str[1]=Current_Parameter_Value&0xff;
str[2]=Current_Parameter_Attrib;
temp=which/5; //每页写5个参数,选择5是因为3*5=15
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -