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

📄 iic.c

📁 是在W77E58上实现的对IIC存储器的读写
💻 C
字号:
//主题说明 title declaration
//-----------------------------------------------------
// Microwave.c
// Copyright 2005 HFUT DSPLAB
// Auth:W.C.ZH
// Date: 19/11/05
// Target: Winband77e58
// Tool chain: KEIL Eval ver7.0
//------------------------------------------------------------------------------------
//************************引用声明 Includes*********************
//------------------------------------------------------------------------------------
//#include "REG54.inc"
#ifndef WIN32
#define   MCU_51
#endif
#include "Para.h"
#include "SetBautRate.h"
#ifndef WIN32
#include "77e58.h"
#include <intrins.h>
#include <pci5.h>
#include "global.h"
#endif
#include <string.h>
#include <stdio.h>
#include <math.h>

//************************符号定义******************************
//--------------------------------------------------

#define uchar unsigned char
#define uint unsigned int
#define NNOP _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
#define NOP _nop_();_nop_()
#define HIGH 1
#define LOW 0
#define LEDADDR 0xf0
#define ZEROADDR 0xf5
#define ADJUSTADDR 0xf1

//***********************p3口定义********************************

sbit RST=P3^2;			//复位输出
sbit TSCL=P3^3;			// CHOP信号输出,TSCL探头SCL



//p1口定义
//P1----P13P12P11P10(DCBA)片选信号
sbit TSDA= P1^4;			//探头I2C,SDA
sbit GPIBINT= P1^5;		//?????????????????
sbit PSCL=P1^6;	    // MAX1282SCLK(时钟),同时又是PI2C的PSCL
sbit PSDA=P1^7;			//参数SDA



//p0口定义
//段码输出,低5位键盘列输入


extern uchar xdata segment[8];		//8个显示位段码,顺序是segment[0]
extern uchar xdata bitselect;	    //位选通,初值=1;每次+1

extern uint xdata  baudrate;		//波特率

extern uchar xdata onoffrem;			//远控标志


/**必须放在片内,不然会计算造成影响 **/
extern xdata uchar chopcount  ;      //chop信号计数标志

//data uchar chopcount1  _at_ 0x7e;      //chop信号计数标志

extern  xdata  uchar maxctlbyte;		//max控制字?????????????
extern xdata  uchar rangectlbyte;		//量程控制字

extern xdata  int avmaxrddata;
extern xdata  unsigned char keycode;
extern xdata  unsigned int DelayMs_time;


//函数声明Function Prototypes
void DelayMs();


uchar PI2CRead (uchar addr);
void PI2CWrite (uchar byte,uchar addr);
//void TI2CRead (uchar startaddr,uchar bytelen);
//void TI2CWrite (uchar * buffer,uchar bytelen,uchar startaddr);


void PI2CStart(void);
void PI2CStop(void);
void PI2CAck(void);
void PI2CCheckAck(void);
void PI2CWirteByte(uchar i2cwrdata);
uchar PI2CReadByte(void);
/*
void TI2CStart(void);
void TI2CStop(void);
void TI2CAck(void);
void TI2CheckAck(void);
void TI2CWirteByte(uchar i2cwrdata);
void TI2CReadByte(uchar i2crddata);






//********************延时函数****************************************
void DelayMs()
{
      extern xdata  unsigned char DelayMs_I  ;

      while(DelayMs_time)
	{
          DelayMs_time--;
          for(DelayMs_I =0;DelayMs_I<13;DelayMs_I++)
          {
             NOP;
          }
        }
}



#define PI2C_DELAYMS    4

//*****************I2C 函数**************************************
void PI2CStart(void)
{
	EA=0;
        PSCL=0;
        DelayMs_time = PI2C_DELAYMS;
        DelayMs();
	PSDA=1;
        DelayMs_time = PI2C_DELAYMS;
        DelayMs();
        PSCL=1;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
	PSDA=0;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
	PSCL=0;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
        PSDA=1;
}

void PI2CStop(void)
{
	PSDA=0;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
        PSCL=1;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
        PSDA=1;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
        PSCL=0;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
        EA=1;
}

void PI2CAck(void)
{
	PSDA=0;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
	PSCL=1;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
	PSCL=0;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
	PSDA=1;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
}
void PI2CCheckAck(void)
{
        CY=1;
	PSDA=1;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
	PSCL=1;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
	CY=PSDA;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();          //取得ACK
	PSCL=0;
}

//****************向I2C存储器中写一个字节数据*************************
void PI2CWirteByte(uchar i2cwrdata)
{
	xdata uchar j=8;
	for(j=0;j<8;j++)
	{
		PSCL=0;
                DelayMs_time = PI2C_DELAYMS;
	        DelayMs();
		i2cwrdata<<=1;
		PSDA=CY;
                DelayMs_time = PI2C_DELAYMS;
	        DelayMs();
		PSCL=1;
               DelayMs_time = PI2C_DELAYMS;
	       DelayMs();
	}
	PSCL=0;
        DelayMs_time = PI2C_DELAYMS*10;
	DelayMs();
         PSDA=1;
}

//*******************从I2C存储器中读一个字节数据**********************
uchar PI2CReadByte(void)
{
	xdata uchar j;
        xdata uchar i2crddata;
        i2crddata = 0;
	PSCL=0;
        DelayMs_time = PI2C_DELAYMS;
        DelayMs();
	PSDA=1;
	for (j=0;j<8;j++)
	{
		PSCL=1;
                  DelayMs_time = PI2C_DELAYMS;
	         DelayMs();
		i2crddata=(i2crddata<<1) | PSDA;
		PSCL=0;
                 DelayMs_time = PI2C_DELAYMS;
        	DelayMs();
	}
	PSCL=0;
	DelayMs_time = PI2C_DELAYMS;
	DelayMs();
	PSDA=1;
        DelayMs_time = PI2C_DELAYMS;
	DelayMs();
	return i2crddata;
}

/*********************************************************************************************
* name:		PI2CRead
* func:		从参数I2C中读一个字节
* para:
			addr: 读I2C的地址
* ret:	    无
* modify:
* comment:
*********************************************************************************************/

uchar  PI2CRead(uchar addr)
{
        static xdata  uchar temp;
        temp = 0;
        PI2CStart();
        PI2CWirteByte(0xa0);
        PI2CCheckAck();
        if (CY)
	{
		return;
	}
        PI2CWirteByte(addr);
        PI2CCheckAck();
        if (CY)
	{
        	return;
	}
        PI2CStart();
        PI2CWirteByte(0xa1);
        PI2CCheckAck();
        if (CY)
		{
			return;
		}

	temp=PI2CReadByte();

        PI2CStop();
        CY=0;
	return temp;
}

/*********************************************************************************************
* name:		PI2CWrite
* func:		向参数I2C中写入一个字节
* para:
         	byte  : 写入字节
			addr  : 写地址
* ret:	    无
* modify:
* comment:
*********************************************************************************************/
void PI2CWrite (uchar byte,uchar addr)
{
        PI2CStart();
	PI2CWirteByte(0xa0);             //写器件地址
	PI2CCheckAck();
	if (CY){return;}
	PI2CWirteByte(addr);       //写数据地址
	PI2CCheckAck();
	if (CY)
	{
		return;
	}

	PI2CWirteByte(byte);
	PI2CCheckAck();
	if (CY)
	{
		return;
	}

	PI2CStop();
        DelayMs_I = 2;
	DelayMs();

}


#define  TI2C_HIGH   1
#define  TI2C_LOW    0
#define TI2C_DELAYMS    2
 #pragma OPTIMIZE(0)
//*****************TI2C 函数**************************************
void TI2CStart(void)
{


	EA=0;

        TSCL=TI2C_LOW;
        DelayMs_time = TI2C_DELAYMS*10;
        DelayMs();

        TSCL=TI2C_HIGH;
        DelayMs_time = TI2C_DELAYMS;
        DelayMs();
	TSDA=1;
        DelayMs_time = TI2C_DELAYMS;
        DelayMs();
        TSCL=TI2C_LOW;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
	TSDA=0;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
	TSCL=TI2C_HIGH;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
        TSDA=1;
}

void TI2CStop(void)
{
	TSDA=0;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
        TSCL=TI2C_LOW;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
        TSDA=1;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
        TSCL=TI2C_LOW;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
        EA=1;
}

void TI2CAck(void)
{
	TSDA=0;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
	TSCL=TI2C_LOW;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
	TSCL=TI2C_HIGH;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
	TSDA=1;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
}




void TI2CCheckAck(void)
{
        CY=1;
	TSDA=1;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
	TSCL=TI2C_LOW;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
	CY=TSDA;
        //DelayMs_time = TI2C_DELAYMS;
	//DelayMs();          //取得ACK
	TSCL=TI2C_HIGH;
}

//****************向I2C存储器中写一个字节数据*************************
void TI2CWirteByte(uchar i2cwrdata)
{
	xdata uchar j=8;
	for(j=0;j<8;j++)
	{
		TSCL=TI2C_HIGH;
                DelayMs_time = TI2C_DELAYMS;
	        DelayMs();
		i2cwrdata<<=1;
		TSDA=CY;
                DelayMs_time = TI2C_DELAYMS;
	        DelayMs();
		TSCL=TI2C_LOW;
                DelayMs_time = TI2C_DELAYMS;
	        DelayMs();
	}
	TSCL=TI2C_HIGH;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
         TSDA=1;
}

//*******************从I2C存储器中读一个字节数据**********************
uchar TI2CReadByte(void)
{
	xdata uchar j;
        xdata uchar i2crddata;
        i2crddata = 0;
	TSCL=TI2C_HIGH;
        DelayMs_time = TI2C_DELAYMS;
        DelayMs();
	TSDA=1;
	for (j=0;j<8;j++)
	{
		TSCL=TI2C_LOW;
                  DelayMs_time = TI2C_DELAYMS;
	         DelayMs();
		i2crddata=(i2crddata<<1) | TSDA;
		TSCL=TI2C_HIGH;
                 DelayMs_time = TI2C_DELAYMS*2;
	         DelayMs();
	}
	TSCL=TI2C_HIGH;
	DelayMs_time = TI2C_DELAYMS;
	DelayMs();
	TSDA=1;
        DelayMs_time = TI2C_DELAYMS;
	DelayMs();
	return i2crddata;
}





/*********************************************************************************************
* name:		TI2CRead
* func:		从参数I2C中读一个字节
* para:
			addr: 读I2C的地址
* ret:	    无
* modify:
* comment:
*********************************************************************************************/

uchar  TI2CRead(unsigned short addr)
{
        static xdata  uchar temp;
        static xdata  uchar addrh,addrl;

        TI2CStart();
        addrl = addr&0xff;
        addrh = (addr>>7)&0x0e;


        temp = 0;
        TI2CWirteByte(0xa0|addrh);
        TI2CCheckAck();
        if (CY)
	{
                TI2CStop();
		return temp ;
	}
        TI2CWirteByte(addrl);
        TI2CCheckAck();
        if (CY)
	{
                TI2CStop();
        	return temp ;
	}
        TI2CStart();
        TI2CWirteByte(0xa1|addrh);
        TI2CCheckAck();
        if (CY)
	{
                        TI2CStop();
			return temp ;
	}

	temp=TI2CReadByte();

        TI2CStop();
        CY=0;
	return temp;
}

/*********************************************************************************************
* name:		TI2CWrite
* func:		向参数I2C中写入一个字节
* para:
         	byte  : 写入字节
			addr  : 写地址
* ret:	    无
* modify:
* comment:
*********************************************************************************************/
void TI2CWrite (uchar byte,unsigned short addr)
{

        static xdata  uchar addrh,addrl;

        TI2CStart();
        addrl = addr&0xff;
        addrh = (addr>>7)&0x0e;


        TI2CStart();
	TI2CWirteByte(0xa0|addrh);             //写器件地址
	TI2CCheckAck();
	if (CY){return;}
	TI2CWirteByte(addrl);       //写数据地址
	TI2CCheckAck();
	if (CY)
	{
		return;
	}

	TI2CWirteByte(byte);
	TI2CCheckAck();
	if (CY)
	{
		return;
	}

	TI2CStop();
//	DelayMs(2);

}

⌨️ 快捷键说明

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