我可以在系統(tǒng)時(shí)鐘分頻選擇那里自己設(shè)置2分頻或1分頻嗎?如果可以該怎么設(shè)置呢?
#include?"..\Public\CH554.H"?????????????????????????????????????????????????? #include?"..\Public\Debug.H" #include?"Timer.H" #include?"stdio.h" #include?"GPIO.H" #pragma??NOAREGS sbit?PWM_1?=?P3^4; UINT16?T0_INIT=0X0056; int?time=0; main(?)? { ????CfgFsys(?);????????????????/ ????mDelaymS(10);?????????????? //??mInitSTDIO(?);???????????? ????Port3Cfg(1,4);???????????? ????mTimer0ClkFsys(); ????????? //????mTimer0Clk4DivFsys(); //????mTimer0Clk12DivFsys();???????? ????mTimer_x_ModInit(0,1);??????? //????mTimer_x_ModInit(0,2);????? ????mTimer_x_SetData(0,T0_INIT); ????mTimer0RunCTL(1);???????????? ????ET0?=?1;?????????????????????? ????EA?=?1; ????while(1) {} } void mTimer0Interrupt(?void?)?interrupt?INT_NO_TMR0?using?1???????????????? {??????????????????????????????????????????????????????????????????????????? mTimer_x_SetData(0,T0_INIT); ???? SCK?=?~SCK; time++; if(time>=4) time=0; if(time<2) PWM_1=1; ???????? else?PWM_1=0;?????????????????????????????????????????? }
現(xiàn)在的程序,希望不改變中斷函數(shù)里面的值通過定時(shí)器實(shí)現(xiàn)輸出86K左右的波形
這個(gè)程序我看過的,這個(gè)是直接把系統(tǒng)時(shí)鐘除以256就得出PWM的頻率,占空比就是調(diào)整256里面高電平所占的數(shù)值長(zhǎng)度。
我覺得這個(gè)和定時(shí)器輸出PWM參考性不大,現(xiàn)在我遇到的問題感覺像是定時(shí)器定時(shí)進(jìn)入中斷的時(shí)間刻度太短的話進(jìn)入中斷有誤
/**********************************?(C)?COPYRIGHT?******************************* *?File?Name??????????:?Main.C *?Author?????????????:?WCH *?Version????????????:?V1.1 *?Date???????????????:?2017/09/05 *?Description????????:?CH554?Time?初始化、定時(shí)器、計(jì)數(shù)器賦值,T2捕捉功能等 ???????????????????????定時(shí)器中斷處理? *******************************************************************************/ #include?"..\Public\CH554.H"?????????????????????????????????????????????????? #include?"..\Public\Debug.H" #include?"Timer.H" #include?"stdio.h" #include?"GPIO.H" #pragma??NOAREGS sbit?PWM_1?=?P3^4; UINT16?T0_INIT=0X0042; int?time=0; main(?)? { ????CfgFsys(?);????????????????//CH554時(shí)鐘選擇配置??? ????mDelaymS(10);??????????????//修改主頻,建議稍加延時(shí)等待主頻穩(wěn)定? ????mInitSTDIO(?);?????????????//串口初始化 ??Port3Cfg(1,4);?????????????//P34設(shè)置推挽模式 ????mTimer0ClkFsys(); ????????? ?//T0定時(shí)器時(shí)鐘設(shè)置??定時(shí)器,時(shí)鐘=Fsys // ??mTimer0Clk4DivFsys(); //T0定時(shí)器時(shí)鐘設(shè)置??定時(shí)器,時(shí)鐘=Fsys/4 // mTimer0Clk12DivFsys();?????????//定時(shí)器,時(shí)鐘=Fsys/12?T0標(biāo)準(zhǔn)時(shí)鐘 ????mTimer_x_ModInit(0,1);?????????//T0?定時(shí)器模式設(shè)置??模式1,16位定時(shí)器 // ??mTimer_x_ModInit(0,2);?????????//T0?定時(shí)器模式設(shè)置??模式2,8位自動(dòng)重裝載值定時(shí)器 ????mTimer_x_SetData(0,T0_INIT); ???//T0定時(shí)器賦值 ??mTimer0RunCTL(1);??????????????//T0定時(shí)器啟動(dòng) ????ET0?=?1;???????????????????????//T0定時(shí)器中斷開啟 ????EA?=?1; ????while(1); } /******************************************************************************* *?Function?Name??:?mTimer0Interrupt() *?Description????:?CH554定時(shí)計(jì)數(shù)器0定時(shí)計(jì)數(shù)器中斷處理函數(shù) *******************************************************************************/ void mTimer0Interrupt(?void?)?interrupt?INT_NO_TMR0?using?1????????????????//timer0中斷服務(wù)程序,使用寄存器組1 {???????????????????????????????????????????????????????????????????????????//方式3時(shí),TH0使用Timer1的中斷資源 mTimer_x_SetData(0,T0_INIT); ?????//T0定時(shí)器賦值 SCK?=?~SCK; time++; if(time>=4) time=0; if(time<2) PWM_1=1; else?PWM_1=0;?????????????????????????????????????????? }
測(cè)了一下,當(dāng)初值給到比0x0042(66)還小時(shí)輸出的頻率就不再怎么變化(大約50K)這個(gè)情況就很詭異。我是按照官網(wǎng)下載例程配置的,大佬們,可以幫忙看看哪里配置錯(cuò)了嗎?
現(xiàn)在能輸出高頻率的PWM波了,問題是板子上的晶振用的是12M的,換成24M的就能實(shí)現(xiàn)要求了。
但現(xiàn)在我還有一個(gè)疑惑,理論來說定時(shí)器的初值(進(jìn)入中斷的計(jì)數(shù)值)減少一半輸出頻率會(huì)增加一倍,但實(shí)際情況是只有在初值(進(jìn)入中斷的計(jì)數(shù)值)很大(初值1000+)的時(shí)候才能有接近兩倍的關(guān)系,在初值(進(jìn)入中斷的計(jì)數(shù)值)小于100的時(shí)候再減半頻率就基本保持不變了,這個(gè)是為什么呢?
謝謝廠家技術(shù)人員的細(xì)致指導(dǎo)