高级会员

- 积分
- 661
- 威望
- 325
- 贡献
- 204
- 兑换币
- 207
- 注册时间
- 2015-1-21
- 在线时间
- 66 小时
- 毕业学校
- 陕西科技大学
|
函数名称: unsigned char Sd_send_cmd(unsigned char cmd, unsigned long sd_value)
功能介绍: 向SD卡写入相应的命令CMD0,即发送命令字:0x40,0x00,0x00,0x00,0x00,0x95;。
如果收到应答信号 01H ,则表示进入SPI模式。
然后向 SD卡发送 CMD1 。
注意:其中扇区的长度为512字节
unsigned char Sd_send_cmd(unsigned char cmd, unsigned long sd_value)
{
unsigned char spi_retu_value;
unsigned char overtime_count=0;
(void)Spi0_rdwr_byte(0xFF); //SD卡上电后进入SD模式,及等待至少74 个时钟周期
Sd_sel(); //片选使能
//发送CMD0信号
(void)Spi0_rdwr_byte(cmd|0x40); //分别写入命令
(void)Spi0_rdwr_byte((unsigned char)(sd_value>>24));
(void)Spi0_rdwr_byte((unsigned char)(sd_value>>16));
(void)Spi0_rdwr_byte((unsigned char)(sd_value>>8));
(void)Spi0_rdwr_byte((unsigned char)(sd_value));
(void)Spi0_rdwr_byte(0x95); //0x95第一个命令不能少, 之后可以省
spi_retu_value = Spi0_rdwr_byte(0xFF);
while(spi_retu_value== 0xFF)
{
spi_retu_value = Spi0_rdwr_byte(0xFF);
if(++overtime_count > 100)
{
break; //超时退出
}
}
Sd_desel(); //片选不使能
return spi_retu_value; //返回状态值
}
函数名称: unsigned char Sd_send_cmd(unsigned char cmd, unsigned long sd_value);
功能介绍: sd卡复位
unsigned char Sd_reset(void)
{
unsigned char i;
unsigned char overtime_count;
unsigned char return_value=0;
overtime_count = 0;
do
{
for(i=0;i<10;i++)
{
(void)Spi0_rdwr_byte(0xFF); //在接收数据时,
// 可以发送并无实际意义的字节(如0xFF) 作为函数的参数
}
return_value = Sd_send_cmd(0, 0);
if(++overtime_count>100)
{
return 1; //超时退出
}
}while(return_value != 0x01);
overtime_count = 0;
do
{
return_value = Sd_send_cmd(1, 0); //发cmd1(active激活)命令,发active命令
if(++overtime_count>100)
{
return 1; //超时退出
}
} while(return_value);
return_value = Sd_send_cmd(59, 0); //关crc校验
return_value = Sd_send_cmd(16, 512); //设置读取一次的字节数512字节
return 0; //正常返回
}
|
|