原创 带SPI接口的EEPROM 25aa080a的读写操作

2009-8-12 16:30 3518 4 4 分类: MCU/ 嵌入式

https://static.assets-stash.eet-china.com/album/old-resources/2009/8/12/b137c898-558d-4ee6-b394-94f11eb65143.rar


/***********************************/
/*          EEPROM主文件                            */
/*          芯片25aa080a                                */
/*            2009.08.11                                   */
/**********************************/


#include <pic.h>
#include <pic1687x.h>
#include "25aa080a.h"
unsigned char SendData[16]={15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
unsigned char ReceiveData[16];
void delay(unsigned int k)  //k ms延时
{
 unsigned char j;
 for(;k>0;k--)
 for(j=106;j>0;j--);
}
void sys_initial()
{
 ADCON1=0x07;
 TRISA = 0x00;
 TRISC = 0x10;
 SSPSTAT=0x00;
 SSPCON=0x30;
}
main()
{
 unsigned char i;
 sys_initial();
 WRStatus();
 WRDIS();
 while(1)
 {
  for(i=0;i<16;i++)
  {
  // WriteFlash(i,0x00,SendData);
   delay(30);
   ReceiveData=ReadFlash(i,0x00);
   delay(30);
  }
 }
 }


= * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = *


以下是.h文件


/***********************************/
/*          EEPROM头文件.h         */
/*          芯片25aa080a           */
/*            2009.08.11           */
/***********************************/
#define WRSR  0x01       //写状态寄存器
#define WRITE 0x02       //写指令
#define READ  0x03       //读指令
#define WRDI  0x04       //禁止写
#define RDSR  0x05       //读状态寄存器
#define WREN  0x06       //写使能
#define CS RA0          //片选


extern void WRStatus();
extern void WRDIS();
extern unsigned char ReadFlash(unsigned char Adr_L,unsigned char Adr_H);
extern void WriteFlash(unsigned char Adr_L,unsigned char Adr_H,unsigned char BYTE);


= * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = * = *


以下是函数文件


/***********************************/
/*          EEPROM头函数文件       */
/*          芯片25aa080a           */
/*            2009.08.11           */
/***********************************/
#include <pic.h>
#include <pic1687x.h>
#include "25aa080a.h"
void Send_Command(unsigned char CMD)
{
 SSPBUF=CMD;
 while(!SSPIF);
 SSPIF=0;
}
void WRENS()
{
 CS=0;
 Send_Command(WREN);
 CS=1;
}
void WRStatus()
{
 WRENS();
 CS=0;
 Send_Command(WRSR);
 Send_Command(0x00);
 CS=1;
}
void WRDIS()
{
 CS=0;
 Send_Command(WRDI);
 CS=1;
}
 
unsigned char ReadFlash(unsigned char Adr_L,unsigned char Adr_H)
{
 unsigned char temp;
 CS=0;
 Send_Command(READ);
 Send_Command(Adr_H);
 Send_Command(Adr_L);
 Send_Command(0);
 temp=SSPBUF;
 CS=1;
 return temp;
}
void WriteFlash(unsigned char Adr_L,unsigned char Adr_H,unsigned char BYTE)
{
 WRENS();
 CS=0;
 Send_Command(WRITE);
 Send_Command(Adr_H);
 Send_Command(Adr_L);
 Send_Command(BYTE);
 CS=1;
}

文章评论0条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /2 下一条