1. 首页
  2. 课程学习
  3. 嵌入式
  4. 24c512/24c256的读写程序

24c512/24c256的读写程序

上传者: 2023-01-07 14:29:18上传 TXT文件 4.375 KB 热度 6次

#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;

}

用户评论