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

📄 iic读写.c

📁 VB环境下的串口通讯设计
💻 C
字号:
/************************************************/
#include <REG52.H>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit I2C_SDA= P1^5;
sbit I2C_SCK= P1^4;
sbit I2C_WP= P1^3;
sbit GRE=P1^7;
uchar tab[4][16]={
				{0x40,0x00,0x27,0xF0,0x24,0x10,0x07,0xF0,0x94,0x10,0x54,0x10,0x17,0xF0,0x20,0x00},
				{0x2F,0xFC,0xC9,0x24,0x49,0x24,0x49,0x24,0x49,0x24,0x49,0x24,0x5F,0xFE,0x40,0x00},
				{0x00,0x00,0x12,0x10,0x12,0x10,0x12,0x10,0x12,0x10,0xFF,0xFE,0x12,0x10,0x12,0x10},
				{0x12,0x10,0x13,0xF0,0x12,0x10,0x10,0x00,0x10,0x08,0x1F,0xFC,0x00,0x00,0x00,0x00}
				};
uchar tab2[2][16]={
				{0x00,0x00,0x12,0x10,0x12,0x10,0x12,0x10,0x12,0x10,0xFF,0xFE,0x12,0x10,0x12,0x10},
				{0x12,0x10,0x13,0xF0,0x12,0x10,0x10,0x00,0x10,0x08,0x1F,0xFC,0x00,0x00,0x00,0x00}
			   };
//===============================================

//=============================
//延时10US
//内部调用函数
void  Delay_10_uS(void)
{
     char i=10;
     while(i--);
}

//延时n_milisecond MS
//供内部调用函数
void delay_n_ms( uint n_milisecond)  /* n mS delay */
{
     uchar i;
     while(n_milisecond--)
     {
          i=37;
          while(i--);
     }
}

//开始位
//内部调用函数
bit I2C_Start(void)
{
     Delay_10_uS();
     I2C_SDA =1;
     Delay_10_uS();
     I2C_SCK =1;
     Delay_10_uS();
     if ( I2C_SDA == 0) return 0;
     if ( I2C_SCK == 0) return 0;
     I2C_SDA = 0;
     Delay_10_uS();
     I2C_SCK = 0;
     Delay_10_uS();
     return 1;
}

//停止位
//内部调用函数
void  I2C_Stop(void)
{
     Delay_10_uS();
     I2C_SDA = 0;
     Delay_10_uS();
     I2C_SCK = 1;
     Delay_10_uS();
     I2C_SDA = 1;
     Delay_10_uS();
}


//写单字节数据
//内部调用函数
bit I2C_Send_Byte( uchar d)
{
     uchar i = 8;
     bit bit_ack;
     while( i-- )
     {
          Delay_10_uS();
          if ( d &0x80 )   I2C_SDA =1;
          else             I2C_SDA =0;
          Delay_10_uS();
          I2C_SCK = 1;
          Delay_10_uS();
          I2C_SCK = 0;
          d = d << 1;
     }
     Delay_10_uS();
     I2C_SDA = 1;
     Delay_10_uS();
     I2C_SCK = 1;
     Delay_10_uS();
     bit_ack = I2C_SDA;
     I2C_SCK =0;
     Delay_10_uS();
     return bit_ack;
}



//写多字节数据
//供外部调用函数
void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count)
{

     while(count--)
     {
          I2C_Start();
          /*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/  /* 24C16  USE */
          I2C_Send_Byte( 0xa0 );//写命令设备地址为10100000最后一位为0"写标志"
          I2C_Send_Byte(  AT24C64_address/256 );//写存储器页地址
          I2C_Send_Byte( AT24C64_address %256 );//写存储器字节地址
          I2C_Send_Byte( *(uchar*)mcu_address );//写MCU地址指针指数据到存储单元
          I2C_Stop();
          delay_n_ms(10);       /* waiting for write cycle to be completed */
          ((uchar*)mcu_address)++;
          AT24C64_address++;
     }
}

//读多字节数据
//供外部调用函数

//=====================================
void main()
{
	//uchar i,j;
	P1=0;
	P2=0;
	P3=0;
	GRE=0;
	AT24C64_W(tab,0x00,64);
	GRE=1;
}

⌨️ 快捷键说明

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