找回密码
 立即注册

QQ登录

只需一步,快速开始

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

我的单片机+iic程序怎么读写不了? 搞了好几天搞不明白

[复制链接]
跳转到指定楼层
楼主
ID:1009775 发表于 2022-3-11 19:41 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
求求大佬们看看哪出问题了,1602模块没问题 仿真里读出来的是一个黑块  

at24c02.c
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5.         
  6. sbit SCK = P1^3;
  7. sbit SDA = P1^4;

  8. void Delayms(uint x);
  9. void Delayus(uchar x);
  10. void Delay5us();
  11. void Delay5ms();

  12. void init(){
  13.         SCK = 1;
  14.         _nop_();_nop_();_nop_();
  15.         SDA = 1;
  16.         _nop_();_nop_();_nop_();
  17. }
  18. void start(){
  19.         SCK = 1;
  20.         _nop_();_nop_();_nop_();
  21.         SDA = 1;
  22.         _nop_();_nop_();_nop_();
  23.         _nop_();_nop_();_nop_();
  24.         SDA = 0;
  25.         _nop_();_nop_();_nop_();
  26.         _nop_();_nop_();_nop_();
  27.         SCK = 0;
  28. }
  29.         
  30. void stop(){
  31.         SCK  =1;
  32.         SDA = 0;
  33.         _nop_();_nop_();_nop_();
  34.         _nop_();_nop_();_nop_();
  35.         SDA = 1;
  36.         _nop_();_nop_();_nop_();
  37.         _nop_();_nop_();_nop_();
  38.         SDA = 0;
  39.         SCK = 0;
  40. }
  41. void WaitAck(){
  42.         uchar i = 0;
  43.         SDA = 1;
  44.         SCK = 1;
  45.         Delayus(5);
  46.         while(SDA == 1 && (i < 250))
  47.                 i++;
  48.         SCK = 0;
  49.         _nop_(), _nop_(), _nop_();
  50. }


  51. //void react(){
  52. //        SCK = 0;
  53. //        SDA = 1;
  54. //        SDA = 0;
  55. //        SCK = 1;
  56. //        _nop_();_nop_();_nop_();
  57. //        _nop_();_nop_();_nop_();
  58. //        SCK = 0;
  59. //        SDA = 1;
  60. //}
  61. void noack(){
  62.          SDA=1;
  63.          _nop_();_nop_();_nop_();
  64.          SCK=1;
  65.          _nop_();_nop_();_nop_();
  66.                 _nop_();_nop_();_nop_();
  67.          SCK=0;
  68.          _nop_();_nop_();_nop_();
  69.                 SDA = 0;
  70. }

  71. uchar readByte(){
  72.         uchar i, k = 0;
  73.         for(i = 0; i < 8; i++){
  74.                 SCK = 0;
  75.                 k <<= 1;
  76.                 Delay5us();        
  77.                 if(SDA == 1){
  78.                         k |= 0x01;
  79.                 }
  80.                 SCK = 0;
  81.                 Delay5us();
  82.         }
  83.         _nop_();
  84.         Delay5ms();
  85.         return k;
  86. }

  87. void writeByte(uchar dat){
  88.         uchar i = 0;
  89.         for(i = 0; i < 8; i++){
  90.                 SCK = 0;
  91.                 Delay5us();
  92.                 SDA = dat & 0x80;
  93.                 _nop_();_nop_();_nop_();_nop_();
  94.                 SCK = 1;
  95.                 Delay5us();
  96.                 dat <<= 1;
  97.         }
  98.         SCK = 0;
  99.         _nop_();_nop_();_nop_();
  100.         Delay5ms();
  101. }

  102. void addByte(uchar wordAddress, uchar dat){
  103.         start();
  104.         writeByte(0xa0);
  105.         WaitAck();
  106.         writeByte(wordAddress);
  107.         WaitAck();
  108.         writeByte(dat);
  109.         WaitAck();
  110.         stop();
  111.         Delayms(5);
  112. }

  113. uchar readAddByta(uchar wordAddress){
  114.         uchar dat;
  115.         start();
  116.         writeByte(0xa0);
  117.         WaitAck();
  118.         writeByte(wordAddress);
  119.         WaitAck();
  120.         stop();
  121.         start();
  122.         writeByte(0xa1);
  123.         WaitAck();
  124.         dat = readByte();
  125.         noack();
  126.         stop();
  127.         return dat;
  128. }

  129. void Delayms(uint x){
  130.         int i, j;
  131.         for(i = x; i > 0; --i){
  132.                 for(j = 110; j > 0; --j);
  133.         }
  134. }
  135. void Delayus(uchar x){
  136.         while(x--);
  137. }

  138. void Delay5us()                //@12.000MHz
  139. {
  140.         _nop_();
  141. }

  142. void Delay5ms()                //@12.000MHz
  143. {
  144.         unsigned char i, j;

  145.         i = 10;
  146.         j = 183;
  147.         do
  148.         {
  149.                 while (--j);
  150.         } while (--i);
  151. }
复制代码

main.c
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #include "at24c02.h"
  4. #include "proteus_1602.h"
  5. #define uchar unsigned char
  6. #define uint unsigned int
  7. sbit SCK = P1^3;
  8. sbit SDA = P1^4;
  9. void main(){
  10.         Delayms(2000);
  11.         while(1){
  12.                 uchar temp = 0;
  13.                 init();
  14.                 init_1602();
  15.                 addByte(0x00, 0x00);
  16.                 Delayms(10);
  17.                 temp = readAddByta(0x00);
  18.                 writeData_1602(temp + 0x38);
  19.                 Delayms(50);
  20.         }         
  21. }
复制代码



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

使用道具 举报

沙发
ID:1009775 发表于 2022-3-11 19:42 | 显示全部楼层
1602模块没问题 仿真里读出来的是一个黑块  
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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