CH376S 寫TXT時亂碼

在ADS1.2 環(huán)境下編繹的,寫時數(shù)據(jù)時,十次有八次是亂碼,偶爾的一兩次能成功,大俠們什么原因?。空{(diào)試好長時間了。

你看下寫進去的是亂碼的時候使用UE的軟件看下里面寫的16進制數(shù)據(jù)是什么?


void save(void) { sprintf(file_title,"/NJ%02d.TXT",test_num); Uart_Printf(file_title); sprintf(txts,"%04x%02x%02x %02x:%02x:",SysTime.year,SysTime.month,SysTime.day, SysTime.hour,SysTime.mini); sprintf(txt_file,"%6.1f,%6.1f,%04.1f,%6.1f,%6.1f,%04.1f,%6.1f,%6.1f,%04.1f,%5.2f",V1,V2,T1,V3,V4,T2,V5,V6,T3,H_en); strcat(txts,txt_file); Uart_Printf(txts); save_Txtdata(file_title,txts); } void Creat_newTxt(void) { UINT8 i,s; UINT16 RealCount; //char *bufs="CH376.TXT "; char tmp[80]; char *txts="Check Time,TP1.S,Temp.1,TP2.S,Temp.2,TP3.S,Temp.1,Hum."; if(store_set==0)return; while ( CH376DiskConnect( ) != USB_INT_SUCCESS ) { /* 檢查U盤是否連接,等待U盤插入,對于SD卡,可以由單片機直接查詢SD卡座的插拔狀態(tài)引腳 */ mDelaymS( 100 ); }

for(i=0;i<55;i++)tmp[i]='A'+i;

for ( s = 0; s < 20; s ++ ) { /* 最長等待時間,10*50mS */ mDelaymS( 100 ); if ( CH376DiskMount( ) == USB_INT_SUCCESS ) break; /* 初始化磁盤并測試磁盤是否就緒 */ } ch376operCreate(); //s = CH376ByteLocate( 0xFFFFFFFF ); // 移到文件的尾部 s=CH376ByteWrite(txts,55,NULL); mDelaymS( 200 ); s=CH376FileClose(TRUE ); mStopIfError( s ); }

void save_Txtdata(U8 *filename,char *dat) { U8 s,i; while ( CH376DiskConnect( ) != USB_INT_SUCCESS ) { /* 檢查U盤是否連接,等待U盤插入,對于SD卡,可以由單片機直接查詢SD卡座的插拔狀態(tài)引腳 */ mDelaymS( 100 ); } if(CH376DiskMount()==USB_INT_SUCCESS) { Uart_Printf("\n USB ok!\n"); mDelaymS( 50 ); if(CH376FileOpen(filename)==USB_INT_SUCCESS) { //CH376ByteRead( txts, 255, &RealCount ); Uart_Printf("find %s",filename); Uart_Printf("Writting %s",dat); s = CH376ByteLocate( 0xFFFFFFFF ); // 移到文件的尾部 mDelaymS( 50 ); if(s==USB_INT_SUCCESS) i=5; while(i--) { s = CH376ByteWrite(dat, strlen(dat),NULL ); mDelaymS( 50 ); } mStopIfError( s ); s = CH376FileClose(TRUE ); } } }


串口打印出結(jié)果 14 Creat/NJ02.TXT 20121008 10:37:1983.4,-1741.6,29.1,2073.6,2327.7,25.7,2260.7,2302.9,28.1,46.22 USB ok! find /NJ02.TXTWritting 20121008 10:37:1983.4,-1741.6,29.1,2073.6,2327.7,25.7,2260.7,2302.9,28.1,46.22/NJ02.TXT 20121008 10:38:1969.0,-1271.8,29.1,2047.6,2350.7,25.7,2284.2,2370.6,28.2,46.22

U盤結(jié)果 Check Time,TP1.S,Temp.1,TP2.S,Temp.2,TP3.S,Temp.1,Hum. 20121008 10:37:1983.4,-1741.6,29'?櫁????????? 20121008 10:37:1983.4,-17?K崑嶬孠?趟崑屘嵥嵥岾嵥實?嵥屘嫀K?孠

媽?20121008 10:37:1983.4,-1741.6,29.1,2073.6,2327.7,25.7,2260.7,2302.9,28.1,46.22 20121008?洕湈藣LM?K崑嶬孠?芿婆咶fF迤鍐Fテ鍐FF?棋咶fE?咷?唵牌F 20121008 10:37:1983.4,-1741.6,29.?櫁?????????


估計有一個字符寫的有問題導致后面全部亂掉。你用UE看下結(jié)果。那個字符寫入錯或者寫入有問題。


現(xiàn)在奇怪的是,新建的txt為1KB,打開看竟然有一個字符。


文件長度是1KB嗎?還是文件占用空間為1KB,如果文件長度為1KB說明你寫進去的數(shù)據(jù)TXT無法顯示。如果是占用空間為1KB,則說明關(guān)閉文件之后文件的內(nèi)容沒有及時的寫入至U盤,你可以再寫完U盤關(guān)閉文件之后發(fā)送“安全移除”的控制請求安全移除U盤?;蛘叩却欢螘r間之后在拔出U盤。


用串口監(jiān)測下:字節(jié)寫和文件關(guān)閉等函數(shù)返回S值是多少


換了一片CH376好多了, 吃飯回來看到結(jié)果是這樣的,只有一條數(shù)據(jù)有問題 Check Time TP1A.S TP1B.S Temp.1 TP2A.S TP2B.S Temp.2 TP3A.S TP3B.S Temp.3 Hum. 20121008 12:47:39 1958.8,-1748.3,30.0,2025.0,2275.6,26.1,2302.3,2284.4,28.9,45.92 20121008 12:48:41 1976.5,-1802.0,30.1,1983.5,2325.2,26.1,2335.3,2344.0,29.1,45.73 20121008 12:49:28 2031.0,-1328.9,29.9,2072.1,2317.1,26.1,2326.3,2291.9,28.9,45.66 20121008 12:50:28 1947.0,-1419.5,30.1,2077.1,2357.2,26.2,2325.8,2369.6,29.1,45.63 20121008 12:51:28 2047.6,-1748.3,29.9,1975.5,2345.2,26.2,2361.4,2369.6,28.9,45.44 20121008 12:52:28 1993.6,-1701.3,29.8,2026.5,2275.1,26.2,2334.8,2384.2,28.9,45.41 20121008 12:53:28 2047.6,-1506.7,30.1,2016.0,2349.2,26.2,2273.7,2283.9,29.0,45.38 20121008 12:54:28 2008.6,-1234.9,30.1,1978.0,2348.7,26.3,2276.7,2398.2,29.1,45.35 20121008 12:55:28 2056.2,-1624.2,30.1,2067.1,2317.1,26.3,2392.5,2267.8,29.1,45.22 20121008 12:56:28 1977.0,-1523.5,30.2,2043.1,2291.6,26.4,2351.9,2286.9,29.2,45.32 20121008 12:57:28 2062.6,-1218.1,30.1,2011.0,2374.2,26.4,2382.5,2323.5,29.1,45.22 20121008 12:58:28 2004.3,-1755.0,30.1,2003.0,2275.1,26.4,2362.4,2283.4,29.1,45.16 20121008 12:59:28 2012.8,-1600.7,30.1,2085.6,2368.7,26.4,2325.3,2339.0,29.1,45.09 20121008 13:00:28 1976.5,-1885.9,30.2,1990.5,2353.2,26.4,2332.8,2287.4,29.2,45.03 20121008 13:01:28 2042.3,-1456.4,30.3,2091.1,2282.6,26.4,2358.9,2297.9,29.2,44.93 20121008 13:02:28 2007.0,-1624.2,30.2,2003???櫅?槡? 20121008 13:03:28 2026.8,-1785.2,30.2,2027.0,2299.1,26.5,2298.7,2331.5,29.2,44.74 20121008 13:04:28 1995.2,-946.3,30.3,2072.6,2287.1,26.5,2328.8,2267.3,29.2,44.71 20121008 13:05:28 2014.4,-1661.1,30.2,2016.5,2345.2,26.6,2306.3,2357.6,29.2,44.74


CH376和你芯片的接口是什么接口?把底層接口函數(shù)貼出來看下。


#define CH376_SPI_SCS(a) rGPGDAT=rGPGDAT&~(1<<8)|((a&0x1)<<8) #define CH376_SPI_SCK(a) rGPEDAT=rGPEDAT&~(1<<7)|((a&0x1)<<7) #define CH376_SPI_SDI(a) rGPEDAT=rGPEDAT&~(1<<5)|((a&0x1)<<5)

#define CH376_SPI_SDO rGPEDAT&(1<<8)

//#define CH376_INT_WIRE INT0 /* 假定CH376的INT#引腳,如果未連接那么也可以通過查詢兼做中斷輸出的SDO引腳狀態(tài)實現(xiàn) */

void CH376_PORT_INIT( void ) /* 由于使用軟件模擬SPI讀寫時序,所以進行初始化 */ {

//CH376_SPI_SCS = 1; /* 禁止SPI片選 */ //CH376_SPI_SCK = 1; /* 默認為高電平,SPI模式3,也可以用SPI模式0,但模擬程序可能需稍做修改 */

rGPECON=rGPECON&~(3<<10)&~(3<<14)&~(3<<16)|(1<<10)|(1<<14); //GPE5 SDI GPE7 SCK GPE8 SDO rGPGCON=rGPGCON&~(3<<16)|(1<<16); //GPG8 SCS rGPEUP=rGPEUP&~(1<<5)&~(1<<7); rGPGUP=rGPGUP&~(1<<8); CH376_SPI_SCS(1); CH376_SPI_SCK(1); } void Spi376OutByte( UINT8 d ) /* SPI輸出8個位數(shù)據(jù) */ { /* 如果是硬件SPI接口,應(yīng)該是先將數(shù)據(jù)寫入SPI數(shù)據(jù)寄存器,然后查詢SPI狀態(tài)寄存器以等待SPI字節(jié)傳輸完成 */ UINT8 i; for ( i = 0; i < 8; i ++ ) { CH376_SPI_SCK(0); mDelayuS(8); if ( d & 0x80 ) CH376_SPI_SDI(1); else CH376_SPI_SDI(0); d <<= 1; /* 數(shù)據(jù)位是高位在前 */ CH376_SPI_SCK(1); /* CH376在時鐘上升沿采樣輸入 */ mDelayuS(8); } }

UINT8 Spi376InByte( void ) /* SPI輸入8個位數(shù)據(jù) */ { /* 如果是硬件SPI接口,應(yīng)該是先查詢SPI狀態(tài)寄存器以等待SPI字節(jié)傳輸完成,然后從SPI數(shù)據(jù)寄存器讀出數(shù)據(jù) */ UINT8 i, d; d = 0; for ( i = 0; i < 8; i ++ ) { CH376_SPI_SCK (0); /* CH376在時鐘下降沿輸出 */ mDelayuS(8); d <<= 1; /* 數(shù)據(jù)位是高位在前 */ if ( CH376_SPI_SDO ) d ++; CH376_SPI_SCK (1); mDelayuS(8); } return( d ); }

void xEndCH376Cmd(void ) { CH376_SPI_SCS (1); } /* SPI片選無效,結(jié)束CH376命令,僅用于SPI接口方式 */

void xWriteCH376Cmd( UINT8 mCmd ) /* 向CH376寫命令 */ { #ifdef CH376_SPI_BZ UINT8 i; #endif CH376_SPI_SCS(1); /* 防止之前未通過xEndCH376Cmd禁止SPI片選 */ mDelay0_5uS( ); mDelayuS(1); /* 對于雙向I/O引腳模擬SPI接口,那么必須確保已經(jīng)設(shè)置SPI_SCS,SPI_SCK,SPI_SDI為輸出方向,SPI_SDO為輸入方向 */ CH376_SPI_SCS(0); /* SPI片選有效 */ mDelayuS(1); Spi376OutByte( mCmd ); /* 發(fā)出命令碼 */ #ifdef CH376_SPI_BZ for ( i = 30; i != 0 && CH376_SPI_BZ; -- i ); /* SPI忙狀態(tài)查詢,等待CH376不忙,或者下面一行的延時1.5uS代替 */ #else mDelay0_5uS( ); mDelay0_5uS( ); mDelay0_5uS( ); mDelay0_5uS( ); /* 延時1.5uS確保讀寫周期大于1.5uS,或者用上面一行的狀態(tài)查詢代替 */ #endif }

void xWriteCH376Data( UINT8 mData ) /* 向CH376寫數(shù)據(jù) */ { Spi376OutByte( mData ); mDelayuS(8) ;/* 確保讀寫周期大于0.6uS */ } UINT8 xReadCH376Data( void ) /* 從CH376讀數(shù)據(jù) */ { mDelayuS(8); /* 確保讀寫周期大于0.6uS */ return( Spi376InByte( ) ); }

/* 查詢CH376中斷(INT#低電平) */ UINT8 Query376Interrupt( void ) { #ifdef CH376_INT_WIRE return( CH376_INT_WIRE ? FALSE : TRUE ); /* 如果連接了CH376的中斷引腳則直接查詢中斷引腳 */ #else return( CH376_SPI_SDO ? FALSE : TRUE ); /* 如果未連接CH376的中斷引腳則查詢兼做中斷輸出的SDO引腳狀態(tài) */ #endif }


時序上沒什么問題,把你的原理圖發(fā)到我郵箱里,看下硬件連接有沒有什么小的問題


已發(fā)到wj@wch.cn 請查收。


Check Time TP1A.S TP1B.S Temp.1 TP2A.S TP2B.S Temp.2 TP3A.S TP3B.S Temp.3 Hum. 20121008 15:04:35 1977.5,-1201.3,29.3,1981.5,2301.6,26.6,2320.3,2359.1,28.4,45.88 20121008 15:05:35 2001.1,-1352.3,29.4,2098.6,2343.7,26.6,2342.4,2276.3,28.5,45.75 20121008 15:06:35 2004.3,-1050.3,29.9,2078.1,2282.6,26.6,2293.2,2282.8,28.9,45.81 20121008 15:07:35 2048.7,-1255.0,29.8,2087.6,2306.1,26.6,2353.9,2323.0,28.8,45.87 20121008 15:08:35 2000.0,-1597.3,30.1,1995.0,2305.1,26.6,2314.8,2295.9,29.0,45.78 20121008 15:09:35 1975.9,-1896.0,29.9,2029.5,2296.1,26.6,2343.4,2305.9,28.9,45.88 20121008 15:10:35 2001.1,-1745.0,29.9,2082.1,2382.3,26.7,2348.9,2282.8,28.9,45.84 20121008 15:11:35 2034.2,-1654.4,29.8,2019.0,2296.6,26.6,2344.9,2347.5,28.8,45.81 20121008 15:12:35 1958.3,-1765.1,29.9,2007.5,2357.7,26.7,2279.7,2264.8,28.9,45.84 20121008 15:13:35 2011.2,-1758.4,30.0,2103.2,2365.7,26.7,2318.8,2343.0,29.0,45.74 20121008 15:14:35 1959.9,-1674.5,30.1,2026.5,2281.1,26.7,2333.8,2311.9,29.1,45.58 20121008 15:15:35 2021.4,-1208.1,30.1,1996.0,2292.1,26.8,2327.8,2356.1,29.1,45.41 20121008 15:16:35 2051.9,-1496.6,30.2,2084.6,2349.7,26.8,2287.7,2321.????

20121008 15:17:35 1996.8,-1969.8,29.9,2014.5,2321.6,26.8,2387.0,2322.0,29.0,45.25 20121008 15:18:35 2034.2,-1718.1,29.9,2017.0,2301.6,26.8,2365.9,2369.1,28.9,45.23 20121008 15:19:35 2櫁?槡???櫁????? 20121008 15:20:35 1974.9,-1681.2,29.8,2016.5,2292.6,26.8,2311.3,2319.0,28.9,44.94 20121008 15:21:35 2008.6,-1302.0,29.8,2078.1,2315.6,26.8,2311.3,2383.7,28.9,44.81 20121008 15:22:35 2056.7,-1721.5,30.0,2013.5,21殬????????

20121008 15:23:35 1988.2,-1748.3,30.2,2016.0,2360.7,26.7,2308.8,2298.4,29.4,43.72 20121008 15:24:35 2007.0,-1503.4,30.2,2052.6,2298.1,26.7,2320.3,2297.4,29.4,43.36 20121008 15:25:35 1965.8,-1698.0,30.2,2055.6,2313.6,26.8,2277.2,2339.5,29.4,43.29 20121008 15:26:35 2058.9,-1278.5,30.3,2069.1,2384.8,26.8,2370.9,2313.4,29.5,43.02 20121008 15:27:35 2065.8,-1597.3,30.3,2098.6,2356.2,26.8,2312.3,2298.4,29.6,43.12 20121008 15:28:35 2049.8,-1614.1,30.2,2050.1,2332.2,26.8,2398.5,2340.0,29.6,42.68 20121008 15:29:35 2049.8,-1187.9,30.2,2084.6,2301.6,26.8,2360.4,2347.5,29.4,42.76 20121008 15:30:35 1952.9,-1322.1,30.2,2047.1,2300.1,26.8,2343.9,2378.6,29.4,42.59 20121008 15:31:35 2050.3,-1785.2,30.1,2005.5,2295.1,26.8,2298.7,2339.0,29.1,42.63 20121008 15:32:35 2041.7,-1661.1,30.3,2075.1,2329.2,26.8,2406.5,2266.3,29.4,42.87 20121008 15:33:35 1981.8,-1318.8,30.4,2035.1,2347.2,26.9,2367.9,2348.0,29.5,42.74 20121008 15:34:35 2005.4,-1932.9,30.5,2002.5,2307.6,26.9,2290.7,2342.0,29.7,42.50 20121008 15:35:35 1996.8,-1667.8,30.7,2060.6,2388.8,26.9,2324.3,2280.8,29.8,42.50 20121008 15:36:35 1976.5,-1298.7,30.4,2035.1,2346.7,26.9,2339.3,2309.4,29.6,42.54 20121008 15:39:36 1973.8,-1245.0,30.7,2072.6,2324.1,27.0,2349.4,2283.9,29.7,42.38 20121008 15:40:35 2022.5,-1456.4,30.8,2090.1,2332.7,27.1,2381.5,2272.8,29.8,42.38 20121008 15:41:35 1957.2,-1422.8,30.8,1996.0,2304.6,27.1,2371.4,2299.9,29.8,42.44 20121008 15:42:35 1960.4,-1144.3,30.8,2077.6,2355.7,27.1,2322.3,2406.2,29.8,42.45 20121008 15:43:35 2001.6,-1318.8,30.6,2041.6,2376.8,27.1,2339.3,2338.5,29.8,42.18 20121008 15:44:35 1985.6,-1758.4,30.8,2039.6,2306.1,27.1,2331.3,2297.4,29.8,41.98 20121008 15:45:35 2020.3,-1845.6,30.8,2032.0,2310.6,27.2,2382.5,2344.0,30.0,41.85 20121008 15:46:35 2062.1,-1429.5,30.7,2033.0,2311.1,27.1,2353.9,2314.9,29.9,42.12 20121008 15:47:35 2067.4,-1781.9,30.8,2062.6,2360.2,27.2,2362.4,2323.0,29.8,41.96 20121008 15:48:35 2029.4,-1459.7,30.6,1996.0,2308.6,27.2,2329.8,2337.5,29.8,42.13


#define CH376_SPI_SCS(a) rGPGDAT=rGPGDAT&~(1<<8)|((a&0x1)<<8) 這個是片選引腳嗎?//GPG8 SCS


是的,


只有登錄才能回復,可以選擇微信賬號登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频