📄 x5045.c
字号:
#include <reg52.h>
#include "common.h"
#include "resource.h"
#include "wdog.h"
#include "x5045.h"
/* here is the instrution of x25045*/
#define WREN 0x06 /* 设置写使能锁存器(允许写操作) */
#define WRDI 0x04 /* 复位写使能锁存器(禁止写操作)*/
#define RDSR 0x05 /* 读状态寄存器 */
#define WRSR 0x01 /* 写状态寄存器(块锁定)*/
#define READ0 0x03 /* 从开始于所选地址的存储器列阵中读出数据 */
#define READ1 0x0b /* */
#define WRITE0 0x02 /*把数据写入开始于所选地址的存储器阵列中(1至4字节)*/
#define WRITE1 0x0a /* */
/*
* SCK下降沿移入数据
*/
static U8 Read8()
{
bit bData;
U8 cLoop;
U8 cData;
for(cLoop=0;cLoop<8;cLoop++)
{
PIN_x5045_sck = 1;
bData = PIN_x5045_so;
PIN_x5045_sck = 0;
cData <<= 1;
if(bData) cData |= 0x01;
}
return cData;
}/* Read8 */
/*
* 在SCK上升沿写入数据
*/
static void Write8(U8 cData)
{
U8 cLoop;
for(cLoop=0;cLoop<8;cLoop++)
{
PIN_x5045_sck=0;
if((cData&0x80))
{
PIN_x5045_si=1;
}
else
{
PIN_x5045_si=0;
}
PIN_x5045_sck=1;
cData<<=1;
}
PIN_x5045_sck=0;
PIN_x5045_si = 0;
}/* Write8 */
/*
* Read Status Register of x5045
*/
static U8 ReadSR()
{
U8 cData;
PIN_x5045_cs = 0;
Write8(RDSR);
cData=Read8();
PIN_x5045_cs = 1;
return cData;
}/* ReadSR */
/*
* Write Status Register of x5045
*/
static U8 WriteSR(U8 cData)
{
U8 cTemp;
register U16 timeout=0xFFFF;
PIN_x5045_cs = 0;
PIN_x5045_sck = 0;
Write8(WRSR);
Write8(cData);
PIN_x5045_sck = 0;
PIN_x5045_cs = 1;
do
{
cTemp = ReadSR();
}while((cTemp&0x01)&&((--timeout))); /* 一直到正确结果 */
return 1;
}
/*
* 写入一个字节,cData为写入的数,cAddress为写入地址,bRegion为页
*/
void x5045Write1B(U8 cData, U8 cAddress, bit bRegion)
{
U16 n = 0;
while(((ReadSR()&0x01)==1)&&(!(++n))) /* 可能会引起死循环 */
{
}
PIN_x5045_cs=0;
Write8(WREN);
PIN_x5045_cs=1;
PIN_x5045_cs=0;
if(bRegion==0)
{
Write8(WRITE0);
}
else
{
Write8(WRITE1);
}
Write8(cAddress);
Write8(cData);
PIN_x5045_sck=0;
PIN_x5045_cs=1;
}
/*
* 读入一个字节,cAddress为读入地址,bRegion为页
*/
U8 x5045Read1B(U8 cAddress, bit bRegion)
{
U8 cData;
U8 n = 0;
while(((ReadSR()&0x01)==1)&&(!(++n)))
{
}
PIN_x5045_cs=0;
if(bRegion==0)
{
Write8(READ0);
}
else
{
Write8(READ1);
}
Write8(cAddress);
cData=Read8();
PIN_x5045_cs=1;
return cData;
}/* x5045Read1B */
/*
* Init X5045
*/
void x5045Init()
{
PIN_x5045_cs = 1;
PIN_x5045_so = 1;
PIN_x5045_sck= 0;
PIN_x5045_si = 0;
}/* x5045Init */
/*
* Init watch dog and set dog delay value
* dogDelay values: WDOG_1400MS, WDOG_600MS, WDOG_200MS
*/
void wdogInit(U8 dogDelay)
{
PIN_x5045_sck = 0;
PIN_x5045_cs = 0;
Write8(WREN); /* 解锁写保护 */
PIN_x5045_sck = 0;
PIN_x5045_cs = 1;
switch(dogDelay)
{
case WDOG_1400MS:
WriteSR(0x00);
break;
case WDOG_600MS:
WriteSR(0x01);
break;
case WDOG_200MS:
WriteSR(0x10);
break;
case WDOG_CLOSE:
WriteSR(0x30);
break;
default: /* default dog delay is set to 1400ms */
WriteSR(0x00);
}/* switch */
}/* wdogInit */
/*
* Close watch dog.
*/
#if 0
void wdogClose()
{
Write8(WREN); /* 解锁写保护 */
WriteSR(WDOG_CLOSE);
}
#endif
/*
* Reset watch dog
*/
#if 0
void wdogReset()
{
PIN_x5045_cs = 0;
PIN_x5045_cs = 1;
}/* wdogReset */
#endif
/*
* wdogFeed
*/
void wdogFeed()
{
PIN_wdog_wdt = 1;
PIN_wdog_wdt = 0;
PIN_wdog_wdt = 1;
}/* wdogFeed */
#if 0
void main(void)
{
U8 ch;
x5045Init();
x5045Write1B(0x0, 0x01, 0);
x5045Write1B(0x0, 0x02, 0);
x5045Write1B(0x0, 0x03, 0);
x5045Write1B(0x0, 0x04, 0);
//ch = x5045Read1B(0xBA, 0);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -