24c256 operation function, continuous read and write

Publisher:ZenMaster123Latest update time:2016-10-29 Source: eefocusKeywords:24c256 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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

Previous article:74HC164 key scan + display example
Next article:Transplant ICCAVR to WINAVR to play the first song (music.c)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号