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

📄 main.c

📁 this file is for keil keyboard
💻 C
字号:
#include "test.h"
#define          GAIN_W_L8             XBYTE[0XB000]
#define          GAIN_W_H4             XBYTE[0XB001]
#define          GAIN_R_L8             XBYTE[0XB002]
#define          GAIN_R_H4             XBYTE[0XB003]
#define          GAIN_UPDATE           XBYTE[0XC000]
#define          AD_START              XBYTE[0XA000]
#define          AD_H8                 XBYTE[0XA000]
#define          AD_L4                 XBYTE[0XA001]
sbit      SCL=P1^3;    
sbit      SDA=P1^4;              /*DEFINE   CLOCK LINE AND  DATA LINE  FIRST  */
int    Sample0(  )
{
	     unsigned   char xdata  IntNum    ;
		 unsigned   int  xdata  Cycle     ;
		 unsigned   int  xdata  DataW     ;
      
		 unsigned   int  xdata  DataWSum  ; 

	     
		 
		 DataWSum      =  0          ;
		 for ( Cycle = 0 ; Cycle < 30  ; Cycle++ )
		 {
			 IntNum     |= 0X80      ; 
			 AD_START    = 0x10      ;        //启动转换 
			 while( IntNum & 0X80 )           //工作通道转换结束,K开始接受数据
			 {
				 IntNum =  ADDR_INT  ; 
			 }     
			 DataW      =  AD_H8     ;
			 DataW      =  _irol_ ( DataW , 4 );
			 DataW     +=  AD_L4  /16    ;
			 DataWSum  +=  DataW / 30 ;
		 }
		 return   DataWSum   ;
 
}  
          
 void  Start()
{
  SDA=1;
  SCL=1;
  SDA=0;
  SCL=0;
}                               /*开始的前导符*/                     
void  Stop()
{
 
  
  SDA=0;
  SCL=1;
  SDA=1;
  SCL=0;
  SDA=0;
  
 
}
void Send(unsigned  char    SendData)   
{
    unsigned    char       temp;
    unsigned    char       i;
   SCL=0;
   for (i=0;i<8;i++)
   {
    temp=SendData ;
    temp=temp&0x80; 
	 
    if (temp==0x80)              /*bit 7 is  1*/
    {
        SDA=1;
    }
    else
    {
        SDA=0; 
    }
   
	SCL=1;
	SCL=0;
        	 
	SendData=SendData<<1;
   } /*  end  for  i*/
   
   SDA=1;
      
   SCL=1;
   while(SDA) {}                    /*JUDGE WHETHER ACKNOWLEDGE IS OCCUR OR  NOT*/  
   SCL=0; 
   
}                                                  /*发送数据*/
 void   Start_2464(unsigned  char       rw,        /*读或写的标志*/
	               unsigned  char       ChipID )    /*24c256的硬件的编址*/
				                                   
			 
 {     
	   unsigned   char            byte;
	   rw&=0x01;
	   byte=0xa0;
	   byte|=rw;
	   ChipID&=0x07;                               /*get  low  three  bit  */
           ChipID=ChipID<<1;
           byte|=ChipID; 
	   Start();
	     
	   Send(byte);
	    
	   
 }
 void   ByteWrite_2464(    unsigned  char     ChipID  ,
                           unsigned  int      addr    ,     /*待发送的数的首址*/
	                       unsigned  char     senddata)     /*   待发送的数   */
 {					  
	 unsigned  char       temp;
	 
     Start_2464(0x00,ChipID);
     temp= addr>>8 ;                                   /*high  address  */
	 Send(temp);
	 temp=addr&0x00ff;                                 /*low   address  */
	 Send(temp);
     Send(senddata);
	 Stop();
	 for (temp=0;temp<0xff ;temp++);           /*对于I2C总线而言延时是最重要的,*/
	 for (temp=0;temp<0xff;temp++);
     for (temp=0;temp<0xff ;temp++);           /*对于I2C总线而言延时是最重要的,*/
	 for (temp=0;temp<0xa0;temp++);
       
      
	   
 }
 char   Get_2464   ()                  /*获取一个字节的数据*/
 {
	 unsigned   char        getdata;
	 unsigned   char        cycle  ;
         getdata=0x00;
	
	 for (cycle=0;cycle<8;cycle++)      /*一共要接受8个字节的数据*/ 
	 {
		 getdata=getdata<<1;
		 SCL=1;
		 if (SDA)                       /*SDA=1*/
		 {
           getdata|=0x01;  
		 }
		 else                           /*SDA=0*/
		 {
           getdata|=0x00;
		 }
		 SCL=0;
                   
	 }                                /*end  for  cycle*/    
	 return   getdata;

	  
 }                                  
 
 void   SequentialRead_2464( unsigned  char     ChipID ,
                             unsigned  char     *Buffer, /*存放数据的首址,读取的数据全部存储到外部的数据存储器*/
                             unsigned  int      num    )    /*读取数据的字节数*/  
 {
     unsigned  int          cycle;  
     unsigned  char         senddata;
   
     
      
     Start_2464(0X01,ChipID);
     for (cycle=0;cycle<num;cycle++)
	 {
          senddata=Get_2464();
          Buffer[cycle]=senddata;                            /*送到外部的数据存储器*/
		  
		  if(cycle==num-1)                             /*the  last byte  data need not  ACK*/ 
		  {                                            
                         SDA=1;
			 SCL=1;
			 SCL=0;                    /*NOACK*/
                         Stop();
			 break;
		  }                            
		  SDA=0;                       /*ACK*/
		  SCL=1;                       /*ACK*/   
		  SCL=0;                       /*ACK*/
		  SDA=1;                       /*ACK*/
	 }                                                  /*end for  cycle*/
 }
 void   RandomRead_2464(unsigned  char          ChipID ,
                        unsigned  char          *Buffer,/*读取的数据放到外部的数据存储器waddr就是外部的数据存储器的地址*/        
                        unsigned  int           raddr,/*raddr就是数据从24C64的哪个地址开始*/
						unsigned  int           num   /*读取数据的字节数*/
						)
 {
     
     unsigned  int        cycle;  
     unsigned  char       senddata;
   
	 Start_2464(0x00,ChipID);
         senddata= raddr>>8 ;          /*high  address  */
	 Send(senddata);
	 senddata=raddr&0x00ff;        /*low   address  */
	 Send(senddata);
       { 
           Start_2464(0X01,ChipID);
           for (cycle=0;cycle<num;cycle++)
	     {
                senddata=Get_2464();
                Buffer[cycle]=senddata;                            /*送到外部的数据存储器*/
	 
		  if(cycle==num-1)                             /*the  last byte  data need not  ACK*/ 
		  {                                            
                         SDA=1;
			 SCL=1;
			 SCL=0;            /*NOACK*/
                         Stop();
			 break;
		  }                            
		  SDA=0;                       /*ACK*/
		  SCL=1;                       /*ACK*/   
		  SCL=0;                       /*ACK*/
		  SDA=1;                       /*ACK*/
	      }                                                  /*end for  cycle*/        
       }
      

 }



void writeins(unsigned char d)
{
 switch(d)
 {case 12:                /*8*16 英文*/    
     G_INSNUM=1;
	 break;
 case 15:
    G_INSNUM=2;           /* 写点   */ 
	break;
case 1:                   /*16*16汉文*/               
     G_INSNUM=3;
	 break;
 case 2:                  /*擦一个字节*/
    G_INSNUM=4;          
	break;
case 3:                   /*擦点*/
     G_INSNUM=5;
	 break;
 case 4:                  /*擦8*16英文*/           
    G_INSNUM=6;
	break;           
case 5:                   /*写一个字节*/      
     G_INSNUM=7;
	 break;
 case 6:                  /*擦8*16字符*/            
    G_INSNUM=8;
	break;
case 7:                   /*24*24汉文*/ 
     G_INSNUM=9;
	 break;
case 8:                   /*16*16英文*/
    G_INSNUM=10;
    break;
 case 9:                  /*

⌨️ 快捷键说明

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