×

24c512/24c256的读写程序

消耗积分:10 | 格式:rar | 大小:666 | 2008-08-13

时见栖鸦

分享资料个


#include "msp430x13x.h"
     
void nop(void)
{
  _NOP();
  _NOP();
  _NOP();
}

void start(void)     
{
  SCLEN &= (~(SDA+SCL)); 
  _NOP();
  _NOP();
  _NOP(); 
  SDAEN |= SDA;   
  SCLEN |= SCL;      
  return;
}
void stop(void)
{
  SDAEN |= SDA;          
  SCLEN &= (~SCL);  
  SDAEN &= (~SDA);  
  return;
}


unsigned char ControlByte(int AddressByte)
{
  unsigned char temp;

  _BIC_SR(0X0001);  //CLEAR SR 'C'
  temp = (char)(AddressByte>>8);
  temp <<= 1 ;
  temp &= 0X0E;
  temp += Code;
  return temp;
}


int shout( unsigned char data ) 
{
 unsigned char mask,i,temp;
 unsigned char Value;

 Value = data;
 mask=0X80;

 for (i=0;i<=7;i++){

      temp = (Value & mask);       
      if( temp !=0 ){  
          SDAEN &=  (~SDA); 
          SCLEN &=  (~SCL); 
         if( !( SDAIN & SDA ) ){
            return 1;
          }
      }
      else {
        SDAEN |= SDA ;   
        SCLEN &= (~SCL); 
      }
   
    mask>>=1;

    SCLEN |= SCL;    
 }
  SDAEN &= (~SDA);
  SCLEN &= (~SCL);

  if ( !(SDAIN & SDA )){
     // return 1;
  }
 
  SCLEN |= SCL;
  return 0;
}

// 写 Number 个字节到 E2 中
int WriteBlock(int number,unsigned char *dataadr,unsigned char *e2adr)
{
  unsigned char *p1;
  int i;
  unsigned char temp;
  unsigned int aaa;
  //unsigned char *temp1;
 
  _DINT();   
  SDADAT &= (~(SCL+SDA)); 
      
  start();                     

  aaa = (int)(e2adr );
  temp = ControlByte(aaa);
  shout(temp);

  shout( (int)(e2adr));

  p1 = dataadr;

  for (i=1;i<=8;i++) {

     shout(*p1);
     p1++;
  }
  stop(); 
  _EINT(); 
 
//Write a block use the address and back..
 return 0;             //if error return 1  right return 0
}
// 写N * 8个字节
int WriteN8Byte(int Number)
{
  unsigned char *e2adr,*dataadr;
  unsigned int i,j;

//int WriteBlock(int number,unsigned char *dataadr,unsigned char *e2adr)
  dataadr = (char*)0x200;
  e2adr = (char*)0x00;

  for( i=1;i<=Number;i++){
 
    WriteBlock(8,dataadr,e2adr);
    dataadr += 8; 
    e2adr += 8;   
    for (j=0;j<=3333;j++);           
   
  }

  return 0;
}

//; Clock out an acknowledge bit (low).
//; SCL expected low on entry. Return with SCL, SDA low.
void Ack(void)
{
  SDAEN |= SDA;
  SCLEN &= ~SCL;
  SCLEN |= SCL;
 
}
//; Clock out a negative acknowledge bit (high).
//; SCL expected low on entry. Return with SCL low, SDA high.
void Nak(void)
{
  SDAEN &= ~SDA;                    //; NAK bit
  SCLEN &= ~SCL;                    //; raise clock
  SCLEN |= SCL;                     //; drop clock

}
//由E2 中 读8位 一个字节 数据
unsigned char shin(void)
{
  int i;
  unsigned char temp;
  unsigned char Mask;

  SDAEN &= ~SDA;                  // make SDA an input
  temp =0;

//读8位数据
  for( i=1;i<=8;i++) {
   
    SCLEN &= ~SCL;                //SCL = H
    Mask = 0;

    if (SDAIN &SDA)
      Mask = 1;
    else
      Mask =0;
//    Mask = SDAIN & SDA;
    Mask <<= (8 - i);
    temp ^= Mask;
    SCLEN |= SCL;   
  }

  return temp;
 }
// 24c256 512 页 64字节 14-15bit Word Address!!!
//**********************************************************************
//  读取N 个字节 到RAM 中
//  参数说明: number 读取的个数,dataadr,读到RAM 的地址?e2adr,E2地址
//**********************************************************************
int  ReadNByte(int number,unsigned char *dataadr,unsigned char *e2adr)
{
 int  i;             
 unsigned char *p; 
 unsigned char temp;             
 unsigned char temp1;
 
  _DINT();   
 SDADAT &= (~(SCL+SDA));
 
 p=dataadr;   

 start();   

  temp = ControlByte((int)(e2adr ));
  shout(temp);    
 
  shout((int)(e2adr ));        

  start();

  temp |= 0X01;          
  shout(temp);         

  temp1=0;
  for (i=1;i<=number;i++){              
    temp1 = shin();
    *p++ = temp1;

    Ack();
  }
 Nak();
 stop();
 _EINT();

 return 0;
}

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(1)
发评论
chenkeqing 2015-08-03
0 回复 举报
我非常感谢楼主 楼主是好人啊!!!~啊~~~ 收起回复

下载排行榜

全部1条评论

快来发表一下你的评论吧 !