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

📄 exi2c.c

📁 本公司的单相CPU卡表的源程序代码
💻 C
字号:
//--------------------------------------------------------------------
/*******************************************************************
* 
********************************************************************/
#include "include\macro.h"  
#include "include\HardConfig.h"
#include "include\SysConfig.h"
#include "include\SubConfig.h"
#include "include\data.h"
#include "include\bitdef.h"

#include "include\FunAnnounce.h"
/*******************************************************************
* ExI2c.c    :
********************************************************************/
#define SDA       pin_I2CSDA
#define SCL       pin_I2CSCL
#define DirSDA    dir_I2CSDA 
#define DirSCL    dir_I2CSCL    

#define E2P_PAGE  16                 //max page write is 16 byte.

//************************************************************//
//
//***********************************************************//
void Delay4us(void)
{
    NOP();
    NOP();
    NOP();  
    NOP();
    NOP();
    NOP();
    NOP();  
    NOP();
    NOP();
    NOP();
    NOP();  
    NOP();
    NOP();
    NOP();
    NOP();  	
}


//*****************************************************************
//  Function:       Start_i2c(void)                                                    
//  Others:           
//***************************************************************
void Start_i2c(void)
{   
    //		
    DirSCL = 0;	
    DirSDA = 1; 
    NOP();
    SCL = 1;
    Delay4us();    
    SCL = 0; 
    DirSDA = 0;
    //
       
    SDA = 1;
    while(SDA==0)
    {;}
    Delay4us();
    SCL = 1;
    while(SCL==0)
    {;}  
    NOP();
    SDA = 0;
    Delay4us();
    SCL = 0;  
}

//*****************************************************************
//  Function:       Stop_i2c(void)             
//  Description:                                            
//  Others:           
//***************************************************************
void Stop_i2c(void)
{
    SDA = 0;
    NOP();
    SCL = 1;
    while(SCL==0)
    {;}
    NOP();
    SDA = 1;
    Delay4us();
    //SCL = 0;    
}

//*****************************************************************
//  Function:       SendAck(void)            
//  Description:  Master Send Ack  To Slaver                                        
//  Others:           
//***************************************************************
void SendAck(void)         
{
    SDA = 0;
    NOP();    
    SCL = 1;
    Delay4us();
    SCL = 0;
    SDA = 1;    
}

//*****************************************************************
//  Function:       SendNotAck(void)             
//  Description:    Master Send NoAck  To Slaver                                           
//  Others:           
//***************************************************************
/*
void SendNotAck(void)      
{
    SDA = 1;
    NOP();
    SCL = 1;
    Delay4us();
    SCL = 0;
    SDA = 0;    
}
*/

//*****************************************************************
//  Function:       void TxByte_i2c(char data00)           
//  Description:                                           
//  Others:           
//***************************************************************
unsigned char TxByte_i2c(uchar data00)
{
 register uchar i=0;
 register uchar ack = 0xa5;
 register uchar tmp;
 
   tmp = data00;
   for (i=0;i<8;i++)
   {
       if( tmp & 0x80 )
       {
           SDA = 1;
       }    
       else
       {
           SDA = 0; 
       }
       tmp <<= 1;
       SCL = 1;
       Delay4us();      
       SCL = 0;             //very important
   }
                      
   //SDA = 1;
   //NOP();  
   // Waitting ACK singel
   DirSDA = 1; 
   NOP();
   SCL = 1;
   Delay4us();    
   if(SDA)
   {
   	ack = 0x5a;
   }
   SCL = 0; 
   DirSDA = 0;
   return (ack); 
}

//*****************************************************************
//  Function:       char RxByte_i2c(char data00)          
//  Description:                                            
//  Others:           
//***************************************************************
uchar RxByte_i2c(void)
{
  register uchar i=0;
  register uchar data00=0;
  
   DirSDA = 1;
   SCL = 0; 
   NOP();
   for (i=0;i<8;i++)
   {
       data00 <<= 1;
       SCL = 1;
       NOP();
       NOP();
       if(SDA==1)
       {
           data00 |= 0x01; 	
       }
       else
       {
           data00 &= (~0x01);	
       }
       SCL = 0;
       Delay4us(); 
   }
   DirSDA = 0;
   return(data00);                       //
}



/*************************************************
  Function:       unsigned char RxNByte_i2c(int Dest, char *Src, char len)                   
  Description:    recived N byte data00 from eeprom device                    
  Calls:                      
  Called By:      main()                                                        
  Input:                                           
  Output:         no                             
  Return:         no                              
  Others:                  
*************************************************/
void RxNByte_i2c(uint Dest, uchar *Src, uchar len)
{
  register uchar buff;
  register uchar i;
  register uchar tmp_d0;
  
LP1:
   //buff = (*((char*)(&Dest+1)));   
   buff =  (Dest>>8);                
   buff &= ~0x01;
   Start_i2c();
   if(TxByte_i2c(buff)==0x5a) goto  LP1; 
   
   buff = Dest;                                           //get  AT24C16 data00 Low address
   if(TxByte_i2c(buff)==0x5a) goto  LP1; 
   
   //buff = (*((char*)(&Dest+1)));   
   buff =  (Dest>>8);                      
   buff |= 0x01;
   Start_i2c();
   if(TxByte_i2c(buff)==0x5a) goto  LP1; 
   
   for(i=0;i<len;i++)
   {
       *Src = RxByte_i2c();
       if(i<(len-1))   SendAck();
       Src++;
   }
   
   // SendNotAck(); 
    SDA = 1;
    NOP();
    SCL = 1;
    Delay4us();
    SCL = 0;
    SDA = 0;  
    
    Stop_i2c();
}

/*************************************************
  Function:       unsigned char TxNByte_i2c(int Dest, char *Src, char len)                   
  Description:    Write  N byte data00 into eeprom device                    
  Calls:                      
  Called By:      main()                                                        
  Input:                                           
  Output:         no                             
  Return:         no                              
  Others:                  
*************************************************/
unsigned char TxNByte_i2c(uint Dest, uchar *Src, uchar len)
{
  register uchar buff;
  register uchar i;
  register uchar cnt=0;
     
LP1:
   cnt++;
   if(cnt>=07)    return(0);                                        //
   //buff = (*((char*)(&Dest+1)));   
   buff =  (Dest>>8);                     
   buff &= ~0x01;
   Start_i2c();
   if(TxByte_i2c(buff)==0x5a) goto  LP1;  
   
   buff = Dest;                                           //get  AT24C16 data00 Low address
   if(TxByte_i2c(buff)==0x5a) goto  LP1; 
   
   for(i=0;i<len;i++)
   {
       if(TxByte_i2c(*Src)==0x5a) goto  LP1; 
       Src++;
   }
   Stop_i2c();
   Delay_ms(8);                                                     //delay 10ms,
   return(1);
}

/*************************************************
  Function:       unsigned char RdEeprom(int Dest, char *Src, char len)                   
  Description:    Write  N byte data00 into eeprom device                    
  Calls:                      
  Called By:      main()                                                        
  Input:                                           
  Output:         no                             
  Return:         no                              
  Others:                  
*************************************************/
unsigned char RdEeprom(uint Dest, uchar *Src, uchar len)
{
 uchar data00[16];
 register uchar i;
  
   if((len<= 0)||(len>E2P_PAGE))                                    //if len is illegal,program run with dead circle
   {
       Sys_err();
   }
   for(i=0;i<6;i++)                                                 //read and compare, if error,repeat 6 times.
   {
       RxNByte_i2c(Dest, Src, len);
       RxNByte_i2c(Dest, &data00[0], len); 
       if(Cmp_TwoArry(Src,data00,len)==0)  return (true);  
   }
   return(false);
}

/*************************************************
  Function:       unsigned char RdEeprom(int Dest, char *Src, char len)                   
  Description:    Write  N byte data00 into eeprom device                    
  Calls:                      
  Called By:      main()                                                        
  Input:                                           
  Output:         no                             
  Return:         no                              
  Others:                  
*************************************************/
unsigned char WtEeprom(uint Dest, uchar *Src, uchar len)
{
 uchar data00[16];
 register uchar i;
  
   if((len<= 0)||(len>E2P_PAGE))                                    //if len is illegal,program run with dead circle
   {
       Sys_err();
   }
   for(i=0;i<3;i++)                                                 //write and read compare, if error,repeat 3 times.
   {
       TxNByte_i2c(Dest, Src, len);
       RxNByte_i2c(Dest, &data00[0], len); 
       if(Cmp_TwoArry(Src,data00,len)== 0 )  return (true);  
   }
   return(false);
}

/*************************************************
  Function:       unsigned char RdEeprom(int Dest, char *Src, char len)                   
  Description:    Write  N byte data00 into eeprom device                    
  Calls:                      
  Called By:      main()                                                        
  Input:                                           
  Output:         no                             
  Return:         no                              
  Others:                  
*************************************************/
void WtEEpage(uint Dest, uchar *Src, uchar len)
{
 register uchar tmp;
 register uchar lenth;
  
   while( len > 0 )
   {
       tmp = Dest & (E2P_PAGE -1);
       if( tmp != 0 ) 
       {
       	   lenth = E2P_PAGE - tmp;
       }
       else 
       {
       	   lenth = E2P_PAGE;
       }	
       if( len < lenth ) lenth = len;	
       if(WtEeprom(Dest, Src, lenth)==0)  Sys_err();
       Dest += lenth;
       Src += lenth;
       len -= lenth;
   } 
}

⌨️ 快捷键说明

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