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

📄 iic_function.c

📁 用单片机的IIC接口做的存储器扩展
💻 C
字号:
/****************************************************************
-------------------ATMEL24C16读写程序----------------------------
---------------------------By nustpq 06/06/16--------------------
*****************************************************************/

#include<reg52.h>
#include<intrins.h>

#define   uchar   unsigned char 
#define   uint    unsigned int
#define   ulong    unsigned long 

sbit SDA         = P1^2;
sbit SCL         = P1^1;
//sbit LED         = P1^0;
unsigned char a[10]={0x20,0x31,0x32,'3','4','5','6','7','8','9'};
unsigned char b[10]={0,0,0,0,0,0,0,0,0,0},*p;

/******读写24C16标准程序段*****************************************/

/*---------------------------------------------
标准等待程序
---------------------------------------------*/
void wait()
{
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();    
}

/*---------------------------------------------
延时程序
---------------------------------------------*/
void delay(int x)
{
 int i=0;
 for(i=0;i<x;i++);  
}

/*-----------------------------------------------
调用方式:void start_bit(void)
函数说明:开始位
-----------------------------------------------*/
void start_bit(void)
{
SCL=1;
SDA=1;wait();
SDA=0;wait();
}
/*-----------------------------------------------
调用方式:void stop_bit(void)
函数说明:停止位
-----------------------------------------------*/
void stop_bit(void)
{
SDA=0;
SCL=1;wait();
SDA=1;wait();
SCL=0;wait();
}

/*-----------------------------------------------
调用方式:void ack(void)
函数说明:应答函数
-----------------------------------------------*/
void ack(void)
{
SDA=0;
SCL=1;wait();
SCL=0;wait();
}
/*-----------------------------------------------
调用方式:void no_ack(void)
函数说明:无需应答位,在读程序中用到
-----------------------------------------------*/
void no_ack(void)
{
SDA=1;wait();
SCL=1;wait();
SCL=0;wait();
}
/*-----------------------------------------------
调用方式:write_8bit(uchar ch)
函数说明:写一个字节(8位)数据
-----------------------------------------------*/
write_8bit(uchar ch)
{
  uchar i,w;
  for(i=0;i<8;i++)
  {
     w=ch&0x80;
     ch<<=1;
     if(w==0)
     {
      SDA=0;
      SCL=1;wait();
     }
     if(w==0x80)
     {
      SDA=1;
      SCL=1;wait();
      SCL=0;wait();
      SDA=0;wait();
     }
  }
}
/*-----------------------------------------------
调用方式:uchar read_8bit(void)
函数说明:读一个字节(8位)数据
-----------------------------------------------*/
uchar read_8bit(void)
{
 uchar i,w=0;
 SDA=1;
 for(i=0;i<8;i++)
 {
   SCL=0;wait();
   SCL=1;wait();
   if(SDA==1)
   { 
     w<<=1;
     w|=0x01;
   }
   else
     w<<=1;  
 }
 return w;
}
/*----------------------------------------------
调用方式:uchar read24c16(uint address)
函数说明:读24c16指定地址数据(字节读)
-----------------------------------------------*/
uchar read24c16(uint address)
{
   uchar data rdata;
   
   start_bit();
   write_8bit(0xA0);
   ack();
   write_8bit(address);
   ack();
   stop_bit();
   
   return rdata;
}
/*-----------------------------------------------
调用方式:void write24c16(uint address,uchar ddata)
函数说明:写数据到24c16的指定地址(字节写)
-----------------------------------------------*/
void write24c16(uint address,uchar ddata)
{
EA=0;  /*避免与串口通讯等中断冲突*/
start_bit();
write_8bit(0xA0);
ack();
write_8bit(address);
ack();
stop_bit();
EA=1;
}
/*-----------------------------------------------
调用方式:void page_wr(uint firstw_ad,uint counter,uint data *firstr_ad)
函数说明:页面写函数,firstw_ad为写入字节单元的首地址,
          *firstr-ad为被写入数据所在首地址指针
          counter为写入数据字节数
-----------------------------------------------*/
void page_wr(uint firstw_ad,uint counter,uchar *firstr_ad)
{
uchar data *ufirstr_ad;
ufirstr_ad=firstr_ad;

start_bit();
write_8bit(0xA0);
ack();
while(counter--)
{
write_8bit(*ufirstr_ad);
ufirstr_ad++;
ack();
}
stop_bit();
}
/*-----------------------------------------------
调用方式:void page_rd(uint firstrd_ad,uint count,uint firstwr_ad)
函数说明:页面读函数,firstrd-ad为所读字节首地址,count为读字节数
           *ufirstwr-ad为读出数据存储首地址指针
-----------------------------------------------*/
void page_rd(uint firstrd_ad,uint countr,uchar *ufirstwr_ad)
{
uchar j=8;
uint count=countr;

start_bit();
write_8bit(0xA0);

while(count--)
{
  *ufirstwr_ad++=read_8bit();  
  mast_ack();
};
no_ack();
stop_bit();
}

⌨️ 快捷键说明

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