📄 x5045.c
字号:
//------------------------------------------------------------------------------------
// x5045.c
//------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <reg52.h>
#include "cpu/cpu.h"
#include "misc/general.h"
//------------------------------------------------------------------------------------
// x5045 Constants
//------------------------------------------------------------------------------------
#define x5045_WREN 0x06 // enable write operations
#define x5045_RDSR 0x05 // read status register
#define x5045_WRDI 0x04 // disable write operations
#define x5045_READ 0x03 // read
#define x5045_WRITE 0x02 // write
#define x5045_WDSR 0x01 // write status register
#define x5045_A8 0x03 // address A8 bit
#define x5045_BUSY 0x01 // Write-In-Progress bit mask
#define x5045_tWAIT 6*0xFF // wait out of Write-In-Progress
#define BUSY 1
#define IDLE 0
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
char x5045_Read_Status (void);
void x5045_Write_Enable (void);
void x5045_Write_Status (void);
void x5045_Wait_Busy (void);
bit x5045_Is_Busy (void);
void x5045_Write ( char addr, char* buf, char n );
void x5045_Read ( char addr, char* buf, char n );
//------------------------------------------------------------------------------------
// x5045 read status register routine
//------------------------------------------------------------------------------------
char x5045_Read_Status (void)
{
char stat = 0;
spi_Start;
spi_Write_Byte ( x5045_RDSR );
stat = spi_Read_Byte ();
spi_Stop;
return stat;
}
//------------------------------------------------------------------------------------
// x5045 write enable routine
//------------------------------------------------------------------------------------
void x5045_Write_Enable (void)
{
spi_Start;
spi_Write_Byte ( x5045_WREN );
spi_Stop;
}
//------------------------------------------------------------------------------------
// x5045 write status register routine
//------------------------------------------------------------------------------------
void x5045_Write_Status (void)
{
spi_Start;
spi_Write_Byte ( x5045_WREN );
spi_Write_Byte ( x5045_WDSR );
spi_Write_Byte ( 0 );
spi_Stop;
x5045_Wait_Busy ();
}
//------------------------------------------------------------------------------------
// x5045 Wait Out Of Busy Routine
//------------------------------------------------------------------------------------
void x5045_Wait_Busy (void)
{
int t = x5045_tWAIT;
while ( t-- )
{
Delay ( 0x00000020 );
if (( x5045_BUSY & x5045_Read_Status ()) == IDLE ) break;
}
}
//------------------------------------------------------------------------------------
// x5045 Poll Busy Routine
//------------------------------------------------------------------------------------
bit x5045_Is_Busy (void)
{
if ( x5045_BUSY & x5045_Read_Status ()) return BUSY;
return IDLE;
}
//------------------------------------------------------------------------------------
// x5045 read routine
//------------------------------------------------------------------------------------
void x5045_Read ( char addr, char* buf, char n )
{
spi_Start;
spi_Write_Byte ( x5045_READ | ((MSBYTE(addr) & 0x01) << x5045_A8));
spi_Write_Byte ( LSBYTE(addr) );
while ( n-- ) *buf++ = spi_Read_Byte ();
spi_Stop;
}
//------------------------------------------------------------------------------------
// x5045 write routine
//------------------------------------------------------------------------------------
void x5045_Write ( char addr, char* buf, char n )
{
spi_Start;
spi_Write_Byte ( x5045_WRITE | ((MSBYTE(addr) & 0x01) << x5045_A8 ));
spi_Write_Byte ( LSBYTE( addr ));
while ( n-- ) spi_Write_Byte ( *buf++ );
spi_Stop;
x5045_Wait_Busy ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -