注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

信息 灵感 创新

III = Information,Inspiration,Innovation

 
 
 

日志

 
 
关于我

we are 5. Mathematics, Computation, Programming, Engineering, and Making fun of life.

网易考拉推荐

25XX08驱动程序  

2017-08-28 17:05:31|  分类: C51(C&ASM) |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

之前一直在研究IIC总线,支持这一总线的的EEPROM——AT24CXX十分常见,也写过了不少代码并且仿真和开发板上都测试OK了,最近想学习一下SPI总线的基础知识,恰好Proteus下有25XX08这种EEPROM,支持SPI总线,但网上并不能找到详细说明的资料,干脆自己查阅DataSheet,根据时序和说明,慢慢鼓捣出来的。

概述

25XX08A/BXX通常是AA或者LC)是采用SPI总线的EEPROM,容量达到8Kbit1024Byte=1KB),AA型正常工作范围是-40℃85℃,而LC型号则达到了-40℃125℃A型和B型则是页面大小不同,A型为16字节每页,而B型为32字节每页。例如25AA08B表示工作温度范围在-40℃85℃,单页为16字节的EEPROM

引脚说明

SI/SO,数据的输入和输出端口,这是从工作器件的角度来看的。

CS:片选端口,如果需要25XX08X工作,则应该使其为低电平。

WP:写保护,WP=0时,不接受数据输入。

HOLDHOLD=0将暂停数据传输。

因此,如果想要正确读写数据,则CS一定要保持低电平,而HOLD必须为高电平,并且在写入数据的时候,使WP为高电平关闭写保护。

驱动程序

25AA08.h头文件:

#include <reg51.h>
#include <intrins.h>
//Pre-defined Instruction Set
#define READ    0x03//Read data from memory array beginning at selected address
#define WRITE   0x02//Write data to memory array beginning at selected address
#define WRDI    0x04//Reset the write enable latch (disable write operations)
#define WREN    0x06//Set the write enable latch (enable write operations)
#define RDSR    0x05//Read Status Register
#define WRSR    0x01//Write Status Register

sbit SCLK=P2^0;//Serial CLOCK
sbit SI=P2^1;//Serial IN, MOSI
sbit SO=P2^2;//Serial OUT, MISO
sbit CS=P2^3;//CHip Select,Or SS
sbit HOLD=P2^6;//HOLD Input
sbit WP=P2^7;//Write Protect

void Delay5ms_25AA08();

void Init_25AA08();

//MCU Send Byte to 25AA08
void SendByte_25AA08(unsigned char dat);


//MCU Receive Byte From 25AA08
unsigned char ReceiveByte_25AA08();

//Write a Byte to a given Address
void WriteByte_25AA08(unsigned int Addr,unsigned char dat);

//Read a Byte from a given Address
unsigned char ReadByte_25AA08(unsigned int Addr);

//Page Write, for 25AA08A, 16 Bytes per Page, for 25AA08B, 32 Bytes per Page
//Note: if reach page boundary,it will wrap around.
void PageWrite_25AA08(unsigned int Addr,unsigned char* buffer,unsigned char n);

//Page Read, for 25AA08A, 16 Bytes per Page, for 25AA08B, 32 Bytes per Page
//Stored in buffer
void PageRead_25AA08(unsigned int Addr,unsigned char* buffer,unsigned char n);

然后是25AA08.c源文件:
#include "25AA08.h"

//delay 5ms @ 12MHz
void Delay5ms_25AA08()
{
    #pragma ASM
    MOV A,R7
    PUSH ACC
    MOV A,R6
    PUSH ACC
    MOV A,R5
    PUSH ACC
    MOV R7,#003H
DL2:
    MOV R6,#008H
DL1:
    MOV R5,#066H
DL0:
    DJNZ R5,DL0
    DJNZ R6,DL1
    DJNZ R7,DL2
    POP ACC
    MOV R5,A
    POP ACC
    MOV R6,A
    POP ACC
    MOV R7,A
    #pragma ENDASM
}

//Initialization 25AA08
void Init_25AA08()
{
    CS=1;//Do NOT Select Chip, Only Select it before Operation
    HOLD=1;//release HOLD
    WP=1;//allow to write
}

//MCU Send Byte to 25AA08
//Note:After calling this function,CS remain LOW
void SendByte_25AA08(unsigned char dat)
{
    unsigned char i;
    CS=0;
    for(i=0;i<8;i++)
    {
        SCLK=0;
        SI=(dat & 0x80)>>7;
        SCLK=1;
        dat<<=1;
    }
}

//MCU Receive Byte From 25AA08
//Note:After calling this function,CS remain LOW
unsigned char ReceiveByte_25AA08()
{
    unsigned char i,dat=0;
    CS=0;
    for(i=0;i<8;i++)
    {
        SCLK=0;
        dat<<=1;
        SCLK=1;
        if(SO)
            dat|=0x01;
    }
    return dat;
}

//Check 25AA08 WIP
//Write In Progress
//return value:
//1: write in progress
//0: no write in progress
bit CheckWIP_25AA08()
{
    unsigned char dat;
    SendByte_25AA08(RDSR);
    dat=ReceiveByte_25AA08();
    if(dat & 0x01)
        return 1;
    else
        return 0;
}

void WriteByte_25AA08(unsigned int Addr,unsigned char dat)
{
    unsigned char addrH,addrL;
    while(CheckWIP_25AA08());
    addrH=(unsigned char)(Addr>>8);
    addrL=(unsigned char)(Addr&0xff);
    SendByte_25AA08(WREN);
    CS=1;//Should be HIGH to Set Write Enable Latch
    SendByte_25AA08(WRITE);
    SendByte_25AA08(addrH);
    SendByte_25AA08(addrL);
    SendByte_25AA08(dat);
    CS=1;
    Delay5ms_25AA08();//A Write Cycle need 5ms at least
}

//Read a Byte from a given Address
unsigned char ReadByte_25AA08(unsigned int Addr)
{
    unsigned char addrH,addrL,dat=0;
    addrH=(unsigned char)(Addr>>8);
    addrL=(unsigned char)(Addr&0xff);
    SendByte_25AA08(READ);
    SendByte_25AA08(addrH);
    SendByte_25AA08(addrL);
    dat=ReceiveByte_25AA08();
    CS=1;
    return dat;    
}

//Page Write, for 25AA08A, 16 Bytes per Page, for 25AA08B, 32 Bytes per Page
void PageWrite_25AA08(unsigned int Addr,unsigned char* buffer,unsigned char n)
{
    unsigned char addrH,addrL,i;
    while(CheckWIP_25AA08());
    addrH=(unsigned char)(Addr>>8);
    addrL=(unsigned char)(Addr&0xff);
    SendByte_25AA08(WREN);
    CS=1;//Should be HIGH to Set Write Enable Latch
    SendByte_25AA08(WRITE);
    SendByte_25AA08(addrH);
    SendByte_25AA08(addrL);
    for(i=0;i<n;i++)
        SendByte_25AA08(buffer[i]);
    CS=1;
    Delay5ms_25AA08();
}

//Page Read, for 25AA08A, 16 Bytes per Page, for 25AA08B, 32 Bytes per Page
//Stored in buffer
void PageRead_25AA08(unsigned int Addr,unsigned char* buffer,unsigned char n)
{
    unsigned char addrH,addrL,i=0;
    addrH=(unsigned char)(Addr>>8);
    addrL=(unsigned char)(Addr&0xff);
    SendByte_25AA08(READ);
    SendByte_25AA08(addrH);
    SendByte_25AA08(addrL);
    for(i=0;i<n;i++)
        buffer[i]=ReceiveByte_25AA08();
    CS=1;
}
供参考。
  评论这张
 
阅读(20)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017