初中生
最后登录1970-1-1
在线时间 小时
注册时间2014-9-18
|
这是要读取FLASH的deviceID 程序读到第一个灯亮就死了。。。 |
但是当把void SPI_FLASH_SendByte(u8 byte) 改成u8 SPI_FLASH_SendByte(u8 byte)
u8 SPI_FLASH_ReadDeviceID(void) 这个函数里的 t = SPI_FLASH_ReceiveByte(); 改成 t = SPI_FLASH_SendByte(0);
调试助手上就显示了 为什么啊。。。。 谁能告诉我。。。。。 |
#include "stm32f10x.h"
#include "stdio.h"
#include "usart1.h"
#include "spi.h"
#include "led.h"
/**
* @brief Ö÷oˉêy
* @param ÎT
* @retval ÎT
*/
u8 DeviceID = 0, FlashID = 0;
void Delay(__IO uint32_t nCount);
int main(void)
{
LED_GPIO_Config();
USART1_Config();
printf("\r\n 这是一个2M串行flash(W25Q16)实验 \r\n");
SPI_FLASH_InitConfig();
LED1(ON);
DeviceID = SPI_FLASH_ReadDeviceID();
LED2(ON);
printf("\r\n Device ID is 0x%X\r\n", DeviceID);
LED3(ON);
while(1);
}
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
#include "spi.h"
/*********************************************************************************
#define PageSize 256
#define BUSY 0x01
#define W25Q_WriteEnable 0x06
#define W25Q_PowerDown 0xB9
#define W25Q_PageWrite 0x02
#define W25Q_ReadStatus 0x05
#define W25Q_DeviceID 0xAB
#define W25Q_ManufacturerID 0x90
#define W25Q_ReadData 0x03
#define W25Q_JEDECID 0X9F
***********************************************************************************/
void SPI_FLASH_InitConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_FLASH_CS_HIGH();
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
u8 SPI_FLASH_ReadDeviceID(void)
{
u8 t = 0;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(W25Q_DeviceID);
SPI_FLASH_SendByte(0);
SPI_FLASH_SendByte(0);
SPI_FLASH_SendByte(0);
t = SPI_FLASH_ReceiveByte();
SPI_FLASH_CS_HIGH();
return t;
}
void SPI_FLASH_SendByte(u8 byte)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET );
SPI_I2S_SendData(SPI1, byte);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == SET);
SPI_I2S_ReceiveData(SPI1);
}
u8 SPI_FLASH_ReceiveByte(void)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET );
SPI_I2S_SendData(SPI1, 0);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == SET);
return SPI_I2S_ReceiveData(SPI1);
}
|
|