Successfully reported this slideshow.

8051 timers--2


Published on

Best of Luck

Published in: Engineering, Technology, Business
  • Be the first to comment

  • Be the first to like this

8051 timers--2

  1. 1. Timers of Intel 8051 Ikram R. Qureshi Instructor: CS-311 Department of Electronic Engineering Timers Used For: l To generate time intervals l To count an external event l To generate Baud Rate 8051 Timers l The 8051 is equipped with two timers l Both of which may be controlled, set, read, and configured individually 8052 equipped with three timers
  2. 2. l Works in similar manner as 8051 Intel8051 TIMERS l Inte8051 has TWO timers (Timer-0 and Timer-1) l Timer-0 l Two Bytes Wide, Total 16 bits l Located in SFRs – TL0-Timer-Low Byte, SFR-Add-8AH (Not Bit Addressable) – TH0-Timer-High Byte, SFR-Add-8CH (Not Bit Addressable) l Low and High Bytes are not consecutive bytes Intel8051 Timers l Timer-1 l Two Bytes Wide, Total 16 bits l Located in SFRs – TL1-Low Byte..SFR-8BH (Not Bit Addressable) – TH1-High Byte..SFR-8DH(Not Bit Addressable) Low and High Bytes are not consecutive bytes 8051 timers location
  3. 3. l Timers are located in SFR’s A Flip Flop used as counter l One flip-flop have two states 0 or 1 – Need two clock cycle to generate one complete cycle at the output Q – Output Q become a clock to the next stage flip-flop if more FF are cascaded – While /Q is used to feed the data input. – Start: (first cycle) 2 nd cycle – Clock=high to low Clock=high to low – Data=Low Data=High – /Q=high /Q0= low – Q0=low Q0= high A Flip Flop used as counter l Hardware concept l Three flip can count up to 8 How does a timer count? l A timer always counts up l It doesn’t matter whether the timer is being used as – a timer, a counter, – a baud rate generator How does a timer count? Timer Clock l Timer needs a pulse to tick (cycle falling edge) l 8051 clock crystal is used to generate this cycle (pulse)
  4. 4. l Each machine cycle generate one tick l 12 crystal cycle makes one machine cycle l 12 MHz crystal generate one tick in 1 micro sec one machine cycle time t = 12/12MHz = 1 micro second 12MHz crystal Timer Operation (How does a timer works?) l After timer is started, it increment once by each machine cycle l Timer keep incrementing until it’s register over flows after reaching to it’s maximum value l Example: l Initial value in TH0, TL0= FFFCH. l By each machine cycle the timer will increment; from FFFCH FFFDH FFFE FFFF 0000 l Timer over flow flag will be set on the 4th m/c TF=1 l Over flow flag set, timer stop working l Need to restart the timer again l For 12 MHz System USING TIMERS TO MEASURE TIME l How long does a timer take to increment? l Timer is incremented once by each machine cycle l With 11.0592 MHz crystal, the timer will be
  5. 5. incremented frequency = 11.0592 MHz/12 = 921,583 Hz or time/sec time lapse = 1/921583 = 1.085 micro sec l If timer has to count 100,00 times, It will take 100,00 x 1.085 micro sec = 0.01085 sec Timer Registers l Both bytes of timer registers can be treated as any other register like R0, A, B Example of assembly instruction (using direct addressing mode): l MOV TL0, #55H ;move hex number 55 into low byte of timer-0 l MOV A, TH1 ;save high byte of timer- into accumulator Timer Mode Register(TMOD) l Eight bit timer mode register is used to set the operating modes of both timers l Located in SFR l Address 89H, Not bit addressable – Lower 4-bits are used to set mode of timer-0 – Higher 4-bits are used to set mode of Timer-1 TMOD
  6. 6. l TMOD register bits l M0 Mode bit 0 l M1 Mode bit 1 l C/T Timer or counter l ‘Set’ to perform counter operations (C/T=1) l ‘Clear’ to perform timer operations (C/T=0) l Gate l G=1, external hardware to set this bit cause the timer to start/stop l G=0, Software instruction may use to start/stop action by manipulation the TR0 and TR1 bits.(SETB RT1, CLR TR1) TMOD (89h) SFR Timer Mode Register(TMOD) l Bit M0 and M1 together set the operating mode of the timer Both bytes THx and TLx are worked together Timer Control Register- TCON l TCON l One Byte Wide l Located in SFRs, Address-88H l Bit addressable
  7. 7. – This register contains status and control bits for timer-0 and timer-1(Scrambled) TCON l Timer/Counter control register l Only 4-bits are used for timer function(to start the timer, overflow flag) MODE-0 l 13 Bit timer, 5 bits pre scalar l This mode is there to keep 8051 compatible with its predecessor 8048. (No longer it has any good use) – Lower 5 bits of TLx with 8 bits of THx generate a 13 bits time action. Upper 3 bits of TLx are not used – It increments from 0 to 1FFFH then it rolls over to 0000 and set TF flag – It will increment 8192 times before setting up the overflow flag Mode-1 l 16 bits timer l In this mode both bytes TLx and THx work together l The timer increments from: l 0000H to FFFFH ( sixteen bit number = 65535) l 2^16 = 65536 [from 65535 65536 timer over flow occurs] Timer Mode -1
  8. 8. l Procedure to set this timer mode – Clear M1 and set M0 bits in TMOD register( to set mode-1) – Load TH and TL with initial value, the timer must be started by setting up the TR0 or TR1 bit in TCON register. SETB TCON.4 or SETB TR0 for timer-0 SETB TCON.6 or SETB TR1 for timer-1 l Timer will count up from the initial value, till it reach max (FFFFH) and rolls over to 0000h, at the time of roll over it will set TFx in TCON register l By monitoring TFx flag the program control may be directed l To restart the timer, a) TF must need to be cleared, b) TH, TL need to be re initialized and c) TR bit need to be set again. Timer Mode-1 in Action Example A program to generate square wave on pin #P1.1 with 50% duty cycle. Use timer-1 to control the pulse width. MOV TMOD, #10H ;set timer-1 to mode-1(M0=1,M1=0) BAK: MOV TL1, #0F0H ;load initial value in timer-1 low byte MOV TH1,#0FFH ;load initial value in time-1 high byte ............ The using software is free version, you can upgrade it to the upgrade version.