📄 twowire.lst
字号:
147 2 Set_SCL_Low;
148 2
149 2 }
150 1
151 1 if(cNum==1)
152 1 Set_SDA_High;
153 1 else
154 1 Set_SDA_Low;
155 1 //SDA = (cNum==1) 1:0;
156 1 Set_SCL_High;
157 1 while(SCL_Low){};
158 1 for(j = 0; j < TWD_SHORT_TIME; j++)
159 1 { };
160 1
161 1 Set_SCL_Low;
162 1
163 1 Set_SDA_High;
164 1 for(j = 0; j < TWD_SHORT_TIME; j++)
165 1 { };
166 1
167 1 return cRetval;
168 1 }
169
170 #else
// for fast Two Wire Bus Speed
void StartCondition(void)
{
// uCHAR ix;
uCHAR cTWtrytime=0;
while(++cTWtrytime)
C51 COMPILER V7.06 TWOWIRE 03/01/2007 08:05:39 PAGE 4
{
Set_SDA_High;
Set_SCL_High; /* make sure two line is release */
Set_SDA_Low;
if((SCL_High)&&(SDA_Low))
break;
}
}
//----------------------------------------
// P_Condition for TWD protocol
//----------------------------------------
void StopCondition(void)
{
// uCHAR ix;
uCHAR cTWtrytime=0;
Set_SDA_Low;
Set_SCL_High;
Set_SDA_High;
}
//--------------------------------------------------
// Send_Byte
// 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_Byte
// Read a byte from master with a acknowledge bit
C51 COMPILER V7.06 TWOWIRE 03/01/2007 08:05:39 PAGE 5
//--------------------------------------------------
uCHAR Read_Byte(void)
{
uCHAR ix, j;
uCHAR cRetval;
uCHAR cTWtrytime=0;
// cRetval = 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;
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
return cRetval;
}
#endif
280
281
282 //--------------------------------------------------
283 // Read Byte to Device
284 //--------------------------------------------------
285 uCHAR I2CReadByte(uCHAR cDevAddr, uCHAR cReg)
286 {
287 1 uCHAR cResult = 0;
288 1
289 1
290 1 /* write reg offset */
291 1 StartCondition();
292 1 if(Send_Byte(cDevAddr))
293 1 return 0; // Write address
294 1 if(Send_Byte(cReg))
295 1 return 0;
296 1
297 1 /* read data */
298 1 StartCondition();
299 1 if (Send_Byte(cDevAddr | 0x01)) // Read address
300 1 return 0;
301 1 cResult = Read_Byte(1);
302 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -