⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fram.c

📁 使用STC单片机的SPI控制器读写FRAM的程序
💻 C
字号:
//-----------------------------------------------------------------------------
// FRAM.C
//-----------------------------------------------------------------------------
// Author  Zhou Ke
// Email   zhoukesec@163.com
// Target:         FRAM_UART
// Tool chain:     Keil C51 8.02 / Keil EVAL C51
// Project Name:   FRAM_UART
//
//
// Release 1.0
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <intrins.h>
#include "stc12c2052.h"
#include "fram.h"



 extern volatile  unsigned char tx_buffer[8];
 extern volatile  unsigned char rx_buffer[4];
 extern volatile  unsigned char tmp_buffer[4];



/*------------------------------------------------------------------------------
    Function Name   :   delay
    Description     :   wait a short while 
    Parameters  	:   
        Input       :   none
        Output      :   none                        
    Return          :   none
    Notes           :    
------------------------------------------------------------------------------*/ 

void delay(void)
{
	unsigned char i;
	for(i=0;i<100;i++)
		_nop_();
}
/*------------------------------------------------------------------------------
    Function Name   :   fram_write_enable
    Description     :   enable the write function 
    Parameters  	:   
        Input       :   none
        Output      :   none                        
    Return          :   none
    Notes           :    
------------------------------------------------------------------------------*/ 
void fram_write_enable()
{
	FRAM_CS = 0;
	SPSTAT = 0xC0;
	SPDAT = WREN;
	while((SPSTAT & 0x80) == 0);
	FRAM_CS = 1;
}


/*------------------------------------------------------------------------------
    Function Name   :   fram_read_status
    Description     :   read status of the fram 
    Parameters  	:   
        Input       :   none
        Output      :   none                        
    Return          :   none
    Notes           :    
------------------------------------------------------------------------------*/ 
/*
unsigned char fram_read_status()
{
	FRAM_CS = 0;

	SPSTAT = 0xC0;
	SPDAT = RDSR;
	while((SPSTAT & 0x80) == 0);

	SPSTAT = 0xC0;
	SPDAT = 0x00;
	while((SPSTAT & 0x80) == 0);

	FRAM_CS = 1;

	return SPDAT;

}
*/

/*------------------------------------------------------------------------------
    Function Name   :   fram_read_data
    Description     :   read data from fram to ram 
    Parameters  	:   
        Input       :   none
        Output      :   none                        
    Return          :   none
    Notes           :    
------------------------------------------------------------------------------*/
void fram_read_data(void)
{
	unsigned char sum,i;

	sum = 0;
	FRAM_CS = 0;  

	//Command
	SPSTAT = 0xC0;
	SPDAT = READ;
	while((SPSTAT & 0x80) == 0);

	//Address
	SPSTAT = 0xC0;
	SPDAT = 0x00;
	while((SPSTAT & 0x80) == 0);

	for(i = 0; i < 4; i++){
		SPSTAT = 0xC0;
		SPDAT = 0x00;
		while((SPSTAT & 0x80) == 0);
		tx_buffer[i+2] = SPDAT;
		sum += tx_buffer[i+2];		
	}

	tx_buffer[6] = sum;

	FRAM_CS = 1;
}


/*------------------------------------------------------------------------------
    Function Name   :   fram_write_data
    Description     :   write data from ram to fram
    Parameters  	:   
        Input       :   none
        Output      :   none                        
    Return          :   none
    Notes           :    
------------------------------------------------------------------------------*/
void fram_write_data(void)
{
	
	unsigned char i;
	
	fram_write_enable();
	delay();
 
	FRAM_CS = 0;  

	//Command
	SPSTAT = 0xC0;
	SPDAT = WRITE;
	while((SPSTAT & 0x80) == 0);

	//Address
	SPSTAT = 0xC0;
	SPDAT = 0x00;
	while((SPSTAT & 0x80) == 0);


	for(i = 0; i < 4; i++){
		SPSTAT = 0xC0;
		SPDAT = tmp_buffer[i];
		while((SPSTAT & 0x80) == 0);
		delay();	
	}

	FRAM_CS = 1;    

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -