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

📄 lib24c16.c

📁 eeprom program for 8051 and atmel at89c52 or At89s52
💻 C
字号:
//**********************************************************************
//* PROJECT: Library for 24c01/02/04/08/16 serial EEPROM
//* Copyright (C) 2003, Pedro Ignacio Martos (pi_martos@hotmail.com)
//**********************************************************************
//* FILE: lib24c16.c
//**********************************************************************
//* CHANGES:
//* date      author                                   description
//* --------------------------------------------------------------------
//* 07/29/03  pmartos (pi_martos@hotmail.com)          initial release
//**********************************************************************
//* DESCRIPTION:
//* This file contains a simple read/write routines for I2C EEPROMS up 
//* to 2KBytes; based on AN 614 from Microchip
//* (http://www.microchip.com)
//**********************************************************************
//  This file is part of lib24c16
//*
//* lib24c16 is free software; you can redistribute it and/or modify
//* it under the terms of the GNU General Public License as published by
//* the Free Software Foundation; either version 2 of the License, or
//* (at your option) any later version.
//*
//* lib24c16 is distributed in the hope that it will be useful,
//* but WITHOUT ANY WARRANTY; without even the implied warranty of
//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//* GNU General Public License for more details.
//*
//* You should have received a copy of the GNU General Public License
//* along with lib24c16; if not, write to the Free Software
//* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
//* USA
//**********************************************************************
//* FUNCTIONS DECLARED:
//* void weeprom (char page, char direction, char datum); 
//* char reeprom (char page, char direction);
//* char read (char readcmd); 
//* void outs (char datum);
//* void out (char datum);
//* char in (void);
//* void stop (void);
//**********************************************************************
//* NOTES:
//* you need to define the port pins where the memory is connected like
//* this:
//* bit	at	0x90	SCK; // P1_0 = EEPROM SCK
//* bit at	0x91	SDA; // P1_1 = EEPROM SDA
//*
//* - For parts other than 24c16, 'page' must be set according to the
//*   logical values in pins A0, A1 & A2 (see particular datasheet)
//* - Remember the 10mS busy time in writing cycle
//* - The routines closely resembles the assembler code of the AN
//**********************************************************************
//* COMPILE TIME OPTIONS: -C (no linking)
//* DEBUG OPTIONS: None
//**********************************************************************
//* TO DO:
//*       	- Reeplace NOPs with a NOPS() routine
//*         - Allow some form of selection of bit rate
//*			- Reeplace outs() with a start() + out() routines
//**********************************************************************


// Port pin declaration
extern volatile bit SCK;
extern volatile bit SDA;


// Prototipe declaration
void weeprom (char page, char address, char datum);
char reeprom (char page, char address);
char read (char readcmd);
void outs (char datum);
void out (char datum);
char in (void);
void stop (void);

//**********************************************************************
//* Function Name: void weeprom (char page, char address, char datum)
//**********************************************************************
//* Function Description: write routine for I2C EEPROM
//**********************************************************************
//* Input parameters:
//* - char page: number of 256 bytes block to use
//* - char address: position inside the 256 bytes block
//* - char datum: 1 byte data to write
//**********************************************************************
//* Output parameters: NONE
//**********************************************************************
/// Notes: NONE
//**********************************************************************
void weeprom (char page, char address, char datum)
{
	char WRCMD; // auxiliary storage to build the write command
	page = page << 1; // move A0, A1 & A2 to their positions
	page = page & 0xFE; // clear r/w bit
	WRCMD = page | 0xA0; // build the write command
	outs (WRCMD); // send the write command with start condition
	out (address); // send address
	out (datum); // send data
	stop (); // send stop condition
}

//**********************************************************************
//* Function Name: char reeprom (char page, char address)
//**********************************************************************
//* Function Description: read routine for I2C EEPROM
//**********************************************************************
//* Input parameters:
//* - char page: number of 256 bytes block to use
//* - char address: position inside the 256 bytes block
//**********************************************************************
//* Output parameters:
//* - char with the data from memory
//**********************************************************************
/// Notes: NONE
//**********************************************************************
char reeprom (char page, char address)
{
	char aux; // auxiliary storage
	char WRCMD; // aux. storage for the write command (to send the add.)
	aux = page; // preparing the write command
	aux = aux << 1; // move A0, A1 & A2 to their positions 
	aux = aux & 0xFE; // clear r/w bit
	WRCMD = aux | 0xA0; // build the write command
	outs (WRCMD); // send the write command with start condition
	out (address); // send address
	aux = read (WRCMD); // send read command and receive data
	return (aux); // return solicited data 
}

//**********************************************************************
//* Function Name: char read (char readcmd)
//**********************************************************************
//* Function Description: reads 1 byte from current memory position
//**********************************************************************
//* Input parameters:
//* - char readcmd: read command byte with A0, A1 & A2 embedded
//**********************************************************************
//* Output parameters:
//* - char with current address data
//**********************************************************************
/// Notes: NONE
//**********************************************************************
char read (char readcmd)
{
	char RDCMD; // auxiliary storage to build the read command
	char aux; // auxiliary storage
	RDCMD = readcmd | 0x01; // set r/w bit
	outs (RDCMD); // send read command with start condition
	aux = in(); // read current position
	stop (); // send stop condition
	return (aux); // return current position data
}


//**********************************************************************
//* Function Name: void outs (char datum)
//**********************************************************************
//* Function Description: sends to memory the input parameter with a
//*                       start condition
//**********************************************************************
//* Input parameters:
//* - char datum: data byte to send to the memory
//**********************************************************************
//* Output parameters: NONE
//**********************************************************************
/// Notes: NONE
//**********************************************************************
void outs (char datum)
{
	char i; // index 
	char aux; // auxiliary storage
	SDA = 1; // set port pin SDA to insure data is HI
	SCK = 1; // set port pin SCK to insure clock is HI
_asm
	nop
	nop
	nop
	nop
	nop
_endasm;
	SDA = 0; // start condition, data = 0
_asm
	nop
	nop
	nop
	nop
	nop
_endasm;
	SCK = 0; // clock = 0
	for (i = 0; i < 8; i++) // bit shifting cycle
	{
		aux = datum & 0x80; // check MSB bit
		if (aux == 0) // MSB = 0
			SDA = 0; // then SDA = 0
		else
			SDA = 1; // else MSB =1, then SDA = 1
		SCK = 1; // clock = 1
_asm
		nop
		nop
		nop
		nop
		nop
_endasm;
		SCK = 0; // clock = 0
		datum = datum << 1; // rotate for next bit 
	}
	SDA = 1; // set port pin for ack
_asm
	nop
	nop
	nop
_endasm;
	SCK = 1; // clock ack
_asm
	nop
	nop
	nop
	nop
	nop
_endasm;
	SCK = 0; // clock = 0
}


//**********************************************************************
//* Function Name: void out (char datum)
//**********************************************************************
//* Function Description: sends to memory the input parameter without a
//*                       start condition
//**********************************************************************
//* Input parameters:
//* - char datum: data byte to send to the memory
//**********************************************************************
//* Output parameters: NONE
//**********************************************************************
/// Notes: NONE
//**********************************************************************
void out (char datum)
{
	char i; // index
	char aux; // auxiliary storage
	for (i = 0; i < 8; i++) // bit shifting cycle
	{
		aux = datum & 0x80; // check MSB bit
		if (aux == 0) // MSB = 0
			SDA = 0; // then SDA = 0
		else
			SDA = 1; // else MSB = 1, the SDA = 1
		SCK = 1; // clock = 1
_asm
		nop
		nop
		nop
		nop
		nop
_endasm;
		SCK = 0; // clock = 0
		datum = datum << 1; // rotate for next bit
	}
	SDA = 1; // set port pin for ack
_asm
	nop
	nop
	nop
_endasm;
	SCK = 1; // clock ack
_asm
	nop
	nop
	nop
	nop
	nop
_endasm;
	SCK = 0; // clock = 0
}


//**********************************************************************
//* Function Name: char in (void)
//**********************************************************************
//* Function Description: receives a byte from memory 
//**********************************************************************
//* Input parameters: NONE
//**********************************************************************
//* Output parameters: 
//* - char with the byte received from memory
//**********************************************************************
/// Notes: NONE
//**********************************************************************
char in (void)
{
	char i; // index
	char aux = 0; // auxiliary storage
	SDA = 1; // insure port pin = 1 for input
	for (i = 0; i < 8; i++) // bit shifting cycle
	{
		SCK = 0; // clock = 0
_asm
		nop
		nop
		nop
		nop
		nop
		nop
_endasm;
		SCK = 1; // clock = 1
		aux = aux << 1; // load bit position
		if (SDA) // check SDA data from port pin
			aux = aux | 0x01; // if port pin = 1, set LSB (bit position)
		else
			aux = aux & 0xFE; // else port pin = ,clear LSB (bit position)
	}
	SCK = 0; // clock = 0
	return (aux); // return data received
}


//**********************************************************************
//* Function Name: void stop (void)
//**********************************************************************
//* Function Description: send stop condition 
//**********************************************************************
//* Input parameters: NONE
//**********************************************************************
//* Output parameters: NONE
//**********************************************************************
/// Notes: NONE
//**********************************************************************
void stop (void)
{
	SDA = 0; // stop condition, data = 0
_asm
	nop
	nop
	nop
	nop
	nop
_endasm;
	SCK = 1; // clock = 1
_asm
	nop
	nop
	nop
	nop
	nop
_endasm;
	SDA = 1; // data = 1
}

⌨️ 快捷键说明

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