电路设计,软件开发,专业项目团队承接电子产品开发!


ISPRO下载型编程器
火爆销售中

RS232/TTL转换器
全IC串口窃电

RS232/TTL转换线
全IC串口窃电

ByteBlasterMV Cable
CPLD/FPGA并口下载线

ByteBlaster II Cable ISPRO下载型编程器
支持S51/AVR/PIC
超级51开发板套件
前卫实用超值

PC/MCU/CPLD
三合一学习套件

全新万年历方案
AVR单片机开发板
 

Min51单片机仿真机
迷你型

ARM7开发板
仅售260元!
 

RS-232/RS-485双向接口电平转换器  

51单片机升级STM32
火爆销售中
MPLAB-ICD2
火爆销售中
LED51开发板
玩具又名图文显示棒

面板模块/开发板
经济危机明智决择

       

GSM MODEM GSM猫

SMS短信模块 工业级/全自动
火爆销售中

大功率数传电台

高速数传电台

您当前位置:电子技术资料 -> 外围驱动 -> EEPROM ->

祝您阅读愉快!

ISPRO下载型编程器

RS232 TO TTL转换器
↑↑本文相关产品介绍↑↑

AT24CXX 芯片驱动程序


类别:技术文章
来源:未知
作者:未知
关键字:AT24CXX
加入日期:2010-8-3

// AT24CXX 芯片驱动程序
// MCU : AT89C52
// XTAL : 11.0592M
// Note : 1 > 需要定义以下宏
// AT24C01 - 芯片类型 \
// AT24C02 - 芯片类型 \
// AT24C04 - 芯片类型 \
// AT24C08 - 芯片类型 > 其中必须定义且仅能一个
// AT24C16 - 芯片类型 /
// AT24C32 - 芯片类型 /
// AT24C64 - 芯片类型 /
// 2 > 需要定义以下口线
// AT24CXX_SCLK - 芯片时钟口线
// AT24CXX_IO - 芯片数据口线
// AT24CXX_WP - 芯片写保护控制口线
// 中国电子网 www.EC66.com
//////////////////////////////////////////////////////////////////

#ifndef __AT24CXX_H__
#define __AT24CXX_H__
//////////////////////////////////////////////////////////////////

#include < StyleDef.H >

//////////////////////////////////////////////////////////////////
// AT24CXX 芯片类型及参数宏定义
//////////////////////////////////////////////////////////////////
#if defined( AT24C01 )
#define AT24CXX_PAGESIZE 8
#define AT24CXX_SIZE 128
#elif defined( AT24C02 )
#define AT24CXX_PAGESIZE 8
#define AT24CXX_SIZE 256
#elif defined( AT24C04 )
#define AT24CXX_PAGESIZE 16
#define AT24CXX_SIZE 512
#elif defined( AT24C08 )
#define AT24CXX_PAGESIZE 16
#define AT24CXX_SIZE 1024
#elif defined( AT24C16 )
#define AT24CXX_PAGESIZE 16
#define AT24CXX_SIZE 2048
#elif defined( AT24C32 )
#define AT24CXX_PAGESIZE 32
#define AT24CXX_SIZE 4096
#elif defined( AT24C64 )
#define AT24CXX_PAGESIZE 32
#define AT24CXX_SIZE 8192
#else
#error Must defined macro AT24C?? , please see < At24CXX.H >.
#endif

//////////////////////////////////////////////////////////////////
// 函数声明
//////////////////////////////////////////////////////////////////
VOID AT24CXX_Init( VOID );
VOID AT24CXX_SetState( BOOL bBitLevel );
BOOL AT24CXX_Enabled( VOID );
VOID AT24CXX_SetWriteAddress( WORD wAddress );
VOID AT24CXX_SetReadAddress( WORD wAddress );
BOOL AT24CXX_Write( BYTE ucData );
VOID AT24CXX_WriteFullPage( WORD wAddress , PBYTE pBuffer );
VOID AT24CXX_WritePartPage( WORD wAddress , PBYTE pBuffer , BYTE ucSize );
VOID AT24CXX_WriteBuffer( WORD wAddress , PBYTE pBuffer , BYTE ucSize );
BYTE AT24CXX_Read( WORD wAddress );
VOID AT24CXX_ReadBuffer( WORD wAddress , PBYTE pBuffer , BYTE ucSize );

//////////////////////////////////////////////////////////////////
宏函数定义
//////////////////////////////////////////////////////////////////
#define AT24CXX_Start() AT24CXX_SetState( 1 )
#define AT24CXX_Stop() AT24CXX_SetState( 0 )

//////////////////////////////////////////////////////////////////
函数定义
//////////////////////////////////////////////////////////////////
VOID AT24CXX_Init( VOID )
{
BYTE ucVar = 0;
do {
AT24CXX_SCLK = LOW;
AT24CXX_IO = HIGH;
AT24CXX_SCLK = HIGH;
ucVar += 1;
}
while ( ( ucVar < 10 ) && ( AT24CXX_IO == LOW ) );
AT24CXX_Start();
}
//////////////////////////////////////////////////////////////////

VOID AT24CXX_SetState( BOOL bBitLevel )
{
AT24CXX_SCLK = LOW;
AT24CXX_IO = bBitLevel;
AT24CXX_SCLK = HIGH;
AT24CXX_IO = ~bBitLevel;
}
//////////////////////////////////////////////////////////////////

BOOL AT24CXX_Enabled( VOID )
{
AT24CXX_Start();
return ( ~AT24CXX_Write( 0xA0 ) );
}
//////////////////////////////////////////////////////////////////

VOID AT24CXX_SetWriteAddress( WORD wAddress )
{
BYTE ucCtrl = 0xA0;
#if AT24CXX_PAGESIZE < 32
ucCtrl |= ( BYTE )( ( ( wAddress ) >> 7 ) & 0x0e );
AT24CXX_Write( ucCtrl );
AT24CXX_Write( ( BYTE )( wAddress & 0xff ) );
#else
AT24CXX_Write( ucCtrl );
AT24CXX_Write( ( BYTE )( wAddress >> 8 ) );
AT24CXX_Write( ( BYTE )( wAddress & 0xff ) );
#endif
}
//////////////////////////////////////////////////////////////////

VOID AT24CXX_SetReadAddress( WORD wAddress )
{
BYTE ucCtrl = 0xA1;
#if AT24CXX_PAGESIZE < 32
ucCtrl |= ( BYTE )( ( ( wAddress ) >> 7 ) & 0x0e );
#endif
AT24CXX_SetWriteAddress( wAddress );
AT24CXX_Start();
AT24CXX_Write( ucCtrl );
}
//////////////////////////////////////////////////////////////////

BOOL AT24CXX_Write( BYTE ucData )
{
BYTE ucVar;
BIT bitAcknowledge;
for ( ucVar = 0 ; ucVar < 8 ; ucVar ++ ) {
AT24CXX_SCLK = LOW;
AT24CXX_IO = ( ( ucData & 0x80) == 0x80 );
AT24CXX_SCLK = HIGH;
ucData <<= 1;
}
AT24CXX_SCLK = LOW;
AT24CXX_IO = HIGH;
AT24CXX_SCLK = HIGH;
bitAcknowledge = AT24CXX_IO;
AT24CXX_IO = HIGH;
return ( bitAcknowledge );
}
//////////////////////////////////////////////////////////////////

VOID AT24CXX_WriteFullPage( WORD wAddress , PBYTE pBuffer )
{
BYTE ucVar;

while ( AT24CXX_Enabled() == FALSE );
AT24CXX_Start();
AT24CXX_SetWriteAddress( wAddress );
for ( ucVar = 0 ; ucVar < AT24CXX_PAGESIZE ; ucVar ++ )
AT24CXX_Write( pBuffer[ ucVar ] );
AT24CXX_Stop();
}
//////////////////////////////////////////////////////////////////

VOID AT24CXX_WritePartPage( WORD wAddress , PBYTE pBuffer , BYTE ucSize )
{
BYTE ucVar;
while ( AT24CXX_Enabled() == FALSE );
AT24CXX_Start();
AT24CXX_SetWriteAddress( wAddress );
for ( ucVar = 0 ; ucVar < ucSize ; ucVar ++ )
AT24CXX_Write( pBuffer[ ucVar ] );
AT24CXX_Stop();
}
//////////////////////////////////////////////////////////////////

VOID AT24CXX_WriteBuffer( WORD wAddress , PBYTE pBuffer , BYTE ucSize )
{
BYTE ucVar = 0;
AT24CXX_WP = LOW;
if ( ( wAddress % AT24CXX_PAGESIZE ) != 0 ) {
ucVar = AT24CXX_PAGESIZE - ( wAddress %
AT24CXX_PAGESIZE );
if ( ucVar > ucSize )
ucVar = ucSize;
AT24CXX_WritePartPage( wAddress , pBuffer , ucVar );
wAddress += ucVar;
pBuffer += ucVar;
ucSize -= ucVar;
}
ucVar = ucSize / AT24CXX_PAGESIZE;
while ( ucVar > 0 ) {
AT24CXX_WriteFullPage( wAddress , pBuffer );
wAddress += AT24CXX_PAGESIZE;
pBuffer += AT24CXX_PAGESIZE;
ucSize -= AT24CXX_PAGESIZE;
ucVar -= 1;
}
if ( ucSize > 0 )
AT24CXX_WritePartPage( wAddress , pBuffer , ucSize );
AT24CXX_WP = HIGH;
}
//////////////////////////////////////////////////////////////////

BYTE AT24CXX_Read( WORD wAddress )
{
BYTE ucData = 0;
BYTE ucVar;
BIT bitAcknowledge;
while ( AT24CXX_Enabled() == FALSE );
AT24CXX_Start();
AT24CXX_SetReadAddress( wAddress );
for ( ucVar = 0 ; ucVar < 8 ; ucVar ++ ) {
AT24CXX_SCLK = LOW;
AT24CXX_IO = HIGH;
ucData <<= 1;
AT24CXX_SCLK = HIGH;
ucData |= AT24CXX_IO;
}
AT24CXX_SCLK = LOW;
AT24CXX_IO = HIGH;
AT24CXX_SCLK = HIGH;
bitAcknowledge = AT24CXX_IO;
AT24CXX_IO = HIGH;
AT24CXX_Stop();
return ( ucData );
}
//////////////////////////////////////////////////////////////////

VOID AT24CXX_ReadBuffer( WORD wAddress , PBYTE pBuffer , BYTE ucSize )
{
BYTE ucVar1 , ucVar2;
BYTE ucData;

while ( AT24CXX_Enabled() == FALSE );
AT24CXX_Start();
AT24CXX_SetReadAddress( wAddress );

for ( ucVar1 = 0 ; ucVar1 < ucSize ; ) {
ucData = 0;
for ( ucVar2 = 0 ; ucVar2 < 8 ; ucVar2 ++ ) {
AT24CXX_SCLK = LOW;
AT24CXX_IO = HIGH;
ucData <<= 1;
AT24CXX_SCLK = HIGH;
ucData |= AT24CXX_IO;
}
pBuffer[ ucVar1 ] = ucData;
ucVar1++;
AT24CXX_SCLK = LOW;
AT24CXX_IO = ( ucVar1 == ucSize );
AT24CXX_SCLK = HIGH;
}
AT24CXX_SCLK = LOW;
AT24CXX_SCLK = HIGH;
AT24CXX_IO = HIGH;
AT24CXX_Stop();
}
//////////////////////////////////////////////////////////////////

#endif

补充的库定义 [EaStar1110] [16次] 02-1-10 上午 10:27:10
#ifndef __STYLEDEF_H__
#define __STYLEDEF_H__
//////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
数据类型宏定义
//////////////////////////////////////////////////////////////////
#ifndef BYTE
#define BYTE unsigned char
#endif

#ifndef CHAR
#define CHAR signed char
#endif

#ifndef PBYTE
#define PBYTE BYTE *
#endif

#ifndef PXBYTE
#define PXBYTE BYTE xdata *
#endif

#ifndef PCHAR
#define PCHAR CHAR *
#endif

#ifndef PXCHAR
#define PXCHAR CHAR xdata *
#endif

#ifndef WORD
#define WORD unsigned int
#endif

#ifndef INT
#define INT signed int
#endif

#ifndef VOID
#define VOID void
#endif

#ifndef PWORD
#define PWORD WORD *
#endif

#ifndef PINT
#define PINT INT *
#endif

#ifndef PVOID
#define PVOID VOID *
#endif

#ifndef DWORD
#define DWORD unsigned long
#endif

#ifndef LONG
#define LONG signed long
#endif

#ifndef PDWORD
#define PDWORD DWORD *
#endif

#ifndef PLONG
#define PLONG LONG *
#endif

#ifndef BIT
#define BIT bit
#endif

#ifndef BOOL
#define BOOL bit
#endif

#ifndef EXT_EEPROM
#define EXT_EEPROM xdata
#endif

////////////////////////////////////////////////////////////////////
// 数据常量宏定义
////////////////////////////////////////////////////////////////////

#define FALSE 0
#define TRUE 1

#define DISABLE 0
#define ENABLE 1

#define LOW 0
#define HIGH 1

#define READ 0
#define WRITE 1

#define NULL 0

#define BYTE_SIZE 1
#define WORD_SIZE 2

////////////////////////////////////////////////////////

#endif


您可能还会关注的相关技术资料:
暂无

中国电子网
www.EC66.com
QQ:34740080 EMAIL:iloveaoe@163.com
最佳浏览:1024*768
Copyright (C) 2001-2010 EC66.COM All Rights Reserved
Aoe/帝国 制作维护
粤ICP备18061901号