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

📄 iic.c

📁 OSD 学习学习.天天向上.STUDY STUDY DAY DAY UP.
💻 C
字号:
#include <reg51.h>
#include "i2c.h"
#include "intrins.h"

// 对24C02的读、写
/***************************************************************************/
void DelayMs(unsigned int number) {
	unsigned char temp;
	for(;number!=0;number--) {
		for(temp=25;temp!=0;temp--) { //112 16mhz   35 --3.58
		}
	}
}
void DelayUs(unsigned int number) 
{
	_nop_();
	_nop_();
}
/***************************************************************************/
void Start() {
	SDA=1;
	DelayUs(1);
	SCL=1;
	DelayUs(1);
	SDA=0;
	DelayUs(1);
	SCL=0;
	DelayUs(1);
}

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

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

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

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

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

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

/***************************************************************************/
unsigned char Read8Bit() {
	unsigned char temp,rbyte=0;
	tet=0;
	for(temp=8;temp!=0;temp--) {
		SCL=1;
		DelayUs(1);
	//	rbyte=rbyte<<1;
	//	if(SDA==1)rbyte|=0x01;   //because rbyte value  not change so define another varible  tet ????
		tet<<=1;
		if(SDA==1)tet|=0x01;
		SCL=0;
		DelayUs(1);
	}
	return(tet);
}

/***************************************************************************/
void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) {
	EA=0;
	Start();
	Write8Bit(WriteDevice_24c02_Address);
	TestAck();
	Write8Bit(RomAddress);
	TestAck();
	Start();
	Write8Bit(ReadDevice_24c02_Address);
	while(TestAck()==1);
	while(bytes!=1) {
	*RamAddress=Read8Bit();
	Ack();
	RamAddress++;
	bytes--;
	}
	*RamAddress=Read8Bit();
	NoAck();
	Stop();
	EA=1;
}

/***************************************************************************/
void Read_Tv_tune(unsigned char *RamAddress) {
	EA=0;
	Start();
	Write8Bit(WriteDevice_Tv_Address);
	TestAck();
	Start();
	Write8Bit(ReadDevice_Tv_Address);
	while(TestAck()==1);
	*RamAddress=Read8Bit();
	NoAck();
	Stop();
	EA=0;
}
/***************************************************************************/
void Write_Tv_tune(unsigned int Wdata,unsigned char freq_flag) {
	unsigned char temp=0;
	EA=0;
	Start();
	Write8Bit(WriteDevice_Tv_Address);
	TestAck();
	temp=(unsigned char )(Wdata>>8&0x00ff);
	Write8Bit(temp);
	TestAck();
	temp=(unsigned char )(Wdata&0x00ff);
	Write8Bit(temp);
	TestAck();
	
	Write8Bit(0x86); //TVTUNE_CTRL_WORD1;
	TestAck();
	if(freq_flag==0)	//TVTUNE_CTRL_WORD2
		Write8Bit(0x41);	//VHF L CTRL	
	else if(freq_flag==1)
		Write8Bit(0x42);	//VHF H
	else if(freq_flag==2)	
		Write8Bit(0x44);	//UHF		
	TestAck();	
	Stop();
	DelayMs(10);
	EA=1;
}

/***************************************************************************/

⌨️ 快捷键说明

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