求教各位大虾,谢谢帮忙:
我用STR710F的SPI接口驱动EEPROM型号为AT25128,调试了很长时间了,实在没办法了,这是我的电路连接图
file:///C:/Documentsfile:///C:/Documents
现在读出来全是0,不知写进去了没.下面是我的调试结果 截屏file:///C:/Documentsfile:///C:/Documents
下面是我的部分程序
/********************************************************/
# define __AT251_H__
#include "bspi.h"
#define SPI_SS_Pin (//0x0001<<8)
#define err 0x00
#define EEPROM_CAPACITY 0x3fff // EEPROM capacity (bytes)
#define WREN 0x06
#define WRDI 0x04
#define RDSR 0x05
#define WRSR 0x01
#define READ 0x03
#define WRITE 0x02
#define READY 0x01
#define BUSY 0x02
#define WR_EN 0x03
#define WR_DI 0x04
extern void AT251_init(void);
extern u8 CheckStatus(enum AT25_STATUS State);
extern void AT251_CmdWrite(u8 cmd);
extern u8 AT251_readbyte(u16 addr);
extern void AT251_AddrWrite(u16 addr);
extern void AT251_ByteWrite(u16 addr,u8 data );//void AT251_DataByteWrite(u16 addr,u8 data);
extern u8 AT251_DataWrite(u8 *buf,u16 addr1,u16 addr2,u16 lastcounts);//void AT251_DataStrWrite(u8 *String,u16 addr,u8 len );
extern void AT251_DataRead(u8 *buf,u16 addr1,u16 addr2,u16 lastcounts);//extern void AT251_DataRead(u8 *buf,u16 addr,u8 n);
#endif
/***********************************************************/
/**************************C文件****************************/
#include "at251.h"
//#include "bspi.h"
/////////////////////////////////////////////////////////////////////////
void BSPI1_init(void)
{
GPIO_Config(GPIO0, 0x0070, GPIO_AF_PP);// GPIO_Config(GPIO0, 0x00F0, GPIO_AF_PP); //Configure GPI00 on mode Alternate function Push Pull
GPIO_Config(GPIO0, 0x0080, GPIO_OUT_PP);
BSPI_Init(BSPI1);
BSPI_ClockDividerConfig(BSPI1, 8); //Configure Baud rate Frequency :-->APB1/8
BSPI_Enable(BSPI1, ENABLE); //Enable BSPI1
BSPI_MasterEnable(BSPI1, ENABLE);
BSPI_ClkActiveHigh(BSPI1, ENABLE); //Configure the clock to be active high
BSPI_ClkFEdge(BSPI1, DISABLE); //Enable capturing the first Data sample on the first edge of SCK
BSPI_8bLEn(BSPI1, ENABLE); //Set the word length to 8 bit
BSPI_RcFifoDepth(BSPI1, 7); //Configure the depth of receive to 5 word
}
/////////////////////////////////////////////////////////////////////////////
void AT251_init(void)
{
BSPI1_init();
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
}
/////////////////////////////////////////////////////////////////////////////////////
u8 AT251_readbyte(u16 addr)
{
u8 buf;
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1,READ);
AT251_AddrWrite(addr);
buf=BSPI_WordReceive(BSPI1);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
return(buf);
}
//////////////////////////////////////////////////////////////////////////////////
void AT251_CmdWrite(u8 cmd)
{
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1, cmd);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
}
////////////////////////////////////////////////////////////////////////////////
void AT251_AddrWrite(u16 addr)
{
BSPI_WordSend(BSPI1,(u8)(addr>>8));
BSPI_WordSend(BSPI1,(u8)addr);
}
//////////////////////////////////////////////////////////////////////
void AT251_ByteWrite(u16 addr,u8 data )//指定地址写一个字节
{int i;
AT251_CmdWrite(WREN);//WRITE ENABLE
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;AT251_CmdWrite(WRITE);
BSPI_WordSend(BSPI1,WRITE);
AT251_AddrWrite(addr);
BSPI_WordSend(BSPI1,data);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
AT251_CmdWrite(WRDI );//写禁止
for(i=0;i<40;i++);
}
//////////////////////////////////////////////////////////////////////////////////
u8 CheckStatus()
{ u8 S;
//AT251_CmdWrite(WREN);//WRITE ENABLE
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1,RDSR);
S=BSPI_WordReceive(BSPI1);
GPIO_BitWrite(GPIO0,0x07,1);//ss=0;
return S;
}
/////////////////////////////////////////////////////////////////////////////////////
void AT251_DataRead(u8 *buf,u16 addr1,u16 addr2,u16 lastcounts)//指定地址读取指定个数据
{
u16 j,m;
u16 dataword;
//AT251_CmdWrite(WREN);
for(j=addr1;j<addr2;j++)
{
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1,READ);//AT251_CmdWrite(READ);
dataword=(j-1);//dataword=32*(j-1);
AT251_AddrWrite(dataword);
buf[(j-addr1)]=BSPI_WordReceive(BSPI1);
//for(m=0;m<8;m++);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
u8 AT251_DataWrite(u8 *buf,u16 addr1,u16 addr2,u16 lastcounts)//指定地址读取指定个数据
{ u8 j,m ;
u8 databyte ;
u16 dataword ;
for(j=addr1;j<addr2;j++)
{ databyte=CheckStatus(READY_BUSY);
if(databyte) return(1);
AT251_CmdWrite(WREN);
GPIO_BitWrite(GPIO0,0x07,0);//ss=0;
BSPI_WordSend(BSPI1,WRITE);
dataword=(j-1);
AT251_AddrWrite(dataword);
BSPI_WordSend(BSPI1,buf[j-addr1]);
//for(m=0;m<8;m++);
GPIO_BitWrite(GPIO0,0x07,1);//ss=1;
}
return(0);
}
////////////////////////////////////////////////////////////////////////////////////////
/**********************主函数如下:***************************/
#include "71x_lib.h"
#include "at251.h"
#include "uart.h"
#define UART2_Rx_Pin (0x0001<<13) // TQFP 64: pin N?5 , TQFP 144 pin N?9
#define UART2_Tx_Pin (0x0001<<14) // TQFP 64: pin N?6 , TQFP 144 pin N?10
#define UART3_Rx_Pin (0x0001<<1) // TQFP 64: pin N?52 , TQFP 144 pin N?123
#define UART3_Tx_Pin (0x0001<<0) // TQFP 64: pin N?53 , TQFP 144 pin N?124
#define Use_UART3
#ifdef Use_UART3
#define UARTX UART3
#define UARTX_Rx_Pin UART3_Rx_Pin
#define UARTX_Tx_Pin UART3_Tx_Pin
#endif /*Use_UART2 */
char buf_TX[22]="aaaaaaaaaaaaaaaaaaaaa";
char buf_RX[22]="11111111111111111111\n";
void def_extclock(void);
void MY_UART_init3(void);
void Main(void)
{
int i;
#ifdef DEBUG
debug();
#endif
def_extclock(); //将CKOUT 分为 48 Mhz
MY_UART_init3(); //UART初始化,包括管脚设置,波特率设置,及发送或接收使能等
AT251_init();
while(1)
{
AT251_DataWrite((u8 *)buf_TX,1,10,0);
for(i=0;i<50000;i++);
AT251_DataRead((u8 *)buf_RX,1,10,0);
UART_DataSend(UARTX, (u8 *)buf_RX,20);
}
} |
|