#include "msp430x26x.h"
void delay(int t) //长时间延时子程序
{
int i,j;
for(i=0;i<t;i++)
{
for(j=0;j<1000;j++)
{
;
}
}
}
unsigned short GetOneHalfword()
{
unsigned short halfword;
P1SEL=0x00;
P2SEL=0x00;
P1DIR=0x02;//P1.0=/WP P1.1=/WE P1.7=WAIT
P2DIR=0x0e;//P2.1=/CE P2.2=/OE P2.3=/RST P2.4=CLK
P1OUT|=BIT1; //写禁止
P4SEL=0x00;
P5SEL=0x00;
P4DIR=0x00;
P5DIR=0x00;
P2OUT&=~BIT1;//片选使能
P2OUT&=~BIT2;//输出使能
halfword=P5IN;
halfword<<=8;
halfword+=P4IN;
P2OUT|=BIT2;//输出禁止
P2OUT|=BIT1;//片选禁止
return halfword;
}
void SendCommand(unsigned long addr,unsigned short data)
{
P1SEL=0x00;
P2SEL=0x00;
P1DIR=0x02;//P1.0=/WP P1.1=/WE P1.7=WAIT
P2DIR=0x0e;//P2.1=/CE P2.2=/OE P2.3=/RST P2.4=CLK
P2OUT|=BIT3;
P4SEL=0x00;
P5SEL=0x00;
P4DIR=0xff;//P5 高8位数据,P4 低8位数据
P5DIR=0xff;
P6SEL=0x00;
P7SEL=0x00;
P8SEL=0x00;
P6DIR=0xff;//地址
P7DIR=0xff;
P8DIR=0xff;
P7OUT=addr;
P6OUT=addr>>8;
P8OUT=addr>>16;
P4OUT=data;
P5OUT=data>>8;
P2OUT|=(BIT1+BIT2); //P2.1=/CE P2.2=/OE P2.3=/RST P2.4=CLK
P1OUT|=BIT1; //P1.0=/WP P1.1=/WE P1.7=WAIT
P2OUT&=~BIT1;// /CE
P1OUT&=~BIT1;// /WE
P1OUT|=BIT1; // /WE
P2OUT|=BIT1; // /CE
}
void Reset()
{
P2SEL&=~BIT3;
P2DIR|=BIT3;//P2.1=/CE P2.2=/OE P2.3=/RST P2.4=CLK
P2OUT&=~BIT3;
for(int i=0;i<5;i++) ;
P2OUT|=BIT3;
}
unsigned short ReadOneHalfword(unsigned long addr)
{
unsigned short halfword;
SendCommand(addr,0xff);
halfword=GetOneHalfword();
return halfword;
}
void Unlock(unsigned long addr)
{
Reset();
SendCommand(addr,0x60);
SendCommand(addr,0xd0);
}
void Erase(unsigned long addr)
{
Unlock(addr);
SendCommand(addr,0x20);
SendCommand(addr,0xd0);
delay(300);
}
void WriteOneHalfword(unsigned long addr,unsigned short data)
{
SendCommand(addr,0x40);
SendCommand(addr,data);
}
void CLearStatusRegister(unsigned long addr)
{
SendCommand(addr,0x50);
}
void main(void)
{
char high,low;
unsigned short halfword;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); // If calibration constants erased
// do not load, trap CPU!!
}
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
UCA0CTL1 |= UCSWRST;
P3SEL = 0x30; // P3.4,5 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 8; // 1MHz 115200
UCA0BR1 = 0; // 1MHz 115200
UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
Erase(0x310000);
for(int j=0;j<50;j++)
WriteOneHalfword(0x310000+j,j+1);
for(int j=0;j<50;j++)
{
halfword=ReadOneHalfword(0x310000+j);
low=halfword;
high=halfword>>8;
}
} |