打印

新手求助:SPI读写Flash问题

[复制链接]
3257|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
lasic|  楼主 | 2012-3-1 09:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用一块M16接W25Q80,用232输出看不出来。程序卡在main里给flash写,求各位前辈帮帮忙。程序如下:
#include<avr/io.h>
#include<util/delay.h>
#include"usart.h"
#define DD_MOSI 5
#define DD_MISO 6
#define DD_SCK  7
#define DD_CS   0

#define Clr_SPI_FLASH_CS { PORTB&=~(1<<DD_CS);}
#define Set_SPI_FLASH_CS { PORTB|=(1<<DD_CS);}

#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long

#define W25Q80_WriteEnable      0x06
#define W25Q80_WriteDisable     0x04
#define W25Q80_ReadStatusReg    0x05
#define W25Q80_WriteStatusReg   0x01
#define W25Q80_ReadData         0x03
#define W25Q80_FastReadData     0x0b
#define W25Q80_PageProgram      0x02
#define W25Q80_BlockErase       0xd8
#define W25Q80_SectorErase      0x20
#define W25Q80_ChipErase        0xc7
#define W25Q80_PowerDown        0xb9
#define W25Q80_ReleasePowerDown 0xab
#define W25Q80_DeviceID         0xab
#define W25Q80_ManufactID       0x90
#define W25Q80_JedecDeviceID    0x9f

uchar Text_Buffer[]={"SPI_W25Q80 TEST"};
#define SIZE sizeof(Text_Buffer)

void Port_init(void)
{
  DDRA  =0x1f;
PORTA =0x00;
DDRB  =0xbf;
PORTB =0xdF;//PB0口设置为W25Q80的CS端,初始上拉输出
   
    DDRC  =0xfc;
PORTC =0x03;
DDRD  =0xf6;   
PORTD =0x03;  
}

unsigned char SPI_MasterTransmit(uchar data) //SPI传输数据函数
{
  SPDR=data;
  while(!(SPSR&(1<<SPIF)));  //等待传输结束
  return SPDR;
}

void SPI_MasterInit(void)           //SPI初始化
{
  DDRB=(1<<DD_MOSI)|(1<<DD_SCK);
  SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0);//使能SPI,使能主机模式,设置时钟分频为f/16
  SPI_MasterTransmit(0xff);
}

void SPI_FLASH_Write_Enable(void) //SPI写使能
{
  Clr_SPI_FLASH_CS;          //置低选中flash
  SPI_MasterTransmit(W25Q80_WriteEnable); //写使能命令发送
  Set_SPI_FLASH_CS;
}
void SPI_FLASH_Write_Disable(void) //SPI写取消
{
  Clr_SPI_FLASH_CS;          //置低选中flash
  SPI_MasterTransmit(W25Q80_WriteDisable); //发送写取消命令
  Set_SPI_FLASH_CS;
}
uchar SPI_FLASH_ReadSR(void)      //读取状态寄存器状态
{
  uchar byte=0;
  Clr_SPI_FLASH_CS;
  SPI_MasterTransmit(W25Q80_ReadStatusReg); //发送读取状态寄存器1命令
  byte=SPI_MasterTransmit(0xff);//读取一个字节
  Set_SPI_FLASH_CS; PORTB&=~(1<<;PB4);
  return byte;
}
void SPI_FLASH_WriteSR(uchar sr)  //写SPI_FLASH状态寄存器
{
  Clr_SPI_FLASH_CS;
  SPI_MasterTransmit(W25Q80_WriteStatusReg); //发送写取状态寄存器命令
  SPI_MasterTransmit(sr);
  Set_SPI_FLASH_CS;
}

void SPI_FLASH_Wait_Busy(void)   //Flash等待空闲状态函数
{
    while((SPI_FLASH_ReadSR()&0x01)==0x01); //等待状态寄存器BUSY位清空
}

void SPI_FLASH_Read(uchar *Buffer,ulong ReadAddr,uint ReadByte)
//从Flash的ReadAddr地址读ReadByte个字节数据
{
  uint i;
  Clr_SPI_FLASH_CS;
  SPI_MasterTransmit(W25Q80_ReadData);    //发送读flash命令0x03
  SPI_MasterTransmit((uchar)(ReadAddr>>16));
  SPI_MasterTransmit((uchar)(ReadAddr>>8));
  SPI_MasterTransmit((uchar)(ReadAddr));
  for(i=0;i<ReadByte;i++)
  {
    Buffer=SPI_MasterTransmit(0xff); //循环读数
  }
  Set_SPI_FLASH_CS;
  
}
void SPI_FLASH_Write_Page(uchar *Buffer,ulong WriteAddr,uint ReadByte)
//向Flash的ReadAddr地址写ReadByte个字节数据
{
  uint i;
  SPI_FLASH_Write_Enable();
  Clr_SPI_FLASH_CS;
  SPI_MasterTransmit(W25Q80_PageProgram);PORTB&=~(1<<;PB1);//发送页读取命令
  SPI_MasterTransmit((uchar)(WriteAddr>>16)); PORTB&=~(1<<;PB2);
  SPI_MasterTransmit((uchar)(WriteAddr>>8)); PORTB&=~(1<<;PB3);
  SPI_MasterTransmit((uchar)(WriteAddr));
  for(i=0;i<ReadByte;i++) SPI_MasterTransmit(Buffer);
  Set_SPI_FLASH_CS;
  SPI_FLASH_Wait_Busy();
}
void main()
{
  ulong a;
  uchar x,y;
  Port_init();
  SPI_MasterInit();  //SPI主机初始化
  Usart_Init();      //串口初始化
  a=0;
  //Usart_PutString("\n\rStart Program");
  //newline();
//    x=SPI_FLASH_ReadSR();
//   Usart_PutChar(x);
   SPI_FLASH_Write_Page((uchar*)Text_Buffer,100,SIZE);//程序卡在这里,没输出下面的内容
   //Usart_PutString("\n\rWrite Finish.");
      y=SPI_FLASH_ReadSR();
   Usart_PutChar(y);
   SPI_FLASH_Read(a,100,SIZE);
   //Usart_PutChar(a);
   //newline();
      while(1);
}

相关帖子

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

本版积分规则

0

主题

8

帖子

1

粉丝