📄 eeprom.c
字号:
trans_bit(k_pin_high);
}
else
{
trans_bit(k_pin_low);
} //its a zero send one
outbyte = outbyte << 1; //Rotate byte left by 1
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
static uint8 recv_bit() reentrant
{
uint8 inbit = 0x00;
_set_util_config_reg(); //set up so these pins are gpios
//Set direction in
x_gpioa_dir &= ~DO; //sets direction to input
//clock goes low -- just in case it was high before
_eesclock_gpio_low();
//Delay a bit
_nop_();
//start clock train clock goes high
_eesclock_gpio_high();
_nop_();
inbit = x_gpioa_in & DO; //stick in inbit is in wrong bit position though
//MSB comes first from gpio 3
//should rotate
inbit = inbit >> k_data_rotate_right; //now its in LSB spot
//clock goes low
_eesclock_gpio_low();
//Delay a bit
_nop_();
return(inbit); //return in MSB of inbit
}
//------------------------------------------------------------------------------
//bit_out Function takes an address and receives it from the EEPROM
//Sent start, write addr, byte in returns a BYTE
//------------------------------------------------------------------------------
static uint8 byte_in() reentrant
{
uint8 temp = 0;
uint8 x;
//Start with MSB to receive Yes!!!!! rcc
//Will loop 8 times(8 bits)
for(x=0; x<=7; x++)
{
temp = temp << 1; //Rotate byte left by 1
temp = temp | recv_bit();
}
return(temp);
}
//------------------------------------------------------------------------------
//function to enable erase and write features
//on power up chip is disabled from write until is receives this function
//Must call before writes can happen!!!
//------------------------------------------------------------------------------
void eeprom_write_enable() reentrant
{
uint8 x;
//Send start bit
start_bit();
_opcode_ewds_ewen();
//Send 9 1s to enable write/erase
for(x=0; x<=8; x++)
{
_eesdata_gpio_high(); //clock in a 1
clock_out();
}
//clean up
_eesdata_gpio_low(); //put data in low
//send stop bit to CS
stop_bit();
}
//------------------------------------------------------------------------------
//function to disable erase and write features
//on power up chip is disabled from write
//------------------------------------------------------------------------------
void eeprom_write_disable() reentrant
{
int x;
//Send start bit
start_bit();
//Send two op code "two bits"
_opcode_ewds_ewen();
//Send 9 0s to disable write/erase
for(x=0; x<=8; x++)
{
_eesdata_gpio_low(); //clock in a 0
clock_out();
}
//clean up
_eesdata_gpio_low(); //put data in low
//send stop bit to CS
stop_bit();
}
//------------------------------------------------------------------------------
//function to write byte at address
//parameters are address and byte to write
//------------------------------------------------------------------------------
void eeprom_write(uint8 addr, uint8 dataout) reentrant
{
trace2(0, dev, 0, "eeprom_write(%d, %02X)", addr, dataout) ;
//Send start bit
start_bit();
//Send two write op code "two bits"
_opcode_write();
//send extra clock
// tbh lo on 9 bit address for '66 part
_eesdata_gpio_low(); \
clock_out();
//send address byte
byte_out(addr); //This can be confusing write MSB first
byte_out(dataout); //data goes out
trace2(0, eep, 0, "byte_write = %02X addr = %02X", dataout, addr);
//send stop bit to CS
stop_bit();
eeprom_write_delay();
}
//------------------------------------------------------------------------------
//function to read byte at address
//parameters are address and return byte MSB first
//------------------------------------------------------------------------------
uint8 eeprom_read(uint8 addr) reentrant
{
uint8 bytein;
trace1(0, dev, 0, "eeprom_read(%d)", addr) ;
//Send start bit
start_bit();
//Send two read op code "two bits"
_opcode_read();
//send extra clock
clock_out();
//send address byte
byte_out(addr);
//clock the byte in
bytein = byte_in();
trace2(0, eep, 0, "byte_read = %02X addr = %02X", bytein, addr);
//send stop bit to CS
stop_bit();
return(bytein);
}
//------------------------------------------------------------------------------
//Function to delay 4 msec after each byte write
//------------------------------------------------------------------------------
void eeprom_write_delay() reentrant
{
uint16 x;
for(x=0; x<120; x++)
{
delay(0xFF);
}
}
//------------------------------------------------------------------------------
//Function to delay
//------------------------------------------------------------------------------
static void delay(uint8 timeout) reentrant
{
if(timeout <= 6)
return;
// If we got here, timeout was >=7, so will be >=1 after subtracting 6.
// As a result, we do not have to worry about wrap-around on pre-decrement.
timeout -= 1;
while(--timeout)
{
_nop_();
}
}
#if 0
****************************************************************************
* delay
****************************************************************************
* Function provides a(fairly) accurate software-controlled delay @ MCUCLK=30 MHZ.
* For requested delays <= 6uS, the actual delay is 5 uS.
* For delays > 6uS, the actual delay is 0.5 uS longer than requested.
* The above times include call/return overhead.
* CHECK ABOVE CALCULATION
****************************************************************************
*
* Looking at the compiler listing, the overhead, including call/return,
* is ?? instruction cycles, which is ??? uS @ 30 MHZ.
* need to calculate time base on new 3 clocks/cycle 8051 used in 201 chip
* As a result, this code subtracts 6 uS from the requested value,
* and returns immediately if it is 6 is or less; if this happens,
* the actual delay time is 5 uS.
; FUNCTION _?delay(BEGIN)
0000 1500 E DEC ?C_IBP
0002 A800 E MOV R0,?C_IBP
0004 A607 MOV @R0,AR7
; SOURCE LINE # 284
; SOURCE LINE # 286
0006 A800 E MOV R0,?C_IBP
0008 E6 MOV A,@R0
0009 D3 SETB C
000A 9406 SUBB A,#06H
000C 400C JC ?C0033
000E ?C0032:
; SOURCE LINE # 292
000E A800 E MOV R0,?C_IBP
0010 16 DEC @R0
0011 ?C0034:
; SOURCE LINE # 293
0011 A800 E MOV R0,?C_IBP
0013 16 DEC @R0
0014 E6 MOV A,@R0
0015 6003 JZ ?C0033
; SOURCE LINE # 294
; SOURCE LINE # 295
0017 00 NOP
; SOURCE LINE # 296
0018 80F7 SJMP ?C0034
; SOURCE LINE # 297
001A ?C0033:
001A 0500 E INC ?C_IBP
001C 22 RET
; FUNCTION _?delay(END)
#endif
//---eof------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -