IIC記憶單片機開機次數(shù)
下面是IIC記憶單片機開機次數(shù)的程序:
本文引用地址:http://m.butianyuan.cn/article/201611/323771.htm程序如下:
//版權所有:單片機網(wǎng)http://www.51hei.com
#include
#include
#define uchar unsigned char//宏定義
#define uint unsigned int
sbit scl=P2^0;//這兩個位定義是控制IIC的引腳
sbit sda=P2^1;
uchar number[10]={ //數(shù)碼管數(shù)組定義
0x3f,
0x06,
0x5b,
0x4f,
0x66,
0x6d,
0x7d,
0x07,
0x7f,
0x6f
};
void delayt(void)//數(shù)碼管的延時
{
uint x,y;
for(x=0;x<5;x++)
for(y=0;y<120;y++);
}
void delay(void)//IIC的適應延時
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
void start()//開始
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()//停止
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void init()//初始化設置
{
sda=1;
scl=1;
}
void answer()//應答
{
uchar i;
scl=1;
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void noanser()//非應答
{
scl=1;
delay();
sda=1;
delay();
scl=0;
delay();
}
void writebyte(uchar dat)//寫字節(jié)
{
uchar i;
scl=0;
delay();
for(i=0;i<8;i++)
{
if(dat&0x80)
{
sda=1;
}
else
{
sda=0;
}
dat=dat<<1;
delay();
scl=1;
delay();
scl=0;
delay();
}
sda=1;
}
uchar readbyte()//讀字節(jié)
{
uchar i,dat;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
dat=dat<<1;
if(sda)
{
dat++;
}
scl=0;
delay();
}
return dat;
}
void write_byte(uchar add,uchar dat)//寫字節(jié)的封裝
{
init();
start();
writebyte(0xae);
answer();
writebyte(add);
answer();
writebyte(dat);
answer();
stop();
}
uchar read_byte(uchar add)//讀字節(jié)的封裝
{
uchar value;
init();
start();
writebyte(0xae);
answer();
writebyte(add);
answer();
start();
writebyte(0xaf);
answer();
value=readbyte();
noanser();
stop();
return value;
}
void main()
{
uchar temp,k,j;
temp=read_byte(0x10);//數(shù)碼管個位和十們的操作
k=temp/10;
j=temp%10;
temp++;
write_byte(0x10,temp);
delay();
while(1)//數(shù)碼管顯示
{
P1=number[k];
P0=0;
delayt();
P1=number[j];
P0=1;
delayt();
}
}
評論