?? shiyan4.c
字號(hào):
//**程序設(shè)計(jì)**作者h(yuǎn)yg214**網(wǎng)名**展凌**
//程序功能:利用I2C總線寫或讀E2PROM
//向E2PROM寫n個(gè)數(shù)據(jù),再按頭尾地址讀出
//當(dāng)然你也可以設(shè)置n=1;那就只寫讀一個(gè)數(shù)據(jù)
//************************************
//********************************************
#include<reg52.h>
#include<intrins.h>//空操作需要的頭文件
#define uchar unsigned char
sbit sda=P2^0;//聲明控制信號(hào)
sbit scl=P2^1;
uchar idata tab[]={
//數(shù)碼顯示從0-9數(shù)字,讀ROM時(shí)
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
uchar idata sdate[50];//接收數(shù)據(jù)的數(shù)組變量
uchar tn=0;//從那個(gè)地址開始讀
uchar m=10;//讀出時(shí)要讀幾個(gè)數(shù)據(jù)
//你可以只讀一個(gè)(m=1),但這個(gè)程序讓它循環(huán)顯示
uchar n=10;//寫入數(shù)據(jù)的個(gè)數(shù)
uchar idata mdate[10]={53,28,37,45,69,74,56,82,30,99};//要寫入的數(shù)據(jù)
//*********************************************
//----------------------------------
void delay()//延時(shí)函數(shù)
{//約等于5us用于控制信號(hào)SDA,SCL的延時(shí)
uchar i;i++;
}
//----------------------------------
void delay_ms(uchar z)//延時(shí)函數(shù)
{//約等于z*0.5ms
//用于寫、讀間時(shí);數(shù)碼動(dòng)態(tài)顯示間時(shí)
uchar i,j;
for(i=z;i>0;i--)
for(j=222;j>0;j--);
}
//----------------------------------
void intc()//初始化函數(shù)
{
sda=1;//控制信號(hào)拉高
scl=1;
P0=0x00;//數(shù)碼管不顯示
}
//----------------------------------
void start()//起始信號(hào)函數(shù)
{//向ROM發(fā)送一個(gè)開始的信號(hào)
sda=1;
scl=1;
delay();
sda=0;
delay();
scl=0;
}
//----------------------------------
void stop()//停止信號(hào)函數(shù)
{//向ROM發(fā)送一個(gè)停止的信號(hào)
sda=0;
scl=1;
delay();
sda=1;
delay();
scl=0;
}
//----------------------------------
void respons()//產(chǎn)生應(yīng)答信號(hào)函數(shù)
{//接收一數(shù)據(jù)后應(yīng)答
sda=0;
scl=1;
delay();
scl=0;
sda=1;
}
//----------------------------------
void no_respons()//產(chǎn)生非應(yīng)答信號(hào)函數(shù)
{//接收一數(shù)據(jù)沒應(yīng)答,則強(qiáng)制
sda=0;
scl=1;
delay();
scl=0;
sda=1;
}
//----------------------------------
void write_date(uchar date)//向E2PEOM寫數(shù)據(jù)函數(shù)
{//向E2PROM寫一字節(jié)數(shù)據(jù)
uchar i,temp;
temp=date;
for(i=0;i<8;i++)//總共寫入8次位=1字節(jié)=1個(gè)數(shù)據(jù)
{
temp=temp<<1;//要存的數(shù)據(jù)左移1位,最高位給CY了
scl=0; //數(shù)據(jù)可以改變
delay();
sda=CY; //寫入一位
delay();
scl=1; //數(shù)據(jù)穩(wěn)定,不能寫了
delay();
}
scl=0;
delay();
sda=1;//穩(wěn)定數(shù)據(jù)
delay();
}
//----------------------------------
uchar read_date()//從E2PROM讀出數(shù)據(jù)函數(shù)
{//根據(jù)
uchar i,k;
scl=0;//數(shù)據(jù)改變
delay();
sda=1;//可讀信號(hào)
for(i=0;i<8;i++)//讀出8次位=1字節(jié)=1個(gè)數(shù)據(jù)
{
scl=1;//數(shù)據(jù)穩(wěn)定,可從sda讀出一位
delay();
k=(k<<1)|sda;//寫入時(shí)是從高位寫,讀出時(shí)也一行
scl=0;//數(shù)據(jù)改變(準(zhǔn)備下一位)
delay();
}
delay();
return k;//返回讀出的一個(gè)數(shù)據(jù)
}
//----------------------------------
void write_sword(uchar sta,uchar n,uchar *date)//寫入串?dāng)?shù)據(jù)函數(shù)
{//根據(jù)起始地址、數(shù)據(jù)個(gè)數(shù)及數(shù)據(jù)寫入E2PEOM中
uchar i;//計(jì)數(shù)變量
start();//起始
write_date(0xa0);//E2PROM地址(ATC02為oxa0)
respons();//應(yīng)答
write_date(sta);//寫入數(shù)據(jù)要存放的地址
respons();//應(yīng)答
for(i=0;i<n;i++)
{
write_date(date[i]);//寫入數(shù)據(jù)
respons();//應(yīng)答
}
stop();//停止
}
//-----------------------------------
uchar read_sword(uchar sta,uchar n)//讀出串?dāng)?shù)據(jù)函數(shù)
{//根據(jù)起始地址和讀出個(gè)數(shù),從E2PROM讀出一串?dāng)?shù)據(jù)
uchar i;//計(jì)數(shù)個(gè)數(shù)
start();//起始
write_date(0xa0);//AO表示主機(jī)向從機(jī)寫數(shù)據(jù)
respons();//應(yīng)答
write_date(sta);//數(shù)據(jù)地址
respons();//應(yīng)答
start();//起始
write_date(0xa1);//A1表示從機(jī)從主機(jī)讀取數(shù)據(jù)
for(i=0;i<n;i++)
{
respons();//應(yīng)答
*(sdate+i)=read_date();//讀出數(shù)據(jù)存入數(shù)組
}
no_respons();//非應(yīng)答
stop();//停止
return 1;//返回讀出的數(shù)組數(shù)據(jù)
}
//----------------------------------
void display(uchar *dat)//數(shù)碼管顯示函數(shù)
{//從E2PROM讀出的數(shù)據(jù)在數(shù)碼管顯示出來
uchar i,j,k,tem1,tem0,temp;
//計(jì)數(shù)變量,2個(gè)延時(shí)用,十位和個(gè)位,傳值變量
for(i=0;i<m;i++)
{
temp=*(dat+tn++);
if(tn==n){tn=0;}//這里讓它循環(huán)顯示
tem1=temp/10;//十位
tem0=temp%10;//個(gè)位
for(j=8;j>0;j--)
for(k=8;k>0;k--)//大約1s一個(gè)數(shù)據(jù)
{
P2=0x10;//選通二個(gè)數(shù)碼管
P0=tab[tem1];//顯示十位
delay_ms(20);//延時(shí)10ms,動(dòng)態(tài)顯示時(shí)暫停時(shí)間
//太快,不夠亮,太慢,不穩(wěn)定(閃爍),一般10ms
P2=0x20;
P0=tab[tem0];
delay_ms(20);
}
}
}
//----------------------------------
void main()//主函數(shù)
{//功能:初始化,寫數(shù)據(jù),讀數(shù)據(jù),顯示數(shù)據(jù)
uchar i=0,temp;//i表示開始讀的地址
intc();//初始化
write_sword(i,n,mdate);//根據(jù)起始地址和個(gè)數(shù),串?dāng)?shù)據(jù)寫入E2PROM
delay_ms(100);//延時(shí),寫入和讀出要有個(gè)時(shí)間的
temp=read_sword(i,n);//根據(jù)地址讀出一串?dāng)?shù)據(jù)
while(temp)
{
display(sdate);//讀出的數(shù)據(jù)用數(shù)碼管顯示出來
}
}
//-----------------------------------
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -