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

📄 i2c_24c64.c

📁 单片机初学者必看内容
💻 C
字号:
/***********************************************************
**模块名称:24C64的读写
**编写人:bradley   日期 2005-5-1
**修改人:psp       日期 2006-10-27
**功能描述:24C64储存开机次数实验
**该试验功能是单片机复位一次, 自动从24C64中读取数据
**然后加1,最终数码管中的数据就是开机的次数,具有一定的实用意义
**烧写后用手按复位键可以看到数码管每按一下加一,也可以断电再开机
**********************************************************/

#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

code unsigned char seg7code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
                               0x82,0xf8,0x80,0x90,0xff}; //共阳数码管段码
sbit    SDA=P3^5;           //定义数据线
sbit    SCL=P3^4;           //定义时钟线
bit     flag;

uint    idata  ucSendBuffer[1]=0;
uint    idata  ucReceData;
uint    idata  ucReceiveBuffer;  //从器件中读出的1字节数据暂存区
void    delay(void);
void    delay_10ms(void);

void    ACK();
void    NoACK();



/**************************************************************/
void delay(void)
{
  uint i;
  for(i=100;i>0;i--)
  _nop_();
}

void delay1ms()
{
 uchar i;
 for(i=124;i>0;i--);  //延时124*8+10=1002us
}


/*********************************************************
**名称:I2C_Start
**功能:启动I2C
**输入:无
**返回:无
*********************************************************/
void I2C_Start()
{
        SDA=1;
        delay();
        SCL=1;
        delay();
        SDA=0;
        delay();
        SCL=0;                //钳位I2C总线,准备发送数据
}



/**********************************************************
**名称:I2C_Stop
**功能:停止I2C
**输入:无
**返回:无
**********************************************************/
void I2C_Stop()
{
        SDA=0;
        delay();
        SCL=1;
        delay();
        SDA=1;
        delay();
}




/**********************************************************
**名称:Ack
**功能:应答信号
**输入:无
**返回:无
**********************************************************/
void Ack()
{
        SDA=0;
        delay();
        SCL=1;
        delay();
        SCL=0;
        delay();
        SDA=1;
        delay();
}



/********************************************************
**名称:NoAck
**功能:发送非应答信号
**输入:无
**返回:无
********************************************************/
void NoAck()
{
        SDA=1;
        delay();
        SCL=1;
        delay();
        SCL=0;
        delay();
        SDA=0;
        delay();
}




/********************************************************
**名称:Test_Ack()
**功能:检测应答位
**输入:无
**返回:flag,有应答时flag为0,无应答时flag为1
*********************************************************/
bit Test_Ack()
{ 
        SCL=0;
        SDA=1;//读入数据
        _nop_();_nop_();_nop_();_nop_();
        SCL=1;
        _nop_();_nop_();_nop_();_nop_();
        if(SDA==0)
                flag=1;
        else        flag=0;
        SCL=0;
        return(flag);
}



/********************************************************
**名称:SendData()        
**功能:发送一字节数据
**输入:buffer
**返回:
*******************************************************/
void SendData(uint buffer)
{
        uint BitCnt=8;//一字节8位
        uint temp=0;
        do
        {
                temp=buffer;
                SCL=0;
                delay();
                if((temp&0x80)==0) //判断最高位是0还是1
                        SDA=0;
                else
                        SDA=1;
                delay();
                SCL=1;
                temp=_crol_(buffer,1);//将buffer中的数据左移一位
                buffer=temp;
                BitCnt--;
        }
        while(BitCnt);
        SCL=0;        
}

/**************************************************************
**名称:uint ReceiveData()
**功能:接收一字节数据
**输入:
**返回:ucReceData
**说明:将接收的数据存放在ucReceData中
**************************************************************/
uint ReceiveData()
{
        uint BitCnt=8;
        uint temp=0;
        SDA=1;//读入数据
        do
        {
                SCL=0;
                delay();
                SCL=1;
                delay();
                if(SDA==1)
                        ucReceData=ucReceData|0x01;  //低位置1
                else
                        ucReceData=ucReceData&0x0fe; //低位清0
                if(BitCnt-1)
                {
                        temp=_crol_(ucReceData,1);   //数据左移一位
                        ucReceData=temp;
                }
                BitCnt--;
        }
        while(BitCnt);
        SCL=0;
        return(ucReceData);
}

/*************************************************************
**名称:bit WriteNByte()
**功能:主机向24C64中写入多字节数据
**输入:
**返回:
**说明:sla-器件地址, suba-数据高8位地址,subab-数据低8位地址,*s-写入的数据,n-写入的字节数(n<=32)
**************************************************************/
bit WriteNByte(uint sla,uint suba,uint subab,uint *s,uint n)
{
        uint i;
        I2C_Start();//启动I2C
        SendData(sla);//发送器件地址
        Test_Ack();
        if(flag==0)        return(0);
        SendData(suba);
        Test_Ack();
		if(flag==0)        return(0);

        SendData(subab);
        Test_Ack();
        if(flag==0)        return(0);

        for(i=0;i<n;i++)//写入32字节数据
        {
                SendData(*(s+i));
                Test_Ack();
                if(flag==0)        return(0);
        }
        I2C_Stop();
        return(1);
}
/*************************************************************
**名称:bit ReadNByte()
**功能:主机从24C64中读出N字节数据(n<=32)
**输入:
**返回:
**说明:随机地址读操作
**************************************************************/
bit ReadNByte(uint sla,uint suba,uint subab,uint *p,uint n)
{
        uint i;
        I2C_Start();//启动I2C
        SendData(sla);//发送器件地址
        Test_Ack();
        if(flag==0)        return(0);
        SendData(suba);//发送器件内部高8位地址
        Test_Ack();
        if(flag==0)        return(0);

		SendData(subab);//发送器件内部低8位地址
        Test_Ack();
        if(flag==0)        return(0);

        I2C_Start();
        SendData(sla+1);
        Test_Ack();
        if(flag==0)        return(0);
        for(i=0;i<n-1;i++)//读取字节数据
        {
                *(p+i)=ReceiveData();//读取数据
                ACK();
        }
        *(p+n-1)=ReceiveData();
        
        NoACK();
        I2C_Stop();
        return(1);
}
/***************************************************************
**名称:main()
**功能:
**输入:
**返回:
**说明:
****************************************************************/
void main()
{       
  ReadNByte(0xa0,0x00,0xff,ucSendBuffer,1);
  ucSendBuffer[0]++;
 
  WriteNByte(0xa0,0x00,0xff,ucSendBuffer,1);

  while(1)
   {
	 P1=0xfd;     //P1.1=0,选通第二位
	 P2=seg7code[ucSendBuffer[0]%1000/100];   //百位
	 delay1ms();
	 P2=0xff;       //消隐
	
	 P1=0xfb;     //P1.3=0,选通第三位
	 P2=seg7code[ucSendBuffer[0]%100/10];   //十位
	 delay1ms();
	 P2=0xff;         //消隐
	
	 P1=0xf7;     //P1.3=0,选通第四位
	 P2=seg7code[ucSendBuffer[0]%10];   //个位
	 delay1ms();
	 P2=0xff;       //消隐

   }



}

⌨️ 快捷键说明

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