这是我写的程序,不知那里错了,望指教,目的就为了练习II2C
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar buf[3]={0x21,0x44,0x00};
uchar buf1[6];
uchar code wei[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
sbit SDA=P1^6;
sbit SCL=P1^7;
void delay1ms(uint z)
{
uint x=0;
uchar y=0;
for(x=z;x>0;x--)
for(y=110;y--;y>0);
}
void delay5us()
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
void start()
{
SCL=1;
delay5us();
SDA=1;
_nop_();_nop_();
SDA=0;
delay5us();
}
void stop()
{
SDA=0;
delay5us();
SCL=1;
delay5us();
SDA=1;
delay5us();
}
void write_byte(uchar temp)
{
uchar i=0;
SCL=1;
SDA=1;
delay5us();
SDA=0;
SCL=0;
delay5us();
for(i=0;i<8;i++)
{
SCL=0;
SDA=(bit)(temp&0x80);
delay5us();
SCL=1;
delay5us();
temp=temp<<1;
}
SCL=0;
delay5us();
SDA=1;
delay5us();
}
uchar read_byte()
{
uchar temp=0,i=0;
SCL=1;
SDA=1;
SDA=0;
delay5us();
SCL=0;
delay5us();
for(i=0;i<8;i++)
{
SCL=1;
if(SDA==1)
{
temp=temp|0x01;
}
SCL=0;
delay5us();
temp=temp<<1;
}
return temp;
}
void ACK()
{
uchar i=0;
SCL=1;
while(SDA==1&&i<256)i++;
SCL=0;
delay5us();
}
void write(uchar add,uchar dat)
{
start();
write_byte(0xd0);
ACK();
write_byte(add);
ACK();
write_byte(dat);
ACK();
stop();
}
uchar read(uchar add)
{
uchar dat=0;
start();
write_byte(0xd0);
ACK();
write_byte(add);
ACK();
start();
write_byte(0xd1);
ACK();
dat=read_byte();
ACK();
stop();
return dat;
}
void init()
{
SCL=1;
delay5us();
SDA=1;
delay5us();
write(0x07,0x93);
write(0x00,0x00);
write(0x01,0x00);
write(0x02,0x00);
}
void disp()
{
uchar i=0;
for(i=0;i<6;i++)
{
P2=wei;
P0=duan[buf1];
delay1ms(5);
P0=0xff;
P2=0x00;
}
}
void main()
{
init();
while(1)
{
buf1[1]=read(0x00)/16;
delay1ms(5);
buf1[0]=read(0x00)%16;
delay1ms(5);
buf1[3]=read(0x01)/16;
delay1ms(5);
buf1[2]=read(0x01)%16;
delay1ms(5);
buf1[5]=read(0x02)/16;
delay1ms(5);
buf1[4]=read(0x02)%16;
delay1ms(5);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>