cpubbs论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

LabVIEW+单片机学习套件全套教程资料下载[免费]LabVIEW论坛精华列表贴USB0816数据采集卡《LabVIEW宝典》
LabWindows/CVI论坛精华贴NET0816以太网数据采集卡RC0210远程设备授权系统 关闭关停锁定打开设备 户外分布式数据采集
NET1624低速高精度以太网数据采集卡WIFI0824SD无线WIFI网络数据采集卡脱机运行 SD存储 小尺寸微型 串口采集远程采集 安卓 手持移动采集 纪录仪
查看: 1657|回复: 0

at24c1024读写C51程序[转贴]

[复制链接]
发表于 2009-7-26 10:48:33 | 显示全部楼层 |阅读模式
原贴地址: http://www.pudn.com/search_db.asp?keyword=AT24C1024



/*实验名称:读写AT24C02实验
  功能:可对24c02单独写或读一个字节,也可对24c02连续写或读多个字节。
  硬件设置:SW1:1-2     SW2: 1-2      
*/
#include<reg51.h>
#include <intrins.h>
sbit SCL=P2^4;
sbit SDA=P2^5;
//****************************************************************
void Delay_ms_24C04(unsigned int t) //Delay t ms for 22.1184MHz crystal //MCU=STC12C5412
{
unsigned int i;
while(t--)
{
   for(i=0;i<1100;i++);
}
}
//**起动24c02时序*******************
void Start()
{
SCL=1;
SDA=1;
SDA=0;
SCL=0;
}
//**停止24c02时序*******************
void Stop()
{
SDA=0;
SCL=1;
SDA=1;
}
//**检测24c02的响应信号****************
bit CxxACK()
{
bit c;
SDA=1;
SCL=1;
c=SDA;
SCL=0;
return c;
}
//**************************************
void MCUACK()
{
SDA=0;
SCL=1;
SCL=0;
SDA=1;
}
//**********************************************
void NoACK()
{
SDA=1;
SCL=1;
SCL=0;
}
//**往24c02发一8位数据*********************************
void SendChar(unsigned char dat)
{
unsigned char i=8;
do
{
  SDA=(dat&0x80);
  SCL=1;
   SCL=0;
  dat<<=1;
   }while(--i!=0);
}
//**从24c02接收一8位数据******************************
unsigned char RecChar()
{
unsigned char i=8,dat;
do
  {SCL=1;
   dat=(dat<<1)|SDA;
   SCL=0;
  }while(--i!=0);
  return dat;
}
//**往24c02写一个字节*************************************
void WriteByte_24C1024(bit A1,bit P0,unsigned int addr,unsigned char dat)
{
unsigned char c=0xa0;
if(A1) c=0xa4;
if(P0) c=c|0x02;
Start();
SendChar(c);
CxxACK();
SendChar(addr>>8);
CxxACK();
SendChar(addr);
CxxACK();
SendChar(dat);
CxxACK();
Stop();
Delay_ms_24C04(10);
}
//**往24c02写连续写多个字节,但最多不能超过16个字节************************
void WriteBytes_24C1024(bit A1,bit P0,unsigned int addr,unsigned char *buf,unsigned int bytes)
{
unsigned char c=0xa0;
if(A1) c=0xa4;
if(P0) c=c|0x02;
Start();
SendChar(c);
CxxACK();
SendChar(addr>>8);
CxxACK();
SendChar(addr);
CxxACK();
for(;bytes>0;bytes--)
    {
  SendChar(*buf++);
     CxxACK();
    }
Stop();
Delay_ms_24C04(10);

}
//**从24c02读一个字节*************************************
unsigned char ReadByte_24C1024(bit A1,bit P0,unsigned int addr)
{
unsigned char dat;
unsigned char c=0xa0;
if(A1) c=0xa4;
if(P0) c=c|0x02;
Start();
SendChar(c);
CxxACK();
SendChar(addr>>8);
CxxACK();
SendChar(addr);
CxxACK();
Start();
SendChar(c|1);
CxxACK();
dat=RecChar();
NoACK();
Stop();
return dat;
}
//**从24c02连续读多个字节,长度不限*******************************
void ReadBytes_24C1024(bit A1,bit P0,unsigned int addr,unsigned char *buf,unsigned int bytes)
{
unsigned char c=0xa0;
if(A1) c=0xa4;
if(P0) c=c|0x02;
Start();
SendChar(c);
CxxACK();
SendChar(addr>>8);
CxxACK();
SendChar(addr);
CxxACK();
Start();
SendChar(c|1);
CxxACK();
while(--bytes>0)
     {*buf++=RecChar();
      MCUACK();
     }
*buf++=RecChar();
NoACK();
Stop();
}
  //*******************************************************
/*
//**往24c02写连续写多个字节,但最多不能超过8个字节************************
void WriteBytes_24C02(unsigned char addr,unsigned char *buf,unsigned char bytes)
{
Start();
SendChar(0xa2);
CxxACK();
SendChar(addr);
CxxACK();
for(;bytes>0;bytes--)
    {SendChar(*buf++);
     CxxACK();
    }
Stop();
Delay_ms_24C04(10);
}
//**从24c02连续读多个字节,长度<=8*******************************
void ReadBytes_24C02(unsigned char addr,unsigned char *buf,unsigned char bytes)
{ Start();
  SendChar(0xa2);
  CxxACK();
  SendChar(addr);
  CxxACK();
  Start();
  SendChar(0xa2);
  CxxACK();
  while(--bytes>0)
     {*buf++=RecChar();
      MCUACK();
     }
  *buf++=RecChar();
  NoACK();
  Stop();
}*/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|cpubbs论坛. ( 粤ICP备09171248号 )

GMT+8, 2025-4-10 11:50 , Processed in 0.531680 second(s), 6 queries , Gzip On, File On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表