/*函数说明*/
#include
#include
***it SCL=P1^1 ;
***it SDL=P1^2 ;
void denop();
void iic_start();
void iic_stop();
void Answer();
void NoAnswer();
void iic_writebyte(unsigned char temp);
unsigned char icc_readbyte();
unsigned char icc_cheakask();
void iic_init();
void stc51_writepage(unsigned char adress,unsigned char *databyte) ;
void stc51_readpage(unsigned char begad) ;
unsigned char tabr[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
unsigned char buf[6] = {3,0,0,0,0,0};
unsigned char buf1[6] = {9,4,4,4,4,4};
/*主函数*/
// 将数组buf1中的主句存储到buf中
void main()
{
unsigned char x;
iic_init();
stc51_writepage(0x01,buf1);
denop();denop();
stc51_readpage(0x01);
denop();denop();
while(1)
{
x = buf[0];
P0 = tabr[x]; //只为了现实数字9,可是怎么都显示不了
}
}
/*延时*/
void denop()
{
_nop_();_nop_();_nop_();_nop_();_nop_();
}
//开始,结束
void iic_start()
{
SCL = 1; denop();
SDL = 1;denop();
SDL = 0;denop();
SCL = 0;denop();
}
void iic_stop()
{ SDL = 0;denop();
SCL = 1;denop();
SDL = 1;denop();
// SCL = 1;denop();
// SCL = 0;denop();
// SDL = 0; denop();
}
//应答,非应答
void Answer()
{
// SCL = 0;
// denop();
SDL = 1;
denop();
SCL = 1;
denop();
SCL = 0;
denop();
}
void NoAnswer()
{
// SCL = 0;
// denop();
SDL = 0;
denop();
SCL = 1;
denop();
SCL = 0;
denop();
}
//写入一个字节
void iic_writebyte(unsigned char temp)
{
unsigned char i,tttemp,x;
tttemp = temp;
for(i = 0;i<8;i++)
{
tttemp=tttemp << 1 ;
SCL = 0;
denop();
SDL = CY;
denop();
SCL = 1;
denop();
}
// SCL = 0; denop();
// SDL = 1; denop();
denop();denop();denop();denop();
}
//读出多个字节
unsigned char icc_readbyte()
{
unsigned char temp=0,i,x;
// SCL = 0;denop();
SDL = 1;denop();
for(i = 0;i<8;i++)
{
SCL = 1; denop();
if(SDL)
temp = temp|0x01;
else
temp = temp&0xfe;
if(i<7)
temp = temp << 1;
SCL = 0; denop();
}
// x = temp;
// P0 = tabr[x];
return temp;
}
//检查应答信号
unsigned char icc_cheakask()
{
bit cv;
SDL = 1;
SCL = 1;denop();
cv = SDL;
SCL = 0;denop();
if(cv)
{
return 1;
}
else
return 0;
}
void iic_init()
{
SCL=1;
SDL=1;
denop();
}
/写入多个字节
void stc51_writepage(unsigned char adress,unsigned char *databyte)
{
unsigned char i,x;
iic_start();
iic_writebyte(0xa0);
if(!icc_cheakask())
{
iic_writebyte(adress);
if(!icc_cheakask())
{
for(i = 0;i < 6;i++)
{
x = databyte;
iic_writebyte(x);
denop();denop();denop();
while(!icc_cheakask());
denop();denop();denop();denop();
}
}
}
iic_stop();
denop();denop();denop();
}
//读出多个字节
void stc51_readpage(unsigned char begad)
{
unsigned char i,x;
iic_start();
iic_writebyte(0xa0);
if(!icc_cheakask())
{
iic_writebyte(begad);
if(!icc_cheakask())
{
iic_start();
iic_writebyte(0xa1);
if(!icc_cheakask())
{
for(i = 0;i<6;i++)
{
buf = icc_readbyte();
x = buf[0];
P0 = tabr[x];
NoAnswer(); denop();denop();
}
Answer();
iic_stop();
denop();denop();
}
}
}
}
0
|
|
|
|