您的位置 首页 主动

STC52单片机的SD卡底层驱动——SPI

最近在做SD卡以及单片机上的FAT32文件系统,这个是我参考别人的源程序以后写出来的自己的SD卡驱动,如有不完整的地方,还望大家不吝指教。

最近在做SD卡以及单片机上的FAT32文件体系,这个是我参阅他人的源程序今后写出来的自己的SD卡驱动,如有不完整的当地,还望我们不吝珠玉。

界说声明:

#ifndef _SD_H_
#define _SD_H_

#include

sbit SD_CS=P2^7; //SD卡片选信号
sbit SD_SI=P2^6; //SD卡数据输入
sbit SD_SCL=P2^5; //SD卡时钟信号
sbit SD_SO=P2^4; //SD卡数据输出

#define DELAY_TIME 100 //SD卡的复位与初始化时SPI延时
#define TRY_TIME 200 //SD卡写入指令今后,读取SD卡的回应次数

#define INIT_CMD0_ERROR 0x01 //CMD0过错
#define INIT_CMD1_ERROR 0x02//CMD1过错
#define WRITE_BLOCK_ERROR 0x03//写块过错
#define READ_BLOCK_ERROR 0x04//读块过错

unsigned char SD_Reset();
unsigned char SD_Init();
unsigned char SD_Write_Sector(unsigned long addr,unsigned char *buffer);
unsigned char SD_Read_Sector(unsigned long addr,unsigned char *buffer);

#endif

代码完成:

#include”SD.h”
#include”myfun.h”

unsigned char Is_init;

unsigned char bdata _dat;
sbit _dat7 = _dat^7;
sbit _dat6 = _dat^6;
sbit _dat5 = _dat^5;
sbit _dat4 = _dat^4;
sbit _dat3 = _dat^3;
sbit _dat2 = _dat^2;
sbit _dat1 = _dat^1;
sbit _dat0 = _dat^0;

void SD_spi_write(unsigned char x)
{
_dat=x;

SD_SI = _dat7;
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL=1;
if(Is_init) Delay20us(DELAY_TIME);

SD_SI = _dat6;
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL=1;
if(Is_init) Delay20us(DELAY_TIME);

SD_SI = _dat5;
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL=1;
if(Is_init) Delay20us(DELAY_TIME);

SD_SI = _dat4;
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL=1;
if(Is_init) Delay20us(DELAY_TIME);

SD_SI = _dat3;
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL=1;
if(Is_init) Delay20us(DELAY_TIME);

SD_SI = _dat2;
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL=1;
if(Is_init) Delay20us(DELAY_TIME);

SD_SI = _dat1;
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL=1;
if(Is_init) Delay20us(DELAY_TIME);

SD_SI = _dat0;
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL=1;
if(Is_init) Delay20us(DELAY_TIME);

}

unsigned char SD_spi_read()
{
SD_SO = 1;

SD_SCL = 1;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
_dat7 = SD_SO;

SD_SCL = 1;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
_dat6 = SD_SO;

SD_SCL = 1;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
_dat5 = SD_SO;

SD_SCL = 1;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
_dat4 = SD_SO;

SD_SCL = 1;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
_dat3 = SD_SO;

SD_SCL = 1;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
_dat2 = SD_SO;

SD_SCL = 1;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
_dat1 = SD_SO;

SD_SCL = 1;
if(Is_init) Delay20us(DELAY_TIME);
SD_SCL = 0;
if(Is_init) Delay20us(DELAY_TIME);
_dat0 = SD_SO;

return(_dat);
}

unsigned char SD_Write_Cmd(unsigned char *pcmd)
{
unsigned char temp,time=0;

SD_CS=1;
SD_spi_write(0xff); //进步程序的兼容性
SD_CS=0;

SD_spi_write(pcmd[0]);
SD_spi_write(pcmd[1]);
SD_spi_write(pcmd[2]);
SD_spi_write(pcmd[3]);
SD_spi_write(pcmd[4]);
SD_spi_write(pcmd[5]);
do
{
temp = SD_spi_read();
time++;
}
while((temp==0xff)&&(timereturn(temp);
}

unsigned char SD_Reset()
{
unsigned char time,temp,i;
unsigned char pcmd[] = {0x40,0x00,0x00,0x00,0x00,0x95};

Is_init = 1;

SD_CS = 1; //封闭片选
for(i=0;i<0x0f;i++){
SD_spi_write(0xff); //弥补120个时钟,唤醒SPI
}
SD_CS = 0; //翻开片选
time=0;
do{
temp=SD_Write_Cmd(pcmd);//写入CMD0
time++;
if(time==TRY_TIME){
SD_CS = 1; //封闭片选
return(INIT_CMD0_ERROR); //CMD0 写入失利
}
}while(temp!=0x01);

SD_CS = 1; //封闭片选

SD_spi_write(0xff);//弥补8个时钟,进步兼容性

return 0 ; //回来0,复位操作成功
}

unsigned char SD_Init()
{
unsigned char time,temp;
unsigned char pcmd[] = {0x41,0x00,0x00,0x00,0x00,0xff};
SD_CS = 0; //翻开片选

time = 0;
do{
temp = SD_Write_Cmd(pcmd);
time++;
if(time==TRY_TIME){ //CMD1写入失利
SD_CS=1;
return(INIT_CMD1_ERROR);
}
}while(temp!=0);

Is_init = 0; //讲Is_init设置为0,进步SPI速度
SD_CS = 1;//封闭片选
SD_spi_write(0xff); //弥补8个时钟
return(0);
}

unsigned char SD_Write_Sector(unsigned long addr,unsigned char *buffer)
{
unsigned char temp,time;
unsigned int i;
unsigned char pcmd[]={0x58,0x00,0x00,0x00,0x00,0xff};

addr<<=9;
pcmd[1] = ((addr&0xff000000)>>24);
pcmd[2] = ((addr&0x00ff0000)>>16);
pcmd[3] = ((addr&0x0000ff00)>>8);

SD_CS = 0;
time = 0;

do{
temp=SD_Write_Cmd(pcmd);//写入CMD0
time++;
if(time == TRY_TIME)
{
SD_CS = 1; //封闭片选
return(temp); //写入失利
}
}while(temp!=0);

for(i=0;i<100;i++) //刺进若干个字节的时钟,坚持稳定性
{
SD_spi_write(0xff);
}

SD_spi_write(0xfe); //写入0xfe,后边便是要写入的数据

for(i=0;i<512;i++)
{
SD_spi_write(buffer[i]);
}

SD_spi_write(0xff); //两个字节的CRC校验码
SD_spi_write(0xff);

temp = SD_spi_read(); //读取回来值
if((temp&0x1f)!=0x05){
SD_CS = 1;
return(WRITE_BLOCK_ERROR); //写块数据失利
}
while(SD_spi_read()!=0xff);//SD卡忙状况,等候

SD_CS = 1;

SD_spi_write(0xff);//弥补8个时钟
return(0);

}

unsigned char SD_Read_Sector(unsigned long addr,unsigned char *buffer)
{
unsigned int j;
unsigned char time,temp;
unsigned char pcmd[]={0x51,0x00,0x00,0x00,0x00,0xff}; //CMD17的字节序列

addr<<=9; //addr=addr*512 将块地址(扇区地址)转为字节地址

pcmd[1]=((addr&0xff000000)>>24);//将字节地址写入到CMD17字节序列中
pcmd[2]=((addr&0x00FF0000)>>16);
pcmd[3]=((addr&0x0000FF00)>>8);

SD_CS=0;//翻开片选

time=0;
do{
temp = SD_Write_Cmd(pcmd);
time++;
if(time==TRY_TIME){
return(READ_BLOCK_ERROR);
}
}while(temp!=0);

while(SD_spi_read()!=0xfe); //读到0xfe,阐明后边是512字节的数据

for(j=0;j<512;j++)
{
buffer[j]=SD_spi_read();
}

SD_spi_read(); //读取两个字节的CRC校验码
SD_spi_read();

SD_CS=1;

SD_spi_write(0xff); //弥补8个时钟

return 0;

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/zhudong/261875.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部