打印

SPI flash读到的数据全是0

[复制链接]
645|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
第8号打板工|  楼主 | 2020-4-26 11:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
spi_flash,将接收到的数据传到flash的地址0位置,然后取出串口读入的数据,但是在调试时看到flash接收数组中的数据全是0
以下是代码
#include "spi.h"

void spi_gpio_init(void )
{
RCC_APB2PeriphClockCmd(SPI_RCC_CLK  , ENABLE);  
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //

/***MOSIÖ÷»úÊä³ö´Ó»úÊäÈë**/
  GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI_MOSI_PORT , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN;
GPIO_Init(SPI_MISO_PORT , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = SPI_CLK_PIN;
GPIO_Init(SPI_CLK_PORT , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = SPI_CLK_PIN;
GPIO_Init(SPI_CLK_PORT , &GPIO_InitStructure);

}
void SPI1_init(void)
{
spi_gpio_init();
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b ;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
SPI_FLASH_CS_HIGH() ;
}
u8 spi_readwrite_char(u8  data)
{
u8 valu;
//SPI_FLASH_CS_LOW();

while (SPI_I2S_GetFlagStatus(SPI1 ,SPI_I2S_FLAG_TXE)==RESET );
SPI_I2S_SendData(SPI1, data);
while (SPI_I2S_GetFlagStatus(SPI1 ,SPI_I2S_FLAG_RXNE)==RESET );
valu=SPI_I2S_ReceiveData( SPI1);
// SPI_FLASH_CS_HIGH() ;
return  valu;
}

void spi_write_enable(void)
{
SPI_FLASH_CS_LOW();
spi_readwrite_char(0x06);
SPI_FLASH_CS_HIGH() ;
}
void spi_WaitWriteEnd(void)
{
u8 valu;
SPI_FLASH_CS_LOW();
do
valu=spi_readwrite_char (0x05);
while((valu&0x01)==SET );
SPI_FLASH_CS_HIGH() ;
}
void spi_SetorErase(u32 addr)
{
spi_write_enable();
spi_WaitWriteEnd();
SPI_FLASH_CS_LOW();
spi_readwrite_char(0x20);
spi_readwrite_char((addr&0xff0000)>>16);
spi_readwrite_char((addr&0xff00)>>16);
spi_readwrite_char(addr&0xff);
SPI_FLASH_CS_HIGH() ;
spi_WaitWriteEnd();

}
void spi_PageWrite(u32 addr,char * data,u32 num)
{
spi_write_enable ();
SPI_FLASH_CS_LOW();
spi_readwrite_char(0x02);
spi_readwrite_char((addr&0xff0000)>>16);
spi_readwrite_char((addr&0xff00)>>8);
spi_readwrite_char((addr)&0xff);
while (num--)
{
spi_readwrite_char(*data);
}
data ++;
SPI_FLASH_CS_HIGH() ;
spi_WaitWriteEnd();
}
void spi_WriteBuffer(u32 addr, u8 * data,u32 num)
{

}
void  spi_ReadBuffer(u32 addr ,u32 num,char *RecAddr)
{
SPI_FLASH_CS_LOW();
spi_readwrite_char(0x03);
spi_readwrite_char((addr&0xff0000)>>16);
spi_readwrite_char((addr&0xff00)>>8);
spi_readwrite_char((addr)&0xff);
while(num--)
{
  *RecAddr =spi_readwrite_char(0xff);
  RecAddr ++;
}
SPI_FLASH_CS_HIGH();
}

主函数部分
/**************´®¿ÚʵÑé***************/
#include "stm32f10x.h"
#include "usart.h"
#include "main.h"
u8 send_buffer[100];
char   count =1;
char flash_BufferRec[100];
char * flash_BufferRecPointer;
int main(void)
{
  NVIC_Configration();
  usart_configration();
  basictimerInit();
  LCD_Init();
  TIM_Cmd (TIM6 ,ENABLE );
   SPI1_init();
  while(1)
   {
   char * Recbufferpointer=Rec_Buffer;
  if(Cmdparse ()==0)
  {
   Send_String("±³¹â´ò¿ª\n");
   spi_SetorErase(0);
   spi_PageWrite (0,Recbufferpointer ,6);
   flash_BufferRecPointer=flash_BufferRec ;
   
   spi_ReadBuffer(0,6,flash_BufferRecPointer);
   usart_send (flash_BufferRecPointer,4);
   Clear_RecBuff();
   LCD_BackLightEnable(1);
  }
  else if (Cmdparse ()==1)
  {
   Send_String("±³¹â¹Ø±Õ\n");
  // Send_String (&count );
   printf("\r\n CH1 value = %d V \r\n",count );
   Clear_RecBuff();
   LCD_BackLightEnable(0);
  }
  else if (Cmdparse ()==2)
  {
   printf ("¶¨Ê±Æ÷ÒÑÍ£Ö¹\n");
   Clear_RecBuff();
   TIM_Cmd (TIM6 ,DISABLE );
  }
  LCD_WriteCmd (0xff);
   }
}

使用特权

评论回复

相关帖子

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

本版积分规则

393

主题

393

帖子

0

粉丝