各位好 我最近在自学stm32现在在做spi读写M25P16的程序 但是现在遇到了问题现在不知道该怎么办了 希望有经验的朋友帮忙指点下
以下是我的程序
#include <stm32f10x_lib.h>
#include "mysysinit.h"
#include "delay.h"
GPIO_InitTypeDef GPIO_InitStructure;//GPIO结构变量
SPI_InitTypeDef SPI_InitStructure;//设置一个spi的结构变量
////////GPIO的设置/////////
void gpioinit()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void spi_init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE);
SPI_Cmd(SPI1,DISABLE);
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);
}
static u8 sendrecieve(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 ))==RESET);
return SPI_I2S_ReceiveData(SPI1);
//SPI_I2S_ClearFlag(SPI1,SPI_FLAG_RXNE);
}
int main()
{
u8 aa;
my_systeminit();
gpioinit();
spi_init();
delayinit(72);
while(1)
{
sendrecieve(0x50);
delay_ms(10000);
aa=sendrecieve(0x50);
if(aa==0x50)GPIO_SetBits(GPIOC,GPIO_Pin_6);
delay_ms(10000);
}
} |