智能车制作
标题:
MC9S08AC60单片机读写外扩Flash
[打印本页]
作者:
seuzhuxi
时间:
2013-3-15 20:56
标题:
MC9S08AC60单片机读写外扩Flash
小弟最近在做一个程序,其中需要用单片机读写外扩flash,AT45DB321.如何检查是否写入呢?每次读芯片的时候都显示FF,是不是表示没读出数据啊?程序如下: SPI_MI_PutVal(1);SPI_SCK_PutVal(1);这些SPI开头的都是在单片机中定义端口后,PE(processor Expert)自动产生的程序。请大家帮帮我!谢谢
/文件名: EFlash.c (外扩Flash芯片操作函数文件) *
//-------------------------------------------------------------------------*
//头文件
#include "EFLASH.h"
#include"PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
#include "SPI_CS.h"
#include "RS232.h"
/*-------------------------------------------------------------------------
功 能:延时
}
-------------------------------------------------------------------------*/
void delay(void)
{unsigned char i;
for(i=0;i<30;i++) i++;
}
/*-------------------------------------------------------------------------
功 能: 从EFlash通过SPI_MI口读入一个字节
-------------------------------------------------------------------------*/
VUINT8 SPI_HostReadByte(void) //SCK下降沿
{
VUINT8 rByte=0,i=0;
bool j;
for(i=0;i<8;i++)
{
SPI_SCK_PutVal(1);//clk=1;
delay();
rByte<<=1;
SPI_SCK_PutVal(0);
j=SPI_MI_GetVal();
if(j==0)
{
rByte|=0x01;
}
else {
rByte&=~0x01;
}
}
return rByte;
}
/*-------------------------------------------------------------------------
功 能: 将1字节数据wByte由SPI_MO口发送给EFlash
//-------------------------------------------------------------------------*/
void SPI_HostWriteByte(VUINT8 wByte) //SCK上升沿
{
作者:
seuzhuxi
时间:
2013-3-15 20:58
void SPI_HostWriteByte(VUINT8 wByte) //SCK上升沿
{
VUINT8 i;
for(i=0;i<8;i++)
{
if((wByte&0x80)==0x80){
SPI_MI_PutVal(1);
}
else {
SPI_MI_PutVal(0);
}
SPI_SCK_PutVal(1);
wByte<<=1;
delay();
SPI_SCK_PutVal(0);
delay();
}
}
/*-------------------------------------------------------------------------
功 能: 读状态寄存器的值
-------------------------------------------------------------------------*/
VUINT8 EFlash_StatusRegisterRead(void)
{
VUINT8 stateRegister;
SPI_CS_PutVal(0);
//clr_spi_cs; //使能片选
SPI_HostWriteByte(0xD7); //发送读寄存器命令
stateRegister = SPI_HostReadByte(); //读取状态值
// RS232_SendChar(0x33);
SPI_CS_PutVal(1);//set_spi_cs;//片选无效
return stateRegister;
}
/*-------------------------------------------------------------------------
功 能: 将指针指向的地址数据发送给Flash
-------------------------------------------------------------------------*/
void EFlash_BufferWrite(VUINT16 BFA,VUINT8 *pHeader,VUINT16 len)
{
VUINT8 i=0;
VUINT8 stateRegister;
stateRegister = EFlash_StatusRegisterRead();//读状态寄存器
while(!(stateRegister & 0x80))//是否空闲
{
RS232_SendChar(0x1B);
//Cpu_Delay100US(100);
stateRegister = EFlash_StatusRegisterRead();
}
SPI_CS_PutVal(0);
SPI_HostWriteByte(0x84);
SPI_HostWriteByte(0x00);
SPI_HostWriteByte((unsigned char)(BFA>>8));
SPI_HostWriteByte((unsigned char)BFA);
for(i=0;i<len;i++)
{
SPI_HostWriteByte(pHeader[i]);
}
//SPI_SendBlock(pHeader,len,snd);
SPI_CS_PutVal(1); //片选无效
}
作者:
seuzhuxi
时间:
2013-3-15 20:59
void EFlash_MemoryWrite(VUINT16 PA,VUINT16 BFA,VUINT8 *pHeader,VUINT16 len)
{
VUINT8 stateRegister;
//将长度为len,首地址为pHeader的数写入以BFA为首地址的缓冲区中
EFlash_BufferWrite(BFA,pHeader,len);
//等待 AT45DB321D 的ready
stateRegister = EFlash_StatusRegisterRead();
while(!(stateRegister & 0x80))//空闲
{
RS232_SendChar(0x1F);
//Cpu_Delay100US(1);
stateRegister = EFlash_StatusRegisterRead();
}
SPI_CS_PutVal(0);//使能片选
//执行命令--带自擦除的缓存内容写主存
SPI_HostWriteByte(0x83);//缓冲1的命令
//页地址
SPI_HostWriteByte((VUINT8)(PA>>7));
SPI_HostWriteByte((VUINT8)(PA<<1));
SPI_HostWriteByte(0x00);
SPI_CS_PutVal(1);//片选无效
Cpu_Delay100US(10);
}
/*-------------------------------------------------------------------------
功 能: 读主存
-------------------------------------------------------------------------*/
void EFlash_MemoryRead(VUINT16 PA,VUINT16 BFA,VUINT8 *pHeader,VUINT16 len)
{
VUINT16 i=0,j=0;
word *Rcv=0;
VUINT8 stateRegister;
stateRegister = EFlash_StatusRegisterRead();//读状态寄存器
while(!(stateRegister & 0x80))//空闲
{
//RS232_SendChar(0x11);
stateRegister = EFlash_StatusRegisterRead();
}
SPI_CS_PutVal(0);
SPI_HostWriteByte(0xD2);
SPI_HostWriteByte((VUINT8)(PA>>7));
SPI_HostWriteByte((VUINT8)(((PA&0x00FF)<<1)|(BFA>>8)));
SPI_HostWriteByte((VUINT8)BFA);
//发送4字节任意码主要是为了给芯片初始化读操作时间的
//在此之后,再变化时钟就可以读取数据了
for(i=0;i<4;i++)
{
SPI_HostWriteByte(0x00); //4字节任意码
}
//读取指定长度数据
for(i=0;i<len;i++)
{
pHeader[i] = SPI_HostReadByte();
}
//SPI_RecvBlock(pHeader,len,Rcv);
SPI_CS_PutVal(1);
}
/*-------------------------------------------------------------------------
功 能: 页擦除
-------------------------------------------------------------------------*/
void EFlash_PageErase(VUINT32 padr)
{
VUINT16 PA;
VUINT8 stateRegister;
PA=padr/512;
stateRegister = EFlash_StatusRegisterRead();//读状态寄存器
while(!(stateRegister & 0x80))//空闲
{
//RS232_SendChar(0x12);
stateRegister = EFlash_StatusRegisterRead();
}
SPI_CS_PutVal(0);
SPI_HostWriteByte(0x81);
SPI_HostWriteByte((VUINT8)(PA>>7));
SPI_HostWriteByte((VUINT8)(PA<<1));
SPI_HostWriteByte(0x00);
SPI_CS_PutVal(1);
}
作者:
lianghuihao
时间:
2013-3-15 20:59
你用它干嘛的啊啊?
作者:
seuzhuxi
时间:
2013-3-16 12:51
lianghuihao 发表于 2013-3-15 20:59
你用它干嘛的啊啊?
保存单片机采集到的数据,能够实现数据的存储和读取
作者:
lianghuihao
时间:
2013-3-16 13:04
摄像头 保存赛道数据,够快吗?
作者:
seuzhuxi
时间:
2013-3-16 13:19
我这个不是智能车,就是一个测量数据,记录数据的程序,用到的是飞思卡尔的单片机,AD采集到数据后存到Flash中,上位机需要的话可以读取Flash中的数据,谢谢你
欢迎光临 智能车制作 (http://111.231.132.190/)
Powered by Discuz! X3.2