#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^0
sbit scl=P2^1
sbit dula=P2^6
sbit wela=P2^7
uchar flag,sec
uint num
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,//0x77,0x7c,
//0x39,0x5e,0x79,0x71
}
void delay()
{ }
void start() //开始信号
{
sda=1
delay()
scl=1
delay()
sda=0
delay()
}
void stop() //停止
{
sda=0
delay()
scl=1
delay()
sda=1
delay()
}
void respons() //应答
{
uchar i=0
scl=1
delay()
while((sda==1)&&(i<250))i++
scl=0
delay()
}
void init()
{
sda=1
delay()
scl=1
delay()
}
void write_byte(uchar date)
{
uchar i,temp
temp=date
scl=0
for(i=0i<8i++)
{
temp=temp<<1
delay()
sda=CY
delay()
scl=1
delay()
scl=0
delay()
}
sda=1
delay()
}
uchar read_byte()
{
uchar i,k
scl=0
delay()
sda=1
delay()
for(i=0i<8i++)
{
scl=1
delay()
k=(k<<1)|sda
scl=0
delay()
}
return k
}
void delayms(uchar x)
{
uchar a,b
for(a=xa>0a--)
for(b=110b>0b--)
}
void write_add(uchar address,uchar date)
{
start()
write_byte(0xa0)
respons()
write_byte(address)
respons()
write_byte(date)
respons()
stop()
}
uchar read_add(uchar address)
{
uchar date
start()
write_byte(0xa0)
respons()
write_byte(address)
respons()
start()
write_byte(0xa1)
respons()
date=read_byte()
stop()
return date
}
void display(uchar shi,uchar ge)
{
dula=1
P0=table[shi]
dula=0
P0=0xff
wela=1
P0=0xfe//01111111
wela=0
delayms(5)
dula=1
P0=table[ge]
dula=0
P0=0xff
wela=1
P0=0xfd
wela=0
delayms(5)
}
void main()
{
init()
num=0
sec=read_add(1)
if(sec>=100)
sec=0
TMOD=0x01
TH0=(65536-45872)/256
TL0=(65536-45872)%256
EA=1
ET0=1
TR0=1
while(1)
{
display(sec/10,sec%10)
if(flag==1)
{
flag=0
write_add(1,sec)
}
}
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256
TL0=(65536-45872)%256
num++
if(num==20)
{
num=0
sec++
flag=1
if(sec==100)
{
sec=0
}
}
}
这是一段读写你的可复写只读存储器/存储芯片的通用程序,write_to_eeprm 写入子程序,
shift_in 是写入时SDA总线控制, 并把写入并行数据转为串行.它是支持write_to_eeprm写入子程序的
read_from_eeprm 读出子程序
shift_out是读出时SDA总线控制, 并把读出串行数据转为并行.它是支持read_from_eeprm 读出子程序的
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)