Showing posts with label timer. Show all posts
Showing posts with label timer. Show all posts

Friday, November 14, 2014

Delay program using 8051 timers

The 8051 microcontroller has two independent 16 bit up counting timers named Timer 0 and Timer 1 and this article is about generating time delays using the 8051 timers. Generating delay using pure software loops have been already discussed here but such delays are poor in accuracy and cannot be used in sensitive applications. Delay using timer is the most accurate and surely the best method.

A timer can be generalized as a multi-bit counter which increments/decrements itself on receiving a clock signal and produces an interrupt signal up on roll over. When the counter is running on the processor’s clock , it is called a “Timer”, which counts a predefined number of processor clock pulses and  generates a programmable delay. When the counter is running on an external clock source (may be a periodic or aperiodic external signal) it is called a “Counter” itself and it can be used for counting external events.

In  8051, the oscillator output is divided by 12 using a divide by 12 network and then fed to the Timer as the clock signal. That means for an 8051 running at 12MHz, the timer clock input will be  1MHz. That means the the timer advances once in every 1uS and the maximum time delay possible using a single 8051 timer is ( 2^16) x (1µS) = 65536µS. Delays longer than this can be implemented by writing up a basic delay program using timer and then looping it for a required number of time. We will see all these in detail in next sections of this article.

Designing a delay program using 8051 timers
While designing delay programs in 8051, calculating the initial value that has to be loaded inot TH and TL registers forms a very important thing. Let us see how it is done.
  • Assume the processor is clocked by a 12MHz crystal.
  • That means, the timer clock input will be 12MHz/12 = 1MHz
  • That means, the time taken for the timer to make one increment = 1/1MHz = 1uS
  • For a time delay of “X” uS the timer has to make “X” increments.
  • 2^16 = 65536 is the maximim number of counts possible for a 16 bit timer.
  • Let TH be the value value that has to be loaded to TH registed and TL be the value that has to be loaded to TL register.
  • Then, THTL =  Hexadecimal equivalent of (65536-X) where (65536-X) is considered in decimal.


Example
Let the required delay be 1000uS (ie; 1mS).
That means X = 1000
65536 – X =  65536 – 1000 = 64536.
64536 is considered in decimal and converting it t0 hexadecimal gives FC18
That means THTL = FC18
Therefore TH=FC and TL=18

Program for generating 1mS delay using 8051 timer
The program shown below can be used for generating 1mS delay and it is written as a subroutine so that you can call it anywhere in the program. Also you can put this in a loop for creating longer time delays (multiples of 1mS). Here Timer 0 of 8051 is used and it is operating in MODE1 (16 bit timer).

DELAY : MOV TMOD,#00000001B // Set Timer0 to MODE1(16bit timer).Timer1 not used
          MOV TH0,#0FCH // Loads TH0 register with FCH
          MOV TL0,#018H // LOads TL0 register with 18H
          SETB TR0 // Starts the Timer 0
HERE : JNB TF0,HERE // Loops here until TF0 is set (ie;until roll over)
       CLR TR0 // Stops Timer 0
       CLR TF0 // Clears TF0 flag
       RET

The above delay routine can be looped twice in order to get a 2mS delay and it is shown in the program below.

MAIN : MOV R6,#2D
LOOP : ACALL DELAY
       DJNZ R6,LOOP
       SJMP MAIN
DELAY : MOV TMOD,#00000001B
        MOV TH0,#0FCH
        MOV TL0,#018H
        SETB TR0
HERE : JNB TF0,HERE
       CLR TR0
       CLR TF0
       RET

Important points to remember while using timers
  • Once timer flag (TF) is set, the programmer must clear it before it can be set again.
  • The timer does not stop after the timer flag is set. The programmer must clear the TR bit in order to stop the timer.
  • Once the timer overflows, the programmer must reload the initial start values to the TH and TL registers to begin counting up from.
  • We can configure the desired timer to create an interrupt when the TF flag is set.
  • If  interrupt is not used, then we have to check the timer flag (TF) is set using some conditional branching instruction. 
  • Maximum delay possible using a single 8051 timer is 65536µS and minimum is 1µS provided that you are using a 12MHz crystal for clocking the microcontroller.


Square wave generation using 8051 timer
Square waves of any frequency (limited by the controller specifications) can be generated using the 8051 timer. The technique is very simple. Write up a delay subroutine with delay equal to half the time period of the square wave. Make any port pin high and call the delay subroutine. After the delay subroutine is finished, make the corresponding port pin low and call the delay subroutine gain. After the subroutine  is finished , repeat the cycle again. The result will be a square wave of the desired frequency at the selected port pin. The circuit diagram is shown below and it can be used for any square wave, but the program has to be accordingly. Programs for different square waves are shown below the circuit diagram.


1KHz Square wave using 8051 Timer
MOV P1,#00000000B
MOV TMOD,#00000001B
MAIN : SETB P1.0
       ACALL DELAY
       CLR P1.0
       ACALL DELAY
       SJMP MAIN
DELAY: MOV TH0,#0FEH
       MOV TL0,#00CH
       SETB TR0
HERE : JNB TF0,HERE
       CLR TR0
       CLR TF0
       SETB P1.0
       RET
END

2 KHz Square wave using 8051 Timer
MOV P1,#00000000B
MOV TMOD,#00000001B
MAIN : SETB P1.0
       ACALL DELAY
       CLR P1.0
       ACALL DELAY
       SJMP MAIN
DELAY : MOV TH0,#0FCH
        MOV TL0,#018H
        SETB TR0
HERE : JNB TF0,HERE
       CLR TR0
       CLR TF0
       SETB P1.0
       RET
END


10 KHz Square wave using 8051 Timer
MOV P1,#00000000B
MOV TMOD,#00000001B
MAIN : SETB P1.0
       ACALL DELAY
       CLR P1.0
       ACALL DELAY
       SJMP MAIN
DELAY : MOV TH0,#0FFH
        MOV TL0,#0CEH
        SETB TR0
HERE : JNB TF0,HERE
       CLR TR0
       CLR TF0
       SETB P1.0
       RET
END

Read More..

Wednesday, November 12, 2014

Digital Countdown Timer





The presented digital timer is built in addition to the before mentioned ultraviolet light box.
Indication is on 4 digits seven segment LED display.
The timer counting backwards from 99 minutes and 59 seconds to 00:00.
It is built with microcontroller PIC 16F84A and integrated circuit CD4511.
Supply for the ultraviolet LED diodes are terminated with the help of the electromagnetic relay 6Volti.
Set the countdown time is done with 2 command keys, and the timer starts with the tally.
Author:Petar Avramovski

Source: Magazine Emiter
Read More..

Thursday, September 25, 2014

Simple UJT Timer Circuit

ln operation, the peak point requirement of UJT O1 is reduced to about } 1/1000 · of its normal requirement by pulsing its upper base with a volt negative pulse derived from the free running oscillator UJT I O2
This regular pulse momentarily reduces the peak point voltage of O1 1 and thus allows the peak point current to be supplied from C1 rather than F1‘1,` as it would be with the more conventional circuits of this type. The pulse rate of oscillator O2 is not very critical but it should have a period that is less than one fiftieth of the overall time delay.

Simple UJT Timer Circuit




Read More..

Sunday, September 7, 2014

Simple Timer with Musical Alarm Wiring diagram Schematic


This is a Simple Timer with Musical Alarm Circuit Diagram.This low-cost timer can be used for introducing a delay of one minute to two hours. After the timing period is over, a musical song is heard.

The schema is built around popular CMOS oscillator/divider CD4060 (IC1). It works off a 9V PP3 battery and its standby current drain is very low.

Pin
 Fig. 1: Pin configuration of melody generator ICUM66

By adjusting preset VR1, the time delay can be adjusted. After time delay is over, output pin 3 of IC1 goes high and npn transistor T1 conducts to provide positive power supply to melody generator IC UM66 (IC2) at its pin 2. Zener diode ZD1 reduces this power supply to 3.3V required for operation of IC2.

The output of IC2 is fed to the loudspeaker (LS1) via driver transistor T1. Preset VR2 is used to control the volume of the loudspeaker.

Simple
 Fig. 2: The schema of timer with musical alarm

The timer gets activated when power is supplied by pressing switch S1. To switch off the alarm, you need to switch off the power supply.


Author:  Pradeep G.
Read More..

Saturday, September 6, 2014

Simple Reaction Timer Wiring diagram Schematic

This is a Simple Reaction Timer Circuit Diagram. This simple schema uses a timer to generate pulses at a 5-ms clock rate. The pulses are shifted into the shift register, one at a time, lighting an LED. An auxiliary timer that generates one pulse per second is used to generate timing to activate the `go` LED and start the 5 ms pulses clocking into the registers. At the go signal each player presses his buttons (S3 or S4). The delay (reaction time) is read out on LED 4 to LED 17; after six seconds, the sequence repeats.

Simple Reaction Timer Circuit Diagram


Simple

Read More..