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

Top 7 domains to expertise after learning python

Top 7 domains to expertise after learning python Python is one of the most popular programming languages. It is an object-oriented, dynamic semantics and high-level programming language. It's high-level built-in data structure combined with dynamic binding and dynamic typing makes it attractive for rapid application development. Often programmers fall in love with python because of the increased productivity it provides. Python is one of the most readable languages in the world right now. This language is very popular among developers and is widely used by many programmers to create application and programs. The implementation of this programming language is simple and at the same time, the language has a very clean structure as compared to other languages.  So if you mastered your python concepts and skills, you can dominate these 7 domains. Machine learning / Artificial intelligence Desktop GUI Data analytics and data visualization  Web development Game development Mobile ap...

Different domains of Artificial intelligence(AI)

Artificial intelligence is a computer system that is able to perform tasks that ordinarily require human intelligence. Artificial intelligence systems are critical for companies that wish to extract value from data by automating and optimizing processes or producing actionable insights. There are certain domains of artificial intelligence on which we can create our expertise Machine learning Deep learning Robotics Expert systems Fuzzy logic Natural language processing Computer vision  1. Machine learning 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. Different types of machine learning models Supervised learning Unsupervised learning Reinforcement learning Use cases Product recommendation on a shopping website. spam fil...

Top 5 free machine learning courses with certificate

In the era of 21st-century artificial intelligence, machine learning and data science became the most demanding and highest paying skills. After the covid-19 pandemic situation, the working style of the corporate sectors and the business had completely changed, now most of the business deals are made on the basis of data analysis, or when it comes to making the businesses automation the people hire a  machine learning engineer.    Hence it becomes really important for those who work in the corporate sector or a student pursuing a degree to get a job should update himself with these skills.  So, I listed you Top 5 machine learning courses from one of the leading organisations with completion certificates at free of cost. 1. Machine learning in the cloud with AWS batch About This course describes how to run and accelerate your machine learning applications in the cloud using AWS batch. AWS Batch is a fully managed service that allows you to easily and efficiently run b...