标题:简略的流水灯试验。用EEPROM完成。
解决方案:(c言语编程)
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f
,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
void delay1(uint);
sbit scl=P3^3;
sbit sda=P3^4;
void delay() //短延时子程序
{
;;
}
void init()//初使化子程序
{
sda=1;
scl=1;
}
void start() //开端子程序
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void write_byte(uchar dat) //写入字节子程序
{
uchar a,temp;
temp=dat;
for(a=0;a<8;a++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte() //读出字节子程序
{
uchar a,b,c;
scl=0;
delay();
sda=1;
delay();
for(a=0;a<8;a++)
{
scl=1;
delay();
b=sda;
c=(c<<1)|b;
scl=0;
delay();
}
return c;
}
void respon()//呼应子程序
{
uchar a;
scl=1;
delay();
while((sda==1)&&(a<100))a++;
scl=0;
delay();
}
void write(uchar add,uchar dat) //写入子程序
{
start();
write_byte(0xa0);
respon();
write_byte(add);
respon();
write_byte(dat);
respon();
stop();
}
uchar read(uchar add) //读出子程序
{
uchar dat;
start();
write_byte(0xa0);
respon();
write_byte(add);
respon();
start();
write_byte(0xa1);
respon();
dat=read_byte();
respon();
stop();
return dat;
}
void main() //main 函数
{
uchar i;
init();
while(1)
{
for(i=0;i<16;i++)
{
write(3,table[i]);
delay1(10);
P0=read(3);
}
}
}
void delay1(uint a) { //延时子函数
uint i,j;
for(j=a;j>0;j–)
for(i=2500;i>0;i–) ;
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/dongtai/261008.html