技术支持
应用方案
方案需求
Application Solutions
/*---------------------------------------------------------------------------------------------------------
;This test program is writen by tiger ,and is based on MCS-51 system,this program provide the signle byte
;write and read ,and Subsequent byte write and read,user can link this module into target system
;date:2003-09-22
*---------------------------------------------------------------------------------------------------------*/
#include
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define NOP _nop_()
//********************************************************************************
#define WREN_INST 0X06
/* Write enable latch instruction (WREN)*/
#define WRDI_INST 0X04
/* Write disable latch instruction (WRDI)*/
#define WRSR_INST 0X01
/* Write status register instruction (WRSR)*/
#define RDSR_INST 0X05
/* Read status register instruction (RDSR)*/
#define WRITE_INST 0X02
/* Write memory instruction (WRITE)*/
#define READ_INST 0X03
/* Read memory instruction (READ)*/
#define STATUS_REG 0X00
/* Status register,BP1 and BP0 are all zero,no menory write protection*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//***********************************************************************************
//0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55
//0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,0XAA,
// 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8
uchar temp[16];
uchar tempw[16]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
uchar tempr[16];
//**************************************************************************************
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
uchar tempa;
uint number;
uchar numtime;
uchar bdata bbyte;
sbit d0=bbyte^0;
sbit d1=bbyte^1;
sbit d2=bbyte^2;
sbit d3=bbyte^3;
sbit d4=bbyte^4;
sbit d5=bbyte^5;
sbit d6=bbyte^6;
sbit d7=bbyte^7;
//********************************************************************************
sbit DIN=P1^2; //define SI
sbit DOUT=P1^1; //define SO
sbit CLK=P1^3; //define CLK
sbit CSX=P1^0; //define /CS
/*;********************************************************************************
*
;* Name: OUTBYT
;* Description: Sends byte to FM25640
;* Function: This routine shifts out a byte, starting with the MSB, to the FM25040
;* Calls: None
;* Input: outbyte= byte to be sent
;* Outputs: None
;* Notes: The pin is sampled on the rising edge of SCK and is ignored at other times.
;*********************************************************************************
*/
void outbyt(uchar outbyte)
{
bbyte=outbyte;
DIN=d7;CLK=0;CLK=1;
DIN=d6;CLK=0;CLK=1;
DIN=d5;CLK=0;CLK=1;
DIN=d4;CLK=0;CLK=1;
DIN=d3;CLK=0;CLK=1;
DIN=d2;CLK=0;CLK=1;
DIN=d1;CLK=0;CLK=1;
DIN=d0;CLK=0;CLK=1;
//CLK=0;
}
/*;*******************************************************************************
*
;* Name: INPUTBYT
;* Description: Recieves byte from FM25640
;* Function: This routine recieves a byte, MSB first, from the FM25040
;* Calls: None
;* Input: None
;* Outputs: bbyte= recieved byte
;* Notes: Data transitions are driven on the falling edge of the serial clock.
;*********************************************************************************
*/
uchar inputbyt(void)
{
DOUT=1;
CLK=0;CLK=1;d7=DOUT;
CLK=0;CLK=1;d6=DOUT;
CLK=0;CLK=1;d5=DOUT;
CLK=0;CLK=1;d4=DOUT;
CLK=0;CLK=1;d3=DOUT;
CLK=0;CLK=1;d2=DOUT;
CLK=0;CLK=1;d1=DOUT;
CLK=0;CLK=1;d0=DOUT;
return bbyte;
}
/*;*******************************************************************************
*
;* Name: WREN_CMD
;* Description: Set write enable latch
;* Function: This routine sends the command to enable writes to the FRAM memory array or
;* status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;********************************************************************************
*/
void wren_cmd(void)
{
CLK=0;CSX=0;/* Bring /CSX low */
outbyt(WREN_INST);/* Send WREN instruction */
CLK=0;CSX=1;
}
/*;*******************************************************************************
*
;* Name: WRDI_CMD
;* Description: Reset write enable latch
;* Function: This routine sends the command to disable writes to the FRAM memory array or
;* status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;********************************************************************************
*/
void wrdi_cmd(void)
{
CLK=0;CSX=0;/* Bring /CSX low */
outbyt(WRDI_INST);/* Send WRDI instruction */
CLK=0;/* Bring CLK low */
CSX=1;/* Bring /CSX high */
}
/*;********************************************************************************
*
;* Name: RDSR_CMD
;* Description: Read Status Register
;* Function: This routine sends the command to read the status register
;* Calls: outbyt, inputbyt
;* Input: None
;* Outputs: aa= status register
;***********************************************************************************
*/
uchar rdsr_cmd (void)
{
uchar aa;
CLK=0;CSX=0;
outbyt(RDSR_INST);
aa=inputbyt();
CLK=0;CSX=1;
return aa;
}
/*;*******************************************************************************
*
;* Name: WRSR_CMD
;* Description: Write Status Register
;* Function: This routine sends the command to write the BP0 and BP0 FRAM
;* bits in the status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;*******************************************************************************
*/
void wrsr_cmd(uchar status)
{
wren_cmd();
CLK=0;/* Bring CLK low */
CSX=0;/* Bring /CSX low */
outbyt(WRSR_INST) ;/* Send WRSR instruction */
outbyt(status);/* Send status register */
CLK=0;/* Bring CLK low */
CSX=1;/* Bring /CSX high */
}
/*;***************************************************************************
*
;* Name: BYTE_WRITE
;* Description: Single Byte Write
;* Function: This routine sends the command to write a single byte to the FRAM memory array
;* Calls: outbyt
;* Input: aa=The data want to be writen,dd=The address of the menory want to be writen
;* Outputs: None
;******************************************************************************
*/
void byte_write(uint dd,uchar aa)
{
wren_cmd(); /*write enable*/
CLK=0;CSX=0;
outbyt(WRITE_INST);/* Send WRITE instruction including MSB of address */
outbyt((uchar)(dd>>8));
outbyt((uchar)(dd));
outbyt(aa);
CLK=0;CSX=1;
}
//======================================
/*;***************************************************************************
*
;* Name: BYTE_SEQ_WRITE
;* Description: Subsequent Byte Write
;* Function: This routine sends the command to write Subsequent byte to the FRAM memory array
;* Calls: outbyt
;* Input: PTR is the data pointer want to be writen,dd=The address of the menory want to be writen
NUM is the byte number want to be writen to the FRAM menory
;* Outputs: None
;******************************************************************************
*/
void write_seq(uchar *ptr,uint dd,uchar num)
{
uchar i;
wren_cmd(); /*write enable*/
CLK=0;CSX=0;
outbyt(WRITE_INST);/* Send WRITE instruction including MSB of address */
outbyt((uchar)(dd>>8));
outbyt((uchar)(dd));
for(i=0;i{outbyt(*(ptr+i));}
CLK=0;CSX=1;
}
/*;****************************************************************************
*
;* Name: BYTE_READ
;* Description: Single Byte Read
;* Function: This routine sends the command to read a single byte from the FRAM memory array
;* Calls: outbyt, inputbyt
;* Input: dd=The address of the menory want to be readed
;* Outputs: temp= read byte
;*****************************************************************************
*/
uchar byte_read(uint dd)
{
uchar temp;
uchar temph,templ;
temph=(uchar)(dd>>8);
templ=(uchar)(dd);
CLK=0;CSX=0;
outbyt(READ_INST);/* Send READ_INST instruction including MSB of address */
outbyt((uchar)(dd>>8));
outbyt((uchar)(dd));
temp=inputbyt();
CLK=0;CSX=1;
return(temp);
}
//===============================
/*;****************************************************************************
*
;* Name: BYTE_SEQ_READ
;* Description: Subsequent Byte Read
;* Function: This routine sends the command to read Subsequent byte from the FRAM memory array
;* Calls: outbyt, inputbyt
;* Input: PTR is the first address of the saved buffer
dd=The address of the menory want to be readed
NUM is the byte number want to be read from the FRAM menory
;* Outputs: temp= read byte
;*****************************************************************************
*/
void read_seq(uchar *ptr,uint dd,uchar num)
{
uchar i;
uchar temp;
temp=(uchar)dd;
CLK=0;CSX=0;
outbyt(READ_INST);/* Send READ_INST instruction including MSB of address */
outbyt((uchar)(dd>>8));
outbyt((uchar)dd);
for(i=0;i{*(ptr+i)=inputbyt();}
CLK=0;CSX=1;
}
//==========================================================================
void main(void)
{
uint i=0;
wrsr_cmd(0x00);//init the protect bit ,no protect
wren_cmd();
number=0;
numtime=0;
//注意:请将number,temp,tempr加入变量观察窗口,如果内容一致,则存储器正确
//请将对应IO口修改为目标硬件对应的IO口,同时将SLK,SI,SO加上4K7的上拉电阻
while(1)
{
numtime=rdsr_cmd ();
//以下代码测试单字节读写,从地址0开始,连续16个字节
//number是将写入的数据,temp数组中存储读出的值
if(number++>200)
{number=0;}
for(i=0;i<0x1fff;i++)
{
byte_write(i,number);
}
for(i=0;i<0x1fff;i++)
{
numtime=byte_read(i);
if(numtime!=number)
{
while(1){;}
}
}
//以下代码测试连读连写,从地址16开始,连续16个字节
//number是将写入的数据,tempr数组中存储读出的值
/*for(i=0;i<16;i++)
{
tempw[i]=number;
}
write_seq(tempw,16,16);//参考分别对应缓冲区首地址,写入内存首地址,写入长度
read_seq(tempr,16,16);*///参考分别对应缓冲区首地址,写入内存首地址,写入长度
i=0;
}
}
销售服务专线:0755-82127888
技术支持专线:0755-82127938
投诉专线:0755-82127989