我写了一个关于iic的函数,但是实现不了我想要的功能,keil没有报错,求大神指出错误
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define nops() {_nop_();_nop_();_nop_();_nop_();}
#define OP_READ 0xa1
#define OP_WRITE 0xa0
sbit SDA=P2^0;
sbit SCL=P2^1;
sbit DS1302=P2^4;
sbit LCDEN=P2^5;
uchar code display[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF};
void delay1ms()
{
uchar i,j;
for(i=0;i<10;i++)
{
for(j=0;j<33;j++);
}
}
void delayms(uchar n)
{
uchar i;
for(i=0;i<n;i++)
{
delay1ms();
}
}
void start()
{
SCL=1;
SDA=1;
nops();
SDA=0;
nops();
SCL=0;
}
void stop()
{
SCL=1;
SDA=0;
nops();
SDA=1;
nops();
SDA=0;
SCL=0;
}
bit writecurren(uchar y)
{
bit ack;
uchar i;
for(i=0;i<8;i++)
{
SDA=(bit)(y&0x80);
_nop_();
SCL=1;
_nop_();
_nop_();
SCL=0;
y<<=1;
}
SDA=1;
_nop_();
_nop_();
SCL=1;
nops();
ack=SDA;
SCL=0;
return ack;
}
void writeset(uchar add,uchar dat)
{
start();
writecurren(OP_WRITE);
writecurren(add);
writecurren(dat);
stop();
delayms(4);
}
uchar readdata()
{
uchar i;
uchar x;
for(i=0;i<8;i++)
{
SCL=1;
x<<=1;
x|=(uchar)SDA;
SCL=0;
}
return(x);
}
uchar readcurrent()
{
uchar x;
start();
writecurren(OP_READ);
x=readdata();
stop();
return x;
}
uchar readset(uchar set_add)
{
start();
writecurren(OP_WRITE);
writecurren(set_add);
return(readcurren());
}
void main()
{
uchar i;
LCDEN=0;
DS1302=0;
P1=0xfe;
SCL=1;
for(i=0;i<16;i++)
{
writeset(i,display[i]);
}
for(i=0;i<16;i++)
{
P1=readset(i);
delayms(200);
}
}
我是菜鸟,弄了一个星期了,还是弄不好求大神求救/(ㄒoㄒ)/~~
#include<intrins.h>
#define uchar unsigned char
#define nops() {_nop_();_nop_();_nop_();_nop_();}
#define OP_READ 0xa1
#define OP_WRITE 0xa0
sbit SDA=P2^0;
sbit SCL=P2^1;
sbit DS1302=P2^4;
sbit LCDEN=P2^5;
uchar code display[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF};
void delay1ms()
{
uchar i,j;
for(i=0;i<10;i++)
{
for(j=0;j<33;j++);
}
}
void delayms(uchar n)
{
uchar i;
for(i=0;i<n;i++)
{
delay1ms();
}
}
void start()
{
SCL=1;
SDA=1;
nops();
SDA=0;
nops();
SCL=0;
}
void stop()
{
SCL=1;
SDA=0;
nops();
SDA=1;
nops();
SDA=0;
SCL=0;
}
bit writecurren(uchar y)
{
bit ack;
uchar i;
for(i=0;i<8;i++)
{
SDA=(bit)(y&0x80);
_nop_();
SCL=1;
_nop_();
_nop_();
SCL=0;
y<<=1;
}
SDA=1;
_nop_();
_nop_();
SCL=1;
nops();
ack=SDA;
SCL=0;
return ack;
}
void writeset(uchar add,uchar dat)
{
start();
writecurren(OP_WRITE);
writecurren(add);
writecurren(dat);
stop();
delayms(4);
}
uchar readdata()
{
uchar i;
uchar x;
for(i=0;i<8;i++)
{
SCL=1;
x<<=1;
x|=(uchar)SDA;
SCL=0;
}
return(x);
}
uchar readcurrent()
{
uchar x;
start();
writecurren(OP_READ);
x=readdata();
stop();
return x;
}
uchar readset(uchar set_add)
{
start();
writecurren(OP_WRITE);
writecurren(set_add);
return(readcurren());
}
void main()
{
uchar i;
LCDEN=0;
DS1302=0;
P1=0xfe;
SCL=1;
for(i=0;i<16;i++)
{
writeset(i,display[i]);
}
for(i=0;i<16;i++)
{
P1=readset(i);
delayms(200);
}
}
我是菜鸟,弄了一个星期了,还是弄不好求大神求救/(ㄒoㄒ)/~~