Search

Friday, December 26, 2014

Timer 0 For PIC 18


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.

  • 8 bit timers – These can count between between 0-255
  • 16 bit timers – These can count between 0-65536
  • 32 bit timers – These can count between 0-4294967296
A timer has a clock source, for example of the clock source of 10KHz is input to a timer then one increment will take 100uS (micro second). This clock source can be obtained from the CPU clock. The CPU clock of popular MCU ranges from 1 MHz to 20Mhz, this can be sometimes too fast. 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.

  1. 256
  2. 128
  3. 64
  4. 32
  5. 16
  6. 8
  7. 4
  8. 2
  9. 1 (Prescaler by-passed)

Overflow
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 255 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.


PIC18F4520 has four different timers. The simplest is the TIMER0 so we will learn how to use it first. In this example the TIMER0 is configured as follows.

  • 8 Bit Mode
  • Clock Source from Prescaler
  • Prescaler = FCPU/256 (Note: FCPU= Fosc/4)
  • Over flow INT enabled
Our FCPU=16MHz/4 (We are running from 16MHz XTAL)
=4MHz
Time Period = 0.25uS

Overflow Period =  0.25us * 256 = 64 uS (Each over flow takes 256 counts)
So we need 1/0.0131072 Overflows to count for 1 sec
= 15625 Overflows
For that we maintain a counter to keep track of overflows. When an over flow occurs the PIC jumps to ISR, where we increment the counter. And when counter becomes 15625 we toggle RB1 pin. This pin is connected to LED. Therefore we have a LED which is ON for 1 sec and Off for 1sec.


Program:

/***************************************/

//Author : Palak Patel
//Contact No:9173211683
//Title:Timer0
//Platform: PIC18f4520
//Frequency:16Mhz
//Timer Frequency:16/4=4Mhz
//Timer Period:1/4Mhz=0.25us
//Timer calculation0.25us*100=0.000025=25us=0.25ms
//256-100=156
//Hex value of 156 is 9C
//for 8Bit set T0CON=0x48   //use only TMR0l
//For 16Bit set T0CON=0x08//TMR0LandTMR0H

/***************************************/

#include<p18f4520.h>
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF

#define led PORTC

void Delay();
void main()

{
         TRISC=0x00;
        T0CON=0x08;                 //T0CON: TIMER0 CONTROL REGISTER
while(1)
{
led=0x00;
Delay();
led=0xFF;
Delay();
}
}

//delay for 25us
void Delay()
{
TMR0L=0x9C;
TMR0H=0xFF;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
INTCONbits.TMR0IF=0;
T0CONbits.TMR0ON=0;
}

Proteus Simulation:



^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^&^

No comments:

Post a Comment