一、c51读写at24c04源代码
/*=============================================*/
/*;***********************************/
/*;起动24c01时序*/
void start()
{
scl=1;
sda=1;
sda=0;
scl=0;
}
/*;************************************/
/*;停止24c01时序*/
void stop()
{
sda=0;
scl=1;
sda=1;
}
/*;**************************************/
/*;检测24c01的响应信号*/
bit ack()
{
bit c;
sda=1;
scl=1;
c=sda;
scl=0;
return c;
}
/*;************************************/
/*;往24c01发一8位数据*/
void sendchar(unsigned char ch)
{
unsigned char i;
i=8;
do
{
sda=(ch&0x80);
scl=1;
scl=0;
ch《《=1;
}while(--i!=0);
}
/*;**************************************/
/*;从24c01接收一8位数据*/
unsigned char recchar()
{
unsigned char i,j;
i=8;
do
{
scl=1;
j=(j《《1)|sda;
scl=0;
}while(--i!=0);
return j;
}
//;**************************************
/*;********************************/
/*;往24c01写一字节*/
void writechar(unsigned int addr,unsigned char ch)
{
unsigned char c;
c=((*((unsigned char *)&addr))《《1)&0x02;
start();
sendchar(0xa0|c);
ack();
sendchar(addr);
ack();
sendchar(ch);
ack();
stop();
// for(addr=4;addr!=0;addr--)
for(ch=0xff;ch!=0;ch--) ;
}
//;**************************************
/*;********************************/
/*;往24c01写多字节*/
void writebuf(unsigned int addr,unsigned char idata *buf,unsigned char count)
{
unsigned char c;
c=((*((unsigned char *)&addr))《《1)&0x02;
start();
sendchar(0xa0|c);
ack();
sendchar(addr);
ack();
do
{
sendchar(*buf++);
ack();
if(count!=1)
{if(((++addr)&0x7)==0)
{
stop();
for(c=0xff;c!=0;c--) ;
c=((*((unsigned char *)&addr))《《1)&0x02;
start();
sendchar(0xa0|c);
ack();
sendchar(addr);
ack();
}
}
else
{
stop();
for(c=0xff;c!=0;c--) ;
}
}while(--count!=0);
}
/*;**********************************/
/*;从24c01读一字节*/
/*;入口:r0中为要读出内容的地址*/
/*;出口:a中为读到的内容*/
unsigned char readchar(unsigned int addr)
{
unsigned char ch;
ch=((*((unsigned char *)&addr))《《1)&0x02;
start();
sendchar(0xa0|ch);
ack();
sendchar(addr);
ack();
start();
sendchar(0xa1|ch);
ack();
ch=recchar();
stop();
return ch;
}
/**********************************/
/*至少读2字节*/
void readbuf(unsigned int addr,unsigned char idata *buf,unsigned char count)
{
unsigned char ch;
ch=((*((unsigned char *)&addr))《《1)&0x02;
start();
sendchar(0xa0|ch);
ack();
sendchar(addr);
ack();
start();
sendchar(0xa1|ch);
ack();
count--;
do
{
*buf++=recchar();
sda=0;
scl=1;
scl=0;
sda=1;
}while(--count!=0);
*buf=recchar();
stop();
}
二、at24c04测试程序
/**************************************
主芯片 : stc12c5a60s2 (1t)
工作频率: 12.000mhz
**************************************/
#include “reg51.h”
#include “intrins.h”
typedef unsigned char byte;
typedef unsigned short word;
sbit scl = p3^4; //at24c04的时钟
sbit sda = p3^5; //at24c04的数据
byte buf[16]; //数据缓存区
byte code testdata[] =
{
0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff
};
void delay5us();
void delay5ms();
void at24c04_start();
void at24c04_stop();
void at24c04_sendack(bit ack);
bit at24c04_recvack();
void at24c04_sendbyte(byte dat);
byte at24c04_recvbyte();
void at24c04_readpage();
void at24c04_writepage();
void main()
{
at24c04_writepage();
delay5ms();
at24c04_readpage();
while (1);
}
/**************************************
向at24c04写1页(16字节)数据
将testdata开始的16个测试数据写如设备的00~0f地址中
**************************************/
void at24c04_writepage()
{
byte i;
at24c04_start(); //起始信号
at24c04_sendbyte(0xa0); //发送设备地址+写信号
at24c04_sendbyte(0x00); //发送存储单元地址
for (i=0; i《16; i++)
{
at24c04_sendbyte(testdata[i]);
}
at24c04_stop(); //停止信号
}
/**************************************
从at24c04读取1页(16字节)数据
将设备的00~0f地址中的数据读出存放在data区的buf中
**************************************/
void at24c04_readpage()
{
byte i;
at24c04_start(); //起始信号
at24c04_sendbyte(0xa0); //发送设备地址+写信号
at24c04_sendbyte(0x00); //发送存储单元地址
at24c04_start(); //起始信号
at24c04_sendbyte(0xa1); //发送设备地址+读信号
for (i=0; i《16; i++)
{
buf[i] = at24c04_recvbyte();
if (i == 15)
{
at24c04_sendack(1); //最后一个数据需要会nak
}
else
{
at24c04_sendack(0); //回应ack
}
}
at24c04_stop(); //停止信号
}
/**************************************
延时5微秒(stc12c5a60s2@12m)
不同的工作环境,需要调整此函数
此延时函数是使用1t的指令周期进行计算,与传统的12t的mcu不同
**************************************/
void delay5us()
{
byte n = 4;
while (n--)
{
_nop_();
_nop_();
}
}
/**************************************
延时5毫秒(stc12c5a60s2@12m)
不同的工作环境,需要调整此函数
此延时函数是使用1t的指令周期进行计算,与传统的12t的mcu不同
**************************************/
void delay5ms()
{
word n = 2500;
while (n--)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
/**************************************
起始信号
**************************************/
void at24c04_start()
{
sda = 1; //拉高数据线
scl = 1; //拉高时钟线
delay5us(); //延时
sda = 0; //产生下降沿
delay5us(); //延时
scl = 0; //拉低时钟线
}
/**************************************
停止信号
**************************************/
void at24c04_stop()
{
sda = 0; //拉低数据线
scl = 1; //拉高时钟线
delay5us(); //延时
sda = 1; //产生上升沿
delay5us(); //延时
}
/**************************************
发送应答信号
入口参数:ack (0:ack 1:nak)
**************************************/
void at24c04_sendack(bit ack)
{
sda = ack; //写应答信号
scl = 1; //拉高时钟线
delay5us(); //延时
scl = 0; //拉低时钟线
delay5us(); //延时
}
/**************************************
接收应答信号
**************************************/
bit at24c04_recvack()
{
scl = 1; //拉高时钟线
delay5us(); //延时
cy = sda; //读应答信号
scl = 0; //拉低时钟线
delay5us(); //延时
return cy;
}
/**************************************
向iic总线发送一个字节数据
**************************************/
void at24c04_sendbyte(byte dat)
{
byte i;
for (i=0; i《8; i++) //8位计数器
{
dat 《《= 1; //移出数据的最高位
sda = cy; //送数据口
scl = 1; //拉高时钟线
delay5us(); //延时
scl = 0; //拉低时钟线
delay5us(); //延时
}
at24c04_recvack();
}
/**************************************
从iic总线接收一个字节数据
**************************************/
byte at24c04_recvbyte()
{
byte i;
byte dat = 0;
sda = 1; //使能内部上拉,准备读取数据
for (i=0; i《8; i++) //8位计数器
{
dat 《《= 1;
scl = 1; //拉高时钟线
delay5us(); //延时
dat |= sda; //读数据
scl = 0; //拉低时钟线
delay5us(); //延时
}
return dat;
}
如何利用好机器学习,数据分析与处理很重要
网络晶振常用频点
比特币跟黄金的避险属性比起来具有什么优势
紫光国微发布2018年度业绩快报
以智能终端为代表的全球IT产业发展趋势
C51读写AT24C04源代码及AT24C04测试程序
使用油烟在线监测系统,实现分散餐饮企业的集中监管
苹果iPhone 16 Pro/Max渲染图曝光
传统行业劳动密集度高 是国产机器人发展的突破点
跟随同茂线性马达走进“BEYOND”博览会
电子芯闻早报:骁龙820飞到三星,台积电怎么办
电源规范
产创融合技术沙龙——“开启OpenHarmony之旅!”讲座
电阻柜都有哪些特点?
变压器有哪些基本结构
ADA4424-6是一个高性能影像重建滤波器
FPGA原型调试环境局限性的解决方案分析
i9-9900K开盖 使用了钎焊散热后实际效果如何
评估相机性能的几个关键要素
基于FPGA的可扩展高速FFT处理器的设计与实现