Board logo

标题: XDT512的片内I2C求助(续)[讨论][求助] [打印本页]

作者: geathem    时间: 2006-6-3 14:26     标题: XDT512的片内I2C求助(续)[讨论][求助]

以下是中断函数
/************************************************************/
#pragma CODE_SEG NON_BANKED


interrupt void I2C_ISR(void){
  I2C_status|=(1<<1);//clear interrupt flag
  switch(I2C_control&0x30|re_send_sladd) {
    case master_write:{ //master,write
      if(byte_write_conter==8){//last byte has been transmitted
        I2C_stop;
        new_inner_page=1;//tell the ISR to access a uper new page nextime
        byte_write_conter=0;
        if(i2c_page_counter==32){//all pages have been written to
          i2c_page_counter=0;
          eep_inner_page_ptr=eep_inner_page_add;//page pointer recover
          i2c_step=2;//tell the loop to start read thread
        }else{
          i2c_page_counter++;
          i2c_step=0;//tell the loop to start a new 8 bytes write thread
        }
      }
      else{//it is not the last byte
        if(I2C_status&0x01==1){  //no acknowledge received
          I2C_stop;//generate STOP signal
          //or generate RE-START signal 
        }
        else{ //received acknowledge
          if(new_inner_page==1){//a new page have to be written
            new_inner_page=0;
            I2C_data=*eep_inner_page_ptr++;
          }
          else{//write to the same page
            byte_write_conter++;
            I2C_data=*str_ptr++;
          }
        }
      }//end of "it is not the last byte"
    }break;
    case master_read:{ //master,receive
      if((byte_write_conter==8)&&(i2c_page_counter==32)) {//last byte to be read
        I2C_stop;//generate STOP signal 
      }
      else if((byte_write_conter==7)&&(i2c_page_counter==32)){//the second last byte to be read
        I2C_control|=(1<<3);//set TXAK=1,not acknowledge the slave 
      }
      if(byte_write_conter!=8) {
        byte_write_conter++; 
      } else{
        byte_write_conter=0;
        if(i2c_page_counter==32){ //the received byte reach to the last
          i2c_page_counter=0;
          receive_str_ptr=i2c_receive_str;//the receive pointer recover 
          i2c_step=0;//restart to loop case 0,master transmiter
        }else{
          i2c_page_counter++;
        }
      }
      *receive_str_ptr++=I2C_data;
    }break;
    case master_r_select_add:{//module in master receive,to select the address to receive
      if(I2C_status&0x01==1){ //no acknowledge received
        I2C_stop;//generate STOP signal 
      }
      else{
        switch(byte_write_conter){
          case 0:{
            byte_write_conter=1;
            I2C_data=*eep_inner_page_ptr;//now send the E2PROM inner address to read out
          }break;
          default:{
            byte_write_conter=0;
            re_send_sladd=2;//the two lines of code make the loop enter precedure of
            i2c_step=2;    //"generate re-START,send slave address;master read"
          }
        }//end of switch
      }
    }break;
    default:{//error condition,wait here
      for(;;);
    }
  }//end of switch
}//end of I2C_ISR


作者: geathem    时间: 2006-6-3 14:30     标题: 情大家帮忙![讨论]

谢谢




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0