i2c.h

来自「基于单片机的自行车计时」· C头文件 代码 · 共 69 行

H
69
字号
#include <REGX52.H>
sbit sda=P2^4;
sbit scl=P2^3;
void sta(){
     sda=1;
	 scl=1;
	 _nop_();_nop_();_nop_();_nop_();
	 sda=0;
	 _nop_();_nop_();_nop_();_nop_();
}
void stop(){
     sda=0;
	 scl=1;
	 _nop_();_nop_();_nop_();_nop_();
	 sda=1;
	 _nop_();_nop_();_nop_();_nop_();
}
void ack(){
     sda=0;
	 _nop_();_nop_();_nop_();_nop_();
	 scl=1;
	 _nop_();_nop_();_nop_();_nop_();
	 scl=0;
 }
 void no_ack(){
      sda=1;
	  _nop_();_nop_();_nop_();_nop_();
	  scl=1;
	  _nop_();_nop_();_nop_();_nop_();
	  scl=0;
}
void wri_one_char(unsigned char ch){
     	unsigned char tempchar;
	    unsigned char bitshift;
    	for(bitshift=0;bitshift<8;bitshift++)
	   {
	    scl=0;
		tempchar=ch&0x80;
		if(tempchar==0){sda=0;}
		else sda=1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		scl=1;
		_nop_();
		_nop_();
		scl=0;
		sda=0;
		_nop_();
		_nop_();
		ch=ch<<1;
		}
}

unsigned char read(){
         unsigned char bitcounter=8,temp=0;
		 sda=1;
         while(bitcounter){
              scl=0; 	 
              _nop_();_nop_();_nop_();_nop_();
              scl=1;
			  _nop_();_nop_();_nop_();_nop_();
              if(sda) temp=temp*2+0x01;
			  else    temp=temp*2+0x00;
			  bitcounter--;
			  }
             return temp;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?