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

📄 eeprom.c

📁 文件中代码实现对e2prom(at93c46/66)的读写
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include <AT8992m.h>
#include "ATConfig.h"
#include "typedef.h" 
#include ATAN_IC_CONFIG
#include "main.h"

#include "util.h"  	
#include "eeprom.h"
#include "uart.h"


int  gEepromType=6;

int EepromInit()
{
	CS = 0;
	SK = 0;
	DI = 1;
	DO = 1;
	Delay(1);
}


int SK_pulse()  
{
	SK = 0;
	SK = 1;
	SK = 1;
	SK = 1;
	SK = 0;
}



void EnableEepromWrite()
{        
    int counter,ewen_data = EWEN_DATA;

	CS = 1;


    for ( counter = 0 ; counter < EWEN_CON ; counter++ )
	{
		DI = ( ewen_data & LSB_MASK ) ? 1 : 0;
		SK_pulse();	
		ewen_data >>= 1;
    }

	for ( counter = 0 ; counter < ( gEepromType - 2 ) ; counter++)
	{
		DI = 0;	
		SK_pulse();
	}
	
	CS = ~CS;
}


void DisableEepromWrite()
{   
    int counter,ewds_data = EWDS_DATA;
	CS = 1;
    for ( counter = 0 ; counter < EWDS_CON ; counter++ )
	{
		DI = ( ewds_data & LSB_MASK ) ? 1 : 0;
		SK_pulse();	
		ewds_data >>= 1;
    }
	for ( counter = 0 ; counter < ( gEepromType - 2 ) ; counter++)
	{
		DI = 0;	
		SK_pulse();
	}
	CS = ~CS;
	        
}   
           

int EepromWriteWord(BYTE address, BYTE lowByte, BYTE highByte)
{
	int counter;    
	BYTE Opcode=OPCODE_WRITE;

	EepromInit();

	Delay(1);

	EnableEepromWrite();
	Delay(1);
	SK_pulse();
	Delay(1);
	

	// CS,DO must keep high when program eeprom!!
	CS = 1; 
	DO = 1;	
	// Instruction of write command (1) (01)   ( An..A0)   (D15....D0)
	//                                         93c46 n->5
	//                                         93c66 n->7
	// Send Start Bit -> 1
    DI = 1;
	SK_pulse();

	// Send OpcodeField (01)
	for ( counter = 0 ; counter < OPCODE_CON ; counter++ )
	{
		DI = ( Opcode & LSB_MASK ) ? 1 : 0 ;
		SK_pulse();
		Opcode >>= 1;
	}


	// Send Address Data 
	for ( counter = 0 ; counter < gEepromType ; counter++ )
	{
		if   ( gEepromType == 6 )	DI = ( address & 0x20 ) ? 1 : 0 ;
		if 	 ( gEepromType == 8 )	DI = ( address & 0x80 ) ? 1 : 0 ;
		SK_pulse();
		address <<= 1;
	}


	for ( counter = 0 ; counter < ONE_BYTE_LEN ; counter++ )
	{
		DI = ( highByte & MSB_MASK ) ? 1 : 0 ;
		SK_pulse();
		highByte <<= 1;
	}		
    
	for ( counter = 0 ; counter < ONE_BYTE_LEN ; counter++ )
	{
		DI = ( lowByte & MSB_MASK ) ? 1 : 0 ;
		SK_pulse();
		lowByte <<= 1;
	}		
             
    
	// CS -> LOW when program end!!
    CS = ~CS;            

	// Detect READY/BUSY status of eeprom!!
	CS = 1;	
	do {
		SK_pulse();
	} while (  DO == 1 );
	CS = ~CS;

    SK_pulse();
  	Delay(1);
	DisableEepromWrite();
	Delay(1);
	return 0;
}


⌨️ 快捷键说明

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