新聞中心

24c16的使用方法

作者: 時(shí)間:2016-11-21 來(lái)源:網(wǎng)絡(luò) 收藏
自己把24c16的使用結(jié)合代碼整理了一遍
之前看pcf時(shí)發(fā)現(xiàn)其實(shí)有很多芯片和24c系列的芯片很像,所以就不得不整理一下了,發(fā)辮以后回顧
r如果整理的不夠好,請(qǐng)不要噴我,我是弱菜

#include
#include //要用到_nop_()精準(zhǔn)延時(shí)
#define uchar unsigned char
#define uint unsigned int
sbit scl=P2^1;//24c16時(shí)鐘線,數(shù)據(jù)線定義
sbit sda=P2^0;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//數(shù)碼管顯示
uchar num,sec; //用來(lái)計(jì)時(shí)
void usdelay()
{
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
}
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void start() //起始信號(hào) 當(dāng)時(shí)鐘線為1,數(shù)據(jù)線有個(gè)下降沿
{
sda=1;
usdelay();
scl=1;
usdelay();
sda=0;
usdelay();
}

void stop()//終止信號(hào) 當(dāng)時(shí)鐘線為1,數(shù)據(jù)線有個(gè)上升沿
{
sda=0;
usdelay();
scl=1;
usdelay();
sda=1;
usdelay();
}

void ack() //應(yīng)答信號(hào)由從機(jī)發(fā)出信號(hào)為sda由1變?yōu)?
{
uchar i;
scl=1;
usdelay();
while((sda==1)&&i<200)i++;
scl=0;
usdelay();
}
void init24c16()//24c16初始化
{
sda=1;
usdelay();
scl=1;
usdelay();
}
void write_byte(uchar dat) //字節(jié)寫(xiě)(寫(xiě)數(shù)據(jù)或地址)數(shù)據(jù)線sda不變,scl有個(gè)上升沿,寫(xiě)入數(shù)據(jù)
{
uchar i;
for(i=0;i<8;i++)
{
scl=0;
usdelay();
sda=(bit)(dat&0x80);
usdelay();
scl=1;
usdelay();
dat<<=1;
}
scl=0;
usdelay();
}


uchar read_byte() //字節(jié)讀 scl有下降沿讀出
{
uchar i,k;
for(i=0;i<8;i++)
{
scl=1;
usdelay();
k=(k<<1)|sda;
scl=0;
usdelay();
}
return k;
}
void write_add(uchar add,uchar dat)
{
start();
write_byte(0xa0);
ack();
write_byte(add);
ack();
write_byte(dat);
ack();
stop();
}


uchar read_add(uchar add)
{
uchar dat;
start();
write_byte(0xa0);
ack();
write_byte(add);
ack();
start();
write_byte(0xa1);
ack();
dat=read_byte();
usdelay();
stop();
return dat;
}

void main()
{
init24c16();
sec=read_add(2);
if(sec>9) sec=0;
TMOD=0x01;//定時(shí)器選擇軟件啟動(dòng),工作方式為1
ET0=1;//開(kāi)定時(shí)器0允許
EA=1;//開(kāi)總中斷允許
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;//給定時(shí)器賦初值,定時(shí)50ms
TR0=1;//啟動(dòng)定時(shí)器0
while(1)
{
if(num==20)
{
num=0;
sec++;
if(sec>9)
sec=0;
}
P3=table[sec];
write_add(2,sec);
}
}
void t0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
num++;
}



關(guān)鍵詞: 24c16使用方

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉