《(62)--CH2-6-2 Usage of Counters and Ti嵌入式系统原理.ppt》由会员分享,可在线阅读,更多相关《(62)--CH2-6-2 Usage of Counters and Ti嵌入式系统原理.ppt(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、第第2 2章章 单片机原理与应用单片机原理与应用Principles Principles and Applications of and Applications of MicrocontrollerMicrocontrollerChapter 2.6 Counters and Timers2.6.1 Usage of Counter/TimerChapter 2.6 Counters and Timers2.6.2 Usage of Counter/Timer (1)Set working mode of Timer 0 (TMOD=0 x01 ;c language);16 bit,ti
2、mer,doesnt use INT0Chapter 2.6 Counters and Timers2.6.1 Usage of Counter/TimerT0M1T0M0MODEDESCRIPTION00013-bit timer01116-bit timer1028-bit auto-reload113Split modeTimer Working ModeChapter 2.6 Counters and Timers2.6.1 Usage of Counter/Timer(2)Set counters initial value For example:The pulses counte
3、d coming from oscillator(12MHz),so the frequency of pulses is 1MHz.Want to get 50 millisecond(0.05 second)time long,we should count for 50,000 pulses.So the initial value of counter should be:65536-50000=15536=3CB0HProgramming(in c language):TH0=0 x3c;TL0=0 xb0;Chapter 2.6 Counters and Timers2.6.1 U
4、sage of Counter/Timer (3)Start Timer 0 (TCON=TCON|0 x10 ;c language)Chapter 2.6 Counters and Timers2.6.1 Usage of Counter/Timer(4)Detect the end flag of counting TF0(bit 5 of TCON)indicate the end of counting(TH0 TL0=0)while(TF0=0);Chapter 2.6 Counters and Timers2.6.1 Usage of Counter/Timer/Example:
5、Using timer0 to delay preciselyvoid delay(int n)/n msecondsint i;TMOD=0 x01;/16 bits,internal pulse counting for(i=0;i20*n;i+)TH0=0 x3c;TL0=0 xb0;/Initial value=65536-50000=15536,0.05s in 12MHz TF0=0;TR0=1;/start Timer 0 while(TF0=0);/Wait for end flag TF0,delay for 0.5s TR0=0;/stop timer 0 Chapter
6、2.6 Counters and TimersSummary:(1)There are two timers in 8051 MCU,which called T0 and T1。(2)They are incremented counter.(3)They can be used as 8/13/16 bits counter according to their work mode(4)The pulses they counted come from oscillator or PINs(5)To operate the counter by programming the related SFRs.