|
24C02實驗中,數(shù)據(jù)能正常寫入,讀出,也能正常顯示,但是在開機的時候,10有8次都顯示||||,只有2次能正常顯示初始界面0000,請教原因。感謝。
下面是程序,
/*******************************************************************************
* 實驗名 : EEPROM實驗
* 使用的IO :
* 實驗效果 : 按K1保存顯示的數(shù)據(jù),按K2讀取上次保存的數(shù)據(jù),按K3顯示數(shù)據(jù)加一,
*按K4顯示數(shù)據(jù)清零。
* 注意 :由于P3.2口跟紅外線共用,所以做按鍵實驗時為了不讓紅外線影響實驗效果,最好把紅外線先
*取下來。
*
*********************************************************************************/
#include<reg51.h>
#include"i2c.h"
//數(shù)碼管IO
#define DIG P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
//按鍵IO
sbit K1=P3^1;
sbit K2=P3^0;
sbit K3=P3^2;
sbit K4=P3^3;
void At24c02Write(unsigned char ,unsigned char );
unsigned char At24c02Read(unsigned char );
void Delay1ms();
void Timer0Configuration();
unsigned char code DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Num=0;
unsigned char disp[8]={0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
/*******************************************************************************
* 函數(shù)名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void main()
{
unsigned char num0=0,n;
Timer0Configuration();
while(1)
{
if(K1==0)
{
Delay1ms();
if(K1==0)
At24c02Write(2,num0); //存num0到2402
while((n<200)&&(K3==0))
{
n++;
Delay1ms();
}
n=0;
}
if(K2==0)
{
Delay1ms();
if(K2==0)
num0=At24c02Read(2); //讀2402給num0
while((n<200)&&(K3==0))
{
n++;
Delay1ms();
}
n=0;
}
if(K3==0)
{
Delay1ms();
if(K3==0)
num0++; //按一次K3,num0+1
while((n<50)&&(K3==0))
{
n++;
Delay1ms();
}
n=0;
if(num0==256) //8位,0-255循環(huán)。
num0=0;
}
if(K4==0)
{
Delay1ms();
if(K4==0)
num0=0; //按K4,num0清零
while((n<200)&&(K3==0))
{
n++;
Delay1ms();
}
n=0;
}
disp[0]=DIG_CODE[num0/1000];//千位
disp[1]=DIG_CODE[num0%1000/100];//百位
disp[2]=DIG_CODE[num0%1000%100/10];//十位
disp[3]=DIG_CODE[num0%1000%100%10];//個位
disp[4]=DIG_CODE[num0/1000];//千位
disp[5]=DIG_CODE[num0%1000/100];//百位
disp[6]=DIG_CODE[num0%1000%100/10];//十位
disp[7]=DIG_CODE[num0%1000%100%10];//個位
}
}
/*******************************************************************************
* 函數(shù)名 : Timer0Configuration()
* 函數(shù)功能 : 設(shè)置計時器
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void Timer0Configuration()
{
TMOD=0X02;//選擇為定時器模式,工作方式2,僅用TRX打開啟動。
TH0=0X0; //給定時器賦初值,定時100us
TL0=0X0;
ET0=1;//打開定時器0中斷允許
EA=1;//打開總中斷
TR0=1;//打開定時器
}
/*******************************************************************************
* 函數(shù)名 : Delay1ms()
* 函數(shù)功能 : 延時
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void Delay1ms() //誤差 0us
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
/*******************************************************************************
* 函數(shù)名 : void At24c02Write(unsigned char addr,unsigned char dat)
* 函數(shù)功能 : 往24c02的一個地址寫入一個數(shù)據(jù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void At24c02Write(unsigned char addr,unsigned char dat)
{
I2cStart();
I2cSendByte(0xa0);//發(fā)送寫器件地址
I2cSendByte(addr);//發(fā)送要寫入內(nèi)存地址
I2cSendByte(dat); //發(fā)送數(shù)據(jù)
I2cStop();
}
/*******************************************************************************
* 函數(shù)名 : unsigned char At24c02Read(unsigned char addr)
* 函數(shù)功能 : 讀取24c02的一個地址的一個數(shù)據(jù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
unsigned char At24c02Read(unsigned char addr)
{
unsigned char num;
I2cStart();
I2cSendByte(0xa0); //發(fā)送寫器件地址
I2cSendByte(addr); //發(fā)送要讀取的地址
I2cStart();
I2cSendByte(0xa1); //發(fā)送讀器件地址
num=I2cReadByte(); //讀取數(shù)據(jù)
I2cStop();
return num;
}
/*******************************************************************************
* 函數(shù)名 : DigDisplay() interrupt 1
* 函數(shù)功能 : 中斷數(shù)碼管顯示
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void DigDisplay() interrupt 1 //定時器0中斷服務(wù)
{
//定時器在工作方式二會自動重裝初,所以不用在賦值。
// TH0=0X9c;//給定時器賦初值,定時1ms 定時器1ms中斷一次,數(shù)碼管1ms移位,顯示一次。
// TL0=0X00;
DIG=0; //消隱
switch(Num) //位選,選擇點亮的數(shù)碼管,
{
case(7):
LSA=0;LSB=0;LSC=0; break;
case(6):
LSA=1;LSB=0;LSC=0; break;
case(5):
LSA=0;LSB=1;LSC=0; break;
case(4):
LSA=1;LSB=1;LSC=0; break;
case(3):
LSA=0;LSB=0;LSC=1; break;
case(2):
LSA=1;LSB=0;LSC=1; break;
case(1):
LSA=0;LSB=1;LSC=1; break;
case(0):
LSA=1;LSB=1;LSC=1; break;
}
DIG=disp[Num]; //段選,選擇顯示的數(shù)字。
Num++;
if(Num>7)
Num=0;
}
|
|