標(biāo)題:
E2PROM 24c02測試程序源代碼
[打印本頁]
作者:
niujia
時間:
2015-7-15 23:16
標(biāo)題:
E2PROM 24c02測試程序源代碼
數(shù)碼管顯示部分的原理圖:
http://www.torrancerestoration.com/f/51hei-5.pdf
源代碼如下:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
bit write=0;
sbit sda=P2^0;
sbit scl=P2^1;
sbit wela=P2^7;
sbit dula=P2^6;
uchar sec,tcnt;
uchar smg_change[6]={10,10,10,10,10,10};
unsigned char code duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
unsigned char code weima[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
void delay()
{;;}
void delay1ms(uchar t)
{
uchar i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
scl=0;
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
scl=0;
}
void respons()
{
uchar i;
sda=1;
scl=1;
delay();
while((sda==1)&&(i<250))
i++;
scl=0;
delay();
}
void no_ack()
{
sda=1;
scl=1;
scl=0;
}
void write_byte(uchar date)
{
uchar i;
for(i=0;i<8;i++)
{
date=date<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
}
uchar read_byte()
{
uchar i,k;
for(i=0;i<8;i++)
{
k<<=1;
scl=1;
delay();
sda=1;
if(sda)
k|=0x01;
scl=0;
delay();
}
return k;
}
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();
no_ack();
stop();
return date;
}
void display(uchar bai,uchar shi)
{
dula=0;
P0=duanma[bai];
dula=1;
dula=0;
wela=0;
P0=weima[0];
wela=1;
wela=0;
delay1ms(5);
dula=0;
P0=duanma[shi];
dula=1;
dula=0;
wela=0;
P0=weima[1];
wela=1;
wela=0;
delay1ms(5);
}
void init_time()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
EA=1;
}
void main()
{
init();
init_time();
sec=read_add(0);
if(sec>60)
sec=sec-250;
while(1)
{
display(sec/10,sec%10);
ET0=0;
write_add(0,sec);
ET0=1;
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tcnt++;
if(tcnt==20)
{
tcnt=0;
sec++;
write=1;
if(sec==60)
sec=0;
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1