?? 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 + -