程序是在定時(shí)器例程里面改的,現(xiàn)在編譯也正常,但就是P34引腳輸出沒(méi)有波形?大佬們這個(gè)是什么情況??
定時(shí)器中的timer1變量為局部變量,應(yīng)該是timer++以及相關(guān)的if判斷工作不正常。
將timer1定義為全局變量。
回來(lái)訂正一下:局部變量的時(shí)候波形輸出沒(méi)有變化,改成全局變量的時(shí)候波形輸出有變化了
錯(cuò)誤信息,不能撤回;希望論壇這邊可以添加一個(gè)消息撤回選項(xiàng)
/**********************************?(C)?COPYRIGHT?******************************* *?File?Name??????????:?Main.C *?Author?????????????:?WCH *?Version????????????:?V1.1 *?Date???????????????:?2017/09/05 *?Description????????:?CH554?Time??????????¨?±?÷???????÷??????T2??×??????? ????????????????????????¨?±?÷???????í? *******************************************************************************/ #include?"..\Public\CH554.H"?????????????????????????????????????????????????? #include?"..\Public\Debug.H" #include?"Timer.H" #include?"stdio.h" #include?"GPIO.H" #pragma??NOAREGS //UINT8?FLAG; //UINT16?Cap[8]?=?{0}; int?time1=0,time2=0; sbit?PWM_1?=?P3^4; sbit?PWM_2?=?P1^0; main(?)? { ????CfgFsys(?);????????????????//CH554?±????????????? ????mDelaymS(50);???????????????//?????÷?????¨?é???????±?????÷?????¨? ????mInitSTDIO(?);?????????????//?????????? ??Port3Cfg(1,4);?????????????//P34?è?????ì???? Port1Cfg(1,0);?????????????//P10?è?????ì???? ????printf("start?...\n");? #ifdef?T0_INT ????printf("T0?Test?...\n");? //????mTimer0ClkFsys(); ????????? ?//T0?¨?±?÷?±???è?????¨?±?÷,?±??=Fsys ??mTimer0Clk12DivFsys(); ?????????//T0?¨?±?÷?±???è?????¨?±?÷,?±??=Fsys/12?T0±ê×??±?? ????mTimer_x_ModInit(0,1);???????????//T0??¨?±?÷?????è????????1??16???¨?±?÷ ????mTimer_x_SetData(0,0xD8FE); ?????//T0?¨?±?÷???? ????mTimer0RunCTL(1);????????????????//T0?¨?±?÷???? ????ET0?=?1;?????????????????????????//T0?¨?±?÷???????? ????EA?=?1; ????while(1); #endif //#ifdef?T1_INT //????printf("T1?Test?...\n");? //????mTimer1Clk12DivFsys(); ?????????//T1?¨?±?÷?±???è?? //????mTimer_x_ModInit(1,2);???????????//T1??¨?±?÷?????è?????8??×?????×°?¨?±?÷ //????mTimer_x_SetData(1,0xEEEE); ?????//T1?¨?±?÷???? //????mTimer1RunCTL(1);????????????????//T1?¨?±?÷???? //????ET1?=?1;?????????????????????????//T1?¨?±?÷???????? //????EA?=?1; //????while(1);? //#endif #ifdef?T2_INT ????printf("T2?Test?...\n");? ????mTimer2Clk12DivFsys(); ???????????????//T2?¨?±?÷?±???è?? ????mTimer_x_ModInit(2,2);???????????//T2??¨?±?÷?????è?? ????mTimer_x_SetData(2,0xD8F0); ?????//T2?¨?±?÷???? ????mTimer2RunCTL(1);????????????????//T2?¨?±?÷???? ????ET2?=?1;?????????????????????????//T2?¨?±?÷???????? ????EA?=?1; ????while(1);? #endif ????while(1); } #ifdef?T0_INT /******************************************************************************* *?Function?Name??:?mTimer0Interrupt() *?Description????:?CH554?¨?±?????÷0?¨?±?????÷???????í???? *******************************************************************************/ void mTimer0Interrupt(?void?)?interrupt?INT_NO_TMR0?using?1????????????????//timer0????·??????ò,?????????÷×é1 {???????????????????????????????????????????????????????????????????????????//·???3?±??TH0????Timer1??????×??? //?????mTimer_x_SetData(0,0x0000);??????????????????????????????????????????//·?×???????·????è??????TH0??TL0????? mTimer_x_SetData(0,0xD8FE); ?????//T0?¨?±?÷??????65535-10000 SCK?=?~SCK; time1++; if(time1>=200) time1=0; if(time1=200) time2=0; if(time2<100) PWM_2=1; else?PWM_2=0;? } #endif
訂正:這個(gè)程序沒(méi)有定義宏,所以定義的引腳用示波器監(jiān)測(cè)的時(shí)候顯示不出來(lái)波形。
錯(cuò)誤信息,可能會(huì)造成誤導(dǎo),撤回
1、檢查T(mén)0_INT、T2_INT兩個(gè)宏是否有效
2、程序中都有保留SCK翻轉(zhuǎn),用示波器觀察P1.7(SCK)是否有翻轉(zhuǎn)
3、不使用定時(shí)器功能,直接在主循環(huán)中定時(shí)翻轉(zhuǎn)兩個(gè)IO,檢查IO本身能否實(shí)現(xiàn)翻轉(zhuǎn)
在程序最開(kāi)始的時(shí)候添加了宏定義,這個(gè)時(shí)候示波器監(jiān)測(cè)到SCK翻轉(zhuǎn),但I(xiàn)O不翻轉(zhuǎn);直接在程序里面翻轉(zhuǎn)IO又是沒(méi)有問(wèn)題的。
時(shí)鐘引腳有翻轉(zhuǎn)信號(hào),但定義的輸出引腳信號(hào)沒(méi)有翻轉(zhuǎn);單獨(dú)翻轉(zhuǎn)定義的輸出引腳又可以實(shí)現(xiàn)翻轉(zhuǎn)。更換引腳和配置還是一樣的效果,只要設(shè)置成輸出模式都可以(一般設(shè)置成推挽輸出)。
自己回復(fù)自己一下,能輸出波形了,但具體不知道是什么問(wèn)題?
我把程序宏全部去掉,只留下定時(shí)器基礎(chǔ)設(shè)置和中斷函數(shù),然后運(yùn)行可以輸出波形。這樣看的話應(yīng)該算是宏定義有誤吧!
但程序是能進(jìn)入中斷的,SCK也能翻轉(zhuǎn)但就是沒(méi)有波形輸出。這樣來(lái)說(shuō)宏應(yīng)該是定義成功了的。
所以我也不知道是什么問(wèn)題,有大佬幫忙給小白解釋一下嗎?
/**********************************?(C)?COPYRIGHT?******************************* *?File?Name??????????:?Main.C *?Author?????????????:?WCH *?Version????????????:?V1.1 *?Date???????????????:?2017/09/05 *?Description????????:?CH554?Time??????????¨?±?÷???????÷??????T2??×??????? ????????????????????????¨?±?÷???????í? *******************************************************************************/ #include?"..\Public\CH554.H"?????????????????????????????????????????????????? #include?"..\Public\Debug.H" #include?"Timer.H" #include?"stdio.h" #include?"GPIO.H" #pragma??NOAREGS int?time=0; sbit?PWM_1?=?P3^4; UINT16?T0_INIT=0X0056; main(?)? { ????CfgFsys(?);????????????????//CH554?±????????????? ????mDelaymS(10);??????????????//?????÷?????¨?é???????±?????÷?????¨? ????mInitSTDIO(?);?????????????//?????????? ??Port3Cfg(1,4);?????????????//P34?è?????ì???? ??UART1Init(?);??????????????//????1?????? ????mTimer0ClkFsys(); ????????? ?//T0?¨?±?÷?±???è?????¨?±?÷,?±??=Fsys // ??mTimer0Clk4DivFsys(); //T0?¨?±?÷?±???è?????¨?±?÷,?±??=Fsys/4 // mTimer0Clk12DivFsys();?????????//?¨?±?÷,?±??=Fsys/12?T0±ê×??±?? ????mTimer_x_ModInit(0,1);?????????//T0??¨?±?÷?????è????????1??16???¨?±?÷ // ??mTimer_x_ModInit(0,2);?????????//T0??¨?±?÷?????è????????2??8??×?????×°?????¨?±?÷ ????mTimer_x_SetData(0,T0_INIT); ???//T0?¨?±?÷???? ??mTimer0RunCTL(1);??????????????//T0?¨?±?÷???? ????ET0?=?1;???????????????????????//T0?¨?±?÷???????? ????EA?=?1; ????while(1) {} } /******************************************************************************* *?Function?Name??:?mTimer0Interrupt() *?Description????:?CH554?¨?±?????÷0?¨?±?????÷???????í???? *******************************************************************************/ void mTimer0Interrupt(?void?)?interrupt?INT_NO_TMR0?using?1????????????????//timer0????·??????ò,?????????÷×é1 {???????????????????????????????????????????????????????????????????????????//·???3?±??TH0????Timer1??????×??? mTimer_x_SetData(0,T0_INIT); ?????//T0?¨?±?÷???? SCK?=?~SCK; time++; if(time>=2) time=0; if(time<1) PWM_1=1; else?PWM_1=0;?????????????????????????????????????????? }
現(xiàn)在可以輸出43K左右的波形,但繼續(xù)調(diào)小T0_INIT(定時(shí)器初始值),波形變化有點(diǎn)奇怪,繼續(xù)減小初始值頻率會(huì)變大,但達(dá)到一定程度后初始值不管如何變化頻率基本保持不變。這是個(gè)什么情況?
我發(fā)現(xiàn)定時(shí)器時(shí)鐘=Fsys和定時(shí)器時(shí)鐘=Fsys/12兩種情況下輸出的頻率并非是12倍的關(guān)系(輸出PWM頻率在43K左右),當(dāng)選擇四倍頻輸出的時(shí)候程序報(bào)錯(cuò),4倍頻沒(méi)測(cè)。我想輸出頻率在43K、占空比可調(diào)的PWM波,這個(gè)芯片能做到嗎?現(xiàn)在能輸出43K但占空比不能調(diào)整,大佬可以幫忙看看嗎?
您好,使用定時(shí)器時(shí)要注意是否和串口初始化時(shí)使用的定時(shí)器沖突。比如T2MOD寄存器。
這個(gè)應(yīng)該是沒(méi)有關(guān)系的,我前面也考慮是否會(huì)有影響,我把串口初始化那些都注釋掉了,輸出還是大概到50K的時(shí)候頻率就不變化了,我監(jiān)測(cè)的是SCK時(shí)鐘引腳。
現(xiàn)在的情況是,定時(shí)器給初值0x0056時(shí)鐘引腳SCK可以輸出43K左右的波形,當(dāng)把初始值改為0x002B的時(shí)候輸出就是50K,按照理論來(lái)說(shuō)應(yīng)該是86K左右才是正常的,但現(xiàn)在好像到50K就達(dá)上限了