找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2649|回复: 1
打印 上一主题 下一主题
收起左侧

关于AT24c02的单片机读取

[复制链接]
跳转到指定楼层
楼主
想写一个单片机程序,用AT24c02来实现四个按键,一个按键读取,一个保存数据,一个让数据从0自加,一个让数据清零,数码管显示,先显示一位。。


现在可以自加和清零,但是保存不行,读取也每次都会读到5,程序如下请指点。


  1. /*******main***************/
  2. #include<I2c.h>

  3. uchar code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

  4. uchar num,disp[4];
  5. sbit k2=P3^0;//¶áè¡
  6. sbit k1=P3^1;//±£′æ
  7. sbit k3=P3^2;//×Ô¼ó
  8. sbit k4=P3^3;        //Çåáã

  9. //uchar code wei[2]={0x80,0x40};

  10. void delay(uint i)
  11. {
  12.         while(i--);        
  13. }

  14. void Keypros()
  15. {P3=P3|0x0f;
  16.         if(k1==0)
  17.         {
  18.                 delay(1000);  //Ïû¶¶′|àí
  19.                 if(k1==0)
  20.                 {
  21.                         At24c02_Write(1,num);   //ÔúμØÖ·1ÄúD′èëêy¾Ynum
  22.                 }
  23.                 while(!k1);
  24.         }
  25.         
  26.                 if(k2==0)
  27.         {
  28.                 delay(1000);  //Ïû¶¶′|àí
  29.                 if(k2==0)
  30.                 {
  31.                         num=At24c02_Read(1);          //¶áè¡EEPROMμØÖ·1ÄúμÄêy¾Y±£′æÔúnumÖD
  32.                 }
  33.                 while(!k2);
  34.         }
  35.                         if(k3==0)
  36.         {
  37.                 delay(100);  //Ïû¶¶′|àí
  38.                 if(k3==0)
  39.                 {
  40.                         num++;           //êy¾Y¼ó1
  41.                         if(num>255)num=0;
  42.                 }
  43.                 while(!k3);
  44.         }
  45.         
  46.         
  47.                 if(k4==0)
  48.         {
  49.                 delay(1000);  //Ïû¶¶′|àí
  50.                 if(k4==0)
  51.                 {
  52.                         num=0;                 //êy¾YÇåáã
  53.                 }
  54.                 while(!k4);
  55.         }        
  56. }


  57. void datapros()
  58. {
  59.   disp[0]=smgduan[num%10];
  60.         disp[1]=smgduan[num/10];        
  61. }

  62. void DigDisplay()
  63. {
  64.         uchar  i;
  65.         for(i=0;i<1;i++)
  66.         {
  67.                 P2=0xfe;
  68.                 P0=disp[i];
  69.                 delay(100);
  70.                 P0=0x00;P2=0x00;
  71.         }
  72. }               

  73. void main()
  74. {
  75.         while(1)
  76.         {               
  77.                 datapros();
  78.                 DigDisplay();
  79.                 Keypros();
  80.         }
  81. }







  82. /********************I2c*/*/*/*/*****************/


  83. #include<I2c.h>

  84. void Delay10us()
  85. {
  86.         unsigned char a,b;
  87.         for(b=1;b>0;b--)
  88.                 for(a=2;a>0;a--);
  89. }

  90. void I2c_Start()
  91. {
  92.         SCL=0;
  93.         Delay10us();        
  94.   SDA=1;
  95.         Delay10us();
  96.         SCL=1;
  97.         Delay10us();
  98.         SDA=0;
  99.         Delay10us();

  100. }

  101. //void ack()
  102. //{
  103. //SCL=0;
  104. //        _nop_();
  105. //        SDA=0;
  106. //        SCL=1;
  107. //        _nop_();
  108. //        SCL=0;
  109. //        _nop_();
  110. //        SDA=1;
  111. //        
  112. //}
  113. void I2c_Stop()
  114. {
  115.         SCL=0;
  116.         Delay10us();        
  117.         SDA=0;
  118.         Delay10us();
  119.         SCL=1;
  120.         Delay10us();
  121.         SDA=1;
  122.         Delay10us();
  123. }

  124. uchar I2c_SendByte(uchar dat)
  125. {
  126.   uchar i,b=0;
  127.         for(i=0;i<8;i++)
  128.         {
  129.           SDA=dat>>7;
  130.           dat=dat<<1;
  131.                 Delay10us();
  132.                 SCL=1;
  133.                 Delay10us();
  134.                 SCL=0;
  135.                 Delay10us();               
  136.         }
  137.         SDA=1;
  138.         Delay10us();
  139.         SCL=1;
  140.         while(SDA)
  141.         {
  142.                 if(++b>200)
  143.                         SCL=0;
  144.                 Delay10us();
  145.                 return 0;
  146.         }
  147.         SCL=0;
  148.         Delay10us();
  149.         return 1;        
  150. //        ack();
  151. }

  152. uchar I2c_ReadByte()
  153. {
  154.         uchar i,dat=0;
  155.         SDA=1;
  156.         Delay10us();
  157.         for(i=0;i<8;i++)
  158.         {
  159.         SCL=1;
  160.         Delay10us();
  161.         dat<<=1;
  162.         dat|=SDA;
  163.         Delay10us();        
  164.                 SCL=0;
  165.                 Delay10us();        
  166.                
  167.         }
  168.         return dat;
  169. }

  170. void At24c02_Write(uchar address,uchar dat)
  171. {

  172.   I2c_Start();
  173.         I2c_SendByte(0xa0);
  174.         I2c_SendByte(address);
  175.         I2c_SendByte(dat);
  176.         I2c_Stop();        
  177. }

  178. uchar At24c02_Read(uchar address)
  179. {
  180. uchar dat;
  181.   I2c_Start();        
  182.         I2c_SendByte(0xa0);
  183.         I2c_SendByte(address);
  184.         I2c_Start();
  185.         I2c_SendByte(0xa1);
  186.         dat=I2c_ReadByte();
  187.         I2c_Stop();        
  188.         return dat;

  189. }



  190. /*************************I2c.h**************************/


  191. #ifndef _I2c_H_
  192. #define _I2c_H_
  193. #include<reg52.h>
  194. #include<intrins.h>
  195. typedef unsigned int uint;
  196. typedef unsigned char uchar;

  197. sbit SCL=P2^1;
  198. sbit SDA=P2^0;

  199. void I2c_Start();
  200. void I2c_Stop();
  201. unsigned char I2c_SendByte(unsigned char dat);
  202. unsigned char I2c_ReadByte();
  203. void At24c02_Write(unsigned char addr,unsigned char dat);
  204. unsigned char At24c02_Read(unsigned char addr);
  205. #endif
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:111634 发表于 2017-10-31 20:40 | 只看该作者
本帖最后由 zl2168 于 2017-10-31 20:41 编辑

实例49  读写AT24C02
Proteus仿真一下,确认有效。
实例49 读写IIC AT24C02.rar (38.96 KB, 下载次数: 9)

以上摘自张志良编著《80C51单片机仿真设计实例教程——基于Keil CProteus》清华大学出版社ISBN 978-7-302-41682-1内有常用的单片机应用100案例,用于仿真实验操作,电路与程序真实可靠可信可行。书中电路和程序设计有详细说明,程序语句条条有注解。仿真电路和Hex文件能在清华出版社网站免费下载,程序源代码只能到书上看了。到图书馆借,或到新华书店翻阅,或到网上书店打折购买。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表