📄 i2c_bus.lst
字号:
C51 COMPILER V7.06 I2C_BUS 01/05/2007 16:54:14 PAGE 5
/**********************************************
*Send a byte to master with a acknowledge bit *
**********************************************/
uCHAR Send_Byte(uCHAR cData)
{
uCHAR ix;
uCHAR cAcknowledge;
uCHAR cTWtrytime=0;
cAcknowledge = 0;
for(ix = 0; ix < 8; ix++)
{
Set_SCL_Low;
// (cData&(1<<(7-ix)))?(Set_SDA_High):(Set_SDA_Low); /* MSB First */
if(cData&0x80)
Set_SDA_High;
else
Set_SDA_Low;
cData<<=1;
Set_SCL_High;
}
Set_SCL_Low;
Set_SDA_High; // release data line for acknowledge
Set_SCL_High; // Send a clock for Acknowledge
if(SDA_High)
cAcknowledge = 1; // No Acknowledge
Set_SCL_Low; // Finish Acknoledge
return(cAcknowledge);
}
/************************************************
*Read a byte from master with a acknowledge bit *
************************************************/
uCHAR Read_Byte(void)
{
uCHAR ix, j;
uCHAR cRetval;
uCHAR cTWtrytime=0;
Set_SDA_High;
for(ix = 0; ix < 8; ix++)
{
Set_SCL_Low;
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SCL_High;
while(SCL_Low)
{ };
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
cRetval<<=1;
if(SDA_High)
cRetval++;
}
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SCL_Low;
C51 COMPILER V7.06 I2C_BUS 01/05/2007 16:54:14 PAGE 6
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
return cRetval;
}
#endif
308 /***************************************************
309 * Read Byte to Device *
310 ***************************************************/
311 uCHAR I2CReadByte(uCHAR cDevAddr, uCHAR cReg)
312 {
313 1 uCHAR cResult = 0;
314 1 /* write reg offset */
315 1 StartCondition();
316 1 if(Send_Byte(cDevAddr))
317 1 return 0; // Write address
318 1 if(Send_Byte(cReg))
319 1 return 0;
320 1 /* read data */
321 1 StartCondition();
322 1 if (Send_Byte(cDevAddr | 0x01)) // Read address
323 1 return 0;
324 1 cResult = Read_Byte(1);
325 1 StopCondition();
326 1 return cResult;
327 1 }
328
329 /***************************************************
330 * Write Byte to Device *
331 ***************************************************/
332
333 uCHAR I2CWriteByte(uCHAR cDevAddr, uCHAR cReg, uCHAR cData)
334 { /* start condition */
335 1 StartCondition();
336 1 cDevAddr &= 0xFE; // Force WRITE address
337 1 /* send device ID and write data */
338 1 if(!Send_Byte(cDevAddr))
339 1 {
340 2 if(!Send_Byte(cReg))
341 2 {
342 3 if(!Send_Byte(cData))
343 3 {
344 4 StopCondition();
345 4 return(0); /* success */
346 4 }
347 3 }
348 2 }
349 1 StopCondition();
350 1 return(1);
351 1 }
352
353 uCHAR I2CWriteByte1(uCHAR cDevAddr, uCHAR cReg, uCHAR cData)
354 {
355 1 Set_SCL_Low;
356 1 Set_SDA_Low;
357 1 Send_Byte1(cDevAddr);
358 1 Send_Byte1(cReg);
359 1 Send_Byte1(cData);
360 1 Set_SCL_Low;
361 1 Set_SDA_Low;
362 1 return(1);
363 1 }
364
C51 COMPILER V7.06 I2C_BUS 01/05/2007 16:54:14 PAGE 7
365 #else
//myself
void Start_I2C(void)
{
P_SCK = False ;
Delay_10us() ;
P_SDA = True ;
Delay_10us() ;
P_SCK = True ;
Delay_10us() ;
P_SDA = False ;
Delay_10us() ;
P_SCK = False ;
}
void Stop_I2C(void)
{
P_SCK = False;
Delay_10us();
P_SDA = False;
Delay_10us();
P_SCK = True;
Delay_10us();
P_SDA = True;
Delay_10us();
P_SCK = False;
}
void WByte_I2C(uchar value)
{
uchar i;
for(i=0;i!=8;i++)
{
if( value&0x80 )
{
P_SDA = True;
}
else
{
P_SDA = False;
}
value <<=1;
Delay_10us();
P_SCK = High;
Delay_10us();
P_SCK = Low;
Delay_10us();
}
P_SDA = High;
P_SCK = High;
do
{
i++;
}while(P_SDA&&i!=255);
P_SCK = Low;
}
uCHAR I2C_RByte(void)
{
uchar i;
uCHAR RI2C_data=0;
C51 COMPILER V7.06 I2C_BUS 01/05/2007 16:54:14 PAGE 8
P_SDA = High;
for(i=0;i!=8;i++)
{
P_SCK = High;
Delay_10us();
RI2C_data<<=1;
if(P_SDA)
{
RI2C_data ++;
}
P_SCK = Low;
Delay_10us();
}
P_SDA = High;
Delay_10us();
P_SCK = High;
Delay_10us();
P_SCK = Low;
return RI2C_data;
}
uCHAR I2CReadByte(uCHAR cDevAddr, uCHAR cReg)
{
uCHAR cResult = 0;
Start_I2C();
WByte_I2C(cDevAddr);
WByte_I2C(cReg);
Start_I2C();
WByte_I2C(cDevAddr | 0x01);
cResult = I2C_RByte();
Stop_I2C();
return cResult;
}
void I2CWriteByte(uCHAR cDevAddr, uCHAR cReg, uCHAR cData)
{
Start_I2C();
cDevAddr &= 0xFE; // Force WRITE address
/* send device ID and write data */
WByte_I2C(cDevAddr);
WByte_I2C(cReg);
WByte_I2C(cData);
Stop_I2C();
}
#endif
471
472
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 504 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 4
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -