#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时序除了问题?麻烦对这方面了解的版友帮帮忙吧。。。小女子跪谢了~~~在线等待
此帖出自小平头技术问答
Command Opcode
Main Memory Page Read D2H
Continuous Array Read (Legacy Command) E8H
Continuous Array Read (Low Frequency) 03H
Continuous Array Read (High Frequency) 0BH
Buffer 1 Read (Low Frequency) D1H
Buffer 2 Read (Low Frequency) D3H
Buffer 1 Read D4H
Buffer 2 Read D6H
Table 15-2. Program and Erase Commands
Command Opcode
Buffer 1 Write 84H
Buffer 2 Write 87H
Buffer 1 to Main Memory Page Program with Built-in Erase 83H
Buffer 2 to Main Memory Page Program with Built-in Erase 86H
Buffer 1 to Main Memory Page Program without Built-in Erase 88H
Buffer 2 to Main Memory Page Program without Built-in Erase 89H
Page Erase 81H
Block Erase 50H
Sector Erase 7CH
Chip Erase C7H, 94H, 80H, 9AH
Main Memory Page Program Through Buffer 1 82H
Main Memory Page Program Through Buffer 2 85H
我没看到是52啊
一周热门 更多>