Timers are common features of most microcontroller. In simplified terms a timer is just a register whose value keeps increasing or decreasing by a constant rate without the help of the CPU. The CPU can read or write this register any time. It reads it find out how much time has elapsed.
The Timer register can have the following bit length. A timer has a clock source, for example of the clock source of 10KHz is input to a timer then one increment will take uS micro second. This clock source can be obtained from the CPU clock. To help us out their is a thing called prescaler in the MCU. The job of prescaler is to divide the CPU clock to obtain a smaller frequency.
The PIC Micro that we will use in this example has the following prescaler division factors available.
Timers are also called Counters this is because they can be used to count external events. The following example illustrate the fact. The above setup can be used to measure the RPM speed in revolution per minute of the rotating wheel. A small magnet is attached in the edge of the wheel.
Whenever this magnet is exactly below the Magnetic sensor its output becomes high. So each time the magnet passes by the sensor the timer register inside the MCU is incremented. You can note one this that all this happens without the help of CPU. CPU can do other task and read the Timer register only when required. An overflow occurs when a timer register has already counted the maximum value it can count. At overflow the counter value become 0 again. For example when an 8 bit timer has the value and receive another clock that will set it to 0 and generate an overflow.
An overflow can trigger an interrupt and the ISR can handle it. PIC18F has four different timers. And when counter becomes 76 we toggle RB1 pin. This pin is connected to LED. The details of each bit is given below.The 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 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. 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. While designing delay programs incalculating 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. 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. The above delay routine can be looped twice in order to get a 2mS delay and it is shown in the program below. 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. 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. I need a square wave vith different delay for each half cycle and voltage levels are 20V and current ratings are 6A so how can i get it by using uc By changing that value we could obtain what you sought.
Author admin. Sreekarthika 2 years ago. Hey how to produce triangular wave with a delay of. Amit Sasane 5 years ago. Art 6 years ago. Prithvi 2 years ago. Santhi Ganesan 6 years ago. Can we write the same hardware delay program using Timer 1??Carry C Flags b.
Zero Z Flags c. Digit Carry DC Flags d. All of the above. Built-in Power-on-reset b. Brown-out reset c. None of the above. Improvement in bandwidth b. Instruction fetching becomes possible over a single instruction cycle c. Independent bus access provision to data memory even while accessing the program memory d. Excogitation of Inputs b. Handling of Outputs c. Interpretation of internal timing for program execution d.
Provision of OTP for large and small production runs. Only C b. Watchdog Timer WDT d. Status Register b. Direct Addressing Mode b.
Indirect Addressing Mode c. Immediate Addressing Mode d. Carry bit C b. Digits Carry bit DC c. Address byte must be always written in FSR as it is independent of any instruction in indirect addressing mode. Normal mode b. Sleep mode c. Power-down mode d.
PIC interrupt tutorial
Any flexible mode. For ensuring the inception and stabilization of an oscillator in a proper manner b. For detecting the rise in V DD c. For enabling or disabling the power-up timers d. For generating the fixed delay of 72ms on power-up timers. Initial address b. Middle address c. Final address d.In this tutorial, we will start with an introduction of 7 segment displays? How to interface a 7-segment display with PIC microcontrollers. Simplest, because its working is easy to understand and its interface with the microcontroller is quite straight-forward.
Seven segments, as the name suggests, consist of seven LEDs organized in a specific pattern. As you can see from this diagram, it consists of 10 pins. A common pin is also associated with the 7-segment, which is used to identify the type of 7-segment display; whether it is the common anode or a common cathode. To turn on a specific segment, we connect that pin to the ground or logic low level. In common cathode display, all the cathode connections of the LEDs are tied together which forms the common pin that needs to be grounded.
To turn on a specific segment in common cathode mode, we connect that pin to the voltage or to a logic high level with a microcontroller. As we mentioned earlier, it consists of 7 light-emitting devices that are arranged in a rectangular design box. By controlling specific lights, we can display numbers from Each segment is referred with the name from a to g. Similarly we can display other numbers by controlling respective light-emitting devices. Note: If we use common anode type 7 segment display, control signals a-g will be active low ground reference and similarly for common cathode type, control signals are always active high level usually 5 volts.
In previous sections, we have seen that how to control 7 segments displays. As you know that we can easily interface 7 segment displays with a pic microcontroller by using GPIO pins of PIC microcontroller as digital output pins. It requires a simple interface similar to an LED interfacing tutorial. In this section, we will see an example of 7 segment displays interfacing with pic microcontroller. This picture shows a connection diagram of 7 segment display with pic microcontroller. A common anode type display is used.If that external device has to send some information to microcontroller, then microcontroller needs to know about this situation to get that information.
An example of such an external device is the digital thermometer. It measures the temperature and at the end of measurements transmits results to the microcontroller. Now the purpose of this article to explain the fact that how does the microcontroller knows to get the required information from an external device. There are two methods of communication between the microcontroller and the external device:. In this method, the external devices are not independent. We fix the time interval in which microcontroller has to contact the external device.
The microcontroller accesses that device at the exact time interval and gets the required information. Polling method is just like picking up our phone after every few seconds to see if we have a call. It needs to wait and check whether the new information has arrived not. This signal requests the microcontroller to stop to perform the current program temporarily time to execute a special code.
It means when external device finishes the task imposed on it, the microcontroller will be notified that it can access and receive the information and use it. Interrupts are just like waiting for the phone to ring. The request to the microcontroller to stop to perform the current program temporarily can come from various sources:. There are 2 types of interrupts for PIC microcontroller that can cause break.
These are the registers for interrupt operation and minimum 1 register can be used to control the interrupt operation in PIC18F which are:. This article also deals with external interrupts of PIC18F so we will discuss it in detail here. This bit is set high to enable all interrupts of PIC18F This bit is set high to enable all the peripheral interrupts Internal interrupts of the microcontroller.
This bit is set high to enable the External Interrupt 0.
This bit is set high to enable the external interrupts. This bit is set high to enable the RB Port Change interrupt pin. These bits are used to select the triggering edge of the corresponding interrupt signal on which the microcontroller is to respond. These bits are used to set priority of the interrupts 1 and 2, respectively. These are External Interrupt 1 and 2 flag bits, respectively. Reset is given through pin 1.A watchdog timer sometimes called a computer operating properly or COP timer, or simply a watchdog is an electronic timer that is used to detect and recover from computer malfunctions.
During normal operation, the computer regularly resets the watchdog timer to prevent it from elapsing, or "timing out". If, due to a hardware fault or program error, the computer fails to reset the watchdog, the timer will elapse and generate a timeout signal.
The timeout signal is used to initiate corrective action or actions. The corrective actions typically include placing the computer system in a safe state and restoring normal system operation.
Watchdog timers are commonly found in embedded systems and other computer-controlled equipment where humans cannot easily access the equipment or would be unable to react to faults in a timely manner. In such systems, the computer cannot depend on a human to invoke a reboot if it hangs ; it must be self-reliant.
For example, remote embedded systems such as space probes are not physically accessible to human operators; these could become permanently disabled if they were unable to autonomously recover from faults.
A watchdog timer is usually employed in cases like these. Watchdog timers may also be used when running untrusted code in a sandboxto limit the CPU time available to the code and thus prevent some types of denial-of-service attacks. The act of restarting a watchdog timer, commonly referred to as "kicking" the watchdog  is typically done by writing to a watchdog control port.
Alternatively, in microcontrollers that have an integrated watchdog timer, the watchdog is sometimes kicked by executing a special machine language instruction or setting a specific bit in a register.
Delay using 8051 timer
In computers that are running operating systemswatchdog resets are usually invoked through a device driver. The device driver, which serves to abstract the watchdog hardware from user space programs, is also used to configure the time-out period and start and stop the timer.
Watchdog timers come in many configurations, and many allow their configurations to be altered.Electronic Basics #30: Microcontroller (Arduino) Timers
Microcontrollers often include an integrated, on-chip watchdog. In other computers the watchdog may reside in a nearby chip that connects directly to the CPUor it may be located on an external expansion card in the computer's chassis. The watchdog and CPU may share a common clock signalas shown in the block diagram below, or they may have independent clock signals.
Two or more timers are sometimes cascaded to form a multistage watchdog timerwhere each timer is referred to as a timer stageor simply a stage.
For example, the block diagram below shows a three-stage watchdog. In a multistage watchdog, only the first stage is kicked by the processor. Upon first stage timeout, a corrective action is initiated and the next stage in the cascade is started. As each subsequent stage times out, it triggers a corrective action and starts the next stage.
Upon final stage timeout, a corrective action is initiated, but no other stage is started because the end of the cascade has been reached. Typically, single-stage watchdog timers are used to simply restart the computer, whereas multistage watchdog timers will sequentially trigger a series of corrective actions, with the final stage triggering a computer restart.
Watchdog timers may have either fixed or programmable time intervals. Some watchdog timers allow the time interval to be programmed by selecting from among a few selectable, discrete values. In others, the interval can be programmed to arbitrary values. Typically, watchdog time intervals range from ten milliseconds to a minute or more.
In a multistage watchdog, each timer may have its own, unique time interval. A watchdog timer may initiate any of several types of corrective action, including maskable interruptnon-maskable interruptprocessor reset, fail-safe state activation, power cycling, or combinations of these. Depending on its architecture, the type of corrective action or actions that a watchdog can trigger may be fixed or programmable.
Some computers e.Suppose you have written a program that is continuously running on a PIC. Now, you want to make sure that this program is always running, and that no matter what happens it will never stop. The first thing you would have, of course, is a loop back at the end of the program that brings us back to the start of the program. But consider this case. Let us say that the PIC is monitoring an input. When this input goes high, it jumps to another part of the program and waits for another pin to go high.
It will only exit if the second pin goes high. Let us consider another example. Suppose you have written a program. You have compiled it successfully, and you have even simulated it over and over again using a simulator such as MPLAB. Everything seems to work fine. You program the PIC and place it into a circuit. However after a long period of time, the program gets stuck somewhere and the PIC gets caught in a loop.
This is the purpose of a watchdog circuit. A watchdog circuit is nothing new. Many microprocessors and microcontrollers have them. But how does it work?
How to use external interrupt of PIC18F452 microcontroller
This provides a unique clock, which is independent of any external clock that you provide in your circuit. Now you can see that if our program does get stuck for some reason, then the WDT will not be set.
In order to use the WDT, we need to know three things. First, how long have we got before we need to reset the WDT, secondly how do we clear it.
This is dependant several factors, such as the supply voltage, temperature of the PIC etc. The time for an RC network to charge depends on the supply voltage. It also depends on the component values, which will change slightly depending on their temperature. So, for the sake of simplicity, just take it that the WDT will reset every 18mS. We can, however, make this longer.
Inside the PIC is a thing called a Prescaler. We can program this prescaler to divide the RC clock. Below is a table showing the bit assignments with the division rates and the time for the WDT to time out:. Think of these times as real time, rather than clock times.
The nearest we have is mS, or 0.