单片机C语言程序设计实训100例中24C04的读写问题
//我用电脑串口测得读出的24C04的输出结果是y上面有几个点点。(电脑上显示的是ASCII数据)//STC89C52,12MHZ,24C04#include<reg51...
//我用电脑串口测得读出的24C04的输出结果是y上面有几个点点。(电脑上显示的是ASCII数据)
//STC89C52,12MHZ,24C04
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop6() {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
sbit scl=P2^0;
sbit sda=P2^1;
sbit spk=P0^1;
uchar code hi_list[]={0,226,229,232,233,236,238,240,241,242,244,245,246,247,248};
uchar code lo_list[]={0,4,13,10,20,3,8,6,2,23,5,26,1,4,3};
uchar code song_24c04[]={1,2,3,1,1,2,3,1,3,4,5,3,4,5};
uchar sidx;
void init()
{
SCON=0x50;
TMOD=0x20;
PCON=0x00;
TH1=243;
TL1=243;
TI=0;
TR1=1;
}
void delayms(uint x)
{
uchar i;
while(x--)for(i=0;i<120;i++);
}
void put_port(uchar c)
{
SBUF=c;while(TI==0);TI=0;
}
void start()
{
sda=1;scl=1;nop6();sda=0;nop6();scl=0;
}
void stop()
{
sda=0;scl=0;nop6();scl=1;nop6();sda=1;
}
void ack()
{
sda=1;nop6();scl=1;nop6();scl=0;
}
void no_ack()
{
sda=1;scl=1;nop6();scl=0;sda=0;
}
void write_byte(uchar b)
{
uchar i;
for(i=0;i<8;i++)
{
b<<=1;sda=CY;_nop_();scl=1;nop6();scl=0;
}
ack();
}
void write_iic(uchar addr,uchar dat)
{
start();
write_byte(0xa0);write_byte(addr);write_byte(dat);
stop();
delayms(10);
}
uchar read_byte()
{
uchar i,b;
sda=1;
for(i=0;i<8;i++)
{
scl=1;b<<=1;b|=sda;nop6();scl=0;nop6();
}
return b;
}
uchar read_current()
{
uchar d;
start();
write_byte(0xa1);d=read_byte();no_ack();
stop();
put_port(d);//串口测试输出
return d;
}
uchar random_read(uchar addr)
{
start();
write_byte(0xa0);write_byte(addr);
return read_current();
}
void to_int() interrupt 1
{
spk=!spk;
TH0=hi_list[sidx];
TL0=lo_list[sidx];
}
void main()
{
uchar i;
IE=0x82;
init();
for(i=0;i<14;i++)
{
write_iic(i,song_24c04[i]);
delayms(10);
}
while(1)
{
for(i=0;i<14;i++)
{
sidx=random_read(i);
TR0=1;
delayms(300);
}
put_port(48);
delayms(10);
put_port(48);
delayms(10);
put_port(48);
delayms(10);
put_port(10);
delayms(10);
put_port(13);//串口输出,作程序执行测试(测试OK)
}
} 展开
//STC89C52,12MHZ,24C04
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop6() {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
sbit scl=P2^0;
sbit sda=P2^1;
sbit spk=P0^1;
uchar code hi_list[]={0,226,229,232,233,236,238,240,241,242,244,245,246,247,248};
uchar code lo_list[]={0,4,13,10,20,3,8,6,2,23,5,26,1,4,3};
uchar code song_24c04[]={1,2,3,1,1,2,3,1,3,4,5,3,4,5};
uchar sidx;
void init()
{
SCON=0x50;
TMOD=0x20;
PCON=0x00;
TH1=243;
TL1=243;
TI=0;
TR1=1;
}
void delayms(uint x)
{
uchar i;
while(x--)for(i=0;i<120;i++);
}
void put_port(uchar c)
{
SBUF=c;while(TI==0);TI=0;
}
void start()
{
sda=1;scl=1;nop6();sda=0;nop6();scl=0;
}
void stop()
{
sda=0;scl=0;nop6();scl=1;nop6();sda=1;
}
void ack()
{
sda=1;nop6();scl=1;nop6();scl=0;
}
void no_ack()
{
sda=1;scl=1;nop6();scl=0;sda=0;
}
void write_byte(uchar b)
{
uchar i;
for(i=0;i<8;i++)
{
b<<=1;sda=CY;_nop_();scl=1;nop6();scl=0;
}
ack();
}
void write_iic(uchar addr,uchar dat)
{
start();
write_byte(0xa0);write_byte(addr);write_byte(dat);
stop();
delayms(10);
}
uchar read_byte()
{
uchar i,b;
sda=1;
for(i=0;i<8;i++)
{
scl=1;b<<=1;b|=sda;nop6();scl=0;nop6();
}
return b;
}
uchar read_current()
{
uchar d;
start();
write_byte(0xa1);d=read_byte();no_ack();
stop();
put_port(d);//串口测试输出
return d;
}
uchar random_read(uchar addr)
{
start();
write_byte(0xa0);write_byte(addr);
return read_current();
}
void to_int() interrupt 1
{
spk=!spk;
TH0=hi_list[sidx];
TL0=lo_list[sidx];
}
void main()
{
uchar i;
IE=0x82;
init();
for(i=0;i<14;i++)
{
write_iic(i,song_24c04[i]);
delayms(10);
}
while(1)
{
for(i=0;i<14;i++)
{
sidx=random_read(i);
TR0=1;
delayms(300);
}
put_port(48);
delayms(10);
put_port(48);
delayms(10);
put_port(48);
delayms(10);
put_port(10);
delayms(10);
put_port(13);//串口输出,作程序执行测试(测试OK)
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询