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

📄 fm25040.c

📁 FM25040的C51驱动程序
💻 C
字号:
#include "FM25040.h"
//*----------------------------------------------------------------------------
//初始化SPI,片选端用:CS2(PA5)
//*----------------------------------------------------------------------------
void Initial_SPI(void)
{
    //输出 PA1,PA2,PA5 为高
    FM25040_IO = 1;
    FM25040_CL = 1;
    FM25040_CS = 1;  
}
/*;********************************************************************************
*
;* Name: outbit 
;*
;*********************************************************************************
*/
void outbit(unsigned char xx)
{
    if(xx==0)
        FM25040_IO = 0;
    else
        FM25040_IO = 1;
}
/*;********************************************************************************
*
;* 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(unsigned char outbyte)
{ 
    outbit((outbyte>>7)&0x01);D10US;FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;
    outbit((outbyte>>6)&0x01);D10US;FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;
    outbit((outbyte>>5)&0x01);D10US;FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;
    outbit((outbyte>>4)&0x01);D10US;FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;
    outbit((outbyte>>3)&0x01);D10US;FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;
    outbit((outbyte>>2)&0x01);D10US;FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;
    outbit((outbyte>>1)&0x01);D10US;FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;
    outbit((outbyte>>0)&0x01);D10US;FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;
    //FM25040_CL = 0;
}

/*;*******************************************************************************
*
;* Name: inputbit 
;*
;*********************************************************************************
*/
unsigned char inputbit(void)
{
    unsigned char bit;
    if(FM25040_IO == 0)
        bit=0; 
    else 
        bit=1;  
    return bit;
}
/*;*******************************************************************************
*
;* 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.
;*********************************************************************************
*/
unsigned char inputbyt(void)
{
    unsigned char bbyte=0;
    FM25040_IO = 1;D10US;
    FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;bbyte=bbyte|(inputbit()<<7);D10US;
    FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;bbyte=bbyte|(inputbit()<<6);D10US;
    FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;bbyte=bbyte|(inputbit()<<5);D10US;
    FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;bbyte=bbyte|(inputbit()<<4);D10US;
    FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;bbyte=bbyte|(inputbit()<<3);D10US;
    FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;bbyte=bbyte|(inputbit()<<2);D10US;
    FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;bbyte=bbyte|(inputbit()<<1);D10US;
    FM25040_CL = 0;D10US;FM25040_CL = 1;D10US;bbyte=bbyte|(inputbit()<<0);D10US;
    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)
{
    FM25040_CL = 0;FM25040_CS = 0;D10US;/* Bring /CSX low */
    outbyt(WREN_INST);/* Send WREN instruction */
    FM25040_CL = 0;FM25040_CS = 1;D10US;
}
/*;*******************************************************************************
*
;* 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)
{
    FM25040_CL = 0;FM25040_CS = 0;D10US;/* Bring /CSX low */
    outbyt(WRDI_INST);/* Send WRDI instruction */
    FM25040_CL = 0;FM25040_CS = 1;D10US;/* Bring CLK low,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
;***********************************************************************************
*/
unsigned char rdsr_cmd (void)
{
    unsigned char aa;
    FM25040_CL = 0;FM25040_CS = 0;D10US;
    outbyt(RDSR_INST);
    aa=inputbyt();
    FM25040_CL = 0;FM25040_CS = 1;D10US;
    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(unsigned char status)
{
    wren_cmd();
    FM25040_CL = 0;FM25040_CS = 0;D10US;
    outbyt(WRSR_INST) ;/* Send WRSR instruction */
    outbyt(status);/* Send status register */
    FM25040_CL = 0;FM25040_CS = 1;D10US;
}

/*;***************************************************************************
*
;* 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(unsigned int dd,unsigned char aa)
{
    wren_cmd(); /*write enable*/
    FM25040_CL = 0;FM25040_CS = 0;D10US;
    outbyt(WRITE_INST);/* Send WRITE instruction including MSB of address */
    outbyt((unsigned char)(dd>>8));
    outbyt((unsigned char)(dd));
    outbyt(aa);
    FM25040_CL = 0;FM25040_CS = 1;D10US;
}
//======================================
/*;***************************************************************************
*
;* 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(unsigned char *ptr,unsigned int dd,unsigned char num)
{
    unsigned char i;
    wren_cmd(); /*write enable*/
    FM25040_CL = 0;FM25040_CS = 0;D10US;
    outbyt(WRITE_INST);/* Send WRITE instruction including MSB of address */
    outbyt((unsigned char)(dd>>8));
    outbyt((unsigned char)(dd));
    for(i=0;i<num;i++)
    {
    	outbyt(*(ptr+i));
    }
    FM25040_CL = 0;FM25040_CS = 1;D10US;
}
/*;****************************************************************************
*
;* 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
;*****************************************************************************
*/
unsigned char byte_read(unsigned int dd)
{
    unsigned char temp;
    unsigned char temph,templ;
    temph=(unsigned char)(dd>>8);
    templ=(unsigned char)(dd);
    FM25040_CL = 0;FM25040_CS = 0;D10US;
    outbyt(READ_INST);/* Send READ_INST instruction including MSB of address */
    outbyt((unsigned char)(dd>>8));
    outbyt((unsigned char)(dd));
    temp=inputbyt();
    FM25040_CL = 0;FM25040_CS = 1;D10US;
    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(unsigned char *ptr,unsigned int dd,unsigned char num)
{
    unsigned char i;
    unsigned char temp;
    temp=(unsigned char)dd;
    FM25040_CL = 0;FM25040_CS = 0;D10US;
    outbyt(READ_INST);/* Send READ_INST instruction including MSB of address */
    outbyt((unsigned char)(dd>>8));
    outbyt((unsigned char)dd);
    for(i=0;i<num;i++)
    {
    	*(ptr+i)=inputbyt();
    }
    FM25040_CL = 0;FM25040_CS = 1;D10US;
}
/*;****************************************************************************
*
;* Name: delay_us
;*
;*****************************************************************************
*/
void delay_us(unsigned int x)
{
    unsigned int i,j;
    for (i=0;i<x;i++)
    for (j=0;j<10;j++)
    ;
}

⌨️ 快捷键说明

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