Skip to main content

The future of AI in business and its potential to transform industries

PIC18 Timer programming in C

cover photo for PIC18 timer programming

The PIC18 timer is divided into 4 types
  1. Timer 0
  2. Timer 1
  3. Timer 2
  4. Timer 3
PIC18 timers can be used to generate a time delay or as a counter to count external event happening outside the microcontroller.

In this article, we will see how to generate a time delay by programming the PIC18 timer.

Timer 0

The timer 0 module has the following features
  • Software is scalable as an 8 bit or 16-bit timer/counter.
  • Readable and writable
  • Dedicated 8 bit software programmable Prescaler
  • Clock source selectable to be internal or external
  • Edge select for external clock

Register required for Timer 0

Control register

Each timer has a control register called TCON to set the various timer operation modes.
T0CON is an 8-bit register used for control of timer 0.

TOCON

Timer control register

TMR0ON (Timer0 on/off control bit)
              
                       1 = Enable timer 0
                       0 = Disable timer 0

T08BIT (Timer0 8-bit/16-bit control bit)
              
                       1 = Configured as an 8-bit timer/counter
                       0 = Configured as an 16-bit timer/counter

T0CS (Timer0 clock source select bit)
              
                       1 = Transition on T0CKL pin
                       0 = Internal instruction cycle clock (CLK0)

T0SE (Timer0 Source Edge select bit)
              
                       1 = High to Low transition on T0CKL pin
                       0 = Low to High transition on T0CKL pin

PSA (Timer0 Prescaler assignment bit)
              
                       1 = Timer 0 Prescaler is not assigned
                       0 = Timer 0 Prescaler is assigned

T0PS2 - T0PS0 (Timer0 prescaler select bit)
              
                       111 - 1:256 prescaler value
                       110 - 1:128 prescaler value
                       101 - 1:64 prescaler value
                       100 - 1:32 prescaler value
                       011 - 1:16 prescaler value
                       010 - 1:8 prescaler value
                       001 - 1:4 prescaler value
                       000 - 1:2 prescaler value

Load register

TMR0H (higher byte)
TMR0L (lower byte)

FLAG bit

TMR0IF (present in INTCON register)

Delay calculation for timer 0 without Prescaler value

The formula for the delay is 

Delay = time period of 1 clock X max value X count

In PIC:

1 machine cycle = 4 clock

XTAL freq of  PIC = 20MHz

So Frequency for I clock = 20/4 = 5MHz

Hence, time period for 1 clock = 1/5 MHz = 0.2us

Max. Value:

For 8 bit = 256

For 16 bit = 65536

Hence the modified formula for delay is

Delay = 0.2 X 10^-6 X 65536 or 256 X count 


Delay calculation for timer 0 (with Prescaler)

Suppose the Prescaler ratio is 1:4  (obtained from T0PS2 - T0PS0)

So, the frequency for 1 clock = (20/4)/4 = 1.25 MHz
  time period for 1clock = 1/1.25 MHz = 0.8us

Hence, the modified formula for the delay is

Delay = 0.8 X 10^-6 X 65536 or 256 X count 

While calculating the count value if the value of count is less than 1 then

Delay = 0.25 X 10^-6 X count

After getting the count value 

Initial count value = Max value (65536 or 256) - count

Then convert the initial count value into the HEX value and then load the MSB to TMR0H and LSB to TMR0L.

Example 1 - 

Generate a delay on RC0 of 1 second, 16-bit operation, XTAL frequency = 20MHz (without Prescaler)

Solve 
  
1 = 0.2 X 10^-6 X 65536 X count 

count = 76.26 
hence count = 77

Code 


#include <xc.h>

void delay_1sec(int count);

void main()
{
    TRISC0=0;
    
    while(1)
    {
    delay_1sec(77);
    RC0=~RC0;
    } 
}

void delay_1sec(int count)
{
    T0CON = 0x08;
    unsigned int i;
    
       for(i=0;i<count;i++)
       {
        TMR0H=0;
        TMR0L=0;
        TMR0ON=1;
        while(TMR0IF==0);
        TMR0ON=0;
        TMR0IF=0;
       }
}


Video



Popular posts from this blog

What is machine learning and it's types?

 What is machine learning(ML)? Machine learning is a subset of artificial intelligence. Machine learning enables computers or machines to make data-driven decisions rather than being explicitly programmed for a certain task. These programs or algorithms are designed in a way that they learn and improve over time when are exposed to new data. Examples:- 1. Product recommendations   While checking for a product did you noticed when it recommends a product similar to what you are looking for? or did you noticed "the person bought this product also bought this" combination of products? How are they doing this recommendation? This is machine learning. 2. Email spam and malware filtering  There are a number of spam filtering approaches that email clients use.  To ascertain that these spam filters are continuously updated they are powered by machine learning. 3. Online customer support A number of websites nowadays offer the option to chat with customer support representati...

Interface relay with PIC18 microcontroller

Hi... today we will talk about another important component used in the embedded domain called a relay. Relays are electric switch which uses electromagnetism to either form or breaks the existing circuits. With the help of a relay, you can trigger a high voltage operation by a low voltage input signal. Relay is a highly versatile component that is as effective in a complex circuit as in a simple circuit. In this article, we will talk about how to interface relay with PIC18 controller. We will cover the topics in the following points: About the interfacing task Software tools used Required components Circuit diagram Code for controller Upload the HEX file Run simulation About the interfacing task In the interface relay task, with the help of a push-button, we will trigger the relay to control the lighting of the bulb which is of higher voltage. When we press the push button the microcontroller will trigger the relay to change its state, when the relay changes its state from normally clo...