|
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit sda=P1^2;
sbit scl=P1^1;
void start(void);
void stop(void);
void ack(void);
void noack(void);
void iicwr_byte(uchar dat);
void init(void);
void delay (void);
void delay1(void);
void write_byte(uchar add,uchar dat);
void read_byte(uchar
uchar iicre_byte(void)
void delay1(void)
{
uint j=30000;
while(j--);
}
void delay (void)
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
uchar step[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar step1[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
void delay_50us(uint t)//延时50us
{
uchar j;
for(t;t>0;t--)
for(j=19;j>0;j--);
}
void start(void)//起始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop(void) //终止信号
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void ack(void)//应答信号
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<200))i++;
scl=0;
delay();
}
void noack(void) //非应答信号
{
sda=1;
delay();
scl=1;
delay();
scl=0;
delay();
}
void init(void)
{
sda=1;
scl=1;
}
void iicwr_byte(uchar dat)
{
uchar i;
scl=0;
for(i=0;i<8;i++)
{
if(dat&0x80)
{
sda=1;
}
else
{
sda=0;
}
dat=dat<<1;
delay();
scl=1;
delay();
scl=0;
delay();
}
sda=1;
delay();
}
uchar iic_byte(void)
{
uchar i,dat;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++);
{
scl=1;
delay();
dat=dat<<1;
if(sda)
{
dat++;
}
scl=0;
delay();
}
return dat;
}
void main()
{
/*it();
start();
iicwr_byte(0xa0);
ack();
iicwr_byte(10);
ack();
iicwr_byte(0x55);
ack();
stop;
delay1();
/////////////////////////
init();
start();
iicwr_byte(0xa0);
ack();
iicwr_byte(10);
ack();
start();
iicwr_byte(0xa1);
ack();
P0=iicre_byte();
noack();
stop();*/
uchar k;
k=read_byte(7);
k=k%10;
P1=step[k];
k++;
write_byte(7,k);
while(1);
}
void write_byte(uchar add,uchar dat)
{
init();
start();
iicwr_byte(0xa0);
ack();
iicwr_byte(add
ack();
iicwr_byte(dat);
ack();
stop();
}
uchar read_byte(uchar add)
{
uchar a;
init();
start();
iicwr_byte(0xa0);
ack();
iicwr_byte(add);
ack();
start();
iicwr_byte(0xa1);
ack();
a=iicre_byte();
noack();
stop();
return a;
}
|
|