STC52单片机的SD卡底层驱动——SPI
最近在做SD卡以及单片机上的FAT32文件系统,这个是我参考别人的源程序以后写出来的自己的SD卡驱动,如有不完整的地方,还望大家不吝指教。
定义声明:
#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
代码实现:
#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;
{
_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()
{
STC52单片机的SD卡底层驱动SP 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)