求STM32 读SD卡程序 寄存器版的 本人不胜感激 我老复位为什么不行?
#include "stm32f10x.h"
#include "delay.h"
#include "usart2.h"
#define SET_SD_CS GPIO_SetBits(GPIOB,GPIO_Pin_7)
#define RESET_SD_CS GPIO_ResetBits(GPIOB,GPIO_Pin_7)
#define SD_RESET_CMD 0x40
#define SD_INIT_CMD 0X41
#define SD_READ_CMD 0X51
#define SD_WRITE_CMD 0x58
/*---------------------------------------------------
函数名称:SPI1_Configuation()
函数功能:初始化SPI
入口参数:无
出口参数:无
----------------------------------------------------*/
void SPI1_Configuation(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE); //开启 SPI1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,ENABLE); //开启IO口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); //PA5 --> SD_SCLK PA7 -->SD_MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); //PA6 -->SD_MISO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure); //PB7 为SD_CS
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双线全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主机模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8位数据
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件设置片选
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //SPI时钟 64分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //写数据 高位在前
SPI_Init(SPI1,&SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE); //使能SPI 要用到的外设 最后都要使能
}
void SDWrite_Byte(vu8 dat)
{
SPI_I2S_SendData(SPI1,dat);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) != SET); //若发送缓存不为空 等待
SPI_I2S_ClearFlag(SPI1,SPI_I2S_FLAG_TXE); //必须软件清零!
}
vu8 SDRead_Byte(void)
{
u8 value = 0;
value = SPI_I2S_ReceiveData(SPI1); //读 数据清除标志位
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) == SET); //若接收缓存非空等待
return (value);
}
void SD_WriteCMD(u8 CMD,u32 Addr,u8 crc)
{
u8 a[4] = {0};
a[0] = (u8) (Addr);
a[1] = (u8) (Addr >> 8);
a[2] = (u8) (Addr >> 16);
a[3] = (u8) (Addr >> 24);
SDWrite_Byte(CMD);
SDWrite_Byte(a[3]);
SDWrite_Byte(a[2]);
SDWrite_Byte(a[1]);
SDWrite_Byte(a[0]);
SDWrite_Byte(crc);
}
u8 SD_Init(void)
{
/*-------复位SD卡--------*/
u8 i=0;
u8 j=0;
SET_SD_CS; //先拉高片选线
for(i=0; i<10; i++)
{
SDWrite_Byte(0xff); //发送74个以上空CLK
}
RESET_SD_CS;
// Delay_us(1);
// SDWrite_Byte(0xff); //发送若干个同步时钟 等待SD卡响应
do
{
SD_WriteCMD(SD_RESET_CMD,0,0x95);
j++;
if(j >= 100)
{
return 0;
}
}while(SDRead_Byte() != 0x01);
SET_SD_CS;
SDWrite_Byte(0xff); //发8个空闲时钟
return 1;
}
/*---------------------------main----------------------------*/
int main(void)
{
u8 flag = 0;
SysTick_Init();
USART2_Configuation();
SPI1_Configuation();
printf("----------------这是SD卡实验-----------------\n");
do
{
flag = SD_Init();
if(flag == 0)
printf("---------SD卡响应失败----------\n");
else
printf("---------SD卡响应成功----------\n");
}while(flag == 0);
while(1)
{
// flag = SDWrite_Block();
// if(flag == 1)
// printf("---------SD卡写数据成功----------\n");
// else
// printf("---------SD卡写数据失败----------\n");
}
}
/*---------------------------main----------------------------*/
|