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

📄 fm24c04.c

📁 FM24C04测试用例程,经本人调试,可以使用.对于51初学者有很好的参考作用.
💻 C
字号:
//THIS CODE WAS DESIGNED TO DEMONSTRATE HOW THE FM24C04/FM24C04A SERIAL FRAM COULD 
//BE INTERFACED TO THE 8051 MICROCONTROLLER. THE INTERFACE USES 2 LINES 
//FROM PORT 2 (P2.7 AND P2.6) TO COMMUNICATE. 
//THE CODE SHOWN DEMONSTRATES A 'RANDOM READ' AND 'BYTE WRITE'. THE OTHER *
//MODES OF OPERATION CAN BE CREATED BY EXPANDING UPON THESE ROUTINES.
//=============================

//#include  <reg52.h>
#include <stchardware.h>
#include<intrins.h>
//#define uchar unsigned char
//#define uint unsigned int
#define IIC_READ 0XA1    //定义读指令
#define IIC_WRITE 0XA0   //定义写指令
#define NOP _nop_()
//==============================
//unsigned char  idata buff[32];	//save the byte read out  form iic device in test operation
//unsigned char  readbuff[32];//测试数组,暂存写入读出数据
//uchar writebuff[32];
//===============================
sbit SDA=P1^4;//P1^1;//P1^2;//P1^1;//P1^2;//P2^7;	//THE SDA BIT IS PORT 2 BIT 7
sbit SCL=P1^7;//P1^0;//P3^5;//P1^3;//P2^6;	//THE SCL BIT IS PORT 2 BIT 6
//sbit SDA=P2^7;	//THE SDA BIT IS PORT 2 BIT 7
//sbit SCL=P2^6;	//THE SCL BIT IS PORT 2 BIT 6
sbit WP=P2^5;

sbit RUN=P1^0;
//===============================
//define a bit_operation byte to use in shift operation
//use this mode can achieve high operation speed 
/*
unsigned char bdata bbyte;
sbit a0=bbyte^0;
sbit a1=bbyte^1;
sbit a2=bbyte^2;
sbit a3=bbyte^3;
sbit a4=bbyte^4;
sbit a5=bbyte^5;
sbit a6=bbyte^6;
sbit a7=bbyte^7;
*/
unsigned char bdata bbyte;//定义位操作用数组,采用此方法可提高位操作速度
sbit c0=bbyte^0;
sbit c1=bbyte^1;
sbit c2=bbyte^2;
sbit c3=bbyte^3;
sbit c4=bbyte^4;
sbit c5=bbyte^5;
sbit c6=bbyte^6;
sbit c7=bbyte^7;
//========================================
unsigned char idata buff[32];	//save the byte read out  form iic device in test operation
unsigned char idata readbuff[32];//测试数组,暂存写入读出数据
unsigned char readdata=0x16;
//uchar writebuff[32];
bit IFACK;	//record the SDA state to confirn if ACK has happened 
bit NO_ACK;	//no ack flag
bit BUS_FAULT;	//bus fault flag
//========================================
void star(void);
void stop(void);
void ack(void);
void nack(unsigned char type);
unsigned char  inbyte(void);
void outbyte(unsigned char outdata);
void writebyte(unsigned int add,unsigned char wbyte);
//unsigned char readbyte (unsigned int add);
void readbyte(unsigned int add);
void writebyteseq(unsigned int add,unsigned char *ptr,unsigned int writelen);
void readbyteseq(unsigned int add,unsigned char *ptr,unsigned int readlen);
void testfm24c04(void);
//extern sbit RUN ;
extern void Dog();
extern void mDelay(unsigned int j);
//========================================
//FUNCTION:ROUTES TO PROVIDE A START SIGNAL
void start(void)
{
SDA=1;
SCL=1;
NOP;
SDA=0;
NOP;
SCL=0;
//	SCL=0;SDA=1;SCL=1;SDA=0;SCL=0;
}
//=======================================
//FUNCTION:ROUTES TO PROVIDE A STOP SIGNAL
void stop(void)
{
SDA=0;
SCL=1;
NOP;
SDA=1;
NOP;
SCL=0;
//	SCL=0;SDA=0;SCL=1;SDA=1;SCL=0;
}
//=====================================
//FUNCTION:ROUTES TO PROVIDE ACK SINGAL
void ack(void)
{
	SCL=0;SDA=0;SCL=1;SCL=0;
}
//=====================================
//FUNCTION:ROUTES TO RELEASE THE SDA TO RECEIVE A ACK SIGNAL 
//	             OR TO PROVIDE A NO_ACK SIGNAL
//type=1等待应答信号
//type=0 产生无应答信号
void nack(unsigned char type)
{


	SCL=0;SDA=1;SCL=1; SCL=0;
//	IFACK=SDA;
	if(type)
	{
		if(SDA)//如果无应答信号,则置标志位NO_ACK,程序中止
		{	
			NO_ACK=1;//
			while(SDA){Dog();}
			//while(1);
		}
		else NO_ACK=0;
	}
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT OUT A BYTE  TO THE MASTER
unsigned char  inbyte(void) {//从IIC器件中读出数据
	SDA=1;
	SCL=1;c7=SDA;SCL=0;
	SCL=1;c6=SDA;SCL=0;
	SCL=1;c5=SDA;SCL=0;
	SCL=1;c4=SDA;SCL=0;
	SCL=1;c3=SDA;SCL=0;
	SCL=1;c2=SDA;SCL=0;
	SCL=1;c1=SDA;SCL=0;
	SCL=1;c0=SDA;SCL=0;
	return(bbyte);
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT IN A BYTE FROM THE MASTER 
void outbyte(unsigned char outdata) {//将数据写放IIC器件
	bbyte=outdata;
	SDA=c7;SCL=1;SCL=0;
	SDA=c6;SCL=1;SCL=0;
	SDA=c5;SCL=1;SCL=0;
	SDA=c4;SCL=1;SCL=0;
	SDA=c3;SCL=1;SCL=0;
	SDA=c2;SCL=1;SCL=0;
	SDA=c1;SCL=1;SCL=0;
	SDA=c0;SCL=1;SCL=0;
}
//======================================================
//FUNCTION:BYTE WRITE.  'add'     THE WRITE ADDRESS, 'wbyte'   THE DATA WANT TO WRITE
void writebyte(unsigned int add,unsigned char wbyte)//add为写入地址,wbyte为写入数据
{
	unsigned char temp;
	temp=IIC_WRITE;
	if(add>>8){temp|=0x02;}
	start(); 			//开始信号
	outbyte(temp);			//写命令
	nack(1);			//等待应答
	outbyte((unsigned char)add);		//写地址
	nack(1);			//等待应答
	outbyte(wbyte);			//写数据
	nack(1);			//等待应答
	stop();				//停止信号
}
/*
//========================test=============================
//FUNCTION:RANDOM READ.'add' IS THE ADDRESS WANT TO READ
void readbyte (unsigned int add)//add为读地址
{
	unsigned char temp,tempr;		
	temp=IIC_WRITE;
	tempr=IIC_READ;
	if(add>>8){temp|=0x02;tempr|=0x02;}
	start();			//开始信号
	outbyte(temp);			//写命令
	nack(1);			//等待应答
	outbyte((unsigned char)add);		//写地址
	nack(1);			//等待应答
	start();			//开始信号
	outbyte(tempr);			//读命令
	nack(1);			//等待应答
	temp=inbyte();			//读数据
	nack(0);			//无应答
	readdata=temp;
	stop();				//停止信号
//	return(temp);
}*/
//=====================================================
//FUNCTION:RANDOM READ.'add' IS THE ADDRESS WANT TO READ

unsigned char readbyte (unsigned int add)//add为读地址
{
	unsigned char temp,tempr;		
	temp=IIC_WRITE;
	tempr=IIC_READ;
	if(add>>8){temp|=0x02;tempr|=0x02;}
	start();			//开始信号
	outbyte(temp);			//写命令
	nack(1);			//等待应答
	outbyte((unsigned char)add);		//写地址
	nack(1);			//等待应答
	start();			//开始信号
	outbyte(tempr);			//读命令
	nack(1);			//等待应答
	temp=inbyte();			//读数据
	nack(0);			//无应答
	stop();				//停止信号
	return(temp);
}
//=================================================
//连写函数
//add为读起始地址,ptr数据保存指针,writelen为写入数据长度
void writebyteseq(unsigned int add,unsigned char *ptr,unsigned int writelen)
{
	unsigned char temp;
	unsigned int i;
	temp=IIC_WRITE;
	if(add>>8){temp|=0x02;}
	start();
	outbyte(temp);	
	nack(1);
		
	outbyte((unsigned char)add);
	nack(1);
	for(i=0;i<writelen;i++)
	{
		outbyte(*(ptr+i));
		nack(1);
	}
	stop();	
}
//=================================================
//连读函数
//add为读起始地址,ptr数据保存指针,writelen为读出数据长度
void readbyteseq(unsigned int add,unsigned char *ptr,unsigned int readlen)
{
	unsigned char temp,tempr;
	unsigned int i;
	temp=IIC_WRITE;
	tempr=IIC_READ;
	if(add>>8){temp|=0x02;tempr|=0x02;}
	start();
	outbyte(temp);
	nack(1);
	outbyte((unsigned char)add);
	nack(1);
	start();
	outbyte(tempr);
	nack(1);
	for(i=0;i<readlen-1;i++)
	{
		*(ptr+i)=inbyte();
		ack();		
	}
	*(ptr+readlen-1)=inbyte();
	nack(0);
	stop();
}
//=======================================================
//THIS IS A TEST OPERATION 
void testfm24c04(void)
{
//	unsigned char i,j=0;
	WP=0;
//	while(1)
//	{
//===========================================================
	writebyte(0x10,readdata);
	readdata=0x00;
	readbyte(0x10);
	RUN=0;
//===========================================================	
/*		for(i=0;i<32;i++) readbuff[i]=0X55;	//RUN=0;
		writebyteseq(300, readbuff, 32);	//RUN=0;
		for(i=0;i<32;i++) readbuff[i]=0;
		readbyteseq(300,readbuff,32);
		for(i=0;i<10;i++){mDelay(1000);Dog();}
		RUN=0;
	//	for(i=0;i<32;i++)
	//	{	
//			if(readbuff[0]==0x55)RUN=0;
	//	 	j++;
	//	}
	//	if(j==32)
		
//		else RUN=1;
	
//		for(i=0;i<10;i++){mDelay(1000);Dog();}
//	}
*/
}

⌨️ 快捷键说明

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