AT45DB021B在電力參數(shù)監(jiān)測中的應(yīng)用
#include Msp430x14x.h>
#define SCK_1 P4OUT = BIT7 //SCK=1
#define SCK_0 P4OUT =~ BIT7 //SCK=0
#define SI_1 P5OUT = BIT0 //CPU向AT寫1
#define SI_0 P5OUT =~ BIT0 //CPU向AT寫0
#define SO_IN ((P5IN 0x02) == 0x02) //CPU讀.
#define DIR_IN P5DIR =~ BIT1; P5OUT = BIT1 //I/O輸入,接受AT的數(shù)據(jù)
#define DIR_OUT P5DIR = BIT1; P5OUT = BIT1 //I/O輸出
#define STARTOP P4OUT = BIT6; _NOP(); P4OUT =~ BIT6
#define ENDOP P4OUT =~ BIT6; _NOP(); P4OUT = BIT6
#define HIGHTOLOW {_NOP(); SCK_1; _NOP(); SCK_0; _NOP();}
int tempArray[150];
void Init(void) //MSP430F149引腳初始化;
{
P4DIR = BIT6;
P4OUT = BIT6;
P4DIR = BIT7;
P4OUT = BIT7;
P5DIR = BIT0;
P5OUT = BIT0;
P5DIR = BIT1;
P5OUT = BIT1;
}
寫一字節(jié)子程序:
void Wr1byte(unsigned char tt)
{ unsigned char i;
for (i=0; i8; j++)
{
if ((tt 0x80) == 0x80)
SI_1;
else
SI_0;
HIGHTOLOW;
tt = tt 1;
}
}
讀一字節(jié)子程序:
unsigned char Rd1byte(void)
{unsigned char i,medBit=0;
char Data=0;
for (i=0; i8; i++)
{ HIGHTOLOW;
_NOP();
if (SO_IN )
medBit = 1; //if(SO==1)medBit=1;
else
medBit = 0;
Data = (Data 1) medBit;
return Data;
}
}
對AT45DB021進(jìn)行寫操作子函數(shù): (通過緩沖存儲(chǔ)器2對主存儲(chǔ)器寫操作)
參數(shù):pInData 指向要寫入數(shù)據(jù)的指針
mm 寫入數(shù)據(jù)數(shù)組的大小
startPage 將數(shù)據(jù)寫入主存的起始頁地址
startByte 將數(shù)據(jù)寫入主存的起始頁地址中的起始字節(jié)地址
void WriteMemory(const int *pInData, unsigned char mm, unsigned int startPage, unsigned int startByte)
{
unsigned char i, j;
unsigned char opCode;
char lowData;
char highData;
opCode = 0x85; //opCode=0x82 is also available.
lowData = highData = 0;
SCK_0;
_NOP();
STARTOP;
_NOP();
Wr1byte(opCode );
for (j=0; j5; j++)
HIGHTOLOW;
//send startPage address //發(fā)送起始頁地址子程序;
//send startByte address //發(fā)送頁內(nèi)起始地址子程序;
for (j=0; jmm; j++) //對目的地址寫操作,先寫低字節(jié)后寫高字節(jié)
{
lowData = (char)(*(pInData + j));
Wr1byte(lowData);
highData = (char)((*(pInData + j)) >> 8);
Wr1byte(highData);
}
ENDOP;
_NOP();
SI_1;
}
讀出AT45DB021存儲(chǔ)的數(shù)據(jù)(主存儲(chǔ)器頁讀, 讀出字)
參數(shù):mm 讀出數(shù)據(jù)數(shù)組的大小
startPage 讀出數(shù)據(jù)的起始頁地址
startByte 讀出數(shù)據(jù)的起始頁地址中的起始字節(jié)地址
返回值:無
void ReadMemory(unsigned char mm, unsigned int startPage, unsigned int startByte)
{
unsigned char i, j;
unsigned char opCode;
unsigned char medBit;
char lowData;
char highData;
opCode = 0x52;
lowData = highData = 0;
medBit = 0;
SCK_0;
_NOP();
STARTOP;
_NOP();
Wr1byte(opCode );
for (j=0; j5; j++)
HIGHTOLOW; //5位保留碼;
//send startPage address //發(fā)送起始頁地址子程序;
//send startByte address //發(fā)送頁內(nèi)起始地址子程序;
for (j=0; j32; j++)
HIGHTOLOW; //32位無關(guān)碼;
DIR_IN;
for (j=0; jmm; j++)
{
lowData= Rd1byte();
highData= Rd1byte();
tempArray[j] = ((int)highData 8) + lowData;
}
ENDOP;
SI_1;
_NOP();
DIR_OUT;
}
評論