📄 main.c
字号:
#include <stm32f10x_lib.h>
//#include <stdio.h>
FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_FLAG)/*检查指定的SPI标志位设置与否*/
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
assert_param(IS_SPI_GET_FLAG(SPI_FLAG));
/* Check the status of the specified SPI/I2S flag */
if ((SPIx->SR & SPI_FLAG) != (uint16_t)RESET)
{
/* SPI_I2S_FLAG is set */
bitstatus = SET;
}
else
{
/* SPI_I2S_FLAG is reset */
bitstatus = RESET;
}
/* Return the SPI_I2S_FLAG status */
return bitstatus;
}
uint8_t SPI_FLASH_SendByte(uint8_t byte)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI_FLASH, SPI_I2S_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_SendData(SPI_FLASH, byte);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI_FLASH, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_ReceiveData(SPI_FLASH);
}
/**********************************************************************
* 名 称:Delay()
* 功 能:延时
* 入口参数:cnt
* 出口参数:
-----------------------------------------------------------------------
* 说明:
***********************************************************************/
void Delay(u16 cnt) {
u16 i,j;
for (i=0;i<cnt;i++)
{ for (j=0;j<1000;j++)
{ }
}
}
/**********************************写使能**********************************************/
void writeEnable(void)
{
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(CMD_WREN);
SPI_FLASH_CS_HIGH();
}
/**********************发送数据到指点地址***************************/
void writedate(unsigned int addr,unsigned char date)
{ unsigned char addh;
addh=(addr&0xff00)>>5;
writeEnable();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0x02|addh);
SPI_FLASH_SendByte(addr&0x00ff);
//SPI_FLASH_SendByte(0xA5)
SPI_FLASH_SendByte(date);
SPI_FLASH_CS_HIGH();
}
/*************************************读一个指定地址的数******************************************/
unsigned char readdat(unsigned int addr)
{
unsigned char temp,addh;
addh=(addr&0xff00)>>5;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(0x03|addh);
SPI_FLASH_SendByte(addr&0x00ff);
temp=SPI_FLASH_SendByte(0xA5);
SPI_FLASH_CS_HIGH();
return temp;
}
int main(void)
{
u8 date;
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
//SystemInit();
RCC_APB2PeriphClockCmd(SPI_FLASH_CLK | SPI_FLASH_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* Configure SPI pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = SPI_FLASH_PIN_SCK | SPI_FLASH_PIN_MISO | SPI_FLASH_PIN_MOSI;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(SPI_FLASH_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI_FLASH_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(SPI_FLASH_CS_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
SPI_Cmd(SPI_FLASH, DISABLE);
/* SPI configuration */
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_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI_FLASH, &SPI_InitStructure);
/* Enable the SPI */
SPI_Cmd(SPI_FLASH, ENABLE);
writedate(0,0x38);
date=readdat(0);
// date=0x32;
/*判断接收与发送数据是否相等*/
while(date==0x38)
{
GPIO_SetBits(GPIOC, GPIO_Pin_11);
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -