欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

iic20080107.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;
sbit RED=P1^6;

//===============================================

//=============================
//延时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 write1()
{
	uchar code tab[96]=
	{//欢迎您	
0x0,0x80,0x40,0x0,0x8,0x0,0x0,0x80,0x21,0x80,0x9,0x0,0xfc,0x80,0x36,0x7c,0x11,0xfe,0x5,0xfe,0x24,0x44,0x12,0x4,0x85,0x4,0x4,0x44,0x34,0x40,0x4a,0x48,0x4,0x44,0x32,0x50,0x28,0x40,0xe4,0x44,0x52,0x48,0x10,0x40,0x24,0x44,0x94,0x44,0x18,0x40,0x25,0x44,0x11,0x44,0x18,0x60,0x26,0x54,0x10,0x80,0x24,0xa0,0x24,0x48,0x0,0x0,0x24,0x90,0x20,0x40,0x29,0x4,0x41,0x18,0x20,0x40,0x28,0x92,0x86,0xe,0x50,0x0,0x68,0x12,0x38,0x4,0x8f,0xfe,0x7,0xf0,0x0,0x0,0x0,0x0,0x0,0x0
	};																			
	AT24C64_W(tab,0x0,0x60);

}

void write2()
{
	uchar code tab[96]={
	//欢迎光临
0x0,0x0,0x0,0x0,0x0,0x0,0x3,0x0,0x0,0x2,0x1,0x0,0x2,0x4,0x7e,0x4,0x1,0x20,0x7a,0x2,0x92,0x4,0x45,0x20,0xb,0xe2,0x92,0x14,0x85,0x7e,0xa,0x20,0x92,0x14,0x85,0x50,0x55,0x40,0x92,0x14,0x5,0xd8,0x35,0x6,0x92,0x7f,0xe5,0x8,0x31,0x2,0x92,0x9,0x5,0x7e,0x11,0x2,0xb2,0x9,0x5,0x52,0x29,0x82,0xf6,0x9,0x5,0x52,0x29,0x82,0x90,0x9,0x5,0x52,0x4a,0x42,0x10,0x11,0x25,0x52,0x46,0x63,0x0,0x31,0x25,0x7e,0xc,0x24,0xfe,0x61,0xe1,0x42,0x0,0x0,0x0,0x0,0x0,0x0
	
	
	};
	AT24C64_W(tab,0x60,0x60);
}

void write3()
{
	uchar code tab[96]={
	//出入平安
0x0,0x0,0x0,0x0,0x0,0x0,0x4,0x0,0x0,0x0,0x0,0x40,0x4,0x1,0x80,0x3f,0xc0,0x20,0x24,0x44,0x40,0x2,0x7,0xfe,0x24,0x40,0x60,0x2,0x44,0x2,0x24,0x40,0x20,0x12,0xc4,0x2,0x24,0x40,0x60,0x12,0x80,0x42,0x3f,0xc0,0x50,0x12,0x80,0x80,0x4,0x0,0xd0,0x2,0x7,0xfe,0x24,0x40,0x90,0x7f,0xe0,0x88,0x24,0x41,0x88,0x2,0x1,0x10,0x24,0x41,0xc,0x2,0x0,0xd0,0x24,0x42,0x6,0x2,0x0,0x30,0x3f,0xc6,0x2,0x2,0x0,0xdc,0x0,0x40,0x0,0x2,0x7,0x87,0x0,0x0,0x0,0x0,0x0,0x0
	
	
	};
	AT24C64_W(tab,0xc0,0x60);
}
void write4()
{
	uchar code tab[96]={
	//温世坚
0x40,0x0,0x0,0x0,0x8,0x0,0x27,0xf0,0x12,0x10,0xb,0xfc,0x24,0x10,0x12,0x10,0x49,0x8,0x7,0xf0,0x12,0x10,0x49,0x10,0x94,0x10,0x12,0x10,0x48,0xa0,0x54,0x10,0xff,0xfe,0x48,0x40,0x17,0xf0,0x12,0x10,0x48,0xb0,0x20,0x0,0x12,0x10,0x49,0xe,0x2f,0xfc,0x12,0x10,0xa,0x4,0xc9,0x24,0x13,0xf0,0x9,0x0,0x49,0x24,0x12,0x10,0x3f,0xf8,0x49,0x24,0x10,0x0,0x1,0x0,0x49,0x24,0x10,0x8,0x1,0x0,0x49,0x24,0x1f,0xfc,0x1,0x0,0x5f,0xfe,0x0,0x0,0xff,0xfe,0x40,0x0,0x0,0x0,0x0,0x0
	
	
	};
	AT24C64_W(tab,0x120,0x60);
}

//=====================================
void main()
{

	//uchar i,j;
	//P1=0;
	//P2=0;

	P3=0;
	RED=0;
	GRE=0;
	write1();
	write2();
	write3();
	write4();
//	write2();
	GRE=1;
	RED=1;
}






⌨️ 快捷键说明

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