#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 OUTvoid 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 statusunsigned 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时序除了问题?麻烦对这方面了解的版友帮帮忙吧。。。小女子跪谢了~~~在线等待
此帖出自小平头技术问答
#include <MSP430X14X.h>#include "Flash_SPI.h"
unsigned char Flash_Data[512]; void SPI_Init(void){ // P3.1 P3.2 P3.3作为SPI的管脚 P3SEL = BIT3 + BIT2 + BIT1; //P3.1作为输出 P3DIR |= BIT1; //P3.0作为输出 P3DIR |= BIT0; //P3.3作为输出 P3DIR |= BIT3; //P3.0输出高电平 P3OUT = BIT0; //P3.1输出高电平 P3OUT = BIT1; //P3.3输出高电平 P3OUT = BIT3; // 以下设置SPI口的参数 U0CTL = 0X00; //将寄存器的内容清零 //数据为8比特,选择SPI模式,单片机为主机模式 U0CTL |= CHAR + SYNC + MM; U0TCTL = 0X00; //将寄存器的内容清零 // 时钟源为SMCLK,选择3线模式 U0TCTL = CKPH + SSEL1 + SSEL0 + STC; UBR0_0 = 0X45; //波特率为115200 UBR1_0 = 0X00; UMCTL_0 = 0X49; //调整寄存器 ME1 = USPIE0; //SPI0模块允许 IE1 |= URXIE0; //接收中断允许 IE1 |= UTXIE0; //发送中断允许 }
void Init_CLK(void){ unsigned int i; BCSCTL1 = 0X00;//将寄存器的内容清零 //XT2震荡器开启 //LFTX1工作在低频模式 //ACLK的分频因子为1 do { IFG1 &= ~OFIFG; // 清除OSCFault标志 for (i = 0x20; i > 0; i--); } while ((IFG1 & OFIFG) == OFIFG); // 如果OSCFault =1 BCSCTL2 = 0X00; //将寄存器的内容清零 BCSCTL2 += SELM1; //MCLK的时钟源为TX2CLK,分频因子为1 BCSCTL2 += SELS; //SMCLK的时钟源为TX2CLK,分频因子为1}
void AT45DB161_cs_low(){ P3OUT &=0xfe; }
void AT45DB161_cs_high(){ P3OUT |=0x01;}
void Delay_ms(unsigned long nValue)//毫秒为单位,8MHz为主时钟{ unsigned long nCount; int i; unsigned long j; nCount = 2667; for(i = nValue;i > 0;i--) { for(j = nCount;j > 0;j--); } return;}void Delay_us(unsigned long nValue)//微秒为单位,8MHz为主时钟{ int nCount; int i; int j; nCount = 3; for(i = nValue;i > 0;i--) { for(j = nCount;j > 0;j--); } return;}
//Send one byte via SPIunsigned char write_AT45DB161_byte(const unsigned char data){ while (IFG1&UTXIFG0 ==0); // wait while not ready for TX halSPITXREADY (IFG1&UTXIFG0) TXBUF0=data; // write while (IFG1&URXIFG0 ==0); // wait for RX buffer (full) return (RXBUF1);}
//Read register statusunsigned char AT45DB161_ReadStatus(void){ unsigned char status; AT45DB161_cs_low(); status=write_AT45DB161_byte(0x57); 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=write_AT45DB161_byte(0xff); 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();}
void main(){ unsigned int i; WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗 Init_CLK(); SPI_Init(); 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=0; AT45DB161_MainMemoryPageRead(100,0,512,Flash_Data); //page 100,addr 0,length 512 while(1);}
哪里有问题求帮助啊。。。
一周热门 更多>