一、硬件连接
MB85RS64连接MSP430F5529 P4口,SPI时序IO口模拟
#include"msp430f5529.h"
#define MB85RS64_PORT P4OUT
#define MB85RS64_CS BIT0
#define MB85RS64_WP BIT5
#define MB85RS64_SCK BIT3
#define MB85RS64_SI BIT2
#define MB85RS64_SO BIT1
#define SET_MB85RS64_CS MB85RS64_PORT|=MB85RS64_CS
#define SET_MB85RS64_WP MB85RS64_PORT|=MB85RS64_WP
#define SET_MB85RS64_SCK MB85RS64_PORT|=MB85RS64_SCK
#define SET_MB85RS64_SI MB85RS64_PORT|=MB85RS64_SI
#define SET_MB85RS64_SO MB85RS64_PORT|=MB85RS64_SO
#define CLR_MB85RS64_CS MB85RS64_PORT&=~MB85RS64_CS
#define CLR_MB85RS64_WP MB85RS64_PORT&=~MB85RS64_WP
#define CLR_MB85RS64_SCK MB85RS64_PORT&=~MB85RS64_SCK
#define CLR_MB85RS64_SI MB85RS64_PORT&=~MB85RS64_SI
#define CLR_MB85RS64_SO MB85RS64_PORT&=~MB85RS64_SO
#define Read_MB85RS64_SO (P4IN&MB85RS64_SO)==MB85RS64_SO
不能将WP 写保护脚接地。
硬件上直接把WP写保护直接上拉,通过配置状态寄存器来实现写保护功能
状态寄存器中相关保护位配置,如下图所示
二、低层驱动程序
SCK管脚说明,SCLK上升沿写,下降沿读
unsigned char Read(void)
{
unsigned char i=0,data=0;
CLR_MB85RS64_SCK;
for(i=0;i<8;i++)
{
data<<=1;
SET_MB85RS64_SCK;
__delay_cycles(15);
if(Read_MB85RS64_SO)
{data|=1;}
CLR_MB85RS64_SCK;
__delay_cycles(5);
}
return data;
}
void Write(unsigned char data)
{
unsigned char i=0;
CLR_MB85RS64_SCK;
for(i=0;i<8;i++)
{
if((data&0x80)==0x80)
{SET_MB85RS64_SI;}
else
{CLR_MB85RS64_SI;}
data<<=1;
SET_MB85RS64_SCK;
__delay_cycles(15);
CLR_MB85RS64_SCK;
}
}
三、寄存器读写
MB85RS64含有6个命令寄存器
//寄存器相关
#define MB85RS64_WREN 0x06//Set Write Enable Latch 0000 0110
#define MB85RS64_WRDI 0x04//Reset Write Enable Latch 0000 0100B
#define MB85RS64_RDSR 0x05//Read Status Register 0000 0101B
#define MB85RS64_WRSR 0x01//Write Status Register 0000 0001B
#define MB85RS64_READ 0x03//Read Memory Code 0000 0011B
#define MB85RS64_WRITE 0x02//Write Memory Code 0000 0010B
四、状态寄存器读写
1、 读状态寄存器:先写MB85RS64_RDSR,然后在读状态寄存器。
读状态寄存器时序
//Read Status Register
CLR_MB85RS64_CS;
Write(MB85RS64_RDSR);
stat=Read();
SET_MB85RS64_CS;
2、 写状态寄存器:写使能MB85RS64_WREN,写状态MB85RS64_WRSR,写状态寄存器
如果要对“状态寄存器”进行写设置操作时要注意WEL标志位 ,WEL=1;才可写操作
这里就要使用 WREN 命令使能写操作
写使能时序
写状态寄存器时序
//Set Write Enable Latch
CLR_MB85RS64_CS;
Write(MB85RS64_WREN);
SET_MB85RS64_CS;
//Write Status Register
CLR_MB85RS64_CS;
Write(MB85RS64_WRSR);
Write(0x00);
SET_MB85RS64_CS;
//Read Status Register
CLR_MB85RS64_CS;
Write(MB85RS64_RDSR);
stat=Read();
SET_MB85RS64_CS;
五、读写数据
读时序:连续读,地址自动累加
写时序
void Write_data(unsigned int address,unsigned char * data,unsigned int number)
{
unsigned int temp_address=0;
unsigned char i=0;
//Set Write Enable Latch
CLR_MB85RS64_CS;
Write(MB85RS64_WREN);
SET_MB85RS64_CS;
temp_address=address&0x1FFF;
//Write Memory Code
CLR_MB85RS64_CS;
Write(MB85RS64_WRITE);
Write((temp_address>>8)&0xFF);
Write(temp_address&0xFF);
for(i=0;i
{
Write(* data);
data++;
}
SET_MB85RS64_CS;
}
void Read_data(unsigned int address,unsigned char * data,unsigned int number)
{
unsigned int temp_address=0;
unsigned char i=0;
temp_address=address&0x1FFF;
//Read Memory Code
CLR_MB85RS64_CS;
Write(MB85RS64_READ);
Write((temp_address>>8)&0xFF);
Write(temp_address&0xFF);
for(i=0;i
{
* data=Read();
data++;
}
SET_MB85RS64_CS;
}
六、小测试读写
unsigned char stat=0;
void Test(void)
{
unsigned char rd[8],wr[8];
Init_PORT();
//Set Write Enable Latch
CLR_MB85RS64_CS;
Write(MB85RS64_WREN);
SET_MB85RS64_CS;
//Write Status Register
CLR_MB85RS64_CS;
Write(MB85RS64_WRSR);
Write(0x00);
SET_MB85RS64_CS;
//Read Status Register
CLR_MB85RS64_CS;
Write(MB85RS64_RDSR);
stat=Read();
SET_MB85RS64_CS;
while(1)
{
Read_data(0,&rd[0],8);
wr[0]=0;wr[1]=0;wr[2]=0;wr[3]=0;wr[4]=0;wr[5]=0;wr[6]=0;wr[7]=0;
Write_data(0,&wr[0],8);
Read_data(0,&rd[0],8);
wr[0]=0;wr[1]=1;wr[2]=2;wr[3]=3;wr[4]=4;wr[5]=5;wr[6]=6;wr[7]=7;
Write_data(0,&wr[0],8);
Read_data(0,&rd[0],8);
wr[0]=0x55;wr[1]=0x55;wr[2]=0x55;wr[3]=0x55;wr[4]=0x55;wr[5]=0x55;wr[6]=0x55;wr[7]=0x55;
Write_data(0,&wr[0],8);
Read_data(0,&rd[0],8);
wr[0]=0xAA;wr[1]=0xAA;wr[2]=0xAA;wr[3]=0xAA;wr[4]=0xAA;wr[5]=0xAA;wr[6]=0xAA;wr[7]=0xAA;
Write_data(0,&wr[0],8);
Read_data(0,&rd[0],8);
}
}