|
我在读SD卡的时候。SD卡的初始化都完成了,但是在接收开始信号0XFE的时候一直接受不到!
//Send the start byte
while(Read_Byte_SD()!=0xfe); 一直运行它!
找不出原因来,请大家帮帮!
程序如下:
#include <iom16v.h>
#include <macros.h>
#include <SD.h>
#include <main.h>
void Write_Byte_SD(char Byte)
{
SPDR=Byte;
while(!(SPSR&(1<<SPIF)));
}
char Read_Byte_SD(void)
{
char Byte;
SPDR=0xff;
while(!(SPSR&(1<<SPIF)));
Byte=SPDR;
return(Byte);
}
char Write_Command_SD(char*CMD)
{
char a;
char tmp=0xff;
char Timeout=0;
// Raise chip select
SD_Disable();
// Send an 8 bitpulse
Write_Byte_SD(0xFF);
// Lower chip select
SD_Enable();
//Send the 6 byte command
for(a=0;a<0x06;a++)
{
Write_Byte_SD(*CMD++);
}
//Wait for the response
while(tmp==0xff)
{
tmp=Read_Byte_SD();
if(Timeout++>100)
{
break;
}
}
//for some reason we need to delay here
Delay_ms(1);
return(tmp);
}
char SDInit(void)
{
char a,b,retry;
char CMD[]={0x40,0x00,0x00,0x00,0x00,0x95};
// Set certain pins to inputs and others to outputs
// Only SPI_DI (data in) is an input
SD_Direction_REG&=~(1<<SPI_DI);
SD_Direction_REG|=(1<<SPI_Clock);
SD_Direction_REG|=(1<<SPI_DO);
SD_Direction_REG|=(1<<SD_Chip_Select);
SD_Direction_REG|=(1<<SPI_SS);
SD_Write|=(1<<SD_Chip_Select);
//We need to wait for the SD_Direction_REG to be ready
for(a=0;a<200;a++)
{
nop();
};
//Enable SPI in Master Mode with IDLE low and clock at 1/128
SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1);
SPSR=(0<<SPI2X);
// We need to give the card about a Hundred cycles to load
for (b=0;b<0x0f;++b)
{
Write_Byte_SD(0xff);
}
//Send the initialization commands to the card
retry=0;
//Note,many examples check for(Write_Command_SD(CMD)==R1_IN_IDLE_STATE)
//However,with our setup the card was in IDLE state and returning another command
while((Write_Command_SD(CMD)&0X01)==0)
{
//fail and return
if(retry++>50)
{
return 1;
}
}
//Send the 2nd command
retry=0;
CMD[0]=0x41;
CMD[5]=0xFF;
while(Write_Command_SD(CMD)!=0)
{
if (retry++>50)
{
return 2;
}
}
/*//Set the SPI bus to full speed
SPCR=SPCR|(0<<SPR0)|(0<<SPR1);
SPSR=SPSR|(1<<SPI2X);
*/
//Raise Chip Select
SD_Disable();
return 0;
}
/*char SD_set_length(unsigned long length)
{char retry;
//Command to set the block length;
char CMD[]={0x50,0x00,0x00,0x00,0x00,0xFF};
CMD[1]=((length&0xFF000000)>>24);
CMD[2]=((length&0x00FF0000)>>16);
CMD[3]=((length&0x0000FF00)>>8);
CMD[4]= (length&0x000000FF);
while(Write_Command_SD(CMD)!=0)
{
if (retry++>50)
{
return 1;
}
}
SD_Disable();
return 0;
}
*/
char SD_Read_Block(unsigned long addr,unsigned char*Buffer,unsigned int length)
{
int a;
char retry;
//Command to read a block;
char CMD[]={0x51,0x00,0x00,0x00,0x00,0xFF};
CMD[1]=((addr&0xFF000000)>>24);
CMD[2]=((addr&0x00FF0000)>>16);
CMD[3]=((addr&0x0000FF00)>>8);
// Send the read command
while(Write_Command_SD(CMD)!=0)
{
if (retry++>50)
{
return 1;
}
}
//Send the start byte
while(Read_Byte_SD()!=0xfe);
//Read off all the bytes in the block
for(a=0;a<length;++a)
{
*Buffer++=Read_Byte_SD();
}
//Read CRC byte
Read_Byte_SD();
Read_Byte_SD();
// Set SD_Chip_Select to high
SD_Disable();
return 0;
}
<font color=red>以下红色文字由版主:judy58 于:2005-12-26,13:45:34 加入。 请更改成能说明帖子大意的标题
本论坛规定:一定要起一个能说明帖子大意的标题。不允许“关于 AVR 的 ADC 使用!”这种笼统标题。作为标题,必须说明清楚:
①你是在请教问题,还是在介绍有关的知识?
②ADC的问题有许多,你是想说那方面的内容?
注意以上两点,标题应该改成如:“请教:ADC 可以不使用中断吗?”。
起一个能说明帖子大意的标题,除了减轻我们的帖子分类整理工作,还方便其它人阅读,节省大家的时间。
谢谢你的支持。
注:你可以使用编辑功能修改标题,并将这段红色文字删除。谢谢你的支持! |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|