FC是一個(gè)自計(jì)數(shù)的寄存器,可以用來做成比較準(zhǔn)確的延時(shí),具體有誰做過嗎?如果第一次讀到的數(shù)據(jù)FF,怎樣判斷寄存器的計(jì)數(shù)溢出了呢?謝謝,希望有做過的人貼一小段程序上來,非常感謝。
也遇到此類問題,無法達(dá)到50us的精度,導(dǎo)致讀數(shù)不確定,時(shí)序有問題了!
us = // 微秒數(shù) mTime = (UCHAR)( ( 50 * us ) / (ULONG)( 50 * 3.84 ) ); /* 計(jì)算硬件計(jì)時(shí)數(shù), 3.84uS */ CH365mReadIoByte( mIndex, & dllIoBaseAddr[ mIndex ] -> mCh365IoTime, &mByte ); /* 讀取硬件循環(huán)計(jì)數(shù)寄存器 */ mTime += mByte; /* 加上起始值為計(jì)數(shù)終值 */ while ( mByte != mTime ) CH365mReadIoByte( mIndex, & dllIoBaseAddr[ mIndex ] -> mCh365IoTime, &mByte ); /* 等待計(jì)數(shù)結(jié)束 */
 是什么,
怎么有亂碼的& nbsp
這是以前寫的,有的時(shí)候不準(zhǔn)確 void CH365_SPI_Delay() { LONGLONG QPart1;//,QPart2; // double dfMinus, dfTim; double dfTim=0;
QueryPerformanceCounter(&litmp); QPart1 = litmp.QuadPart; // ??μ?3?ê??μ do { CH365ReadIoByte(&testAddr,&testRead); //dfTim ++; QueryPerformanceCounter(&litmp); dfTim = ((double)(litmp.QuadPart-QPart1)) / dfFreq; }while(dfTim<0.00004990); // if (dfTim<0.000049) { //intf("df=%f\n",dfTim); } }
int main(int argc, char* argv[]) { unsigned long testData; HANDLE dev; BOOL isTrue; QueryPerformanceFrequency(&litmp); dfFreq = (double)litmp.QuadPart; // ??μ???êy?÷μ?ê±?ó?μ?ê //CH365ReadIoByte(&testAddr,&testRead);
printf("Now Let's Start to Check The PCI Device\n"); dev = CH365OpenDevice(false,false); if(dev <= 0) { printf("Sorry! Can't Find The Exact Device\n"); exit(0); } //for(int i=0;i<100;i++) CH365_SPI_Delay(); //return 0; }
用計(jì)算硬件計(jì)時(shí)數(shù)好像也準(zhǔn)確,大家有什么好的方法或者經(jīng)驗(yàn)嗎?
void CH365_SPI_Delay() { LONGLONG QPart1;//,QPart2; // double dfMinus, dfTim; double dfTim=0; UCHAR us = 50;// ?¢??êy UCHAR mTime; UCHAR mByte; QueryPerformanceCounter(&litmp); QPart1 = litmp.QuadPart; mTime = (UCHAR)((50 * us)/(ULONG)(50 *3.84)); /* ????ó2?t??ê±êy,3.84uS */ CH365ReadIoByte(& m_IOBase->mCh365IoTime,&mByte ); /* ?áè?ó2?t?-?·??êy??′??÷ */ mTime +=mByte; /* ?óé??eê??μ?a??êy???μ */ /* QueryPerformanceCounter(&litmp); dfTim = ((double)(litmp.QuadPart-QPart1)) / dfFreq;// ??μ???ó|μ?ê±???μ£?μ¥???a?? printf("d11f=%f\n",dfTim); */ while (mByte != mTime) CH365ReadIoByte(& m_IOBase->mCh365IoTime, &mByte); /* μè′y??êy?áê? */ QueryPerformanceCounter(&litmp); dfTim = ((double)(litmp.QuadPart-QPart1)) / dfFreq;// ??μ???ó|μ?ê±???μ£?μ¥???a?? printf("d22f=%f\n",dfTim); return; }
店小二的答案好像有點(diǎn)問題,如果第一次讀的數(shù)據(jù)是:0xff或者比較德的數(shù),延時(shí)就完全不準(zhǔn)了。我寫的程序如下,請(qǐng)大家評(píng)判。 void CCh365::DelayUs(unsigned int us) { unsigned char i,j; i=unsigned char(us); CH365mReadIoByte(Device_Number,&mIoBase->mCh365IoPort[0xfc],&mByte); j=mByte; i=i+j; if(i>=j) { again: CH365mReadIoByte(Device_Number,&mIoBase->mCh365IoPort[0xfc],&mByte); if(mByte goto again; } else { again1: CH365mReadIoByte(Device_Number,&mIoBase->mCh365IoPort[0xfc],&mByte); if((mBytej)) goto again1; } }
海天,我也是剛開始做PCI驅(qū)動(dòng),比較菜鳥,留個(gè)聯(lián)系方式,以后也可以交流交流!
頂一下,也沒有人有這方面的經(jīng)驗(yàn)???
jixiaohu的兩個(gè)程序, 第一個(gè)沒有看懂,好象沒有很硬件相結(jié)合 第二個(gè)沒有參考價(jià)值,那樣是不能比較的. 我仍然堅(jiān)持店小二的做法,不要加任何指令,或者改變變量的類型. 你的數(shù)據(jù)讀出來有誤可能是其他的問題.
jixiaohu:我也是第一次做pci的驅(qū)動(dòng),我覺得店小二的做法問題在于,當(dāng)WINDOWS其他的線程把(mByte= mTime)的時(shí)刻錯(cuò)過去的時(shí)候,這個(gè)本來只是微秒的延時(shí)就變成1毫秒+微秒的延時(shí)了。YIN你不這么覺得嗎?而我寫的程序是一個(gè)范圍,即使錯(cuò)過1個(gè)2個(gè),時(shí)間也就錯(cuò)過4微秒或者8微秒,不會(huì)誤差1毫秒,我的電子信箱是:LCLXY@263.NET,大家可以交流。
一次io操作的時(shí)間是0.2-0.5us吧 ,這樣好象是不會(huì)錯(cuò)過哪個(gè)值 的
測(cè)試下來好像相差很大,在用戶程序中調(diào)用CH365mReadIoByte和應(yīng)用程序中調(diào)用CH365mReadIoByte,會(huì)不會(huì)有什么不一樣呢? 也就驅(qū)動(dòng)級(jí)別的和App級(jí)別會(huì)不會(huì)不一樣?一次io操作的時(shí)間不一樣? 麻煩大家討論一下!
但是WINDOWS是一個(gè)多任務(wù)的操作系統(tǒng),它的時(shí)間是分片給所有的線程的,如果不是這樣,那應(yīng)該SLEEP也是很準(zhǔn)的,但SLEEP實(shí)際上只有10毫秒的精度.如果是DOS下,我絲毫也不懷疑會(huì)錯(cuò)過.但WINDOWS下,簡(jiǎn)單的延時(shí)程序都有長(zhǎng)有短,讀IO操作怎么就能保證一定在0.2-0.5us讀完呢?
經(jīng)驗(yàn)證正確的程序如下: void CCh365::DelayUs(unsigned int us) { unsigned char i,j; i=unsigned char(us); CH365mReadIoByte(Device_Number,&mIoBase->mCh365IoPort[0xfc],&mByte); j=mByte; i=i+j; if(i>=j) { again: CH365mReadIoByte(Device_Number,&mIoBase->mCh365IoPort[0xfc],&mByte); if(mBytegoto again; } else { again1: CH365mReadIoByte(Device_Number,&mIoBase->mCh365IoPort[0xfc],&mByte); if((mByte=j)) goto again1; } } 跟第7樓比在倒數(shù)第4行多了一個(gè)等于號(hào),如果沒有等于號(hào),有的時(shí)候延時(shí)不到4微秒就過去了.