我正在使用PIC18F26K80和XC8编译器。我正在尝试初始化SD卡并创建文件。我已经简单地格式化了Windows上的SD卡,使其具有“ FAT32”文件系统和512字节的“分配单元大小”。SD卡的容量为2GB。我正在使用MLA旧版中的MDD库。我的主要内容如下:
FSFILE * file;
char sendBuffer[22] = "This is test string 1";
//**************************************************
// main function
//**************************************************
int main()
{
initIO();
LATBbits.LATB0 = 0;
// Initialise SPI and SD-card
while ( !MDD_MediaDetect() );
// Initialize the device
while ( !FSInit() );
// Initialize
#ifdef ALLOW_WRITES
// Create a new file
file = FSfopenpgm ( "FILE.TXT", "w" );
if ( file == NULL )
while(1);
// Write 21 1-byte objects from sendBuffer into the file
if ( FSfwrite ( (void *) sendBuffer, 1, 21, file ) != 21 )
while(1);
// Close the file
if ( FSfclose ( file ) )
while(1);
#endif
LATBbits.LATB0 = 1; //LED
while(1) {}
return (0);
}
该程序卡在函数“ FSInit()”中,并且从该函数中得到的错误是“ CE_BAD_PARTITION”,这意味着“启动记录错误”。
“ initIO()”函数如下:
//==============================================================================
// void initIO( void );
//==============================================================================
// Sets the pins on the PIC to input or output and determines the speed of the
// internal oscilaltor
// input: none
// return: none
//==============================================================================
void initIO()
{
OSCCON = 0x75; // Clock speed = 32MHz (4x8Mhz)
TRISA = 0;
TRISB = 0;
TRISC = 0;
TRISBbits.TRISB0 = 0; //LED
TRISCbits.TRISC3 = 0; // set SCL pin as output
TRISCbits.TRISC4 = 1; // set RC4 pin as input
TRISCbits.TRISC5 = 0;
TRISAbits.TRISA5 = 0;
}
扇区0的最后两个字节是引导签名,它们的含义是0x55和0xAA,我提供的图片证实了这一点。但是,在函数“ LoadMBR”中进行了以下检查:
if((Partition->Signature0 != FAT_GOOD_SIGN_0) || (Partition->Signature1 != FAT_GOOD_SIGN_1))
{
FSerrno = CE_BAD_PARTITION;
error = CE_BAD_PARTITION;
}
else
{
...
}
并且尽管字节相同,但第一个条件得到满足,并返回“ CE_BAD_PARTITION”错误。