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

📄 i2c-qing.c

📁 C51的I2C代码,针对PHILIPS的时钟芯片与24C32
💻 C
字号:
/***********************************************************************
*                                                                      *
* File:         I2c.C (Source)                                         *
*                                                                      *
* Version:      V0.02                                                  *
*                                                                      *
* Created:      17.12.00                                               *
*                                                                      *
* Author:       Qinpeng                                                *
*                                                                      *
* Compiler:     KEIL C51 V5.20                                         *
*                                                                      *
* Description:  89C51RD+(@22.1184MHz)  I2c Bus  Devices                *
*               Include Devices:       DS1307  (RTC with 1HZ out)      *
*                                                                      *
*			 		                               *
***********************************************************************/

//#include <p89c51rx.h>
#include <reg52.h>
#include <intrins.h>
#include "comm.h"
#include "flash.h"
#include "jnmain.h"

#define	WR1307	0xd0
#define	RD1307	0xd1

#define	WR1203	0xde
#define	RD1203	0xdf
	
#define	WR2464	0x42	//a0
#define	RD2464	0x43	//a1

//sbit	SDA=P3^5;	 
sbit	SCL=P3^4;
sbit	SDA1=P1^7;
// Local Procedures

extern	uchar	RW24_tmp1[];
static void delay_50us(uchar idata _50us);
static void delay_1ms(uchar idata _1ms);
static void delay_10ms(uint idata _10ms);

extern void    TOGGLE_WD(void);
/***********************************************************************
     *     			I2c BUS Related                  		*
***********************************************************************/

void	Delay5(void)        	// 5 us
{
	unsigned char data i; 
	for(i=0;i<8;i++);
	TOGGLE_WD();
}

void	Init_I2c(void)
{	SDA=1;SCL=1;	}

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

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

void	SendByte(unsigned char b)
{
	unsigned char i;
	unsigned char bb=0x80;
	for(i=0;i<8;i++)
	{
 		SDA=b&bb;
  		bb=bb/2;
   		Delay5();
    		SCL=1;
     		Delay5();
     		SCL=0;
	}	
	Delay5();
	SCL=1;
	Delay5();
	SCL=0;
}

unsigned char ReceByte(bit ack)
{
	unsigned char i;
	unsigned char b=0;
	for(i=0;i<8;i++)
	{
   		Delay5();
    		SDA=1;
     		SCL=1;
     		Delay5();
     		b=2*b+(unsigned char)SDA;
		SCL=0;
	}
	SDA=ack;	//ACK
	Delay5();
	SCL=1;
	Delay5();
	SCL=0;
	return b;	
}



void	Init_I2c1(void)
{	SDA1=1;SCL=1;	}

void	Start1(void)
{	SDA1=1;SCL=1;Delay5();SDA1=0;Delay5();SCL=0;	}

void	Stop1(void)
{	SDA1=0;Delay5();SCL=1;Delay5();SDA1=1;Delay5();	}

void	SendByte1(unsigned char b)
{
	unsigned char i;
	unsigned char bb=0x80;
	for(i=0;i<8;i++)
	{
 		SDA1=b&bb;
  		bb=bb/2;
   		Delay5();
   		SCL=1;
   		Delay5();
   		SCL=0;
	}	
	Delay5();
	SCL=1;
	Delay5();
	SCL=0;
}

unsigned char ReceByte1(bit ack)
{
	unsigned char i;
	unsigned char b=0;
	for(i=0;i<8;i++)
	{
   		Delay5();
   		SDA1=1;
   		SCL=1;
   		Delay5();
   		b=2*b+(unsigned char)SDA1;
		SCL=0;
	}
	SDA1=ack;	//ACK
	Delay5();
	SCL=1;
	Delay5();
	SCL=0;
	return b;	
}



/**************************************************
*                                                 *
*         	DS1307  relate Functons           *
*                                                 *
**************************************************/

void Init_1307(unsigned char control)
{
	Start();
	SendByte(WR1307);
	SendByte(0x7);
	SendByte(control);
	Stop();	
}

void GetTime(unsigned char *t)
{
	Start();
	SendByte(WR1307);
	SendByte(0x00);
	Start();
	SendByte(RD1307);
	t[0]=ReceByte(0);
	t[1]=ReceByte(0);
	t[2]=ReceByte(0);
        t[3]=ReceByte(0);
        t[4]=ReceByte(0);
        t[5]=ReceByte(0);
        t[6]=ReceByte(1);
	Stop();
        t[0]=(t[0]&0x0f)+((t[0]&0xf0)>>4)*10;
        t[1]=(t[1]&0x0f)+((t[1]&0xf0)>>4)*10;
        t[2]=(t[2]&0x0f)+((t[2]&0xf0)>>4)*10;
        t[3]=(t[3]&0x0f)+((t[3]&0xf0)>>4)*10;
        t[4]=(t[4]&0x0f)+((t[4]&0xf0)>>4)*10;
        t[5]=(t[5]&0x0f)+((t[5]&0xf0)>>4)*10;
        t[6]=(t[6]&0x0f)+((t[6]&0xf0)>>4)*10;
}

void SetTime(unsigned char *t)
{
	Start();
	SendByte(WR1307);
	SendByte(0x00);
	SendByte(t[0]);
	SendByte(t[1]);
	SendByte(t[2]);      
	SendByte(t[3]);      
	SendByte(t[4]);      
	SendByte(t[5]);      
	SendByte(t[6]);      
	Stop();
}


unsigned char Rd1307(unsigned char Adrr)
{
	unsigned char b;
	Start();
	SendByte(WR1307);
	SendByte(Adrr);
	Start();
	SendByte(RD1307);
	b=ReceByte(1);
	Stop();
	return(b);	
}

void Wr1307(unsigned char Addr,unsigned char b)
{
	Start();
	SendByte(WR1307);
	SendByte(Addr);
	SendByte(b);
	Stop();
}





/**************************************************
*                                                 *
*         	AT2416  relate Functons           *
*                                                 *
**************************************************/

void Read2416(uchar PageNumber,uchar DataAddress){
     uchar i;
/*this program is for 24c32*/
          Init_I2c1();
	      Start1();
          SendByte1(WR2464);
	      SendByte1(PageNumber);
	      SendByte1(DataAddress);
	      Start1();
	      SendByte1(RD2464);
	      RW24_tmp1[0]=ReceByte1(1);
	      Stop1();
	      for(i=1;i<16;i++){
			Start1();
			SendByte1(RD2464);
			RW24_tmp1[i]=ReceByte1(1);
			Stop1();
	      }
/*
          Init_I2c1();
	      Start1();
          SendByte1(WR2464);
	      SendByte1(PageNumber);
	      SendByte1(DataAddress);
	      Start1();
	      SendByte1(RD2464);
	      for(i=0;i<16;i++){
			RW24_tmp1[i]=ReceByte1(1);
	      }
		  Stop1();*/
/*     end           */
}

void Write2416(unsigned char PageNumber,unsigned char DataAddress ){
unsigned char i;
/*this program is for 2464*/
	Clear_Cntla(SAM_RST);    // 2464 wp
	Start1();
	SendByte1(WR2464);
	SendByte1(PageNumber);
	SendByte1(DataAddress);
	for(i=0;i<16;i++){
	     SendByte1(RW24_tmp1[i]);
	     Delay5();
	}
	Stop1();
	Set_Cntla(SAM_RST); 
/*    end    */

}


/****************************************************************************
*                                                                           *
* Function:     delay_50us                                                  *
*                                                                           *
* Input:        _50us                                                       *
* Output:       -                                                           *
*                                                                           *
* Description:  Time delay with a resolution of 50 =E6s.                    *
*                                                                           *
****************************************************************************/


void delay_50us(uchar idata _50us)
{
	unsigned char i,j;
   for(j=0;j<_50us;j++){
       for(i=0;i<14;i++);TOGGLE_WD();//1999.08.28
   }
//   for(j=0;j<_50us;j++){
//       for(i=0;i<44;i++);TOGGLE_WD();//1999.08.28
//   }
}

void delay_1ms(uchar idata _1ms)
{
 unsigned char i, j , k;
  for(k=0;k<_1ms;k++)
  {
     for(j=0;j<20;j++)
	{
 //	  for(i=0;i<44;i++)TOGGLE_WD();//1999.08.28
 	  for(i=0;i<14;i++)TOGGLE_WD();//1999.08.28
	}   
   }
}

void delay_10ms(uint idata _10ms)
{
 unsigned char i,j,k;
  for(k=0;k<_10ms;k++) 
  {
      for(j=0;j<200;j++)
	{
//	  for(i=0;i<44;i++)TOGGLE_WD();//1999.08.28
	  for(i=0;i<14;i++)TOGGLE_WD();//1999.08.28
	}    
  }
} 

⌨️ 快捷键说明

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