⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iic.h

📁 本源代码是一个51单片机控制pcf8563时钟日历芯片的源代码
💻 H
字号:
#ifndef	_IIC_H 				// 防止IIC.h被重复引用
#define	_IIC_H

#include <reg52.h>
#include <intrins.h>
#include "common.h"

/* 起始条件子函数 */
void start(void)
{
	SDA = 1;
	_nop_();
	SCL = 1; 
	_nop_();
    SomeNOP();
	SDA = 0;
	_nop_();
	SomeNOP();
    SCL = 0;
	_nop_();
	_nop_();
	
}

/* 停止条件子函数 */
void stop(void)
{
	SDA = 0;
    _nop_();
	SCL = 1;
	_nop_();
	SomeNOP();
	SDA = 1;
	_nop_();
	_nop_();
	
}

/* 应答子函数 */
void ack(void)
{
	SDA = 0;
	_nop_();
	_nop_();
	SCL = 1;
	_nop_();
    SomeNOP();
	SCL = 0;
	_nop_();
	_nop_();   
}

/* 发送非应答子函数 */
void n_ack(void)
{
	SDA = 1;			// 发送非应答位
	_nop_();
	_nop_();
	SCL = 1;
    _nop_();
    SomeNOP();
	SCL = 0;
	_nop_();
	_nop_();
}

bool WaitAck(void)
{
 uchar errtime=255;//因故障接收方无ACK,超时值为255。
 SDA=1;SomeNOP();
 SCL=1;SomeNOP();
 while(SDA) {errtime--; if (!errtime) {stop();SystemError=0x11;return false;}}
 SCL=0;
 return true;
}


/* 发送数据子程序,ch为要发送的数据 */
void sendbyte(uchar ch)
{
	uchar BitCounter = 8; 			//位数控制
	uchar tmp; 						//中间变量控制
	tmp = ch;
	do
	{
		SCL = 0;
		if ((tmp&0x80)==0x80)		//如果最高位是1
			SDA = 1;
		else
			SDA = 0;
		SCL = 1;
		tmp = (tmp<<1);				//左移
		BitCounter--;
	}
	while(BitCounter);
	SCL = 0;
}

uchar receivebyte(void)
{
 uchar i=8;
 uchar ddata=0;
 SDA=1;
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 while (i--)
 {
 SCL=1;
 ddata<<=1;
        if(SDA == 1)
			ddata = ddata|0x01;    // 若接收到的位为1,则数据的最后一位置1
		else 
			ddata = ddata&0xfe;    // 否则数据的最后一位置0
 SCL=0;
 }
 return ddata;
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -