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

📄 i2c.lst

📁 I2C实用源代码。KEIL C51编译器编写。
💻 LST
📖 第 1 页 / 共 5 页
字号:
 723   3      /*							   		      */
 724   3      /******************************************************************************/
 725   3      
 726   3      	      		    case 50: /* write all zeroes or ones pattern to the RAM */	
 727   3      		       		     RAM_Data_In = _getkey();
 728   3      				     RTC_Reg_Size = _getkey();
 729   3      				     RAM_Density = RAM_Size - RTC_Reg_Size;
 730   3      		       		     I2CStart();                   		 
 731   3      
 732   3      		       		     if (I2CMasterWrite(Dev_Add))
 733   3      	   	   	 		I2CStop();    
 734   3      
 735   3      		       		     if (I2CMasterWrite(Word_Add))
 736   3      	   	   	 		I2CStop();
 737   3      
 738   3                             		     for (j=0; j<RAM_Density; j++)
 739   3      		       		     {
 740   4              		  		if (I2CMasterWrite(RAM_Data_In))
 741   4      	   	   	     		I2CStop();
 742   4                             		     }
 743   3      				     I2CStop();
 744   3                             		     break;
 745   3      
 746   3      
 747   3      	      		    case 51: /* write data to certain location of the RAM */
 748   3      				     RAM_Add = _getkey();
 749   3      		       		     Number_Bytes = _getkey();
 750   3      		       		     RAM_Data_In = _getkey();
 751   3      		       		     I2CStart();                   		 
 752   3      
 753   3      		       		     if (I2CMasterWrite(Dev_Add))
 754   3      	   	   	  		I2CStop();    
 755   3      
 756   3      		       		     if (I2CMasterWrite(RAM_Add))
 757   3      	   	   	  		I2CStop();
 758   3      
 759   3                             		     for (j=0; j<Number_Bytes+1; j++)
 760   3      		       		     {
 761   4              		  		if (I2CMasterWrite(RAM_Data_In))
 762   4      	   	   	     		   I2CStop();
 763   4                             		     }
 764   3      				     I2CStop();
 765   3                             		     break;
 766   3      
 767   3      
 768   3      
 769   3      /******************************************************************************/
 770   3      /*							 		      */
 771   3      /*  	case 55 - Read all zeroes and all ones from the RAM. 		      */
 772   3      /*	case 56 - Read entire data from the RAM.			      */
 773   3      /*							   		      */
 774   3      /******************************************************************************/
 775   3      
 776   3                    		    case 55: /* read all zeroes or all ones from the RAM */
 777   3      			 	     RAM_Data_In = _getkey();
 778   3      				     RTC_Reg_Size = _getkey();
 779   3      				     RAM_Density = RAM_Size - RTC_Reg_Size;
 780   3      		       		     Error_Flag = 0x00;
 781   3      				     I2CStart();           	 		
 782   3      
 783   3       		       		     if (I2CMasterWrite(Dev_Add))
 784   3      	   	   	  		I2CStop(); 
 785   3      
C51 COMPILER V5.50,  I2C                                                                   02/12/03  05:12:36  PAGE 13  

 786   3      		       		     if (I2CMasterWrite(Word_Add))
 787   3      	      	   	  		I2CStop();
 788   3              
 789   3      		       		     I2CStart();                     	
 790   3      		 		     if (I2CMasterWrite(Dev_Add+1))  	                                   
 791   3      	   	   	  		I2CStop();
 792   3              
 793   3      		       		     for (i=0; i < RAM_Density-1 ; i++)
 794   3      		       		     {
 795   4      		    	   		RAM_Data_Out = I2CMasterRead(0);
 796   4      					if (RAM_Data_Out != RAM_Data_In)
 797   4      		       	   		{
 798   5      			      		    Error_Flag = 0x01;
 799   5      		    	   		}
 800   4      				     }
 801   3      	             
 802   3      		       		     RAM_Data_Out = I2CMasterRead(1);
 803   3      		       		     if (RAM_Data_Out != RAM_Data_In) 
 804   3      		       		     {
 805   4      		   	  		Error_Flag = 0x01;
 806   4      		       		     }
 807   3      
 808   3      		       		     if (Error_Flag == 0x01)
 809   3      	    	          		putchar (Bad_Device);
 810   3      	               		     else
 811   3      	    	          		putchar (Good_Device);
 812   3      
 813   3      		       		     I2CStop();
 814   3              	       		     break;
 815   3      
 816   3      
 817   3                    		    case 56: /* read entire data from the RAM */
 818   3      				     I2CStart();           	 		
 819   3      
 820   3       		       		     if (I2CMasterWrite(Dev_Add))
 821   3      	   	   	  		I2CStop(); 
 822   3      
 823   3      		       		     if (I2CMasterWrite(0))
 824   3      	      	   	  		I2CStop();
 825   3              
 826   3      		       		     I2CStart();                     	                                        	
 827   3       		       		     if (I2CMasterWrite(Dev_Add+1))  	                                   
 828   3      	   	   	  		I2CStop();
 829   3              
 830   3      		       		     for (i=0; i<RAM_Size-1; i++)
 831   3      		       		     {
 832   4      		    	   		RAM_Data_Out = I2CMasterRead(0);
 833   4      		    	   		putchar(RAM_Data_Out);
 834   4      		       		     }
 835   3      	             
 836   3      		       		     RAM_Data_Out = I2CMasterRead(1);
 837   3      		       		     putchar(RAM_Data_Out);
 838   3      		       		     I2CStop();
 839   3              	       		     break;
 840   3      
 841   3         	      		    default:
 842   3      		       		     break;
 843   3      	   		}        /* End of RTC Reg. Selection */
 844   2                         }             /* End of I2C Mode */
 845   1      
 846   1      }                                /* End of Main Program */
 847          
 848          
 849          /******************************************************************************/
 850          /*      Subroutine:	Delay	  					      */
 851          /*                                                                            */
C51 COMPILER V5.50,  I2C                                                                   02/12/03  05:12:36  PAGE 14  

 852          /*      Description:    This routine creates a time delay. It will	      */
 853          /*			loop until the 'count' becomes zero. 		      */     															
 854          /*                                                                            */
 855          /*      Input:	        None				   		      */
 856          /*                                                                            */
 857          /*      Return:		None						      */
 858          /*                                                                            */
 859          /******************************************************************************/
 860          
 861          
 862          void Delay(unsigned int count)
 863          {
 864   1      	while (count--);
 865   1      }
 866          
 867          
 868          void Board_Init()
 869          {
 870   1      	PCON = 0x00 ; 
 871   1      	SCON = 0x52 ;
 872   1      	TMOD = 0x20 ;
 873   1      	TCON = 0xC0 ;
 874   1      	TH1 =  0xFD ;
 875   1      
 876   1      	P1 = 0xFF;
 877   1      	P4 = 0xFF;
 878   1      }
 879          
 880          
 881          /******************************************************************************/
 882          /*	Subroutine:	I2C_Init 				    	      */
 883          /*			                             			      */
 884          /*	Description:	Initialize the I2C bus 			              */	
 885          /*                                                                            */
 886          /*      Input:	    	None						      */
 887          /*                       	                                              */
 888          /******************************************************************************/
 889          
 890          void I2C_Init()
 891          {
 892   1      	PCON = 0x00 ; 
 893   1      	SCON = 0x52 ;
 894   1      	TMOD = 0x20 ;
 895   1      	TCON = 0xC0 ;
 896   1      	TH1 =  0xFD ;
 897   1      
 898   1      	P1 = 0xFF;
 899   1      	P4 = 0xEF;
 900   1      	P5 = 0xFF;
 901   1      }
 902          		
 903          
 904          
 905          /******************************************************************************/
 906          /*	Subroutine:	I2CStart     					      */
 907          /*			                              			      */
 908          /*	Description:	Generate a START condition on I2C bus		      */	
 909          /*                                                                            */
 910          /*      Input:	    	None						      */
 911          /*                                                                            */
 912          /*      Return:		None						      */
 913          /*                                                                            */
 914          /******************************************************************************/
 915          
 916          void I2CStart()
 917          {
C51 COMPILER V5.50,  I2C                                                                   02/12/03  05:12:36  PAGE 15  

 918   1      	SDA = 1;        /* to make sure the SDA and SCL are both high */
 919   1      	SCL = 1;
 920   1      	Delay(5);       /* add delay */
 921   1      
 922   1      	SDA = 0;        /* SDA line go LOW first */
 923   1      	Delay(10);
 924   1      	SCL = 0;        /* then followed by SCL line with time delay */
 925   1      }
 926          
 927          
 928          
 929          /******************************************************************************/
 930          /*	Subroutine:	I2CMasterWrite			 		      */
 931          /*			                                 		      */
 932          /*	Description:	Output one byte of data to slave device. Check for    */
 933          /* 			WAIT condition before every bit is sent.	      */ 
 934          /*                                                                            */
 935          /*      Input:	    	one byte of data to be sent to slave device.	      */
 936          /*                                                                            */
 937          /*      Return:		acknowledgement from slave:         		      */
 938          /*		        0 = acknowledge is received			      */
 939          /*	       	 	1 = no acknowledge is received			      */                       
 940          /*                                                                    	      */
 941          /******************************************************************************/
 942          
 943          unsigned char I2CMasterWrite(unsigned char input_byte)
 944          {
 945   1      	unsigned char i;
 946   1      	unsigned int mask;
 947   1      
 948   1              mask = 0x80;                      
 949   1      	for (i=0; i<8; i++)		/* send one byte of data */
 950   1      	{
 951   2         	   if (mask & input_byte) 	/* send bit according to data */
 952   2      	      SDA = 1;
 953   2      	   else SDA = 0;
 954   2      	
 955   2      	   mask = mask >> 1;		/* shift right for the next bit */
 956   2      	   Delay(0);
 957   2      	   SCL = 1;			/* clock is high */
 958   2                 Delay (1);
 959   2      	   SCL = 0;                     /* clock is low */
 960   2              }
 961   1      
 962   1            	mask = SDA;			/* read acknowledge */
 963   1            	Delay(3);
 964   1              SCL = 1;			/* generate 9th clock pulse */
 965   1      	Delay(1);
 966   1      	SCL = 0;                	/* clock is low */	
 967   1      	Delay(6);			/* to avoid short pulse transition on SDA line */	

⌨️ 快捷键说明

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