93LC46B
This file contains functions that read and write data to the eeprom 93LC46B
//Bit-banging method is used to keep the code generic
//The code is written with CCS PIC C compiler
//edit by tuan anh
//last update 2021-8-10
//
#define PIN_CS PIN_C2
#define PIN_CLK PIN_C3
#define PIN_DIN PIN_C5
#define PIN_DO input(PIN_C4)
#define start 0x01
#define write_enable 0x30
#define write_disable 0x00
#define code_write 0x40
#define code_read 0x80
//Command format start bit 0x01 + (opcode (2 bits) + address) + data
//EEPROM only starts functioning when both clock and data lines are high, i.e when it received a start bit
void E9346B_init();
void pulse_clock();
void write_value(unsigned char value);
unsigned char read_value();
void E9346B_write(unsigned char address, unsigned long value);
unsigned long E9346B_read(unsigned char address);
void E9346B_write_state(int state);//Enable or Disable write
void E9346B_init()
{
set_TRIS_C(0xD3);//C2_out,C3_out,c4_IN, C5_out
output_C(0x00);
}
void pulse_clock(){
output_high(PIN_CLK);
delay_us(1);
output_low(PIN_CLK);
delay_us(1);
}
void write_value(unsigned char value)
{
unsigned char i;
for(i=1;i<=8;++i)
{
output_bit(PIN_DIN,value & 0x80);
pulse_clock();
value <<= 1;
};
}
unsigned char read_value()
{
unsigned char i,temp = 0x00;
for(i=1;i<=8;++i)
{
pulse_clock();
temp <<= 1;
temp |= PIN_DO;
};
return temp;
}
void E9346B_write(unsigned char address, unsigned long value)
{
unsigned char hb = 0x00;
unsigned char lb = 0x00;
unsigned long timeout = 0x0000;
hb = ((value & 0xFF00) >> 0x08);
lb = (value & 0x00FF);
E9346B_write_state(write_enable);
output_high(PIN_CS);
write_value(start);
write_value(code_write | address);
write_value(hb);
write_value(lb);
output_low(PIN_CS);
delay_us(20);
output_high(PIN_CS);
while((!PIN_DO) && (timeout < 10000))
{
delay_us(1);
timeout++;
};
output_low(PIN_CS);
delay_us(20);
E9346B_write_state(write_disable);
}
unsigned long E9346B_read(unsigned char address)
{
unsigned char lb = 0x00;
unsigned long hb = 0x00;
output_high(PIN_CS);
write_value(start);
write_value(code_read | address);
hb = read_value();
lb = read_value();
output_low(PIN_CS);
delay_us(20);
hb <<= 0x08;
hb |= lb;
return hb;
}
void E9346B_write_state(int state)
{
output_high(PIN_CS);
write_value(start);
write_value(state);
output_low(PIN_CS);
delay_us(10);
}