|
发表于 2021-4-8 09:53:17
|
显示全部楼层
:lol::lol::lol:一个在P1.0和P1.1口,另一个在P2.6和P2.7口
教你一个笨方法:
#define uchar unsigned char /*宏定义*/
#define uint unsigned int
#define _Nop() _nop_() /*定义空指令*/
/*端口位定义*/
sbit SDA1=P1^0;
sbit SCL1=P1^1;
sbit SDA2=P2^6;
sbit SCL2=P2^7;
bit ack1; /*应答标志位*/
bit ack2; /*应答标志位*/
/******以下是两个IIC设备用的分开的IIC模拟代码*******/
- <p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">/*******************************************************************
- 起动总线函数
- 函数原型: void Start_I2c1();
- 功能: 启动I2C总线,即发送I2C起始条件.
-
- ********************************************************************/
- void Start_I2c1()
- {
- SDA1=1; /*发送起始条件的数据信号*/
- _Nop();
- SCL1=1;
- _Nop(); /*起始条件建立时间大于4.7us,延时*/
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- SDA1=0; /*发送起始信号*/
- _Nop(); /* 起始条件锁定时间大于4μs*/
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- SCL1=0; /*钳住I2C总线,准备发送或接收数据 */
- _Nop();
- _Nop();
- }</p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
- /*******************************************************************
- 结束总线函数
- 函数原型: void Stop_I2c()1;
- 功能: 结束I2C总线,即发送I2C结束条件.
-
- ********************************************************************/
- void Stop_I2c1()
- {
- SDA1=0; /*发送结束条件的数据信号*/
- _Nop(); /*发送结束条件的时钟信号*/
- SCL1=1; /*结束条件建立时间大于4μs*/
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- SDA1=1; /*发送I2C总线结束信号*/
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- }</p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
- /*******************************************************************
- 字节数据传送函数
- 函数原型: void SendByte(uchar c);
- 功能: 将数据c发送出去,可以是地址,也可以是数据,发完后等待应答,并对
- 此状态位进行操作.(不应答或非应答都使ack=0 假)
- 发送数据正常,ack=1; ack=0表示被控器无应答或损坏。
- ********************************************************************/
- void SendByte1(uchar c)
- {
- uchar BitCnt;
-
- for(BitCnt=0;BitCnt<8;BitCnt++) /*要传送的数据长度为8位*/
- {
- if((c<<BitCnt)&0x80)SDA1=1; /*判断发送位*/
- else SDA1=0;
- _Nop();
- SCL1=1; /*置时钟线为高,通知被控器开始接收数据位*/
- _Nop();
- _Nop(); /*保证时钟高电平周期大于4μs*/
- _Nop();
- _Nop();
- _Nop();
- SCL1=0;
- }
-
- _Nop();
- _Nop();
- SDA1=1; /*8位发送完后释放数据线,准备接收应答位*/
- _Nop();
- _Nop();
- SCL1=1;
- _Nop();
- _Nop();
- _Nop();
- if(SDA1==1)ack1=0;
- else ack1=1; /*判断是否接收到应答信号*/
- SCL1=0;
- _Nop();
- _Nop();
- }</p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
- /*******************************************************************
- 字节数据传送函数
- 函数原型: uchar RcvByte1();
- 功能: 用来接收从器件传来的数据,并判断总线错误(不发应答信号),
- 发完后请用应答函数。
- ********************************************************************/
- uchar RcvByte1()
- {
- uchar retc;
- uchar BitCnt;
-
- retc=0;
- SDA1=1; /*置数据线为输入方式*/
- for(BitCnt=0;BitCnt<8;BitCnt++)
- {
- _Nop();
- SCL1=0; /*置时钟线为低,准备接收数据位*/
- _Nop();
- _Nop(); /*时钟低电平周期大于4.7μs*/
- _Nop();
- _Nop();
- _Nop();
- SCL1=1; /*置时钟线为高使数据线上数据有效*/
- _Nop();
- _Nop();
- retc=retc<<1;
- if(SDA1==1)retc=retc+1; /*读数据位,接收的数据位放入retc中 */
- _Nop();
- _Nop();
- }
- SCL1=0;
- _Nop();
- _Nop();
- return(retc);
- }</p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
- /********************************************************************
- 应答子函数
- 原型: void Ack_I2c1(bit a);
-
- 功能:主控器进行应答信号,(可以是应答或非应答信号)
- ********************************************************************/
- void Ack_I2c1(bit a)
- {
-
- if(a==0)SDA1=0; /*在此发出应答或非应答信号 */
- else SDA1=1;
- _Nop();
- _Nop();
- _Nop();
- SCL1=1;
- _Nop();
- _Nop(); /*时钟低电平周期大于4μs*/
- _Nop();
- _Nop();
- _Nop();
- SCL1=0; /*清时钟线,钳住I2C总线以便继续接收*/
- _Nop();
- _Nop();
- }</p><p>/************************设备2******************************************************************/</p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">/*******************************************************************
- 起动总线函数
- 函数原型: void Start_I2c2();
- 功能: 启动I2C总线,即发送I2C起始条件.
-
- ********************************************************************/
- void Start_I2c2()
- {
- SDA2=1; /*发送起始条件的数据信号*/
- _Nop();
- SCL2=1;
- _Nop(); /*起始条件建立时间大于4.7us,延时*/
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- SDA2=0; /*发送起始信号*/
- _Nop(); /* 起始条件锁定时间大于4μs*/
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- SCL2=0; /*钳住I2C总线,准备发送或接收数据 */
- _Nop();
- _Nop();
- }</p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
- /*******************************************************************
- 结束总线函数
- 函数原型: void Stop_I2c2();
- 功能: 结束I2C总线,即发送I2C结束条件.
-
- ********************************************************************/
- void Stop_I2c2()
- {
- SDA2=0; /*发送结束条件的数据信号*/
- _Nop(); /*发送结束条件的时钟信号*/
- SCL2=1; /*结束条件建立时间大于4μs*/
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- SDA2=1; /*发送I2C总线结束信号*/
- _Nop();
- _Nop();
- _Nop();
- _Nop();
- }</p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
- /*******************************************************************
- 字节数据传送函数
- 函数原型: void SendByte2(uchar c);
- 功能: 将数据c发送出去,可以是地址,也可以是数据,发完后等待应答,并对
- 此状态位进行操作.(不应答或非应答都使ack=0 假)
- 发送数据正常,ack=1; ack=0表示被控器无应答或损坏。
- ********************************************************************/
- void SendByte2(uchar c)
- {
- uchar BitCnt;
-
- for(BitCnt=0;BitCnt<8;BitCnt++) /*要传送的数据长度为8位*/
- {
- if((c<<BitCnt)&0x80)SDA2=1; /*判断发送位*/
- else SDA2=0;
- _Nop();
- SCL2=1; /*置时钟线为高,通知被控器开始接收数据位*/
- _Nop();
- _Nop(); /*保证时钟高电平周期大于4μs*/
- _Nop();
- _Nop();
- _Nop();
- SCL2=0;
- }
-
- _Nop();
- _Nop();
- SDA2=1; /*8位发送完后释放数据线,准备接收应答位*/
- _Nop();
- _Nop();
- SCL2=1;
- _Nop();
- _Nop();
- _Nop();
- if(SDA2==1)ack2=0;
- else ack2=1; /*判断是否接收到应答信号*/
- SCL2=0;
- _Nop();
- _Nop();
- }</p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
- /*******************************************************************
- 字节数据传送函数
- 函数原型: uchar RcvByte2();
- 功能: 用来接收从器件传来的数据,并判断总线错误(不发应答信号),
- 发完后请用应答函数。
- ********************************************************************/
- uchar RcvByte()
- {
- uchar retc;
- uchar BitCnt;
-
- retc=0;
- SDA2=1; /*置数据线为输入方式*/
- for(BitCnt=0;BitCnt<8;BitCnt++)
- {
- _Nop();
- SCL2=0; /*置时钟线为低,准备接收数据位*/
- _Nop();
- _Nop(); /*时钟低电平周期大于4.7μs*/
- _Nop();
- _Nop();
- _Nop();
- SCL2=1; /*置时钟线为高使数据线上数据有效*/
- _Nop();
- _Nop();
- retc=retc<<1;
- if(SDA2==1)retc=retc+1; /*读数据位,接收的数据位放入retc中 */
- _Nop();
- _Nop();
- }
- SCL2=0;
- _Nop();
- _Nop();
- return(retc);
- }</p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> </p><p style="list-style: none; margin: 0px; padding: 0.5em 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: 微软雅黑, 黑体, 宋体; font-size: 18.2px; font-style: normal; font-weight: 400; word-spacing: 0px; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
- /********************************************************************
- 应答子函数
- 原型: void Ack_I2c2(bit a);
-
- 功能:主控器进行应答信号,(可以是应答或非应答信号)
- ********************************************************************/
- void Ack_I2c2(bit a)
- {
-
- if(a==0)SDA2=0; /*在此发出应答或非应答信号 */
- else SDA2=1;
- _Nop();
- _Nop();
- _Nop();
- SCL2=1;
- _Nop();
- _Nop(); /*时钟低电平周期大于4μs*/
- _Nop();
- _Nop();
- _Nop();
- SCL2=0; /*清时钟线,钳住I2C总线以便继续接收*/
- _Nop();
- _Nop();
- }</p><p>
- </p><p> </p>
复制代码 方法虽笨,但是可以帮助你解决当前的问题。单总线挂接多设备的方法,以后再学。先解决有无的问题。
|
|