全新论坛MCU智学网上线,欢迎访问新论坛!稀缺资源、技术干货、参考设计、原厂资料尽在MCU智学网
更新自动建库工具PCB Footprint Expert 2023.13 Pro / Library Expert 破解版

新手求教I2C,盼大虾回复!!!

[复制链接]
5162 3

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
各位大虾,作为新手的小僧想研究研究I2C总线,就写了一些代码,主要实现这样一个功能:
先向EEPROM发送一个数01010101,然后当P1.0按下后,将这个数据再回传至MCU,送至P3口,驱动8展LED。
似乎木有反应啊~
启动,停止,发送,接收,应答子程序省去~

main()
{
        static unsigned char A;

        delay(2000);

        Start_I2C();
        SendByte(0x00);
        Ack_I2C(0);
        SendByte(0x33);
        Ack_I2C(1);
        Stop_I2C();

        while(!P^0)
        {
        Start_I2C();
        SendByte(0x01);
        Ack_I2C(0);
        A=RcvByte();
        Ack_I2C(1);
        Stop_I2C();
        }
         P3=A;
         delay(1000);
}

是否缺子地址的说?~

举报

回复

3 个评论

ad***  管理员  发表于 2013-5-29 08:29:56  | 显示全部楼层
把你的IIC子程序贴出来
永登***  新手上路  发表于 2013-5-29 21:46:46  | 显示全部楼层
admin 发表于 2013-5-29 08:29
把你的IIC子程序贴出来

多谢大虾的关注!
就列出启动、停止、发送、接收、应答及延时的子程序吧!



/*************************此部分为I2C总线的驱动程序*************************************/

#include<reg51.h>
#include <intrins.h>

#define  _Nop()  _nop_()   

sbit     SCL=P2^1;     
sbit     SDA=P2^0;      

void Start_I2C()
{
  SDA=1;        
  _Nop();
  SCL=1;
  _Nop();        
  _Nop();
  _Nop();
  _Nop();
  _Nop();   
  SDA=0;        
  _Nop();        
  _Nop();
  _Nop();
  _Nop();
  _Nop();      
  SCL=0;     
  _Nop();
  _Nop();
}


void Stop_I2C()
{
  SDA=0;      
  _Nop();      
  SCL=1;      
  _Nop();
  _Nop();
  _Nop();
  _Nop();
  _Nop();
  SDA=1;     
  _Nop();
  _Nop();
  _Nop();
  _Nop();
}


void  SendByte(unsigned char  c)
{
unsigned char  Cnt;

for(Cnt=0;Cnt<8;Cnt++)
    {
     if((c<<Cnt)&0x80)SDA=1;  
       else  SDA=0;               
     _Nop();
     SCL=1;            
      _Nop();
      _Nop();            
      _Nop();
      _Nop();
      _Nop();         
     SCL=0;
    }
    _Nop();
    _Nop();
    SDA=1;               
    _Nop();
    _Nop();   
    SCL=1;
    _Nop();
    _Nop();
    _Nop();      
    SCL=0;
    _Nop();
    _Nop();
}


unsigned char RcvByte()
{
  unsigned char  result;
  unsigned char  Cnt;
  
  result=0;
  SDA=1;                       
  for(Cnt=0;Cnt<8;Cnt++)
      {
        _Nop();           
        SCL=0;                  
        _Nop();
        _Nop();                 
        _Nop();
        _Nop();
        _Nop();
        SCL=1;                  
        _Nop();
        _Nop();
        result=(result<<1)|SDA;  
        _Nop();
        _Nop();
      }
  SCL=0;   
  _Nop();
  _Nop();
  return result;
}


void Ack_I2C(bit a)
{
  
  if(a==0)SDA=0;            
  else SDA=1;
  _Nop();
  _Nop();
  _Nop();      
  SCL=1;
  _Nop();
  _Nop();                    
  _Nop();
  _Nop();
  _Nop();  
  SCL=0;                     
  _Nop();
  _Nop();   
}

void delay1ms(unsigned int x)
{
  unsigned int i,j;
  for(i=0;i<x;i++)
  {
    for(j=0;j<=120;j++)
    {;}
  }
}



个人估摸着,可能在数据发送和接收时,命令字节不对,还有,没有发送内部子地址的说?~

永登***  新手上路  发表于 2013-5-29 21:51:54  | 显示全部楼层
小僧失误了,01010101明显0x55的说~~
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

打开支付宝扫一扫,最高立得1212元红包
搜索

图文热点

更多

社区学堂

更多

客服中心

QQ:187196467 服务时间:周一至周日 8:30-20:30

关注我们

关于我们
关于我们
友情链接
联系我们
帮助中心
网友中心
购买须知
支付方式
服务支持
资源下载
售后服务
定制流程
关注我们
官方微博
官方空间
官方微信
快速回复 返回顶部 返回列表