新聞中心

51單片機-AT24C

作者: 時間:2016-12-02 來源:網(wǎng)絡(luò) 收藏
void write(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
SCL=0;
delay();
SDA=CY;
delay();
SCL=1;
delay();
}
SCL=0;
delay();
SDA=1;
delay();
}
uchar read()
{
uchar i,k;
SCL=0;
delay();
SDA=1;
delay();
for(i=0;i<8;i++)
{
SCL=1;
delay();
k=(k<<1)|SDA; // 將SDA賦給K的每一位
SCL=0;
delay();
}
return k;
}
void response(){
int i;
SCL = 1; //在SCL為高電平期間,進行應(yīng)答
delay();
while((SDA == 1) && (i < 170))i++; //SCL釋放總線,等待從設(shè)備應(yīng)答,從設(shè)備會把SDA拉低
SCL = 0;
delay();
}
void At24c_Write(uchar address, uchar value){
start();
write(0xa0); //寫入設(shè)備地址
response();
write(address); //寫入首地址
response();
write(value); //寫入數(shù)據(jù)
response();
stop();
}
uchar At24c_Read(uchar address){
uchar value;
start();
write(0xa0); //偽寫,先寫入設(shè)備地址
response();
write(address); //再寫入要讀取數(shù)據(jù)的首地址
response();
start(); //再次
write(0xa1); //寫入要讀取數(shù)據(jù)的地址,方向為1
response();
value = read(); //讀取數(shù)據(jù)
stop();
return value;
}
void main(){
uint i;
flag = 1;
P1 = 0x0; // 位選,使最地位數(shù)碼管被選中
EA = 1;
ET0 = 1;
sum = 0;
temp = 0;
TMOD = 0x01;
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
TR0 = 1;
Init();
P2 = At24c_Read(23);
for(i = 0; i < 10; i++)
if(duan[ i] == P2){
sum = i;
break;
flag = 0;
}
while(1){
if(temp == 20){
temp = 0;
if(sum == 10){
sum = 0;
}
if(flag == 1){
At24c_Write(23,duan[sum]);
delay1(100);
P2 = duan[sum];
}
sum++;
}
}
}
void time0() interrupt 1{
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
temp++;
}
上一頁 1 2 3 下一頁

關(guān)鍵詞: 51單片機AT24C數(shù)字芯

評論


技術(shù)專區(qū)

關(guān)閉