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

📄 eeprom.c

📁 profibus TO RS485 profibus TO RS485
💻 C
字号:
#include	<reg52.h>
#include	"link.h"
//对EEprom进行操作的指令的宏定义
///////////////////////////////////////////////////////////////////////////
//#define EE_SO	   	SO&0x01	 /*25045输出*/
#define EE_SI1		SI=1	//对25045输出1
#define EE_SI0		SI=0	//对25045输出0
#define EE_CS1		HW_WATCHDOG_TRIGGER=1/*25045输出*/
#define EE_CS0		HW_WATCHDOG_TRIGGER=0/*25045输出*/
#define EE_SCK1		SCK=1/*25045输出*/
#define EE_SCK0		SCK=0/*25045输出*/

///////////////////////////////////////////////////////////////////////////
//EEprom的一些宏定义
///////////////////////////////////////////////////////////////////////////
#define EE_WREN_INST 	0X06	// Write enable latch instruction (WREN)
#define EE_WRDI_INST 	0X04	// Write disable latch instruction (WRDI)
#define EE_WRSR_INST 	0X01	// Write status register instruction (WRSR)
#define EE_RDSR_INST 	0X05	// Read status register instruction (RDSR)
#define EE_WRITE_INST 0X02	//写初始化命令
#define EE_READ_INST	0X03	//读初始化命令
#define EE_CONFIG_PRI_ADDR 	0X0000	//配置信息区首地址
#define EE_PRAM_PRI_ADDR  	0X0020	//参数化信息区首地址
#define EE_TEMPER_PRI_ADDR 	0x0040	//温度较正区首地址
#define EE_PRECI_PRI_ADDR 	0x00b0	//精度较正区首地址
#define EE_STATUS_REG 			0X20		//EEprom状态寄存器命令
#define EE_MAX_POLL 				0x99		//EEprom最大写入等待时间
#define EE_INIT_STATE 	    0x09		// Initialization value for control ports
#define EE_SLIC 	 					0x30	/* Address location of SLIC*/
#define CUT_BIT0	 		0x01	//定义一个截取第一位的变量
#define CUT_BIT7	 		0x80	//定义一个截取第七位的变量

sbit  HW_WATCHDOG_TRIGGER = P3^5;   /* HW-WD输入端   */
sbit EE_SO=P3^0;	/*25045输出*/
sbit SI=P3^3;	/*25045输入*/
sbit SCK=P3^1;	/*25045时钟*/

void EE_wren_cmd(void)
{
	EE_CS1;
	EE_SCK0;/* Bring SCK low */
	EE_CS0;/* Bring /CS low */
	EE_out_byte(EE_WREN_INST);/* Send WREN instruction */
	EE_SCK0;/* Bring SCK low */
	EE_CS1;/* Bring /CS high */
}


void EE_wrdi_cmd(void)
{
 	EE_SCK0;/* Bring SCK low */
        EE_CS0;/* Bring /CS low */
 	EE_out_byte(EE_WRDI_INST);/* Send WRDI instruction */
 	EE_SCK0;/* Bring SCK low */
 	EE_CS1;/* Bring /CS high */
}


void EE_wrsr_cmd(uchar uc_reset_time)
{
	EE_SCK0;/* Bring SCK low */
 	EE_CS0;/* Bring /CS low */
 	EE_out_byte(EE_WRSR_INST) ;/* Send WRSR instruction */
 	EE_out_byte(uc_reset_time);/* Send status register */
 	EE_SCK0;/* Bring SCK low */
 	EE_CS1;/* Bring /CS high */
	EE_wip_poll();/* Poll for completion of write cycle */
}




uchar EE_rdsr_cmd (void)	//读EEprom状态寄存器
{
 	uchar uc_EE_data_temp;
 	EE_SCK0;
 	EE_CS0;
 	EE_out_byte(EE_RDSR_INST);
 	uc_EE_data_temp=EE_input_byte();
 	EE_SCK0;
 	EE_CS1;
 	return uc_EE_data_temp;
}


void EE_byte_write(uchar uc_EE_data,uint ui_EE_address) //EEprom写一字节
{
	EE_CS1;
 	EE_SCK0;
 	EE_CS0;
 	EE_out_byte((((uchar)(ui_EE_address>>8))<<3)|EE_WRITE_INST);//将高位地址左移3位与写入先导字相或,得到正确的先导字写入25045
 	EE_out_byte(((uchar)(ui_EE_address)));//输出低位地址到25045
 	EE_out_byte(uc_EE_data); //写入数据到25045的对应单元
 	EE_SCK0;
 	EE_CS1;
 	EE_wip_poll(); //检测是否写完
}


uchar EE_byte_read(uint ui_EE_address)
{
	uchar uc_EE_data_temp=0;
//	set_LED_CS4();
	EE_CS1;
 	EE_SCK0;
 	EE_CS0;

 	/*将高位地址左移3位与读出先导字相或,得到正确的先导字写入25045*/
 	EE_out_byte((((uchar)(ui_EE_address>>8))<<3)|EE_READ_INST);/* Send READ_INST instruction including MSB of address */
 	/*输出低位地址到25045*/
 	EE_out_byte((uchar)(ui_EE_address));
 	uc_EE_data_temp=EE_input_byte();/*得到读出的数据*/

 	EE_SCK0;
 	EE_CS1;
 	return uc_EE_data_temp;
}


void EE_rst_wdog (void)   //复位看门狗
{
unsigned 	char i;
 	EE_CS0;
 	delay(10);
 	EE_CS1;
}


void EE_wip_poll(void)		//等待写入结束
{
 	uchar aa;
 	uchar  uc_EE_date_temp;
 	for (aa=1;aa<=EE_MAX_POLL;aa++)
 	{
  		uc_EE_date_temp=EE_rdsr_cmd();
  		if ((uc_EE_date_temp&CUT_BIT0)==0)
  			aa=EE_MAX_POLL; /*判断是否WIP=0,即判断是否写入过程已经结束,若结束就跳出,否则继续等待直到达到最大记数值*/
 	}
}


void  EE_out_byte(uchar uc_EE_data)  //输出一字节到EEprom中
{
 	uchar uc_EE_date_temp,ii;
 	uc_EE_date_temp=uc_EE_data;
 	for (ii=0;ii<=7;ii++)
 	{
   		EE_SCK0;
  		if(uc_EE_date_temp&CUT_BIT7)	//数据是1还是0?
   		{
   		EE_SI1;
   		}	//输出高电平
   		else
   		{
   		EE_SI0;
   		}		//输出低电平
   		uc_EE_date_temp<<=1;
   		EE_SCK1;
 	}
 	EE_SI0;/*使SI处于确定的状态*/
}


uchar EE_input_byte(void)	//从EEprom输入一个字节
{
 	uchar uc_EE_data=0;
 	char ii;
// 	uchar ui;
 	for (ii=0;ii<=7;ii++)
 	{
        delay(10);
        EE_SCK0;
    	EE_SI1;	//读之前先往口上写1
        delay(10);
   		if(EE_SO)	//输入端口是否为1
    		uc_EE_data|=(CUT_BIT7>>ii);  //置相应位
 		EE_SCK1;
 	}
 	return uc_EE_data;
}


⌨️ 快捷键说明

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