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

📄 24c02.c

📁 该程序是iic程序的测试程序
💻 C
字号:
#include <reg51.h>
#include  <absacc.h>
#include "INTRINS.H"
#define  uchar unsigned char
/***************************************************************************/
#define WriteDeviceAddress 0xa0 /*24c02的写地址*/
#define ReadDviceAddress 0xa1   /*24c02的读地址*/
/***************************************************************************/
sbit    P1_5=P1^5; 
sbit    P1_4=P1^4; 
sbit    SDA =P2^5; 
sbit    SCL =P2^6; 
sbit    P2_7=P2^7;
sbit    P3_5=P3^5; 
sbit    P3_4=P3^4; 
unsigned char code table[1]={0x01}; 
unsigned char code table2[1]={0x02}; 
unsigned char table1[1]; 
/*********************延时程序*******************************************/
void DelayMs(unsigned int number)
 {  unsigned char temp;
    for(;number!=0;number--) 
    {
        for(temp=112;temp!=0;temp--);      
    }
}

/***************************************************************************
***************************24c02开始程序************************************ 
***************************************************************************/
void Start() 
{
    SDA=1;
    SCL=1;
    SDA=0;
    SCL=0;
}
/***************************************************************************
***************************24c02停止程序************************************ 
***************************************************************************/
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;
}
/***************************************************************************
*返回错误标志,ErrorBit=SDA,为1错误,0正确*********************************
***************************************************************************/
bit TestAck() 
{
    bit ErrorBit;
    SDA=1;
    SCL=1;
    ErrorBit=SDA;
    SCL=0;
    return(ErrorBit);
}
/***************************************************************************
***************************24c02写一个字节程序****************************** 
***************************************************************************/
void Write8Bit(unsigned char input) 
{
    unsigned char temp;
    for(temp=8;temp!=0;temp--)
   {
        SDA=(bit)(input&0x80);/*从高位依次取input的各位*/
        SCL=1;
        SCL=0;
        input=input<<1; /*相等与RLC,取了CY位*/
    }
}
/***************************************************************************
***************************24c02写程序************************************** 
***************************************************************************/
void Write24c02(unsigned char *Wdata,unsigned char RomAddress,unsigned char number)
{
    Start();/*IIC开始*/
    Write8Bit(WriteDeviceAddress);/*写入器件地址0xa0*/
    TestAck();/*测试ACK位*/
    Write8Bit(RomAddress);/*写入器件控制单元地址*/
    TestAck();/*测试ACK位*/
    for(;number!=0;number--) 
    {
        Write8Bit(*Wdata);
        TestAck();
        Wdata++;
    }
    Stop();/*IIC停止*/
    DelayMs(10);/*延长10MS,保证数据写入*/
}
/***************************************************************************
***************************24c02读一个字节程序****************************** 
***************************************************************************/
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);  /*把数据返回*/
}
/***************************************************************************
***************************24c02读程序************************************** 
***************************************************************************/
void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) 
{    
    Start(); /*IIC开始*/
    Write8Bit(WriteDeviceAddress);/*写入器件地址0xa0*/
    TestAck();/*测试ACK位*/
    Write8Bit(RomAddress);/*写入器件控制单元地址*/ 
    TestAck();/*测试ACK位*/
    Start();/*IIC再次发送开始*/
    Write8Bit(ReadDviceAddress);
    TestAck();/*测试ACK位*/
    while(bytes!=1) 
    {
    *RamAddress=Read8Bit();/*存一个读到的数据到RamAddress+I*/
    Ack();/*发送IIC再读*/
    RamAddress++;/*存取地址加一*/
    bytes--;
    }
    *RamAddress=Read8Bit();
    NoAck();/*发送IIC停止读*/
    Stop();/*IIC停止*/
}
/***************************************************************************/
chushihua()
{  
    P0=0xff;
	P1=0xff; 
	P2=0xff; 
    P3=0xff;   
 	P2_7=0;
}   
/*申明:你自己看懂以后自己写,我给你的是最简单的程序,就是上电根据最后的状态,在运行中根据你的按键来说明问题*/    
main()
{ 
chushihua();/*初始化程序*/
Read24c02(table1,0x10,0x01);
  if(table1[0]==0x01)  {P1_4=0;P1_5=1;}
  else                 {P1_4=1;P1_5=0;} 
  /*以上第一次上电的时候检查,对应的 LED

⌨️ 快捷键说明

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