繁体中文
设为首页
加入收藏

推荐文章

更多

07-14·[可控硅] 可控硅及其整流
07-14·[可控硅] 集成化六脉冲触发组件KCZ6电原理图
07-27·[单片机技术] PIC单片机16F84的内部硬件资源

 

最新文章

更多

· 高精度模数转换芯片CS11...
· 最新10位数字温度传感器...
· AT45D081/AT45DB021/AT4...
· X24C44一款老的EEPROM,(...
· 93C46/93c06/93c46/93c5...
· 128x64液晶KS0108控制器...
· 本站液晶程序里使用的ab...
· DS1302驱动程序(C51)
· 学习GCC Complete Refer...
· 农历转换函数(C语言版)
当前位置:技术文章首页 >> 单片机技术 >> 单片机源码 >> i2c虚拟总线通用软件包

i2c虚拟总线通用软件包

1970-01-01 08:00:00  作者:本站  来源:本站整理  浏览次数:284  文字大小:【】【】【

i2c虚拟总线通用软件包

// ****************************************************************//
//------------- 虚拟 i2c软件包头文件  iic.h  -----------------------//
//****************************************************************//

sbit SDA=P1^7;                        //定义IIC数据线//
sbit SCL=P1^6;                       //定义IIC时钟线//
#define uchar unsigned char
#define uint unsigned int
extern void delay(uint x);          //延时子程序//
extern void sta();           //启动IIC总线//
extern void stop();          //停止IIC总线//
extern void mack();          //发送应答位//
extern void mnack();          //发送非应答位//
extern void cack();        //应答位检查//
extern void wrbyt(uchar shu);        //发送 1 个字节//
extern viod  wrbyt0();                  
extern void wrbyt1();
extern uchar rdbyt();                    //读取 1 个字节//
extern void wrnbyt(uchar slaw,uchar number,uchar ff[]);   //发送N个字节//
extern void rdnbyt(uchar slar,uchar number,uchar qq[]);   //读N个字节//



//************************************************************//
//---------   虚拟i2c软件包,12MHz晶振  ----------------------//
//***********************************************************//
#pragma db cd
#include <intrins.h>
#include <reg51.h>
#include<iic.h>



void sta()        //启动iic总线//
{
SDA=1;
SCL=1;
while(SCL==0)
{;}
_nop_();
_nop_();
SDA=0;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}


void stop()
{
SDA=0;
SCL=1;
while(SCL==0)
{;}
_nop_();
_nop_();
SDA=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}


void mack()
{
SDA=0;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
SDA=1;
}


void mnack()
{
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
SDA=0;
}


void cack()
{
SDA=1;
SCL=1;
F0=0;
if(SDA==0)
   {
        SCL=0;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
    }
 else
    {
         F0=1;
         SCL=0;
         _nop_();
         _nop_();
         _nop_();
         _nop_();
     }
}




void wrbyt(uchar shu)
{
uchar i;
for(i=0;i<8;i++)
   {
      if ((shu&0x80)>0)
         { wrbyt1(); }
      else
         { wrbyt0();}
      shu<<=1;
    }
}



void wrbyt0()
{
SDA=0;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}



void wrbyt1()
{
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
SDA=0;
}



uchar rdbyt()
{
uchar nn=0xff;
uchar j;
for (j=0;j<8;j++)
    {
         SDA=1;
         SCL=1;
         if(SDA==0)
            {
                    nn<<=1;
                nn=(nn&0xfe);                 
                SCL=0;
             }
          else
             {
                    nn<<=1;
                nn=(nn|0x01);                 
                SCL=0;
             }
     }
     return(nn);
}







//********************写N个字节子程序************************//
//slaw--写寻址字节, number--字节数, ff[]--N个字节数组//

void wrnbyt(uchar slaw,uchar number,uchar ff[])
{
  uchar idata k;
writ: do
   {
         sta();
         wrbyt(slaw);
         cack();
    }while(F0==1);
for(k=0;k<number;k++)
    {  
         wrbyt(ff[k]);
         cack();
         if(F0==1)
             goto writ;
      }
 stop();
}



//***************读N个字节子程序*********************//
//number--字节数,slar--读寻址字节,qq--贮存单元数组//

void rdnbyt(slar,number,qq)
uchar slar,number,qq[];
{
uchar idata data0,l;
do
    {
       sta();
       wrbyt(slar);
       cack();
     }while(F0==1);
    
  for(l=0;l<number;l++)
     {
        data0=rdbyt();
        qq[l]=data0;
           if(l<(number-1))
              { mack(); }
      }
   mnack();
   stop();
}

责任编辑:

启蒙电子http://www.51c51.net 启蒙电子网http://www.atc51.com 启蒙电子论坛
相关文章