STC52單片機(jī)的SD卡底層驅(qū)動(dòng)——SPI
最近在做SD卡以及單片機(jī)上的FAT32文件系統(tǒng),這個(gè)是我參考別人的源程序以后寫出來(lái)的自己的SD卡驅(qū)動(dòng),如有不完整的地方,還望大家不吝指教。
本文引用地址:http://butianyuan.cn/article/201611/319664.htm定義聲明:
#ifndef _SD_H_
#define _SD_H_
#include
sbit SD_CS=P2^7;
sbit SD_SI=P2^6;
sbit SD_SCL=P2^5;
sbit SD_SO=P2^4;
#define DELAY_TIME 100
#define TRY_TIME 200
#define INIT_CMD0_ERROR
#define INIT_CMD1_ERROR
#define WRITE_BLOCK_ERROR 0x03
#define READ_BLOCK_ERROR
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
代碼實(shí)現(xiàn):
#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
while((temp==0xff)&&(time
}
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)
{
評(píng)論