I just adjusted the operation of 24c256, and made a continuous read and write function in the program! I was annoyed by the problem of page writing across pages, so I simply made a more general function. If the write is less than 3 bytes, it will use random write, and if it is greater than 3 bytes, it will use page write! Share it!
The function actually called by other operations is:
unchar SeqWriteTo24c256(unchar sla_add,unint addr_op,unchar write_size,unchar *write_buf);
unchar SeqReadFrom24c256(unchar sla_add,unint addr_op,unchar read_size,unchar *read_buf);
The others are functions called by this file!!
//************************************************************ Debug function
void DebugEepromService(void)
{
unchar debug_buf[255];
debug_buf[0]=0x91;
debug_buf[1]=0x92;
debug_buf[2]=0x93;
debug_buf[3]=0x94;
debug_buf[4]=0x95;
debug_buf[250]=0x05;
debug_buf[251]=0x06;
debug_buf[252]=0x07;
debug_buf[253]=0x08;
debug_buf[254]=0x09;
SeqWriteTo24c256(EEP1_ADDR,1,255,debug_buf);
SeqReadFrom24c256(EEP1_ADDR,1,255,debug_buf);
}
//************************************************* ************
#define IIC_SDA_PB 0x20
#define IIC_SCL_PB 0x80
#define IIC_DEL_WAIT 0x10 //>4.7us(12.80us) for Fre=11.0592M
#define IIC_DEL_WRITE 0x2700 //>6ms(7266.54us=7.266ms) for Fre=11.0592M
#define EEP1_ADDR 0xa4
#define PAGE_CAP_BYTE 64 //24C256 page write capacity: 64 bytes
/*
Function function file
2005-9-22 9:54 by xth
Version: v1.0
--------------------------------------------
Mcu: avr mega32 Frequency: 11.0592M
--------------------------------------------
Function overview: Eeprom operation file
--------------------------------------------
*/
//============================== Function declaration
//----------IIC operation call function
void IicDelayService(unint delay_time);
void IicStartBitSend(void);
void IicStopBitSend(void);
void IicAckService(unchar ack_data);
unchar IicSendByteService(unchar tx_data);
unchar IicAccByteService(void);
//----------At24c256 operation function
unchar RandWriteByteTo24c256(unchar sla_add,unint addr_op,unchar data_op);
unchar WritePageTo24c256(unchar sla_add,unint addr_op,unchar *write_data_buf);
unchar SeqWriteTo24c256ByPage(unchar sla_add,unint addr_op,un char write_size,unchar *write_buf);
unchar SeqWriteTo24c256(unchar sla_add,unint addr_op,unchar write_size,unchar *write_buf);
unchar SeqReadFrom24c256(unchar sla_add,unint addr_op,unchar read_size,unchar *read_buf);
//=============================Function definition
void IicDelayService(unint delay_count)
{
unint count;
for(count=0;count
asm("NOP");
}
void IicStartBitSend(void)
{
PORTB |= IIC_SCL_PB; //Send the clock signal of the start condition
asm("NOP");
PORTB |= IIC_SDA_PB; //The start condition establishment time is greater than 4.7us, delay
IicDelayService(IIC_DEL_WAIT);
PORTB &= ~IIC_SDA_PB;
IicDelayService(IIC_DEL_WAIT);
PORTB &= ~IIC_SCL_PB; //Clamp the I2C bus and prepare to send or receive data
asm("NOP");
}
void IicStopBitSend(void)
{
PORTB &= ~IIC_SDA_PB;//Send the clock signal of the end condition
IicDelayService(IIC_DEL_WAIT);
PORTB |= IIC_SCL_PB; //The end condition establishment time is greater than 4μs
IicDelayService(IIC_DEL_WAIT);
PORTB |= IIC_SDA_PB;
asm("NOP");
}
void IicAckService(unchar ack_data)
{//As the master device response->send response or non-response signal
if(ack_data==0) PORTB &= ~IIC_SDA_PB;
else PORTB |= IIC_SDA_PB;
IicDelayService(IIC_DEL_WAIT);
PORTB |= IIC_SCL_PB;
IicDelayService(IIC_DEL_WAIT);
PORTB &= ~IIC_SCL_PB;//Clear the clock line and clamp the I2C bus to continue receiving
asm("NOP");
}
unchar IicSendByteService(unchar tx_data)
{//Send the byte, which can be an address or data. After sending, wait for the response and return
unchar bit_count, ack_flag;
for(bit_count=0;bit_count<8;bit_count++)
{
if((tx_data<
PORTB |= IIC_SDA_PB;
else
PORTB &= ~IIC_SDA_PB;
IicDelayService(IIC_DEL_WAIT);
PORTB |= IIC_SCL_PB; //Set the clock line high to notify the controlled device to start receiving data bits
IicDelayService(IIC_DEL_WAIT); //Ensure that the clock high level period is greater than 4μs
PORTB &= ~IIC_SCL_PB;
}
IicDelayService(IIC_DEL_WAIT);
PORTB &= ~IIC_SDA_PB;
DDRB &= ~IIC_SDA_PB; //Set SDA to input
asm("NOP");
PORTB |= IIC_SCL_PB;
IicDelayService(IIC_DEL_WAIT);
IicDelayService(IIC_DEL_WAIT);
if(PINB&IIC_SDA_PB) //Judge whether the response signal is received
ack_flag=NO;
else
ack_flag=YES; //There is a response signal
DDRB |= IIC_SDA_PB;
PORTB &= ~IIC_SCL_PB;
asm("NOP");
return(ack_flag);
}
unchar IicAccByteService(void)
{//Receive data from the device and judge the bus error
unchar bit_count, get_data;
DDRB &= ~IIC_SDA_PB;
get_data=0;
for(bit_count=0;bit_count<8;bit_count++)
{
asm("NOP");
PORTB &= ~IIC_SCL_PB; //Set the clock line low and prepare to receive data bits
IicDelayService(IIC_DEL_WAIT); //Clock low level period is greater than 4.7μs;
PORTB |= IIC_SCL_PB; //Set the clock line high to make the data on the data line valid
get_data<<=1;
if(PINB&IIC_SDA_PB)
get_data++;
asm("NOP");
asm("NOP");
}
PORTB &= ~IIC_SCL_PB;
DDRB |= IIC_SDA_PB;
asm("NOP");
return(get_data);
}
unchar RandWriteByteTo24c256(unchar sla_add,unint addr_op,unchar data_op)
{
unchar result_now,temp_data;
IicStartBitSend(); //Start condition
temp_data=sla_add; //Slave device addressresult_now
=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op>>8; //Operation unit address high 8
bitsresult_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op; //Operation unit address low 8
bitsresult_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=data_op; //Operation dataresult_now
=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
IicStopBitSend(); //Stop conditionIicDelayService
(IIC_DEL_WRITE);
result_now=YES;
return(result_now);
}
unchar SeqReadFrom24c256(unchar sla_add,unint addr_op,unchar read_size,unchar *read_buf)
{//addr “roll over” during read:from last byte of the last page, to the first byte of the first page
unchar result_now,temp_data,read_count;
IicStartBitSend(); //Start condition
temp_data=sla_add; //Slave device address
result_now=IicSendByteService(temp_data);
t_now==NO) return(result_now);
temp_data=addr_op>>8; //The upper 8 bits of the operation unit address
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op; //The lower 8 bits of the operation unit address
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
IicStartBitSend();
temp_data=sla_add+1; //Read operation
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
for(read_count=0 ;read_count
{ //Continuously read data
*(read_buf+read_count)=IicAccByteService();
IicAckService(NO);
}
*(read_buf+read_count)=IicAccByteService();
IicAckService(YES);
IicStopBitSend( );
result_now=YES;
return(result_now);
}
unchar WritePageTo24c256(unchar sla_add,unint addr_op,unchar *write_data_buf)
{//Page write
unchar count,result_now,temp_data;
IicStartBitSend(); //Start condition
temp_data=sla_add; //Slave device address
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op>>8; //Operation unit address high 8-bit
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op; //Operation unit address low 8 bits
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now );
for(count=0;count
{//Continuous writing
temp_data=*(write_data_buf+count);
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
}
IicStopBitSend( ); //Stop condition
IicDelayService(IIC_DEL_WRITE);
result_now=YES;
return(result_now);
}
unchar SeqWriteTo24c256ByPage(unchar sla_add,unint addr_op,unchar write_size,unchar *write_buf)
{//addr “roll over” during write:from last byte of the current page to first byte of the same page.
unint page_write,read_addr,temp_op_int;
unchar data_count,result_out,modify_count,count,write_data_buf[PAGE_CAP_BYTE];
result_out=YES;
data_count=0;
while(write_size>0)
{
page_write=addr_op /PAGE_CAP_BYTE; //Get the current page
read_addr=page_write*PAGE_CAP_BYTE;
SeqReadFrom24c256(sla_add,read_addr,PAGE_CAP_BYTE,write_data_buf);
temp_op_int=addr_op&(PAGE_CAP_BYTE-1); //Get the starting byte address in the pageif
(temp_op_int+write_size>=PAGE_CAP_BYTE)
{
modify_count=PAGE_CAP_BYTE;
addr_op=(page_write+1)*PAGE_CAP_BYTE; //Write the starting address of the next page
}
else
modify_count=write_size;
count=temp_op_int;
write_size=write_size-modify_count+count; //Write the amount of data
for the next pagefor(;count
write_data_buf[count]=*(write_buf+data_count);
result_out=WritePageTo24c256(sla_add,read_addr,write_data_buf);
}
return(result_out);
}
unchar SeqWriteTo24c256(unchar sla_add,unint addr_op,unchar write_size,unchar *write_buf)
{//Continuous write (non-page write)
unchar write_result;
if(write_size<3)
{//If the data to be written is less than 3, use random write to implement
write_result=RandWriteByteTo24c256(sla_add,addr_op,*write_buf);
write_result=RandWriteByteTo24c256(sla_add,addr_op+1,*(write_buf+1));
}
else
write_result=SeqWriteTo24c256ByPage(sla_add,addr_op,write_size,write_buf);
return(write_result);
}
Keywords:24c256
Reference address:24c256 operation function, continuous read and write
The function actually called by other operations is:
unchar SeqWriteTo24c256(unchar sla_add,unint addr_op,unchar write_size,unchar *write_buf);
unchar SeqReadFrom24c256(unchar sla_add,unint addr_op,unchar read_size,unchar *read_buf);
The others are functions called by this file!!
//************************************************************ Debug function
void DebugEepromService(void)
{
unchar debug_buf[255];
debug_buf[0]=0x91;
debug_buf[1]=0x92;
debug_buf[2]=0x93;
debug_buf[3]=0x94;
debug_buf[4]=0x95;
debug_buf[250]=0x05;
debug_buf[251]=0x06;
debug_buf[252]=0x07;
debug_buf[253]=0x08;
debug_buf[254]=0x09;
SeqWriteTo24c256(EEP1_ADDR,1,255,debug_buf);
SeqReadFrom24c256(EEP1_ADDR,1,255,debug_buf);
}
//************************************************* ************
#define IIC_SDA_PB 0x20
#define IIC_SCL_PB 0x80
#define IIC_DEL_WAIT 0x10 //>4.7us(12.80us) for Fre=11.0592M
#define IIC_DEL_WRITE 0x2700 //>6ms(7266.54us=7.266ms) for Fre=11.0592M
#define EEP1_ADDR 0xa4
#define PAGE_CAP_BYTE 64 //24C256 page write capacity: 64 bytes
/*
Function function file
2005-9-22 9:54 by xth
Version: v1.0
--------------------------------------------
Mcu: avr mega32 Frequency: 11.0592M
--------------------------------------------
Function overview: Eeprom operation file
--------------------------------------------
*/
//============================== Function declaration
//----------IIC operation call function
void IicDelayService(unint delay_time);
void IicStartBitSend(void);
void IicStopBitSend(void);
void IicAckService(unchar ack_data);
unchar IicSendByteService(unchar tx_data);
unchar IicAccByteService(void);
//----------At24c256 operation function
unchar RandWriteByteTo24c256(unchar sla_add,unint addr_op,unchar data_op);
unchar WritePageTo24c256(unchar sla_add,unint addr_op,unchar *write_data_buf);
unchar SeqWriteTo24c256ByPage(unchar sla_add,unint addr_op,un char write_size,unchar *write_buf);
unchar SeqWriteTo24c256(unchar sla_add,unint addr_op,unchar write_size,unchar *write_buf);
unchar SeqReadFrom24c256(unchar sla_add,unint addr_op,unchar read_size,unchar *read_buf);
//=============================Function definition
void IicDelayService(unint delay_count)
{
unint count;
for(count=0;count
}
void IicStartBitSend(void)
{
PORTB |= IIC_SCL_PB; //Send the clock signal of the start condition
asm("NOP");
PORTB |= IIC_SDA_PB; //The start condition establishment time is greater than 4.7us, delay
IicDelayService(IIC_DEL_WAIT);
PORTB &= ~IIC_SDA_PB;
IicDelayService(IIC_DEL_WAIT);
PORTB &= ~IIC_SCL_PB; //Clamp the I2C bus and prepare to send or receive data
asm("NOP");
}
void IicStopBitSend(void)
{
PORTB &= ~IIC_SDA_PB;//Send the clock signal of the end condition
IicDelayService(IIC_DEL_WAIT);
PORTB |= IIC_SCL_PB; //The end condition establishment time is greater than 4μs
IicDelayService(IIC_DEL_WAIT);
PORTB |= IIC_SDA_PB;
asm("NOP");
}
void IicAckService(unchar ack_data)
{//As the master device response->send response or non-response signal
if(ack_data==0) PORTB &= ~IIC_SDA_PB;
else PORTB |= IIC_SDA_PB;
IicDelayService(IIC_DEL_WAIT);
PORTB |= IIC_SCL_PB;
IicDelayService(IIC_DEL_WAIT);
PORTB &= ~IIC_SCL_PB;//Clear the clock line and clamp the I2C bus to continue receiving
asm("NOP");
}
unchar IicSendByteService(unchar tx_data)
{//Send the byte, which can be an address or data. After sending, wait for the response and return
unchar bit_count, ack_flag;
for(bit_count=0;bit_count<8;bit_count++)
{
if((tx_data<
else
PORTB &= ~IIC_SDA_PB;
IicDelayService(IIC_DEL_WAIT);
PORTB |= IIC_SCL_PB; //Set the clock line high to notify the controlled device to start receiving data bits
IicDelayService(IIC_DEL_WAIT); //Ensure that the clock high level period is greater than 4μs
PORTB &= ~IIC_SCL_PB;
}
IicDelayService(IIC_DEL_WAIT);
PORTB &= ~IIC_SDA_PB;
DDRB &= ~IIC_SDA_PB; //Set SDA to input
asm("NOP");
PORTB |= IIC_SCL_PB;
IicDelayService(IIC_DEL_WAIT);
IicDelayService(IIC_DEL_WAIT);
if(PINB&IIC_SDA_PB) //Judge whether the response signal is received
ack_flag=NO;
else
ack_flag=YES; //There is a response signal
DDRB |= IIC_SDA_PB;
PORTB &= ~IIC_SCL_PB;
asm("NOP");
return(ack_flag);
}
unchar IicAccByteService(void)
{//Receive data from the device and judge the bus error
unchar bit_count, get_data;
DDRB &= ~IIC_SDA_PB;
get_data=0;
for(bit_count=0;bit_count<8;bit_count++)
{
asm("NOP");
PORTB &= ~IIC_SCL_PB; //Set the clock line low and prepare to receive data bits
IicDelayService(IIC_DEL_WAIT); //Clock low level period is greater than 4.7μs;
PORTB |= IIC_SCL_PB; //Set the clock line high to make the data on the data line valid
get_data<<=1;
if(PINB&IIC_SDA_PB)
get_data++;
asm("NOP");
asm("NOP");
}
PORTB &= ~IIC_SCL_PB;
DDRB |= IIC_SDA_PB;
asm("NOP");
return(get_data);
}
unchar RandWriteByteTo24c256(unchar sla_add,unint addr_op,unchar data_op)
{
unchar result_now,temp_data;
IicStartBitSend(); //Start condition
temp_data=sla_add; //Slave device addressresult_now
=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op>>8; //Operation unit address high 8
bitsresult_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op; //Operation unit address low 8
bitsresult_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=data_op; //Operation dataresult_now
=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
IicStopBitSend(); //Stop conditionIicDelayService
(IIC_DEL_WRITE);
result_now=YES;
return(result_now);
}
unchar SeqReadFrom24c256(unchar sla_add,unint addr_op,unchar read_size,unchar *read_buf)
{//addr “roll over” during read:from last byte of the last page, to the first byte of the first page
unchar result_now,temp_data,read_count;
IicStartBitSend(); //Start condition
temp_data=sla_add; //Slave device address
result_now=IicSendByteService(temp_data);
t_now==NO) return(result_now);
temp_data=addr_op>>8; //The upper 8 bits of the operation unit address
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op; //The lower 8 bits of the operation unit address
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
IicStartBitSend();
temp_data=sla_add+1; //Read operation
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
for(read_count=0 ;read_count
*(read_buf+read_count)=IicAccByteService();
IicAckService(NO);
}
*(read_buf+read_count)=IicAccByteService();
IicAckService(YES);
IicStopBitSend( );
result_now=YES;
return(result_now);
}
unchar WritePageTo24c256(unchar sla_add,unint addr_op,unchar *write_data_buf)
{//Page write
unchar count,result_now,temp_data;
IicStartBitSend(); //Start condition
temp_data=sla_add; //Slave device address
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op>>8; //Operation unit address high 8-bit
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
temp_data=addr_op; //Operation unit address low 8 bits
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now );
for(count=0;count
temp_data=*(write_data_buf+count);
result_now=IicSendByteService(temp_data);
if(result_now==NO) return(result_now);
}
IicStopBitSend( ); //Stop condition
IicDelayService(IIC_DEL_WRITE);
result_now=YES;
return(result_now);
}
unchar SeqWriteTo24c256ByPage(unchar sla_add,unint addr_op,unchar write_size,unchar *write_buf)
{//addr “roll over” during write:from last byte of the current page to first byte of the same page.
unint page_write,read_addr,temp_op_int;
unchar data_count,result_out,modify_count,count,write_data_buf[PAGE_CAP_BYTE];
result_out=YES;
data_count=0;
while(write_size>0)
{
page_write=addr_op /PAGE_CAP_BYTE; //Get the current page
read_addr=page_write*PAGE_CAP_BYTE;
SeqReadFrom24c256(sla_add,read_addr,PAGE_CAP_BYTE,write_data_buf);
temp_op_int=addr_op&(PAGE_CAP_BYTE-1); //Get the starting byte address in the pageif
(temp_op_int+write_size>=PAGE_CAP_BYTE)
{
modify_count=PAGE_CAP_BYTE;
addr_op=(page_write+1)*PAGE_CAP_BYTE; //Write the starting address of the next page
}
else
modify_count=write_size;
count=temp_op_int;
write_size=write_size-modify_count+count; //Write the amount of data
for the next pagefor(;count
result_out=WritePageTo24c256(sla_add,read_addr,write_data_buf);
}
return(result_out);
}
unchar SeqWriteTo24c256(unchar sla_add,unint addr_op,unchar write_size,unchar *write_buf)
{//Continuous write (non-page write)
unchar write_result;
if(write_size<3)
{//If the data to be written is less than 3, use random write to implement
write_result=RandWriteByteTo24c256(sla_add,addr_op,*write_buf);
write_result=RandWriteByteTo24c256(sla_add,addr_op+1,*(write_buf+1));
}
else
write_result=SeqWriteTo24c256ByPage(sla_add,addr_op,write_size,write_buf);
return(write_result);
}
Previous article:74HC164 key scan + display example
Next article:Transplant ICCAVR to WINAVR to play the first song (music.c)
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
Guess you like
- 28335 memory space
- Circuit Learning
- [Silicon Labs BG22-EK4108A Bluetooth Development Review] Bluetooth Protocol Basics
- GD32e231 ADC acquisition
- A preliminary study of k210 - MixNo - graphical programming
- 【i.MX6ULL】Driver Development 6——Pinctrl subsystem and GPIO subsystem light up LED
- Audi A6 computer version internal structure analysis, please come and teach me
- Excellent Works of the National College Student Electronic Design Competition - Smart Car Album
- Tektronix RF Communication Laboratory Innovation Experiment Platform
- What is the use of the C language comments in the figure?