单片机扩展存储24C08时序图那里有,数据存储的原理是怎样的
2个回答
展开全部
百度一下,PDF版的文件
24C08读写操作,适用于初学者
#include <AT89X52.H>
#include <stdio.h>
#include <absacc.h>
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07, 0x7f,0x6f,};
unsigned char sec; //定义计数值,每过1秒,sec加1
unsigned int tcnt; //定时中断次数
bit write=0; //写24C08的标志;
sbit gewei=P2^5; //个位选通定义
sbit shiwei=P2^6; //十位选通定义
/////////24C08读写驱动程序////////////////////
sbit scl=P3^6; // 24c08 SCL
sbit sda=P3^7; // 24c08 SDA
void delay1(unsigned char x)
{ unsigned int i;
for(i=0;i<x;i++);
;}
void flash() //延时2us
{
;
;
}
void x24c08_init() //24c08初始化子程序
{
scl=1;
flash();
sda=1;
flash();
}
void start() //启动I2C总线
{
sda=1;
flash();
scl=1;
flash();
sda=0;
flash();
scl=0;
flash();
}
void stop() //停止I2C总线
{
sda=0;
flash();
scl=1;
flash();
sda=1;
flash();
}
void writex(unsigned char j) //写一个字节
{
unsigned char i,temp;
temp=j;
for (i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
flash();
sda=CY;
flash();
scl=1;
flash();
}
scl=0;
flash();
sda=1;
flash();
}
unsigned char readx() //读一个字节
{
unsigned char i,j,k=0;
scl=0;
flash();
sda=1;
for (i=0;i<8;i++)
{
flash();
scl=1;
flash();
if (sda==1) j=1;
else j=0;
k=(k<<1)|j;
scl=0;
}
flash();
return(k);
}
void clock() // I2C总线时钟
{
unsigned char i=0;
scl=1;
flash();
while ((sda==1)&&(i<255))i++;
scl=0;
flash();
}
////////从24c02的地址address中读取一个字节数据/////
unsigned char x24c08_read(unsigned char address)
{
unsigned char i;
start();
writex(0xa0);
clock();
writex(address);
clock();
start();
writex(0xa1);
clock();
i=readx();
stop();
delay1(10);
return(i);
}
//////向24c02的address地址中写入一字节数据info/////
void x24c08_write(unsigned char address,unsigned char info)
{
EA=0;
start();
writex(0xa0);
clock();
writex(address);
clock();
writex(info);
clock();
stop();
EA=1;
delay1(50);
}
/////////////24C08读写驱动程序完/////////////////////
void Delay(unsigned int tc) //延时程序
{
while( tc != 0 )
{unsigned int i;
for(i=0; i<100; i++);
tc--;}
}
void LED() //LED显示函数
{
shiwei=0;
P0=table[sec/10];
Delay(8);
shiwei=1;
gewei=0;
P0=table[sec%10];
Delay(5);
gewei=1;
}
void t0(void) interrupt 1 using 0 //定时中断服务函数
{
TH0=(65536-50000)/256; //对TH0 TL0赋值
TL0=(65536-50000)%256; //重装计数初值
tcnt++; //每过250ust tcnt加一
if(tcnt==20) //计满20次(1秒)时
{
tcnt=0; //重新再计
sec++;
write=1; //1秒写一次24C08
if(sec==100) //定时100秒,在从零开始计时
{sec=0;}
}
}
void main(void)
{
TMOD=0x01; //定时器工作在方式1
ET0=1;
EA=1;
x24c08_init(); //初始化24C08
sec=x24c08_read(2);//读出保存的数据赋于sec
TH0=(65536-50000)/256; //对TH0 TL0赋值
TL0=(65536-50000)%256; //使定时器0.05秒中断一次
TR0=1; //开始计时
while(1)
{
LED();
if(write==1) //判断计时器是否计时一秒
{
write=0; //清零
x24c08_write(2,sec); //在24c08的地址2中写入数据sec
}
}
}
24c08读写程序
#include<at89x52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCL="P2"^6;
sbit SDA="P2"^7;
uchar code displaycode[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
uchar code displaybit[]={0xfe,0xfd,0xfb,0xf7};
uchar displaybuf[3];
void delayX2us(uchar x) //微秒延时
{
while(--x);
}
void delayXms(uchar x) //毫秒延时
{
uchar i;
while(x--)
{
for(i=250;i>0;i--)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void start ()
{
SDA=1;
delayX2us(2);
SCL=1;
delayX2us(2);
SDA=0;
delayX2us(2);
}
void stop ()
{
SDA=0;
delayX2us(2);
SCL=1;
delayX2us(2);
SDA=1;
delayX2us(2);
}
void init()
{
SCL=1;
delayX2us(2);
SDA=1;
delayX2us(2);
}
void responds()
{
uchar i;
i=0;
SCL=1;
delayX2us(2);
while(SDA==1&&i<200)
i++;
SCL=0;
delayX2us(2);
}
void write_dat (uchar dat) //写一个字节
{
uchar i;
for(i=0;i<8;i++)
{
SCL=0;
delayX2us(2);
SDA=(bit)(dat&0x80);
delayX2us(2);
SCL=1;
delayX2us(2);
SCL=0;
delayX2us(2);
dat<<=1;
}
SDA=1;
delayX2us(2);
}
void write(uchar dat,uchar add) // 写一个字节dat到地址add
{
start();
write_dat(0xa0);
responds();
write_dat(add);
responds();
write_dat(dat);
responds();
stop();
}
uchar read_dat () //读一个字节
{
uchar i;
uchar dat;
bit j;
for (i=0;i<8;i++)
{
dat<<=1;
SCL=0;
j=SDA;
delayX2us(2);
SCL=1;
if(j==1)
{
dat|=0x01;
}
SCL=0;
}
return dat;
}
uchar read (uchar add) //从地址add中读取一个字节
{
uchar dat;
start();
write_dat(0xa0);
responds();
write_dat(add);
responds();
start();
write_dat(0xa1);
responds();
dat=read_dat();
delayX2us(2);
stop();
return dat;
}
void display(uchar dat) //显示程序
{
uchar bai,shi,ge,i;
bai=dat/100;
shi=dat%100/10;
ge=dat%10;
displaybuf[0]=bai;
displaybuf[1]=shi;
displaybuf[2]=ge;
for(i=0;i<3;i++)
{
P0=displaycode[displaybuf[i]];
P1=displaybit[i];
delayX2us(5);
P1=0xff;
}
}
void main ()
{
uchar dat,add,k;
dat=212;
add=2;
init();
write(dat,add);
delayXms(1000);
k=read(add);
while(1)
{
display(k);
}
}
24C08读写操作,适用于初学者
#include <AT89X52.H>
#include <stdio.h>
#include <absacc.h>
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07, 0x7f,0x6f,};
unsigned char sec; //定义计数值,每过1秒,sec加1
unsigned int tcnt; //定时中断次数
bit write=0; //写24C08的标志;
sbit gewei=P2^5; //个位选通定义
sbit shiwei=P2^6; //十位选通定义
/////////24C08读写驱动程序////////////////////
sbit scl=P3^6; // 24c08 SCL
sbit sda=P3^7; // 24c08 SDA
void delay1(unsigned char x)
{ unsigned int i;
for(i=0;i<x;i++);
;}
void flash() //延时2us
{
;
;
}
void x24c08_init() //24c08初始化子程序
{
scl=1;
flash();
sda=1;
flash();
}
void start() //启动I2C总线
{
sda=1;
flash();
scl=1;
flash();
sda=0;
flash();
scl=0;
flash();
}
void stop() //停止I2C总线
{
sda=0;
flash();
scl=1;
flash();
sda=1;
flash();
}
void writex(unsigned char j) //写一个字节
{
unsigned char i,temp;
temp=j;
for (i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
flash();
sda=CY;
flash();
scl=1;
flash();
}
scl=0;
flash();
sda=1;
flash();
}
unsigned char readx() //读一个字节
{
unsigned char i,j,k=0;
scl=0;
flash();
sda=1;
for (i=0;i<8;i++)
{
flash();
scl=1;
flash();
if (sda==1) j=1;
else j=0;
k=(k<<1)|j;
scl=0;
}
flash();
return(k);
}
void clock() // I2C总线时钟
{
unsigned char i=0;
scl=1;
flash();
while ((sda==1)&&(i<255))i++;
scl=0;
flash();
}
////////从24c02的地址address中读取一个字节数据/////
unsigned char x24c08_read(unsigned char address)
{
unsigned char i;
start();
writex(0xa0);
clock();
writex(address);
clock();
start();
writex(0xa1);
clock();
i=readx();
stop();
delay1(10);
return(i);
}
//////向24c02的address地址中写入一字节数据info/////
void x24c08_write(unsigned char address,unsigned char info)
{
EA=0;
start();
writex(0xa0);
clock();
writex(address);
clock();
writex(info);
clock();
stop();
EA=1;
delay1(50);
}
/////////////24C08读写驱动程序完/////////////////////
void Delay(unsigned int tc) //延时程序
{
while( tc != 0 )
{unsigned int i;
for(i=0; i<100; i++);
tc--;}
}
void LED() //LED显示函数
{
shiwei=0;
P0=table[sec/10];
Delay(8);
shiwei=1;
gewei=0;
P0=table[sec%10];
Delay(5);
gewei=1;
}
void t0(void) interrupt 1 using 0 //定时中断服务函数
{
TH0=(65536-50000)/256; //对TH0 TL0赋值
TL0=(65536-50000)%256; //重装计数初值
tcnt++; //每过250ust tcnt加一
if(tcnt==20) //计满20次(1秒)时
{
tcnt=0; //重新再计
sec++;
write=1; //1秒写一次24C08
if(sec==100) //定时100秒,在从零开始计时
{sec=0;}
}
}
void main(void)
{
TMOD=0x01; //定时器工作在方式1
ET0=1;
EA=1;
x24c08_init(); //初始化24C08
sec=x24c08_read(2);//读出保存的数据赋于sec
TH0=(65536-50000)/256; //对TH0 TL0赋值
TL0=(65536-50000)%256; //使定时器0.05秒中断一次
TR0=1; //开始计时
while(1)
{
LED();
if(write==1) //判断计时器是否计时一秒
{
write=0; //清零
x24c08_write(2,sec); //在24c08的地址2中写入数据sec
}
}
}
24c08读写程序
#include<at89x52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCL="P2"^6;
sbit SDA="P2"^7;
uchar code displaycode[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
uchar code displaybit[]={0xfe,0xfd,0xfb,0xf7};
uchar displaybuf[3];
void delayX2us(uchar x) //微秒延时
{
while(--x);
}
void delayXms(uchar x) //毫秒延时
{
uchar i;
while(x--)
{
for(i=250;i>0;i--)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void start ()
{
SDA=1;
delayX2us(2);
SCL=1;
delayX2us(2);
SDA=0;
delayX2us(2);
}
void stop ()
{
SDA=0;
delayX2us(2);
SCL=1;
delayX2us(2);
SDA=1;
delayX2us(2);
}
void init()
{
SCL=1;
delayX2us(2);
SDA=1;
delayX2us(2);
}
void responds()
{
uchar i;
i=0;
SCL=1;
delayX2us(2);
while(SDA==1&&i<200)
i++;
SCL=0;
delayX2us(2);
}
void write_dat (uchar dat) //写一个字节
{
uchar i;
for(i=0;i<8;i++)
{
SCL=0;
delayX2us(2);
SDA=(bit)(dat&0x80);
delayX2us(2);
SCL=1;
delayX2us(2);
SCL=0;
delayX2us(2);
dat<<=1;
}
SDA=1;
delayX2us(2);
}
void write(uchar dat,uchar add) // 写一个字节dat到地址add
{
start();
write_dat(0xa0);
responds();
write_dat(add);
responds();
write_dat(dat);
responds();
stop();
}
uchar read_dat () //读一个字节
{
uchar i;
uchar dat;
bit j;
for (i=0;i<8;i++)
{
dat<<=1;
SCL=0;
j=SDA;
delayX2us(2);
SCL=1;
if(j==1)
{
dat|=0x01;
}
SCL=0;
}
return dat;
}
uchar read (uchar add) //从地址add中读取一个字节
{
uchar dat;
start();
write_dat(0xa0);
responds();
write_dat(add);
responds();
start();
write_dat(0xa1);
responds();
dat=read_dat();
delayX2us(2);
stop();
return dat;
}
void display(uchar dat) //显示程序
{
uchar bai,shi,ge,i;
bai=dat/100;
shi=dat%100/10;
ge=dat%10;
displaybuf[0]=bai;
displaybuf[1]=shi;
displaybuf[2]=ge;
for(i=0;i<3;i++)
{
P0=displaycode[displaybuf[i]];
P1=displaybit[i];
delayX2us(5);
P1=0xff;
}
}
void main ()
{
uchar dat,add,k;
dat=212;
add=2;
init();
write(dat,add);
delayXms(1000);
k=read(add);
while(1)
{
display(k);
}
}
东莞市友贸实业有限公司_
2023-11-22 广告
2023-11-22 广告
第五代双倍数据速率DDR5双列直插式内存模块是一种高速、高性能的内存模块,适用于需要高带宽和低延迟的应用场景。与前几代内存模块相比,DDR5双列直插式内存模块具有更高的数据传输速率和更低的功耗,同时提供了更大的内存容量和更高的可靠性。在数据...
点击进入详情页
本回答由东莞市友贸实业有限公司_提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询