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

📄 iic.c

📁 含有大量的单片机代码
💻 C
字号:

#include <reg51.h>

//unsigned char code tab[22]={0x0bf,0x86,0x0db,0x0cf,0x0e6,0x0ed,0x0fd,0x087,0x0ff,0x0e7,0x0f7,0x0fc,0x0b9,0x0de,0x0f9,0x0f1};
//unsigned char code tab[22]={0x0bf,0x86,0x0db,0x0cf,0x0e6,0x0ed,0x0fd,0x087,0x0ff,0x0e7,0x0f7,0x0fc,0x0b9,0x0de,0x0f9,0x0f1};
unsigned char code tab[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
   
// 对24C04的读、写
// extern void DelayMs(unsigned int);
// extern void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
// extern void Write24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);

/***************************************************************************/
#define	WriteDeviceAddress 0xa0
#define	ReadDviceAddress 0xa1

/***************************************************************************/
//sbit	SCL=P1^6;
//sbit	SDA=P1^7;
sbit	SCL=P1^3;
sbit	SDA=P1^4;
//sbit	DOG=P3^7;

/***************************************************************************/
void DelayMs(unsigned int number) {
	unsigned char temp;
	for(;number!=0;number--/*,DOG=!DOG*/)
    {
		for(temp=112;temp!=0;temp--) 
		{
		}
	}
}

/***************************************************************************/
void Start() {
	SDA=1;
	SCL=1;
	SDA=0;
	SCL=0;
}

/***************************************************************************/
void Stop() {
	SCL=0;
	SDA=0;
	SCL=1;
	SDA=1;
}

/***************************************************************************/
void Ack() {
	SDA=0;
	SCL=1;
	SCL=0;
	SDA=1;
}

/***************************************************************************/
void NoAck() {
	SDA=1;
	SCL=1;
	SCL=0;
}

/***************************************************************************/
bit TestAck() {
	bit ErrorBit;
	SDA=1;
	SCL=1;
	ErrorBit=SDA;
	SCL=0;
	return(ErrorBit);
}

/***************************************************************************/
void Write8Bit(unsigned char input) {
	unsigned char temp;
	for(temp=8;temp!=0;temp--) {
		SDA=(bit)(input&0x80);
		SCL=1;
		SCL=0;
		input=input<<1;
	}
}

/***************************************************************************/
void Write24c02(unsigned char *Wdata,unsigned char RomAddress,unsigned char number) {
	Start();
	Write8Bit(WriteDeviceAddress);
	TestAck();
	Write8Bit(RomAddress);
	TestAck();
	for(;number!=0;number--) {
		Write8Bit(*Wdata);
		TestAck();
		Wdata++;
	}
	Stop();
	DelayMs(10);
}

/***************************************************************************/
unsigned char Read8Bit() {
	unsigned char temp,rbyte=0;
	for(temp=8;temp!=0;temp--) {
		SCL=1;
		rbyte=rbyte<<1;
		rbyte=rbyte|((unsigned char)(SDA));
		SCL=0;
	}
	return(rbyte);
}

/***************************************************************************/
void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) 
{
   	//unsigned char temp,rbyte;
	Start();
	Write8Bit(WriteDeviceAddress);
	TestAck();
	Write8Bit(RomAddress);
	TestAck();
	Start();
	Write8Bit(ReadDviceAddress);
	TestAck();
	while(bytes!=1) {
	*RamAddress=Read8Bit();
	Ack();
	RamAddress++;
	bytes--;
	}
	*RamAddress=Read8Bit();
	NoAck();
	Stop();
}

/***************************************************************************/
//李澄20061118添加
void main()
{
 unsigned char WrBuff[10];//写十个数据进去
 unsigned char RdBuff[10];
 int i,j;
 for(i=0;i<10;i++)WrBuff[i]=i;
 Write24c02(WrBuff,0x10,10);
 P1=0x01;
 P0=tab[WrBuff[0]];
 for(i=0;i<6000;i++)
  for(j=0;j<100;j++);
 Read24c02(RdBuff,0x10,10);
 P1=0x02;
 P0=tab[RdBuff[1]];
 while(1);
}

⌨️ 快捷键说明

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