標(biāo)題:
關(guān)于AT24c02的單片機(jī)讀取
[打印本頁(yè)]
作者:
陳小手43970
時(shí)間:
2017-10-31 18:42
標(biāo)題:
關(guān)于AT24c02的單片機(jī)讀取
想寫(xiě)一個(gè)單片機(jī)程序,用AT24c02來(lái)實(shí)現(xiàn)四個(gè)按鍵,一個(gè)按鍵讀取,一個(gè)保存數(shù)據(jù),一個(gè)讓數(shù)據(jù)從0自加,一個(gè)讓數(shù)據(jù)清零,數(shù)碼管顯示,先顯示一位。。
現(xiàn)在可以自加和清零,但是保存不行,讀取也每次都會(huì)讀到5,程序如下請(qǐng)指點(diǎn)。
/*******main***************/
#include<I2c.h>
uchar code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar num,disp[4];
sbit k2=P3^0;//¶áè¡
sbit k1=P3^1;//±£′æ
sbit k3=P3^2;//×Ô¼ó
sbit k4=P3^3; //Çåáã
//uchar code wei[2]={0x80,0x40};
void delay(uint i)
{
while(i--);
}
void Keypros()
{P3=P3|0x0f;
if(k1==0)
{
delay(1000); //Ïû¶¶′|àí
if(k1==0)
{
At24c02_Write(1,num); //ÔúμØÖ·1ÄúD′èëêy¾Ynum
}
while(!k1);
}
if(k2==0)
{
delay(1000); //Ïû¶¶′|àí
if(k2==0)
{
num=At24c02_Read(1); //¶áè¡EEPROMμØÖ·1ÄúμÄêy¾Y±£′æÔúnumÖD
}
while(!k2);
}
if(k3==0)
{
delay(100); //Ïû¶¶′|àí
if(k3==0)
{
num++; //êy¾Y¼ó1
if(num>255)num=0;
}
while(!k3);
}
if(k4==0)
{
delay(1000); //Ïû¶¶′|àí
if(k4==0)
{
num=0; //êy¾YÇåáã
}
while(!k4);
}
}
void datapros()
{
disp[0]=smgduan[num%10];
disp[1]=smgduan[num/10];
}
void DigDisplay()
{
uchar i;
for(i=0;i<1;i++)
{
P2=0xfe;
P0=disp[i];
delay(100);
P0=0x00;P2=0x00;
}
}
void main()
{
while(1)
{
datapros();
DigDisplay();
Keypros();
}
}
/********************I2c*/*/*/*/*****************/
#include<I2c.h>
void Delay10us()
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=2;a>0;a--);
}
void I2c_Start()
{
SCL=0;
Delay10us();
SDA=1;
Delay10us();
SCL=1;
Delay10us();
SDA=0;
Delay10us();
}
//void ack()
//{
//SCL=0;
// _nop_();
// SDA=0;
// SCL=1;
// _nop_();
// SCL=0;
// _nop_();
// SDA=1;
//
//}
void I2c_Stop()
{
SCL=0;
Delay10us();
SDA=0;
Delay10us();
SCL=1;
Delay10us();
SDA=1;
Delay10us();
}
uchar I2c_SendByte(uchar dat)
{
uchar i,b=0;
for(i=0;i<8;i++)
{
SDA=dat>>7;
dat=dat<<1;
Delay10us();
SCL=1;
Delay10us();
SCL=0;
Delay10us();
}
SDA=1;
Delay10us();
SCL=1;
while(SDA)
{
if(++b>200)
SCL=0;
Delay10us();
return 0;
}
SCL=0;
Delay10us();
return 1;
// ack();
}
uchar I2c_ReadByte()
{
uchar i,dat=0;
SDA=1;
Delay10us();
for(i=0;i<8;i++)
{
SCL=1;
Delay10us();
dat<<=1;
dat|=SDA;
Delay10us();
SCL=0;
Delay10us();
}
return dat;
}
void At24c02_Write(uchar address,uchar dat)
{
I2c_Start();
I2c_SendByte(0xa0);
I2c_SendByte(address);
I2c_SendByte(dat);
I2c_Stop();
}
uchar At24c02_Read(uchar address)
{
uchar dat;
I2c_Start();
I2c_SendByte(0xa0);
I2c_SendByte(address);
I2c_Start();
I2c_SendByte(0xa1);
dat=I2c_ReadByte();
I2c_Stop();
return dat;
}
/*************************I2c.h**************************/
#ifndef _I2c_H_
#define _I2c_H_
#include<reg52.h>
#include<intrins.h>
typedef unsigned int uint;
typedef unsigned char uchar;
sbit SCL=P2^1;
sbit SDA=P2^0;
void I2c_Start();
void I2c_Stop();
unsigned char I2c_SendByte(unsigned char dat);
unsigned char I2c_ReadByte();
void At24c02_Write(unsigned char addr,unsigned char dat);
unsigned char At24c02_Read(unsigned char addr);
#endif
復(fù)制代碼
作者:
zl2168
時(shí)間:
2017-10-31 20:40
本帖最后由 zl2168 于 2017-10-31 20:41 編輯
實(shí)例49 讀寫(xiě)AT24C02
先
Proteus
仿真一下,
確認(rèn)有效。
實(shí)例49 讀寫(xiě)IIC AT24C02.rar
(38.96 KB, 下載次數(shù): 9)
2017-10-31 20:40 上傳
點(diǎn)擊文件名下載附件
24c02.jpg
(210.53 KB, 下載次數(shù): 48)
下載附件
2017-10-31 20:41 上傳
以上摘自張志良編著《
80C51
單片機(jī)仿真設(shè)計(jì)實(shí)例教程
——
基于
Keil C
和
Proteus
》清華大學(xué)出版社
ISBN 978-7-302-41682-1
,
內(nèi)有常用的單片機(jī)應(yīng)用
100
案例,用于仿真實(shí)驗(yàn)操作,
電路與程序真實(shí)可靠可信可行。書(shū)中電路和程序設(shè)計(jì)有詳細(xì)說(shuō)明,程序語(yǔ)句條條有注解。仿真電路和
Hex
文件能在清華出版社網(wǎng)站免費(fèi)下載,程序源代碼只能到書(shū)上看了。
到圖書(shū)館借,或到新華書(shū)店翻閱,或到網(wǎng)上書(shū)店打折購(gòu)買(mǎi)。
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1