|
# include <reg52.h>
# define uchar unsigned char
# define uint unsigned int
sbit scl = P2^1;
sbit sda = P2^0;
sbit P_HC595_SER = P2^7; //pin 14 SER data input
sbit P_HC595_RCLK = P2^5; //pin 12 RCLk store (latch) clock
sbit P_HC595_SRCLK = P2^6; //pin 11 SRCLK Shift data clock
char code t_display[]={
// 0 1 2 3 4 5 6 7 8 9 //
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F, 0};
void init();
void start();
void stop();
void respons();
void write_byte(uchar);
uchar read_byte();
void delay();
//void delay1(uint);
void write_add(uchar, uchar);
uchar read_add(uchar);
void init1();
void we();
void dage(uchar, uchar);
void Send_595(uchar);
uchar shu, shi, ge, write, aa;
void main()
{
init();
//delay1(100);
shu = read_add(2);
init1();
dage(11, 11);
while(1)
{
we();
dage(shi, ge);
if(write == 1)
{
write = 0;
write_add(2, shu);
}
}
}
void init()
{
sda = 1;
delay();
scl = 1;
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;
scl = 1;
delay();
while((sda == 1) && (i<250))
i++;
scl = 0;
delay();
}
void write_byte(uchar date)
{
uchar i, temp;
scl = 0;
delay();
temp = date;
for(i=0; i<8; i++)
{
temp = temp << 1;
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=0; i<8; ++i)
{
scl = 1;
delay();
k = (k<<1)|sda;
scl = 0;
delay();
}
return k;
}
void delay()
{; ;}
/*void delay1(uint z)
{
uchar x, y;
for(x=z; x>0; x--)
for(y=110; y>0; y--);
}*/
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 init1()
{
TMOD = 0x01;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
aa = 0;
shu = 0;
}
void timer0() interrupt 1
{
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
aa++;
}
void we()
{
if(aa == 20)
{
aa =0;
shu++;
if(shu == 100)
shu = 0;
shi = shu/10;
ge = shu%10;
write = 1;
}
}
void dage(uchar aa, uchar bb)
{
Send_595(0xfe);
Send_595(t_display[aa]);
P_HC595_RCLK = 0;
P_HC595_RCLK = 1;
Send_595(0xff);
Send_595(0xfd);
Send_595(t_display[bb]);
P_HC595_RCLK = 0;
P_HC595_RCLK = 1;
}
void Send_595(uchar dat)
{
uint i;
for(i=0; i<8; i++)
{
if(dat & 0x80) P_HC595_SER = 1;
else P_HC595_SER = 0;
P_HC595_SRCLK = 0;
P_HC595_SRCLK = 1;
dat = dat << 1;
}
}
|
|