【正文】
。 定時(shí)誤差校準(zhǔn)的簡單原理是:在定時(shí)溢出響應(yīng)后,停止定時(shí)器計(jì)數(shù),讀出當(dāng)時(shí)計(jì)數(shù)值(它反映了定時(shí)響應(yīng)的延遲時(shí)間),然后將完成這一任務(wù)的程序段執(zhí)行時(shí)間考慮進(jìn)去,作為修正因子校正定時(shí)初值,以下程序段以中斷處理方式為例: clr ea clr tr1 clr c mov a,#n0 clr tr0 subb a,tl0 subb a,#08 mov tl0,a mov a,#n1 subb a,th0 mov th0,a setb tr0 定時(shí)處理語句段 reti 由于執(zhí)行從指令clr tr0(停止計(jì)數(shù))到指令setb tr0(重新開啟計(jì)數(shù))之間的指令需8個(gè)機(jī)器周期,應(yīng)將此消耗考慮進(jìn)去。 查詢方式處理程序如下: l1:jbc tf0,lo1 sjmp l1 lo1:mov th0,#n1 mov th1,#n2 定時(shí)處理語句段 sjmp l1 5 定時(shí)的誤差校準(zhǔn) 由于單片機(jī)的機(jī)器周期為1μs~2μs,定時(shí)誤差一般應(yīng)在0μs~20μs之內(nèi),對于一般應(yīng)用,此誤差可以忽略,但是對于精確度要求比較高的應(yīng)用場合,此誤差必須進(jìn)行校正。 步驟2編寫初始化程序 初始化程序與定時(shí)編程方式有關(guān),中斷方式初始化程序如下(以0路定時(shí)器為例): mov tmod,#n(規(guī)定定時(shí)工作方式) mov th0,#n1(設(shè)置計(jì)數(shù)器低位) mov tl1,#n2(設(shè)置計(jì)數(shù)器高位) setb ea(開放中斷) setb et0(開放定時(shí)中斷0) setb tr0(啟動(dòng)定時(shí)) 查詢方式初始化程序如下: mov tmod,#n(規(guī)定定時(shí)工作方式) mov th0,#n1(設(shè)置計(jì)數(shù)器低位) mov tl1,#n2(設(shè)置計(jì)數(shù)器高位) clr et0(開放定時(shí)中斷0) setb tr0(啟動(dòng)定時(shí)) 其中#n1和#n2與計(jì)數(shù)初值x有關(guān),如果采用方式2,有#n1=#n2,所有初始化程序都以0路定時(shí)器為例,如果用1路定時(shí)器,則將程序中的寄存器和控制位的下標(biāo)1改為0。假定單片機(jī)機(jī)器周期為k0,所選定的定時(shí)方式計(jì)數(shù)器為n位,所實(shí)現(xiàn)的定時(shí)時(shí)間長度為t,則有: (2n-x)k0=t (1) 其中x為計(jì)數(shù)器的初值,根據(jù)式1可以求出初值x為: x=2n-(t/k0) (2) 4 定時(shí)器的編程步聚和各步聚的通用程序 定時(shí)器應(yīng)用編程主要分為以下幾個(gè)步聚。 定時(shí)編程有兩種基本方式,一種方式為查詢方式,在啟動(dòng)定時(shí)時(shí)關(guān)閉相應(yīng)定時(shí)中斷,然后循環(huán)檢測tf0(tf1)進(jìn)行相應(yīng)處理;另一種方式為中斷方式,啟動(dòng)定時(shí)時(shí)開放相應(yīng)定時(shí)中斷,并將定時(shí)溢出處理程序編寫為中斷處理程序形式通過在相應(yīng)中斷入口地址(000b,001b)放置跳轉(zhuǎn)指令轉(zhuǎn)入定時(shí)溢出處理程序。 c/t位為定時(shí)與計(jì)數(shù)選擇位,c/t=0為定時(shí)方式,c/t=1為計(jì)數(shù)方式。 定時(shí)器的工作方式選擇主要依靠設(shè)置tmod寄存器的值,其各位定義如下: 位序 b7 b6 b5 b4 b3 b2 b1 b0 位符號 gate c/t m1 m0 gate c/t m1 m0 其中,b0~b3用來控制0路定時(shí)器,b4~b7用來控制1路定時(shí)器。 2定時(shí)器工作方式與方式設(shè)置 mcs51單片機(jī)有兩路獨(dú)立的定時(shí)器,每路定時(shí)器有4種工作方式(0~3),方式0是13位計(jì)數(shù)結(jié)構(gòu),計(jì)數(shù)器由th全部8位與tl的低5位構(gòu)成;方式1是16位計(jì)數(shù)結(jié)構(gòu),計(jì)數(shù)器由th與tl全部8位共16位組成;方式2是8位計(jì)數(shù)結(jié)構(gòu)方式,計(jì)數(shù)器由tl8位組成,與其他方式不同的是,當(dāng)定時(shí)溢出時(shí),硬件自動(dòng)將th的值裝入tl中,有自動(dòng)加載功能。而定時(shí)器是單片機(jī)應(yīng)用中解決某類復(fù)雜問題的最有為效的方法,應(yīng)用非常廣泛。s working determination counter figure n, obtained k0 and n substitution type 2, extracts the counting starting value x, and according to the counting way which chooses x value divides enters th0 (th1) and tl0 (tl1). Step 2 initialization procedures to prepareInitialization procedures and the way of regular programming, interrupt initialization procedure is as follows (Road Timer 0 as an example):mov tmod, n (regular work requirements)mov th0, n1 (set counter low)mov tl1, n2 (counter high settings)setb ea (open interruption)setb et0 (open regularly interrupted 0)setb tr0 (start timing)Query initialization procedure is as follows:mov tmod, n (regular work requirements)mov th0, n1 (set counter low)mov tl1, n2 (counter high settings)clr et0 (open regularly interrupted 0)setb tr0 (start timing)One n1 and n2 and x of the initial count, if we adopt the way of 2, n1 = n2, initialization procedures are all the way to Timer 0 as an example, if a road timer, it will process bit register and control the subscript 1 to 0. Step 3 preparation process from time to timeInterrupt handling procedures are as follows:org 000bjmp b1b1: clre amov th0, n1mov th1, n2Paragraph statement from time to time to deal withretiAmong them, the timer 0 for Road 000b, if the timer for 1 Road 001b, if the timer 2 in the way, you do not need to th0, th1 reassignment.Query process is as follows:l1: jbc tf0, lo1sjmp l1lo1: mov th0, n1mov th1, n2Paragraph statement from time to time to deal withsjmp l15 Timing Error CalibrationAs the MCU machine cycle for 1μs ~ 2μs, timing errors should normally be within 0μs ~ 20μs, for general application, this error can be ignored, but the requirements for higher accuracy applications, the error must be corrected[7]. From time to time after the error is regularly overflow into the implementation of paragraph statement from time to time to deal with the amount of time spent between, this time mainly by the overflow from time to time into the above statement from time to time to deal with the need to implement the instructions or hardware process.Timing error calibration principle is simple: to respond in time to time after the spill, stop the timer count, read out at the time of value (which reflects the timing of the delayed response time), and then plete the task execution time of the program segment into account, as amended from time to time initial correction factor, the following procedures in order to interrupt the treatment above as an example:clr eaclr tr1clrcmov a, n0clr tr0subb a, tl0subb a, 08mov tl0, amov a, n1subb a, th0mov th0, asetb tr0Paragraph statement from time to time to deal withretiAs a result of the implementation of directives from the clr tr0 (stop counting) to the mand setb tr0 (reopen count) mand to be between 8 machine cycles, this consumption should be taken into account. The above procedure will reduce the timing errors in a machine cycle.單片機(jī)定時(shí)器的應(yīng)用與誤差糾正摘 要:對mcs51單片機(jī)定時(shí)器的原理與使用中的幾個(gè)關(guān)鍵問題進(jìn)行了討論,給出定時(shí)器的使用方法與誤差補(bǔ)