找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机+AT24C64读写有问题,求分析!

[复制链接]
跳转到指定楼层
楼主
EEPROM AT24C64地址1写入数据68,结果为255,怎么判断数据是否写入,或者问题是否出现在读?以下是程序和逻辑分析仪时序,求分析。

  1. void Datapros(uint num)
  2. {

  3.         disp[0]=num/100+0x30;
  4.         disp[1]=num%100/10+0x30;
  5.         disp[2]=num%100%10+0x30;
  6.         Disp_shuzi(100,100,disp[0],Red);
  7.         Disp_shuzi(108,100,disp[1],Red);
  8.         Disp_shuzi(116,100,disp[2],Red);
  9. }


  10. main()                     
  11.   {            
  12.            while(1)      
  13.             {   _24c64WriteByte(1,68);                                 num=_24c64ReadByte(1);        
  14.                 Datapros(num);
  15.             }
  16.         
  17.    }
  18. /******************************************
  19. AT24C64C语言程序  
  20.             copyright@lance  
  21. ******************************************/   


  22. sbit   ISCL = P6^4;
  23. sbit   ISDA = P6^3;

  24. void delay1ms(Uint n)
  25. {
  26.         Uint i,j;
  27.                 for(i=n;i>0;i--)
  28.                 {
  29.                         for(j=750;j>0;j--)
  30.                                 {
  31.                                 }
  32.                 }
  33. }

  34. /*****************************************  
  35. I2c Wait  
  36. Wait for some time to get proper I2C timing  
  37. ******************************************/   
  38. void I2cWait(void)   
  39. {   
  40. //  unsigned int i=10; //24C64
  41.            unsigned int i=5;//24C256
  42.     while(i--);
  43. }   


  44. /*****************************************  
  45. i2c start  
  46. condition SDA 1-0 while SCL=1  
  47. ******************************************/   
  48. void I2cStart(void)   
  49. {   
  50.     ISDA=1;   
  51.     ISCL=1;   
  52.     I2cWait();   
  53.     ISDA=0;   
  54.     I2cWait();   
  55.     ISCL=0;  
  56.         I2cWait();
  57. }   


  58. /*****************************************  
  59. I2c sotp  
  60. condition SDA=0-1 while SCL=1  
  61. ******************************************/   
  62. void I2cStop(void)   
  63. {   
  64.     ISDA=0;   
  65.     I2cWait();   
  66.     ISCL=1;   
  67.     I2cWait();   
  68.     ISDA=1;
  69.         I2cWait();           
  70. }   




  71. /*****************************************  
  72. I2c Init  
  73. Initialize I2C interface  
  74. Release I2c BUS  
  75. ****************************************  
  76. void I2cInit(void)   
  77. {   
  78.     ISDA=1;   
  79.     ISCL=1;   
  80.         I2cWait();
  81. }   
  82. **/   

  83. /*****************************************  
  84. I2c SentByte  
  85. master transfer data to slave and return acknowledge bit  
  86. don't include<intrins.h>  
  87. ******************************************/   
  88. bit I2cSentByte(unsigned char bytedata)   
  89. {   
  90.     unsigned char i;   
  91.     bit ack;   

  92.     for(i=0;i<8;i++)   
  93.     {   
  94.        if(bytedata & 0x80)   
  95.            ISDA=1;   
  96.        else   
  97.            ISDA=0;   
  98.            bytedata<<=1;   
  99.        I2cWait();   

  100.        ISCL=1;   
  101.        I2cWait();   
  102.        ISCL=0;   
  103.        I2cWait();   
  104.     }   
  105.     ISDA=1;   
  106.     I2cWait();   
  107.     ISCL=1;   
  108.     I2cWait();   
  109.     ack=ISDA;   
  110.     ISCL=0;   
  111.     I2cWait();   
  112.     return ack;   
  113. }   


  114. /*****************************************  
  115. I2c ReceiveByte  
  116. slave trransfer data to master  
  117. ******************************************/   
  118. unsigned char I2cReceiveByte(void)   
  119. {   
  120.     unsigned char i;   
  121.     unsigned char bytedata=0;   

  122.         ISCL=0;   
  123.         I2cWait();
  124.     for(i=0;i<8;i++)   
  125.     {   
  126.         ISCL=1;   
  127.         I2cWait();   
  128.         bytedata=bytedata<<1;   
  129.         if(ISDA==1)
  130.                 {   
  131.                 bytedata = bytedata|0x01;
  132.                 }

  133.         I2cWait();   
  134.         ISCL=0;   
  135.                 I2cWait();
  136.     }   
  137.     return bytedata;   
  138. }   


  139. /*****************************************  
  140. I2c SendAcknowledge  
  141. Master send acknowledge bit to slave  
  142. acknowledge="0",non-acknowledge="1"  
  143. ******************************************/   
  144. void SendAcknowledge(bit ack)   
  145. {   
  146.     ISDA=ack;   
  147.     ISCL=1;   
  148.     I2cWait();   
  149.     ISCL=0;           
  150. }   


  151. /*****************************************  
  152. 24c64 WriteByte  
  153. addr:0-8192  
  154. value:数据  
  155. ******************************************/   
  156. void _24c64WriteByte(unsigned int addr,unsigned char value)   
  157. {   

  158.     I2cStart();   
  159.     I2cSentByte(0xA0);   

  160.     I2cSentByte(addr>>8);   //送高位地址   
  161.     I2cSentByte(addr&0x00ff); //送低位地址   

  162.     I2cSentByte(value);

  163.     I2cStop();   
  164.     delay1ms(15);  
  165. }   



  166. /*****************************************  
  167. 24c64 WriteMulti  
  168. page:0-255  
  169. count:要写入的数个数  
  170. ******************************************/
  171. void _24c64WriteMulti(unsigned int page,unsigned char count,unsigned char *SenBuf)   
  172. {   
  173.     unsigned char i;   
  174.     unsigned int addr=page*32;

  175.     I2cStart();   
  176.     I2cSentByte(0xa0);   

  177.     I2cSentByte(addr>>8);   //送高位地址   
  178.     I2cSentByte(addr&0x00ff); //送低位地址  

  179.      for(i=0;i<count;i++)   
  180.      {     
  181.       I2cSentByte(SenBuf[i]);   
  182.      }   
  183.     I2cStop();   
  184.     delay1ms(15);
  185. }   

  186. /*****************************************  
  187. 24c64 ReadByte  
  188. addr:0-8192  
  189. ******************************************/   
  190. unsigned char _24c64ReadByte(unsigned int addr)   
  191. {   
  192.     unsigned char temp;

  193.     I2cStart();   
  194.     I2cSentByte(0xa0);   

  195.     I2cSentByte(addr>>8);   //送高位地址   
  196.     I2cSentByte(addr&0x00ff); //送低位地址   


  197.     I2cStart();   
  198.     I2cSentByte(0xa1);
  199.     temp=I2cReceiveByte();   
  200.     SendAcknowledge(1);   
  201.     I2cStop();   
  202.     return temp;   
  203. }   


  204. /*****************************************  
  205. 24c64 ReadMulti  
  206. page:0-256,count0-32  
  207. count:要读出的数的个数  
  208. ******************************************/   
  209. void _24c64ReadMulti(unsigned int page,unsigned char count,unsigned char *RedBuf)   
  210. {   unsigned int addr=page*32;   
  211.     unsigned char i;   
  212.     for(i=0;i<count;i++)   
  213.     {                        
  214.          RedBuf[i]=_24c64ReadByte(addr);   
  215.          addr++;   
  216.     }   
  217.     SendAcknowledge(1);   
  218.     I2cStop();   
  219. }   
复制代码

2.png (29.34 KB, 下载次数: 54)

2.png

1.png (34.28 KB, 下载次数: 51)

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

使用道具 举报

沙发
ID:401564 发表于 2020-10-19 20:16 | 只看该作者
你这么一堆代码,别人也不好找问题的
IIC时序要求严格,所以,最好是从最基本的写函数开始,先写一个写入的函数,再设置一个IO连接LED来指示,写入一个字节的数据之后,等待应答,有应答了再通过LED来提示,只要是有应答了,就说明写函数是没有问题了,然后再慢慢的一步一步的向上找问题
你一下一个完整的程序出来,哪里出了问题都不知道的
有开发板的话,最好是开发板验证,不推荐仿真
回复

使用道具 举报

板凳
ID:518902 发表于 2020-10-19 23:15 | 只看该作者
同意楼上的观点,一点点看的确很费眼.
i2c时序要求严格 建议你用逻辑分析仪抓一下信号看一看~
回复

使用道具 举报

地板
ID:743654 发表于 2020-10-20 08:46 | 只看该作者
laopihappy123 发表于 2020-10-19 23:15
同意楼上的观点,一点点看的确很费眼.
i2c时序要求严格 建议你用逻辑分析仪抓一下信号看一看~

逻辑分析仪已抓取时序,貌似跟手册上时序一样
回复

使用道具 举报

5#
ID:814291 发表于 2020-10-20 10:32 | 只看该作者
你换一换地址,有的盗版的芯片,地址1没法写入,你往后错错位置,写入
回复

使用道具 举报

6#
ID:743654 发表于 2020-10-20 11:19 | 只看该作者
问题找到了,是我IO口没有设置成开漏模式,现在读写正常。
回复

使用道具 举报

7#
ID:337139 发表于 2020-10-20 17:52 | 只看该作者
读写时首先要做的是对IO口的设置,然后是对协议的解读。找到问题就是好样的。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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