搜档网
当前位置:搜档网 › SD卡读写程序

SD卡读写程序



SD卡读写程序

//实验目的:学习SD卡的操作

//软件设计
// 1、SD卡采用SPI通信
// 2、先往SD里顺序写入0-255共256个数据,然后再读回送LCD1602显示
//硬件要求:
// 拨码开关S11置ON
// 跳线J18全部接通

#include //dsPIC30F6014标准头文件

_FOSC(CSW_FSCM_OFF & XT_PLL4); //4倍频晶振,Failsafe 时钟关闭
_FWDT(WDT_OFF); //关闭看门狗定时器
_FBORPOR(PBOR_OFF & MCLR_EN); //掉电复位禁止,MCLR复位使能。
_FGS(CODE_PROT_OFF); //代码保护禁止

#define cs PORTGbits.RG9 //定义SD卡片选脚

#define rs https://www.sodocs.net/doc/ef2205906.html,TB4 //定义LCD控制位(注意这里只能用LATB寄存器,不能直接用PORTB寄存器)
#define rw https://www.sodocs.net/doc/ef2205906.html,TB5
#define e https://www.sodocs.net/doc/ef2205906.html,TB6

unsigned char __attribute__((address(0x900))) lcd[3]={0,0,0};

void spi_init(); //申明系统初始函数
void spi_low(); //申明产生低波特率函数(SD卡初始化使用)
void spi_high(); //申明产生高波特率函数(SD卡初始化后使用)
unsigned char sd_reset(); //申明SD卡初始化函数
unsigned char SD_SendCommand(unsigned char cmd,unsigned long arg); //申明写SD卡命令函数
unsigned char SPI_WriteByte(unsigned char val); //申明写一字节函数
unsigned char SPI_ReadByte(void); //申明接收一字节函数
unsigned char SD_WriteSingleBlock(unsigned long sector); //申明写SD卡单BLOCK数据函数
unsigned char SD_ReadSingleBlock(unsigned long sector); //申明读SD卡单BLOCK数据函数
void lcd_display(); //申明结果显示函数
void delay(); //申明延时函数(显示时用)


//系统初始化函数
void spi_init()
{
TRISG="0x00d0"; //设置SDI为输出,其他C口为输出
TRISB="0X0000"; //设置B口为输出
TRISD="0X0000"; //设置D口为输出
SPI2CON=0x0278; //空闲时总线为高电平,fosc/64
SPI2STAT=0x8000; // 输出数据的末尾采样输入数据,上升沿发送数据
}

//*************************写LCD程序****************************************
//写一个字节数据函数
//在电平发生改变后需要插入一段延时时间,否则LCD反应不过来。
void write(unsigned char x)
{
PORTD="x"; //待显示数据送PORTD口
delay();
rs="1"; //该字节数据为数据,而不是命令
delay();
rw="0"; //此次操作为写,而不是读
delay();
e="0"; //拉低使能信号
delay(); //保

持使能信号为低一段时间
e="1"; //拉高使能信号,建立LCD操作所需要的上升沿
delay();
}

//********************LCD显示设置函数**************************************
//在电平发生改变后需要插入一段延时时间,否则LCD反应不过来。
void lcd_enable()
{
delay();
rs="0"; //该字节数据为命令,而不是数据
delay();
rw="0"; //此次操作为写,而不是读
delay();
e="0"; //拉低使能信号
delay(); //保持使能信号为低一段时间
e="1"; //拉高使能信号,建立LCD操作所需要的上升沿
delay();
}

//*********************LCD初始化函数**************************************
void lcd_init()
{
PORTD="0X1"; //清除显示
lcd_enable();
PORTD="0X38"; //8位2行5*7点阵
lcd_enable();
PORTD="0X0e"; //显示开,光标开,闪烁
lcd_enable();
PORTD="0X06"; //文字不动,光标右移
lcd_enable();
PORTD="0X86"; //时间显示地址
lcd_enable();
}

//***********************LCD显示函数************************************
void lcd_display()
{
unsigned char i,j;
lcd_init();
for(i=0;i<3;i++) //一共3字节数据
{
write(lcd); //查表获取数据并调用写一个字节数据函数送LCD显示
for(j=0;j<5;j++) //延时一段时间(主要是为了控制显示的速度)
{delay();}
}
}

//****************写一字节函数***************************
unsigned char SPI_WriteByte(unsigned char val)
{
SPI2BUF = val; //待发送数据装载到发送寄存器
while(!IFS1bits.SPI2IF); //等待发送完成
IFS1bits.SPI2IF=0; //清除发送完成标志位
return SPI2BUF; //读取接收寄存器(即使是无效数据也需清空)
}

//****************接收一字节函数**************************
unsigned char SPI_ReadByte(void)
{
SPI2BUF = 0xff; //发送寄存器装载数据,以启动数据接收
while(!IFS1bits.SPI2IF); //等待接收完成
IFS1bits.SPI2IF=0; //清除接收完成标志位
return SPI2BUF; //读取接收到的数据
}

//*****************发送命令函数****************************
unsigned char SD_SendCommand(unsigned char cmd,unsigned long arg)
{
unsigned char r1;
unsigned char retry1=0; //重复操作次数

cs=0; //使能片选信号

SPI_WriteByte(cmd | 0x40); //分别写入命令
SPI_WriteByte(arg>>24); //数据段第4字节
SPI_WriteByte(arg>>16); //数据段第3字节
SPI_WriteByte(arg>>8); //数据段第2字节
SPI_WriteByte(arg); //数据段第1字节
SPI_W

riteByte(0x95); //CRC效验和

while((r1 = SPI_WriteByte(0xff)) == 0xff)//等待响应
if(retry1++ > 200) break;//超时退出

cs=1; //清初片选信号

return r1; //返回状态值
}

//*******************SD开初始化函数**************************
unsigned char sd_reset()
{
unsigned char i,tmp;
unsigned char retry; //重复次数
unsigned char r1=0;
retry=0;
delay();
delay();
do
{
for(i=0;i<100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(0,0);//发idle命令
retry++;
if(retry>20) return 1; //超时退出
} while(r1 != 0x01); //等待IDLE命令返回

retry = 0;
cs="0";
do
{
for(i=0;i<100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(1, 0); //发Active命令
retry++;
if(retry>254) return 1; //超时退出
} while(r1);

for(i=0;i<100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(59, 0); //关crc
if (r1) return 1; //返回不正确,退出初始化

for(i=0;i<100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(16, 512); //设扇区大小512
if(r1!=0) return 1; //返回不正确,退出初始化
return 0; //正常返回
}

//********************写一个扇区**************************
unsigned char SD_WriteSingleBlock(unsigned long sector)
{
unsigned char r1;
unsigned int i;
unsigned char retry="0";
do
{
for(i=0;i<100;i++) SPI_WriteByte(0xff);

r1 = SD_SendCommand(24, sector<<9);//写命令
retry++;
if(retry>10) return 1; //超时退出
} while(r1 != 0x00);

cs=0;

SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);

SPI_WriteByte(0xfe); //发开始符

for(i=0; i<512; i++) //送512字节数据
{
if(i<255) SPI_WriteByte(i); //发送0--255
else SPI_WriteByte(512-i); //发送255--0

}

SPI_WriteByte(0x95);
SPI_WriteByte(0x95); //16-bits CRC

r1 = SPI_WriteByte(0xff); //读响应位
if(retry++ >10) return 1; //超时退出
while(!((r1&0x0f)==5)); //等待数据成功接受返回信息
while(!(SPI_WriteByte(0xff))); //等待SD卡内部编程完成

return 0;
}


//******************读SD卡一个扇区************************
unsigned char SD_ReadSingleBlock(unsigned long sector)
{
unsigned char r1,temp;
unsigned int i,j;
unsigned char retry="0";

do
{
r1 = SD_SendCommand(17, sector<<9);//读命令
retry++;
if(retry>10) return 1; //超时退出
} while(r1 != 0x00);
cs=0;
while(SPI_WriteByte(0xff)!= 0xfe) //等待接收

到开始字节
{
if(retry++ >100) return 1; //超时退出
}
for(i=0; i<512; i++) //读512个数据
{
temp = SPI_WriteByte(0xff); //读取接收到的数据
lcd[0]=(temp/100)+48;
lcd[1]=((temp%100)/10)+48;
lcd[2]=((temp%100)%10)+48;
lcd_display(); //读取数据送显示
for(j=0;j<500;j++) {delay();}
}

SPI_WriteByte(0xff); //伪16-bits crc
SPI_WriteByte(0xff);

cs=1;

return 0;
}

//***********************延时程序*************************
void delay() //延时程序
{
int i; //定义整形变量
for(i=0x100;i--;); //延时
}

//************************主函数**************************
int main(void)
{
unsigned char loop,res;
delay();
delay();
delay();
loop="1";

cs="1";
while(loop)
{
spi_init(); //调用系统初始化函数
res= sd_reset(); //调用SD卡初始化函数
if(res) break; //SD卡初始化是否正常,不正常,退出循环,不执行下面的读写操作
SD_WriteSingleBlock(1); //调用写SD卡单BLOCK函数,其中扇区号为1
if(res) break;
SD_ReadSingleBlock(1); //调用读SD卡单BLOCK函数,其中扇区号为1
if(res) break;
loop="0";
while(1);
}
while(1);
}


相关主题