首 页文档资料下载资料维修视频包年699元
请登录  |  免费注册
当前位置:精通维修下载 > 文档资料 > 家电技术 > 单元电路介绍 > 其它电路
AT93C46 SPI接口EEPROM读写程序
来源:本站整理  作者:佚名  2011-02-05 21:44:27



//-----------------------函数声明--------------------------------------------------------
#include "reg51.h"
#include "intrins.h"
//-----------------------管脚声明-----www.pICavr.com-------------------------------------
sbit CS_93=P1^0;
sbit CLK_93=P1^1;
sbit DI_93=P1^2;
sbit DO_93=P1^3;
sbit VCC_93=P1^4;
#define READ_c   0x02
#define WRITE_c  0x01
#define ERASE_c  0x03
#define EWEN_c   0x00
#define EWDS_c   0x00
#define ERAL_c   0x00
#define WRAL_c   0x00
//--------------------------------------------------------------------------------------------------
// 函数名称: delay
// 入口参数: N
// 函数功能:延时子程序,实现(16*N+24)us的延时 
// 系统采用11.0592MHz的时钟时,延时满足要求,其它情况需要改动
//--------------------------------------------------------------------------------------------------
  void delay(unsigned  int N)  
  {
  int i; 
  for(i=0;i<N;i++);
  }
//--------------------------------------------------------------------------------------------------
// 函数名称: SEND_1
// 函数功能: 送1位数据"1"
//--------------------------------------------------------------------------------------------------
void SEND_1(void)
      { 
      DI_93=1; 
      CLK_93=1; 
      _nop_(); 
      CLK_93=0;
      _nop_();
      }
//--------------------------------------------------------------------------------------------------
// 函数名称: SEND_0
// 函数功能: 送1位数据"0"
//--------------------------------------------------------------------------------------------------
void SEND_0(void)
        { 
         DI_93=0; 
         CLK_93=1;
         _nop_();
         CLK_93=0;
         _nop_();
         }
//--------------------------------------------------------------------------------------------------
// 函数名称: SEND_0
// 函数功能: 送1位数据"0"
//--------------------------------------------------------------------------------------------------
void start_operation(unsigned char operation_code)
        { 
         CS_93=1; 
          SEND_1();                   //起始位
          if (operation_code&0x02)    //操作码
          SEND_1(); 
          else 
          SEND_0();
          if (operation_code&0x01) 
          SEND_1(); 
          else 
          SEND_0();
         }
//--------------------------------------------------------------------------------------------------
// 函数名称: SEND_BYTE
// 入口参数: dd,待发送的数据
// 函数功能: 送1BYTE数据
//--------------------------------------------------------------------------------------------------
void SEND_BYTE(unsigned char dd)
     { 
      unsigned char i; 
      for (i=0;i<8;i++) 
          {
          if (dd>=0x80) SEND_1(); 
          else SEND_0();
          dd=dd<<1; 
          }
     }
//--------------------------------------------------------------------------------------------------
// 函数名称: RECEIVE_BYTE
// 返回值:   dd,待接收的数据
// 函数功能: 送1BYTE数据
//--------------------------------------------------------------------------------------------------
unsigned char RECEIVE_BYTE(void)
              {
              unsigned char i,dd; 
              DO_93=1;
              for (i=0;i<8;i++) 
              { 
              dd<<=1; 
              CLK_93=1;_nop_();
              CLK_93=0;_nop_(); 
              if (DO_93) dd|=1; 
              } 
              return(dd);
             }
//--------------------------------------------------------------------------------------------------
// 函数名称: EWEN
// 函数功能: 擦/写允许指令
// 捎谠谏系绺次缓螅珹T93CXX首先将处于擦/写不允许状态。
// 故该指令必须在所有编程模式前执行,一旦该指令执行后,
// 只要外部没有断电就可以对芯片进行编程。
//--------------------------------------------------------------------------------------------------
void EWEN(void)
           {
           _nop_(); 
           CS_93=1; 
           start_operation(EWEN_c); 
           SEND_BYTE(0xC0); 
           CS_93=0;
           }
//--------------------------------------------------------------------------------------------------
// 函数名称:EWDS
// 函数功能: 擦写截止
// 使用该指令可对写入的数据进行保护
//--------------------------------------------------------------------------------------------------
void EWDS(void)
           {
           _nop_(); 
           CS_93=1; 
           start_operation(EWDS_c); 
           SEND_BYTE(0x00); 
           CS_93=0;
           }
//--------------------------------------------------------------------------------------------------
// 函数名称: ERAL
// 函数功能: 片擦
// 该指令可将整个存贮器阵列置为1,其它功能与地址擦指令相同
// 要求VCC = 4.5V to 5.5V。
//--------------------------------------------------------------------------------------------------
void ERAL(void)
           {
           _nop_(); 
           CS_93=1; 
           start_operation(ERAL_c); 
           SEND_BYTE(0x80); 
           CS_93=0;
           }
//--------------------------------------------------------------------------------------------------
// 函数名称: READ_BYTE
// 函数功能: 从存储区读一字节数据
//--------------------------------------------------------------------------------------------------
unsigned char READ_BYTE(unsigned char address)
             { 
             unsigned char dat; 
             CS_93=CLK_93=0;
             CS_93=1; 
             start_operation(READ_c); 
             SEND_BYTE(address); 
             dat=RECEIVE_BYTE();
             CS_93=0;
             return(dat);
             }
//--------------------------------------------------------------------------------------------------
// 函数名称: WRITE_BYTE
// 入口参数: address。dat
// 返回值:   1或0,表示成功写入
// 函数功能: 写一字节数据到存储区
//--------------------------------------------------------------------------------------------------
bit WRITE_BYTE(unsigned char address,unsigned char dat)
                {
                 unsigned char e,time,temp=address; 
                  e=0; 
                  while (e<3) 
                   {
                  CS_93=CLK_93=0; 
                  CS_93=1;
                  EWEN();
                  _nop_();
                  CS_93=1;
                  _nop_();
                  start_operation(WRITE_c); 
                  SEND_BYTE(address); 
                  SEND_BYTE(dat); 
                  CS_93=0;
                  _nop_();
                  CS_93=1;
                  time=0;
                  DO_93=1;
                  while (1) 
                  {
                  if (DO_93==1) break;
                  if (time>20) break;
                  time++;
                  } 
                  CS_93=0; 
                 if (READ_BYTE(temp)==dat) 
                    {
                    return(0);
                    }
                    e++;
                    } 
                    return(1);
    }

关键词:

·上一文章:IC卡控制程序
·下一文章:SJA1000 CAN总线程序

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分)
Copyright © 2007-2017 down.gzweix.Com. All Rights Reserved .
页面执行时间:286,425.80000 毫秒