?? i2c的應(yīng)用.c
字號:
#include"reg52.h"
#define uchar unsigned char
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit wl=P2^0;
sbit du=P2^1;
sbit scl=P3^4;
sbit sda=P3^5;
uchar flag,a;
void delay()
{
; ;
}
void delay2(int z)
{
uchar x,y;
for(x=110;x>0;x--)
for(y=z;y>0;y--);
}
void display(uchar temp)
{
uchar shi,ge;
shi=temp/10;
ge=temp%10;
wl=1;
P1=0xfe;
wl=0;
du=1;
P1=table[shi];
delay2(2);
du=0;
wl=1;
P1=0xfd;
wl=0;
du=1;
P1=table[ge];
delay2(2);
du=0;
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void response()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))
i++;
scl=0;
delay();
}
void writebyte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar readbyte()
{
uchar i,temp;
scl=0;
for(i=0;i<8;i++)
{
scl=1;
delay();
temp=(temp<<1)|sda;
scl=0;
delay();
}
return(temp);
}
void write(uchar add ,uchar date)
{
start();
writebyte(0xa0);
response();
writebyte(add);
response();
writebyte(date);
response();
stop();
}
uchar read(uchar add)
{
uchar temp;
start();
writebyte(0xa0);
response();
writebyte(add);
response();
start();
writebyte(0xa1);
response();
temp=readbyte();
stop();
return(temp);
}
void init()
{
TMOD=0x01;
TH0=0x4c;
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
a=read(2);
init();
while(1)
{
if(flag==20)
{
flag=0;
a++;
if(a==100)
a=0;
write(2,a);
}
display(a);
}
}
void timer0() interrupt 1
{
TH0=0x4c;
TL0=0x00;
flag++;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -