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

📄 i2ceeprom.c

📁 Counter
💻 C
字号:
/*! \file i2ceeprom.c \brief Interface for standard I2C EEPROM memories. */
//*****************************************************************************
//
// File Name	: 'i2ceeprom.c'
// Title		: Interface for standard I2C EEPROM memories
// Author		: Pascal Stang - Copyright (C) 2003
// Created		: 2003.04.23
// Revised		: 2003.04.23
// Version		: 0.1
// Target MCU	: Atmel AVR series
// Editor Tabs	: 4
//
// This code is distributed under the GNU Public License
//		which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************

//#include <avr/io.h>
//#include <avr/signal.h>
//#include <avr/interrupt.h>

#include "i2c.h"
#include "i2ceeprom.h"

// Standard I2C bit rates are:
// 100KHz for slow speed
// 400KHz for high speed

// functions
void i2ceepromInit(void)
{
	// although there is no code here
	// don't forget to initialize the I2C interface itself
}

u08 i2ceepromReadByte(u08 i2cAddr, u32 memAddr)
{
	u08 packet[2];

	// prepare address
	packet[0] = (memAddr>>8);
	packet[1] = (memAddr&0x00FF);
	// send memory address we wish to access to the memory chip
	i2cMasterSendNI(i2cAddr, 2, packet);
	// retrieve the data at this memory address
	i2cMasterReceiveNI(i2cAddr, 1, packet);
	// return data

	return packet[0];
}

void i2ceepromWriteByte(u08 i2cAddr, u32 memAddr, u08 data)
{
	u08 packet[3];
	
	// prepare address + data
	packet[0] = (memAddr>>8);
	packet[1] = (memAddr&0x00FF);
	packet[2] = data;
	// send memory address we wish to access to the memory chip
	// along with the data we wish to write
	i2cMasterSendNI(i2cAddr, 3, packet);

        __delay_cycles (_XTALL * 10E-3);

}

u16 i2ceepromReadWord(u08 i2cAddr, u32 memAddr)
{
	u08 packetH,packetL;
	u16 retWord;
	
     packetH = i2ceepromReadByte( i2cAddr,  memAddr);
     packetL = i2ceepromReadByte( i2cAddr,  memAddr+1);

	retWord = packetH;
	retWord = retWord << 8;
	retWord = retWord + packetL;
	return retWord;
}	
void i2ceepromWriteWord(u08 i2cAddr, u32 memAddr, u16 data)
{
	u08 packetH,packetL;

        packetH = data >> 8;
	packetL = data;

        i2ceepromWriteByte( i2cAddr,  memAddr,   packetH);
        i2ceepromWriteByte( i2cAddr,  memAddr+1, packetL);




}
u08 i2ceepromReadString(u08 i2cAddr, u32 memAddr,u08 length, u08 *data)
{
	u08 i;

        for (i=0; i<length;i++)
          {
           *data++ = i2ceepromReadByte( i2cAddr,  memAddr+i);
          }

	return 1;
}

void i2ceepromWriteString(u08 i2cAddr, u32 memAddr, u08 length, u08 *data)
{
	// PORTG &= ~0x01;
  //u08 packet[3];
	// prepare address + data
	u08 i,a;
         for (i=0; i<length;i++)
          { a = *data++;
	      i2ceepromWriteByte(i2cAddr, memAddr+i, a);

          }
 //PORTG |= 0x01;
                // send memory address we wish to access to the memory chip
	// along with the data we wish to write
	
}

⌨️ 快捷键说明

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