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

📄 iic.c

📁 单片机仿真电路里面对于初学者很有帮助
💻 C
字号:
/************************************************************
Copyright (C) Xiong,Hui at www.51embed.com
FileName: iic.c
Author: Xiong,Hui huixiong73@gmail.com
Date:08/4/18
Description: 
Version: 1.0
Function List: 

History: 
    1. Huixiong 08/4/18 1.0 build this moudle
***********************************************************/
#include "at89x52.H"
#include <intrins.h>
#include "iic.h"
/*************************************************************
*  Function: i2cstart()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
void i2cstart(void)
{
	EA=0;
	SDA=1; SCL=1; _nop_(); _nop_();_nop_();_nop_();//INI
	SDA=0; _nop_(); _nop_();_nop_();_nop_(); //START
	SCL=0;
}
/*************************************************************
*  Function:I2CStop()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
void i2cstop(void)
{
	SDA=0; SCL=1; _nop_(); _nop_();_nop_();_nop_();//INI
	SDA=1; _nop_(); _nop_();_nop_();_nop_(); //STOP
    SDA=0;
	EA=1;
}
/*************************************************************
*  Function:WaitAck()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
unsigned char waitack(void)
{
    unsigned char data errtime;
    errtime = 250;//fault,receiver no ACK,timeout value 255。
	SDA=1;_nop_();_nop_();_nop_();_nop_();_nop_();
	SCL=1;_nop_();_nop_();_nop_();_nop_();_nop_();
	while(SDA) {errtime--; _nop_();_nop_();_nop_();_nop_();if (!errtime) {i2cstop();return FALSE;}}
	SCL=0;
    SDA=0;
	return TRUE;
}
/*************************************************************
*  Function:SendAck()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
void sendack(void)
{
	SDA=0; _nop_();_nop_();_nop_();_nop_();
	SCL=1; _nop_();_nop_();_nop_();_nop_();
	SCL=0;
}
/*************************************************************
*  Function:SendNotAck()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
void sendnotack(void)
{
	SDA=1; _nop_();_nop_(); _nop_();_nop_();
	SCL=1; _nop_();_nop_();_nop_();_nop_();
	SCL=0;
}
/*************************************************************
*  Function:I2CSendByte()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
void i2csendbyte( unsigned char ch)
{
	unsigned char data m = 8;
	while (m--)
	{
        SCL=0;_nop_();_nop_();_nop_();_nop_();
		SDA=(bit)(ch&0x80); ch<<=1;_nop_();_nop_();_nop_();_nop_();
		SCL=1;
	}
	SCL=0;
}
/*************************************************************
*  Function: I2CReceiveByte()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
unsigned char i2creceivebyte(void)
{
	unsigned char data m = 8;
	unsigned char data ddata = 0;
	SDA = 1;
	while (m--)
	{
		SCL = 0;
		ddata <<= 1;
        _nop_();_nop_();
        _nop_();_nop_();
        _nop_();_nop_();
        _nop_();_nop_();
        _nop_();_nop_();
        SCL = 1;
		ddata |= SDA;
	}
	SCL = 0;
	return ddata;
}
/*************************************************************
*  Function:I2CRead()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
unsigned char  i2cread(unsigned char page, unsigned char address, unsigned char  size)
{
	unsigned char data m;

	i2cstart();

	i2csendbyte(0xa0);
	if(!waitack())return FALSE;

	i2csendbyte(page);
	if(!waitack())return FALSE;

	i2csendbyte(address);
	if(!waitack())return FALSE;

	i2cstart();
	i2csendbyte(0xa1);
	if(!waitack())return FALSE;

	for (m=0;m<size;m++)
	{
		i2cstrout[m]=i2creceivebyte();
		if (m!=size-1) sendack();//send answer except the last data
	}

	sendnotack();
	i2cstop();

	return TRUE;
}
/*************************************************************
*  Function:I2CWrite()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
unsigned char i2cwrite(unsigned char page,unsigned char address,unsigned char size)
{
	unsigned char data m;

	i2cstart();
	i2csendbyte(0xa0);
	if(!waitack())return FALSE;

	i2csendbyte(page);
	if(!waitack())return FALSE;

	i2csendbyte(address);
	if(!waitack())return FALSE;

	for (m=0;m<size;m++)
	{
		i2csendbyte(i2cstrin[m]);
		if(!waitack())return FALSE;
	}

	i2cstop();

	return TRUE;
}
/*************************************************************
*  Function:delay()
*  Description:
*  Calls:
*  Called by:
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
*************************************************************/
void delay( unsigned int i)
{
	unsigned int m,n;
	for(m=0;m<i;m++){
		for(n=0;n<3800;n++ ){
			 _nop_();
		}
	}
}

⌨️ 快捷键说明

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