Код: Выделить всё
/* now you can include the I2C Functions */
#include <i2c.h>
/* function declaration for delay_ms */
#include <delay.h>
#define EEPROM_BUS_ADDRESS 0xa0 // Tyt stavim adress for EEPROM
/* read a byte from the EEPROM */
unsigned char eeplc24_read(unsigned char address_high, unsigned char address_low) {
unsigned char data;
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address_high);
i2c_write(address_low);
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS | 1);
data=i2c_read(0);
i2c_stop();
return data;
}
/* write a byte to the EEPROM */
void eeplc24_write(unsigned char address_high, unsigned char address_low, unsigned char data) {
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS);
i2c_write(address_high);
i2c_write(address_low);
i2c_write(data);
i2c_stop();
/* 10ms delay to complete the write operation */
delay_ms(7);
}Код: Выделить всё
#ifndef _24lcxx_INCLUDED_
#define _24lcxx_INCLUDED_
#pragma used+
/* read a byte from the EEPROM */
unsigned char eeplc24_read(unsigned char address_high, unsigned char address_low);
/* write a byte to the EEPROM */
void eeplc24_write(unsigned char address_high, unsigned char address_low, unsigned char data);
#pragma used-
#pragma library 24lcxx.lib
#endifКод: Выделить всё
unsigned int EEPROM_read_int(unsigned int addr) {
unsigned char buf[2];
unsigned char i;
for( i = 0; i < 2; i++ ) buf[i] = EEPROM_read(addr+i);
unsigned int &num = (unsigned int&)buf;
return num;
Код: Выделить всё
void EEPROM_write_int(unsigned int addr, unsigned int num) {
unsigned char buf[2];
unsigned char i;
(unsigned int&)buf = num;
for( i = 0; i < 2; i++ ) EEPROM_write(addr+i, buf[i]);
}


