⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 24lc04q.c.bak

📁 这是一个PIC单片机的实时操作系统
💻 BAK
字号:
//#include <pic.h>
//#include <pic1687x.h>
//#include "DJ485headB.h"
#include "config.H"
//unsigned char write[5];/*待写入数缓存*/
unsigned char  WriteDate;
bit   err_flag;/*iic工作是否正常标志(为1不正常;为0正常)*/
bit   err_shao ;//烧写错误
void EEBankSet(uchar buf);
void dlay(void);
void tx(unsigned char k)/*向iic里写数据*/; 
void start(void)/*启动iic*/;
void stop(void)/*停止iic*/;
unsigned char err_count=50;
uint8 temp,startad; 
//P3的程序在原来的基础上对写数据,等待数据写完的格式做了改变,不再采用延时,而采用多次读写判断的方式来加
//加快速度2007-4-17 20:19
//p4  把读数据的程序做了修正,可以读正常的数据,但是联控在线读有点问题2007-4-22 21:33

//----------------------24lc04
/*************************************************
start_address 需操作iic的首地址
count         需读写个数
**************************************************/
void dlay(void)
{
 //#asm
 nop;
 nop;
 nop;
 nop;
 //#endasm
}
/*************************************************/
void tx(unsigned char k)/*向iic里写数据*/
{
unsigned char temp=8;
err_flag=1;//无应答超时
 while(temp--)
 {
 	//SCL=0;
 // SDA=0;
  SCL_IO=0; //
  SDA_IO=0;
  dlay( );
  if((k&0b10000000))
  {
   SDA=1;
 // SDA_IO=1;
  }
  else
  {
   SDA=0;
  // SDA_IO=0;
  }
  SCL=1;
 dlay();
  SCL=0;

  k<<=1;
 }//End while
 

 SDA_IO=1;
 SDA=1;
 dlay();
 SCL=1;
 dlay();
 //SCL_IO=0;
 
 if(SDA==0)
 	err_flag=0;
 dlay();
 SCL=0;
// SCL_IO=0;
 
 
}
/*************************************************/
void start(void)/*启动iic*/
{
SCL_IO=0;
SDA_IO=0;
SDA=1;
dlay();
SCL=1;
dlay();
SDA=0;
dlay();
SCL=0;
//dlay();

 return;
}
/*************************************************/
void stop(void)/*停止iic*/
{

SCL_IO=0;
SDA_IO=0;
SDA=0; //
dlay();
SCL=1;
dlay();
SDA=1;
dlay();
//SCL=0;
//dlay();
 return;
}
/*************************************************/
unsigned char rx(void)/*从iic里读出数据*/
{
unsigned char temp=8,buf;
 SDA_IO=1;
 SCL_IO=0;
 SDA=1;
// dlay( );
 nop;
 while(temp--)
 {
  SCL=1;
  dlay();
 //  SCL=0;
 // SCL_IO=1;
  buf=(buf<<1)|SDA;
  // dlay();
  SCL=0;
//  dlay();
  nop;
  nop;
  nop;
 // SCL_IO=0;
 }
 return(buf);
}
/*************************************************/
void iic_write(uint16 Start_Buf )
{
	//unsigned char temp=0,
 // err_flag=1;
 uint16 Start_Address,Start_Bank,START_HAdrr;
 //EEBankSet(start_address );
 if (EEType==EE8)
 	{
	if(Start_Buf>255)
		{
		 Start_Address=Start_Buf%256;  //取余的数是表示数据是数据的低位地址
			Start_Bank=Start_Buf>>8;   //取出数据的高位地址和数据头相并
			Start_Bank=Start_Bank<<1;
			//数据要再与1010进行相与
			Start_Bank=Start_Bank|0xa0;
			nop;
	  }
	  else
	  	{
	   Start_Address=Start_Buf;
	   Start_Bank=0xa0;		
	   }
   
 }  
else  
	{
	  Start_Address=Start_Buf%256;  //取余的数是表示数据是数据的低位地址
	  START_HAdrr=Start_Buf>>8;
  	Start_Bank=0xa0;   //取出数据的高位地址和数据头相并	
		}  
   

start( );
// tx(0xa2);
tx(Start_Bank);
tx(Start_Address);
// while(1)
// {
 if(EEType==EE32)
 	{
tx(START_HAdrr); 		
 		}
 tx(WriteDate);
//    	temp++;
 // if (count<temp)
//       break;
 //   }  10100010
 stop( );
/* 
 while(err_count--)
  { 
 // err_shao=1;
  start();
  tx(0xa0);
  if (err_flag==0)
  	{
  	//	err_shao=0;
  		 break;
  	}    
  
 }  
 */
  }
 //257 
  //01  01 
  
/*************************************************/
uint8 iic_read(uint16 Start_Buf)
{
uint16 i;
uint16 Start_Address,Start_Bank,Start_HAddr;
 //EEBankSet(start_address );
 
 		err_flag=0;
 if (EEType==8)
	 {	
		 if(Start_Buf>255)
			 	{
					Start_Address=Start_Buf%256;  //取余的数是表示数据是数据的低位地址
					Start_Bank=Start_Buf>>8;   //取出数据的高位地址和数据头相并
					Start_Bank=Start_Bank<<1;
					//数据要再与1010进行相与
					nop;
					nop;
					Start_Bank=Start_Bank|0x01;
					Start_Bank=Start_Bank|0xa0;
					nop;
			  }
			//         0101   111 (1)     1 (1)            111011011
		 else 
			 	{
			    Start_Address=Start_Buf;
			    Start_Bank=0xa1;	
			 		
			 		}
				nop;
				nop;
				start( );
				tx(0xa0);
				//  tx(Start_Bank);
				nop;
				tx(Start_Address);
				//tx(start_address);
				start( );
				//tx(0xa3);
				tx(Start_Bank);
		} //end EESyte=08
 else
 	{
 		  Start_Bank=0xa1;
 		  Start_Address=Start_Buf%256;  //取余的数是表示数据是数据的低位地址
	 	 	Start_HAddr=Start_Buf>>8;   //取出数据的高位地址和数据头相并	
 	    	start( );
				tx(0xa0);
				//  tx(Start_Bank);
				nop;
				tx(Start_HAddr);//发关高位地址
				//tx(start_address);
				tx(Start_Address);
				start( );
				//tx(0xa3);
				tx(Start_Bank);
 		}
  i=rx();
   //  while(count--)
    // {
   //             read[count]=rx();
     //  delay();  这个要再验证一下
      dlay();
       dlay();
   return(i);
//}
}
void EEBankSet(uint8 buf)
{	 
//	uint16  temp,startad; 
	uint8   start;
	startad=buf%256;  //取余的数是表示数据是数据的低位地址
	temp=buf>>8;   //取出数据的高位地址和数据头相并
	temp=temp<<1;
	//数据要再与1010进行相与
	temp=temp|0xae;
	nop;
	
	}

⌨️ 快捷键说明

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