打印

AT45DB161用MSP430F149普通I/O口模拟SPI读写数据状态寄存器读数总

[复制链接]
1972|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yangguangaisha|  楼主 | 2013-8-26 21:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <msp430x14x.h>


unsigned char Flash_Data[512];
unsigned char Flash_Data1[512];
void AT45DB161_cs_low(void);
void AT45DB161_cs_high(void);
void AT45DB161_si_low(void);
void AT45DB161_si_high(void);
void AT45DB161_sck_low(void);
void AT45DB161_sck_high(void);
unsigned char AT45DB161_so(void);
void write_AT45DB161_byte(unsigned char data);
unsigned char read_AT45DB161_byte(void);
//===============================================================================



//================================================================================
void init_AT45DB161(void);
//===============================================================================
//
//P64 SO IN
//P65 CS OUT 低有效
//P66 SCK OUT
//P67 SI OUT
void init_AT45DB161(void)
{
P6OUT|=0xe0; //1110 0000 CS SCK SI输出1
}
//片选有效
void AT45DB161_cs_low(void)
{
P6OUT&=0xdf; //1101 1111 CS=0 P65 CS
}
//片选无效
void AT45DB161_cs_high(void)
{
P6OUT|=0x20; //0010 0000 CS=1 P65 CS
}
//SI置低
void AT45DB161_si_low(void)
{
P6OUT&=0x7f; //0111 1111 SI=0 P67 SI
}
//SI置高
void AT45DB161_si_high(void)
{
P6OUT|=0x80; //1000 0000 SI=1 P67 SI
}
//SCK置低
void AT45DB161_sck_low(void)
{
P6OUT&=0xbf; //1011 1111 SCK=0 P66 SCK
}
//SCK置高
void AT45DB161_sck_high(void)
{
P6OUT|=0x40; //0100 0000 SCK=1 P66 SCK
}

void delay_AT5DB161(unsigned char time)
{
while(time--);
}

//读SO状态
unsigned char AT45DB161_so(void)
{
unsigned char c;
c=P6IN;
c=c&0x10;
if(c==1)
{
c=1;
}
else
{
c=0;
}
return(c);
}


//写1个字节
void write_AT45DB161_byte(unsigned char data)//-------------------------
{
unsigned int i;
AT45DB161_cs_low();
AT45DB161_sck_low();
for(i=0;i<8;i++)
{
if(data&0x80)
AT45DB161_si_high();
else
AT45DB161_si_low();
delay_AT5DB161(20);
AT45DB161_sck_low();
delay_AT5DB161(20);
AT45DB161_sck_high();
data<<=1;

}
AT45DB161_cs_high();
}
//读1个字节
unsigned char read_AT45DB161_byte(void)//-------------------------
{
unsigned int i;
unsigned char data,c;
AT45DB161_cs_low();
data=0;
for(i=0;i<8;i++)
{
AT45DB161_sck_high();
delay_AT5DB161(20);
AT45DB161_sck_low();
delay_AT5DB161(20);
c=AT45DB161_so();
if(c)
{
data|=0x80>>i;
}
else
{
data>>=1;
}

}
AT45DB161_cs_high();
return(data);
}

//Read register status
unsigned char AT45DB161_ReadStatus(void)
{
unsigned char status;
AT45DB161_cs_low();
write_AT45DB161_byte(0xd7);
write_AT45DB161_byte(0x00);
write_AT45DB161_byte(0x00);
write_AT45DB161_byte(0x00);
status=read_AT45DB161_byte();
AT45DB161_cs_high();
return(status);
}


//Read from AT45DB161 to MSP430 ----Main Memory Page Read(52H+24bits addr+32bits dc)
void AT45DB161_MainMemoryPageRead(unsigned int pageaddr,unsigned int baseaddr,unsigned int len,unsigned char *pbuf)
{
unsigned int i;
while(!(AT45DB161_ReadStatus()&0x80));
AT45DB161_cs_low();
write_AT45DB161_byte(0x52);
write_AT45DB161_byte((unsigned char)(pageaddr>>6));
write_AT45DB161_byte((unsigned char)((pageaddr<<2)|(baseaddr>>8)));
write_AT45DB161_byte((unsigned char)(baseaddr));
for(i=0;i<4;i++)
write_AT45DB161_byte(0x00);
for(i=0;i<len;i++)
pbuf=read_AT45DB161_byte();
AT45DB161_cs_high();
}

//Write from MSP430 to buffer-----Buffer1/2 Write(84H/87H+14bits dc+10bits addr)
void AT45DB161_BufferWrite(unsigned char nbuf,unsigned int baseaddr,unsigned int len,unsigned char *pbuf)
{
unsigned int i;
while(!(AT45DB161_ReadStatus()&0x80));
AT45DB161_cs_low();
switch(nbuf)
{
case 1: write_AT45DB161_byte(0x84);
break;
case 2: write_AT45DB161_byte(0x87);
break;
}
write_AT45DB161_byte(0x00);
write_AT45DB161_byte((unsigned char)(baseaddr>>8));
write_AT45DB161_byte((unsigned char)baseaddr);
for(i=0;i<len;i++)
write_AT45DB161_byte(pbuf);
AT45DB161_cs_high();
}

//Write from MSP430 to AT45DB161 through Buffer------Main Memory Page Program Through Buuffer1/2
//---------------------------------------------------
void AT45DB161_MainMemoryPageProgramThroughBuuffer(unsigned char nbuf,unsigned int pageaddr,unsigned int baseaddr,unsigned int len,unsigned char *pbuf)
{
while(!(AT45DB161_ReadStatus()&0x80));
AT45DB161_cs_low();
switch(nbuf)
{
case 1: write_AT45DB161_byte(0x82);
break;
case 2: write_AT45DB161_byte(0x85);
break;
}
write_AT45DB161_byte((unsigned char)(pageaddr>>6));
write_AT45DB161_byte((unsigned char)((pageaddr<<2)|(baseaddr>>8)));
write_AT45DB161_byte((unsigned char)(baseaddr));
AT45DB161_cs_high();
}


/* 初始化P6口 */
void Iint_Port6(void)
{
P6SEL=0x00; //0000 0011 P6口的低两位,设置为ADC12功能
P6DIR=0xe0; //1110 0000
}
void Iint_OSCF(void)
{
int i;
BCSCTL1=0x04; //ACLK来源于LFXT1低频模式,其频率为32K,XT2开启
BCSCTL2=0x88; //MCLK与SMCLK均来源于XT2,分频系数为1,频率为8M
do
{
IFG1 &= ~OFIFG; // Clear OSCFault flag
for (i = 0xFF; i > 0; i--); // Time for flag to set
}
while ((IFG1 & OFIFG) != 0); // OSCFault flag still set
}

void main(void)
{
unsigned int i;
//unsigned char Flash_Data[512];
WDTCTL=WDTPW+WDTHOLD;
Iint_OSCF();
Iint_Port6();
init_AT45DB161();
for(i=0;i<512;i++)
Flash_Data=i;
AT45DB161_MainMemoryPageProgramThroughBuuffer(1,100,0,512,Flash_Data); //buffer 1,page 100,addr 0,length 512
for(i=0;i<512;i++) //清空Flash_Data
Flash_Data=0;
AT45DB161_MainMemoryPageRead(100,0,512,Flash_Data); //page 100,addr 0,length 512
while(1);
}



以上是程序,已经测试很久了。。。不知道问题出在哪里了?是不是I/O口模拟SPI时序除了问题?

相关帖子

沙发
gexingyouxian| | 2013-8-26 21:56 | 只看该作者
用449和5418做过,不过不是模拟的,是用硬件SPI接口

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

168

主题

1522

帖子

3

粉丝