我用的是mega128控制376讀寫sd卡,目前可以通過初始化,但是我用CH376DiskCapacity函數(shù)去讀sd卡容量時,總是返回5f 7f 7f 7f,我知道返回是卡的總扇區(qū)數(shù)量,就是乘以512也不是卡容量???這是怎么回事啊?
還有,我希望貴公司能寫一個完整的只支持sd卡的范例,所有代碼都是基于u盤的,就算是sd的范例也需要用戶改寫,sd這塊我弄了好幾天了一些基本函數(shù)都沒有通過。。。。
實(shí)際上讀寫SD卡和讀寫U盤的區(qū)別只有2個地方,一個是模式的設(shè)置,SD卡設(shè)置模式3,U盤設(shè)置模式6.另一個就是設(shè)備的插拔的檢測,U盤采用我們芯片的INT#引腳來檢測,而SD卡則需要單獨(dú)的使用一個單片機(jī)的I/O口來進(jìn)行檢測。至于其他的軟件上面完全一樣。
為什么調(diào)用CH376DiskCapacity,返回是5f7f7f7f呢?
而且還不是sd卡的總扇區(qū)數(shù)?
CH376DiskMount( )函數(shù)是否返回成功?然后你在做i = CH376ReadBlock( buf );看看是否也返回成功。
調(diào)用CH376ReadBlock,回復(fù)的是0x24這個值
不過讀卡器這部分好使,我已經(jīng)用函數(shù)建立出一個txt文件了,但是就是sd卡這里,我一直調(diào)不通。。
CH376DiskMount這個函數(shù)在sd卡上通過了,返回0x14,我用示波器觀察sd的鏈接腳,信號線正常工作,但是調(diào)用CH376DiskCapacity,返回是5f7f7f7f,調(diào)用CH376DiskQuery則返回0x1f。
5f7f7f7f值是CH376DiskCapacity顯示的容量,返回值為0x14.。。
從你的描述來看,你所有的操作都是正確的。 5f7f7f7f這個數(shù)據(jù)你是通過什么方式讀到的?你把你這塊的代碼貼出來
底層部分: /ICC-AVR application builder : 2009-11-13 16:24:24 //Target : M8515 //Crystal: 16.000Mhz /*******************************************************************************/ /* ?£?aD?o??? òy??·??? ??ó|CH376òy?? SCS PB0 SCS SCK PB1 SCK MOSI PB2 SDI MISO PB3 SDO ????D?o?:è?1?ê1ó??D??òy?? INT PB7 INT# //?′ê1ó? ò2?′?¨ò?μ?D?o? BZ PB4 BZ */ /*******************************************************************************/ #include"iom128v.h" #include"macros.h" #include"CH376INC.H" #include"AVRFUNCTION.H"
/***************************?£?aSPIμ????úoíòy???¨ò?***********************************/ #define SW_SPI_PORTx PORTB #define SW_SPI_DDRx DDRB #define SW_SPI_PINx PINB #define SW_SCS 0 #define SW_SCK 1 #define SW_MOSI 2 #define SW_MISO 3 /*************************?£?aSPIμ????úéè??oí2ù×÷*******************************/ /*òy??2ù×÷?¨ò?*/ //????D?o?2ù×÷:SCS #define SW_SCS_SET (SW_SPI_PORTx|=1< #define SW_SCS_RESET (SW_SPI_PORTx&=(~(1< //ê±?óD?o?2ù×÷:SCK #define SW_SCK_SET (SW_SPI_PORTx|=1< #define SW_SCK_RESET (SW_SPI_PORTx&=(~(1< //ê?3?êy?Y:MOSI #define SW_MOSI_SET (SW_SPI_PORTx|=1< #define SW_MOSI_RESET (SW_SPI_PORTx&=(~(1< //?áè?êy?Y:MISO #define SW_MISO_READ (SW_SPI_PINx&(1</*********************?D??òy?????úéè??oí?¨ò?,2?ê1ó??D???òê1ó?SDO??×??D??òy??************************/ //#define USE_INT_WIRE 1 #ifdef USE_INT_WIRE #define CH376_INT_PORTx PORTB #define CH376_INT_DDRx DDRB #define CH376_INT_PINx PINB #define CH376_INT_NUMx PINB3 #define CH376_INT_WIRE (CH376_INT_PINx&(1<#else //ê1ó?CH376μ?SDO??×??D??ê?3?<--->AVR:MISO?D??ê?è? #ifndef CH376_SPI_SDO #define CH376_SPI_SDO SW_MISO_READ #endif #endif
//CH376???ú3?ê??ˉ:′?′|êμ?êé??íê?3?ê??ˉ?£?aSPI?ùê1ó?μ?4?????úoí?D??òy??(è?1?ê1ó??D??òy??) //CH376?§3?SPIμ??£ê?0oí?£ê?3. ??è??a??μ???,SPI?£ê?3,ò2?éò?ó?SPI?£ê?0,μ??£?a3ìDò?é?üDèé?×?DT?? //è?1?ê?ó2?tSPI?ó?ú,???′?éê1ó?mode3(CPOL=1&CPHA=1)?òmode0(CPOL=0&CPHA=0) //CH376?úê±?óé?éy??2é?ùê?è?,???μ??ê?3?,êy?Y??ê??????ú?° void CH376_PORT_INIT( void ) { SW_SPI_PORTx|=((1< SW_SPI_DDRx|=((1< SW_SPI_DDRx&=~(1< #ifdef USE_INT_WIRE CH376_INT_PORTx|=(1< CH376_INT_DDRx&=~(1< #endif } /*****************************?óê±ns**************************************/ void mDelaynS(unsigned char i) { while(i--); } /*****************************?óê±1us**************************************/ void mDelayuS(unsigned char i) {//2éó?16M?§??,?óê±1us while(i--) { NOP();NOP();NOP();NOP(); NOP();NOP(); //NOP();NOP(); //NOP();NOP();NOP();NOP();
} } /*****************************?óê±MS**************************************/ void mDelaymS( unsigned int delay ) { unsigned int i, j, c; for ( i = delay; i != 0; i -- ) { for ( j = 50; j != 0; j -- ) { for ( c = 10; c != 0; c -- ) mDelayuS(1); } } }
//AVRí¨1y?£?aSPIê±Dò?òCH376·¢?íêy?Y void SpiOut_CH376(unsigned char DatOut) { unsigned char i; for ( i = 0; i < 8; i ++ ) { SW_SCK_RESET; if ( DatOut & 0x80 ) SW_MOSI_SET ; //AVR SDOê?3?êy?Y else SW_MOSI_RESET; DatOut <<= 1; /* êy?Y??ê??????ú?° */ SW_SCK_SET ; /* CH376?úê±?óé?éy??2é?ùê?è? */ mDelaynS(1); /*?óê±1???ú?÷?ü?úμ?ê±??*/ } }
//AVRí¨1y?£?aSPIê±Dò′óCH376?áè?êy?Y unsigned char SpiIn_CH376(void) { unsigned char i,RecvDat=0; for ( i = 0; i < 8; i ++ ) { SW_SCK_RESET ; /* CH376?úê±?ó???μ??ê?3? */ RecvDat <<= 1; /* êy?Y??ê??????ú?° */ mDelaynS(1); /*?óê±1???ú?÷?ü?úμ?ê±??,Dèòaμè′yê&yacu
不知道為什么漢字是亂碼。。。fat部分: #define EN_DISK_QUERY #ifdef EN_DISK_QUERY extern unsigned long memory; UINT8 CH376DiskCapacity( unsigned long *DiskCap ) /*PUINT32 2é?ˉ′??ì??àíèYá?,éè??êy */ { UINT8 s; s = CH376SendCmdWaitInt( CMD0H_DISK_CAPACITY ); printf("s=%x\r\n",s); xEndCH376Cmd( );
if ( s == USB_INT_SUCCESS ) { /* 2???CH376INC.H???t?DCH376_CMD_DATA?á11μ?DiskCapacity */ printf("yes memory pass,but cmd...\r\n"); xWriteCH376Cmd( CMD01_RD_USB_DATA0 ); xReadCH376Data( ); /* 3¤?è×üê?sizeof(CH376_CMD_DATA.DiskCapacity) */ printf(); *DiskCap = CH376Read32bitDat( ); /* CH376_CMD_DATA.DiskCapacity.mDiskSizeSec,′óCH376D????áè?32??μ?êy?Y2¢?áê??üá? */ printf("disk=%ld",*DiskCap); } else *DiskCap = 0; printf("memory s=%x",s); xEndCH376Cmd( ); return( s ); }
UINT8 CH376DiskQuery( PUINT32 DiskFre ) /* 2é?ˉ′??ìê£óà????D??¢,éè??êy */ { UINT8 s; UINT8 c0, c1, c2, c3; s = CH376SendCmdWaitInt( CMD0H_DISK_QUERY ); printf("query=%x",s); if ( s == USB_INT_SUCCESS ) { /* 2???CH376INC.H???t?DCH376_CMD_DATA?á11μ?DiskQuery */ printf("pass diskquery\r\n"); xWriteCH376Cmd( CMD01_RD_USB_DATA0 ); xReadCH376Data( ); /* 3¤?è×üê?sizeof(CH376_CMD_DATA.DiskQuery) */ xReadCH376Data( ); /* CH376_CMD_DATA.DiskQuery.mTotalSector */ xReadCH376Data( ); xReadCH376Data( ); xReadCH376Data( ); c0 = xReadCH376Data( ); /* CH376_CMD_DATA.DiskQuery.mFreeSector */ c1 = xReadCH376Data( ); c2 = xReadCH376Data( ); c3 = xReadCH376Data( ); printf("%x",c0); printf("%x",c1); printf("%x",c2); printf("%x\r\n",c3); *DiskFre = c0 | (UINT16)c1 << 8 | (UINT32)c2 << 16 | (UINT32)c3 << 24; xReadCH376Data( ); /* CH376_CMD_DATA.DiskQuery.mDiskFat */ xEndCH376Cmd( ); } else *DiskFre = 0; return( s ); }
#endif
unsigned long memory; void main(void) { unsigned char s,i; unsigned char buffer1[512];//這里是為了測試你說的讀塊的函數(shù)設(shè)的緩沖區(qū) init_devices(); CH376_PORT_INIT(); printf("test!"); mDelaymS(100);//μè′yCH376?′???áê? s=mInitCH376Host();/*Init CH376*/ printf("init=%x\r\n",s); printf("pass init\r\n");
while(1) { if ( CH376DiskMount( ) == USB_INT_SUCCESS ) { break; } printf("mount=%x\r\n",CH376DiskMount( )); // 3?ê??ˉ′??ì2¢2aê?′??ìê?·??íD÷ mDelaymS(1000); } printf("ok\r\n");//測試時,mount一直查詢,然后就通過了 i=CH376ReadBlock(buffer1); printf("readblock=%x\r\n",i);//在這里返回的是0x24
printf("res=%x\r\n",CH376DiskCapacity(memory));//容量。。。。問題1所在 printf("test1=%ld",memory); printf("res=%x\r\n",CH376DiskQuery(memory));// 剩余。。。。問題2所在 printf("test1=%ld",memory);
while(1);//在這里死循環(huán)是因?yàn)槲蚁胱x出sd卡的容量就可以了 while(1) { //if(CH376DiskConnect( )== USB_INT_SUCCESS) //{ printf("pass connect\r\n"); mDelaymS( 200 ); // ?óê±,?é??2ù×÷,óDμ?USB′?′¢?÷Dèòa??ê?oá??μ??óê± for ( s = 0; s < 20; s ++ )// ×?3¤μè′yê±??,10*50mS { mDelaymS( 100 ); if ( CH376DiskMount( ) == USB_INT_SUCCESS ) break; // 3?ê??ˉ′??ì2¢2aê?′??ìê?·??íD÷ } ch376operCreate(123,456);//D′???t??2¢′ò?a???t 1.???t′??úD′????ì?μ??22? 2.??óD???t?áD??¨???t ch376write(2009,11,23,7463);//?ú?ù′ò?a?òD??¨μ????tà?D′???t ch376write(2009,11,24,7456); strcpy(buf,"??ììμ?ADC\xd\xa");//?áê?ó? s = CH376ByteWrite( buf, strlen( buf ), NULL ); /* ò?×??ú?aμ¥???ò???tD′è?êy?Y */ printf("bytewrite=%x\r\n",s); mDelaymS( 200 ); s = CH376FileClose( TRUE ); //1?±????t,×??ˉ???????t3¤?è,ò?×??ú?aμ¥??D′???t,?¨òéè?3ìDò?a1?±????tò?±?×??ˉ?üD????t3¤?è printf("close=%x\r\n",s); while ( CH376DiskConnect( ) == USB_INT_SUCCESS ) // ?ì2éU?ìê?·?á??ó,μè′yU?ì°?3? { mDelaymS( 100 ); } while(1); //} } }
5f7f7f7f涉及到的函數(shù)有:xReadCH376Data:第一段;CH376DiskCapacity:第二段;調(diào)用在主函數(shù)main里面:第三段;
u盤,讀卡器這種usb類的沒有問題~唯獨(dú)sd卡這里出問題,線路,信號線方向我特意注意了一下,沒問題。