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

📄 i2cwr1.c

📁 程序实现了对24c256的I2C存储器的存储与读取
💻 C
字号:
#include "reg52.h"
#include <stdio.h>
#include <intrins.h>

sbit SCL = P1 ^ 6;
sbit SDA = P1 ^ 7;
struct buf {
	unsigned char command, addressh, addressl, rdata;

} data buf_IIC;

void send_start()
{
	SCL = 0;
	SDA = 1;
	SCL = 1;
	SCL = 1;
	SCL = 1;
	SDA = 0;
	SCL = 0;
}

void send_stop()
{
	SDA = 0;SDA = 0;SCL = 0;SCL = 0;
	SCL = 1;SCL = 1;SDA = 1;SDA = 1;
}

void i2c_init()
{
	SCL = 0;
	send_stop();
}


bit i2c_clock()
{
	bit sm;
	SCL = 1;SCL = 1;
	sm = SDA;
	SCL = 0;SCL = 0;
	return(sm);
}


void flashA()
{ bit NOP;
  NOP=~NOP; NOP=~NOP; NOP=~NOP; NOP=~NOP; NOP=~NOP; }


void I2C_Ack(bit a)
{
if(a==0)SDA=0;
else SDA=1;
flashA();
SCL=1;
flashA();
flashA();
SCL=0;
flashA();
}

bit get_bit(unsigned char my_data, n)
{
	bit bdata byte_bit;
	unsigned char x;
	x = (my_data >>= n);
	byte_bit = x & 0x01;
	return(byte_bit);
}


void send_command()
{
	char i;
	for (i = 0; i < 8; i++) {
		SDA = get_bit(buf_IIC.command, 7 - i);
		i2c_clock();
	};
}

void send_address()
{
	char i;
	bit get_ack();
	for (i = 7; i >= 0; i--) {
		SDA = get_bit(buf_IIC.addressh, i);
		i2c_clock();
	};
	get_ack();
	for (i = 7; i >= 0; i--) {
		SDA = get_bit(buf_IIC.addressl, i);
		i2c_clock();
	};
}

delay(unsigned char mm10)
{
	unsigned char i,j;
	for (j = 1; j <= mm10; j++) {
		for (i = 0; i <= 108; i++) {
			_nop_();
			_nop_();
		};
	};
}
/*IIC总线发送一个字节*/
void IIC_send_data()
{
	char i;
	for (i = 7; i >= 0; i--) {
		SDA = get_bit(buf_IIC.rdata, i);
		i2c_clock();
	};
}

void IIC_get_data() /*IIC总线接收一个字节*/
{
	char i; bit ss;
	buf_IIC.rdata = 0;
	SDA = 1;
	for (i = 7; i >= 0; i--) {
		ss = i2c_clock();
		buf_IIC.rdata = buf_IIC.rdata * 2;
		if (ss)
			buf_IIC.rdata = buf_IIC.rdata + 1;
	};
}

bit get_ack()
{
	bit bb;
	bb = i2c_clock();
	return(bb);
}

void no_ack()
{
	//i2c_clock();
	i2c_clock();
}
/*=============================*/
void i2c_write(int address, rdata, delays)/*rdata是16位的,实际被写入EEPROM的只有低8位*/
{
	wr_led = 0;
	buf_IIC.command = 0xa0;
	buf_IIC.addressl = address & 0xff;//0x100;
	buf_IIC.addressh = address >>= 8;//0x100;
	i2c_init();
	buf_IIC.rdata = rdata;
	send_start();
	send_command();get_ack();
	send_address();get_ack();
	IIC_send_data();get_ack();
	send_stop();
	if (delays != 0)
		delay(8);
	wr_led = 1;
}
char i2c_read(int address)
{
	buf_IIC.command = 0xa0;
	buf_IIC.addressl = address & 0xff;//0x100;
	buf_IIC.addressh = address >>= 8;//0x100;
	i2c_init();
	send_start();
	send_command();get_ack();
	send_address();get_ack();
	send_start();
	buf_IIC.command = 0xa1;           
	send_command();get_ack();           
	IIC_get_data();no_ack();
	send_stop();
	return(buf_IIC.rdata);
}

void i2c_read_leng(uint address,uchar leng)       //读短信内容
{  unsigned char iq;

   buf_IIC.command = 0xa0;
   buf_IIC.addressl = address & 0xff;//0x100;
   buf_IIC.addressh = address >>= 8;//0x100;
   i2c_init();
   send_start();
   send_command();get_ack();
   send_address();get_ack();
   send_start();
   buf_IIC.command = 0xa1;           
   send_command();get_ack();
   
   Poutbuf=&Key[9];
   
   for(iq=0;iq<(leng-1);iq++){
       IIC_get_data();
       I2C_Ack(0);
       *Poutbuf=buf_IIC.rdata;        
       Poutbuf++;
   }
   IIC_get_data();
   I2C_Ack(1);
   *Poutbuf=buf_IIC.rdata;
   Poutbuf++;
   send_stop();
}

void i2c_write_int(int address, rdata) /*rdata是16位的,实际被写入EEPROM低16位*/
{
	char hc, lc;
	lc = rdata % 0x100;hc = rdata >>= 8;
	i2c_write(address, hc, 1);
	i2c_write(address + 1, lc, 1);
}

int i2c_read_int(int address)
{
	uint i;
	i = i2c_read(address);
	i = i <<= 8;
	i = i | (i2c_read(address + 1) & 0x00ff);
	return(i);
}
void i2c_write_leng(uint address,uchar leng,uchar indata)          //rdata是16位的,实际被写入EEPROM的只有低8位
{
   unsigned char iq;
   wr_led = 0;
   buf_IIC.command = 0xa0;
   buf_IIC.addressl = address & 0xff;//0x100;
   buf_IIC.addressh = address >>= 8;//0x100;
   i2c_init();
   send_start();
   send_command();get_ack();
   send_address();get_ack();


   for(iq=0;iq<leng;iq++)
      { buf_IIC.rdata = indata;
       IIC_send_data();get_ack();       //数据连续写

      }
   send_stop();
   delay(8);
   wr_led = 1;
}

//======删除所有卡========================
void erase_all()
{       uchar leng;
	unsigned int i;

        for(i=5;i<3167;) {
            leng=32-(i%32);
            if((i+leng)>3167){
               leng=3167-i;
            }
            i2c_write_leng(i,leng,0);
            i=i+leng;

        }
        for(i=255;i<767;) {
            leng=32-(i%32);
            if((i+leng)>767){
               leng=767-i;
            }
            i2c_write_leng(i,leng,0xff);
            i=i+leng;

        }
}
//======删除所有密码========================
void erase_all_password()
{
        i2c_write_int(PW_Total,0);

}

⌨️ 快捷键说明

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