Problems with Microcontroller Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you the Codes for Registered Members of this site only, at free service...Follow Me.

pic16f628a Timers and Interrupts

Background 
Many microcontroller applications like generating periodic signals, measuring time interval, keeping date and time, use time as their variable. Therefore, microcontrollers need to have some internal resources to accurately measure time. The PIC16F628A has 3 timer modules which are known as Timer0, Timer1, and Timer2. The basic unit of a timer is a free-run 8-bit or 16-bit incrementing synchronous counter which can be programmed to count internal or external pulses. The count number stored by each counter can be read or modified by accessing the special function register associated with that timer. Some of the bits in these registers are also the indicators of timer overflow, which, therefore, can generate interrupt request to the microcontroller. The use of timer modules to keep record of time elapsed allows the microcontroller to carry on with its other operations like controlling outputs, reading inputs, etc.

Timers can also have another asynchronour counter, known as prescaler, which can be configured to divide the  number of pulses received by the timer register to be divided by a factor of 2, 4, 8, 16, 32, 64, 128 or 256.

1. Timer0 Module

The Timer0 module is an  8-bit timer/counter which can be configured to count machine cycles or external pulses. When counting machine cycles, it is known to operate as a timer, and when counting the external pulses, it is called to operate as a counter. The external pulses are given to RA4/T0CKI pin. The Timer0 also consists of an 8-bit prescaler which is a programmable division factor.

The operation of the timer is set up by moving a suitable control code into the OPTION register. Timer mode is selected by clearing the T0CS bit (OPTION<5>). In Timer mode, the TMR0 register value will increment every instruction cycle (without prescaler). Counter mode is selected by setting the T0CS bit. In this mode, the TMR0 register value will increment either on every rising or falling edge of pin RA4/T0CKI pin. The incrementing edge is determined by the source edge (T0SE) control bit (OPTION<4>). Clearing the
T0SE bit selects the rising edge.

Timer0 interrupt is generated when the TMR0 register timer/counter overflows from FFh to 00h. This overflow sets the T0IF bit of INTCON register. The interrupt can be masked by clearing the T0IE bit (INTCON<5>). The T0IF bit (INTCON<2>) must be cleared in software by the Timer0 module interrupt service routine before reenabling this interrupt.

The prescalar value can be set using the PSA and PS<2:0> bits of OPTION register.

2. Timer1 and Timer2
Refer to datasheet.
So in this experiment, we are going to use Timer0 to generate delay for a 4-bit up counter. Connect RB0-RB3 to the 4 LEDs on the board using jumpers.

Software:
/*
  Project Name: Use of Timer 0 and Interrupt
  * Copyright:
     (c) Rajendra Bhatt, 2009.
 * Description:
     This code is an example of using Timer 0 and Interrupt for delay.

 * Test configuration:
     MCU:             PIC16F628A
     Oscillator:      XT, 4.0 MHz
  */

  unsigned cnt, num;
  void interrupt() {   // Interrupt routine
    num ++;            // Interrupt causes Num to be incremented by 1
    if(num == 20) {
    cnt ++;           // 20 Interrupts causes cnt to be incremented by 1
    num = 0;
    }
    TMR0 = 0;        // Timer (or counter) TMR0 returns its initial value
    INTCON = 0x20;   // Bit T0IE is set, bit T0IF is cleared
  }

  void main() {
  cnt=0;
  num=0;
  OPTION_REG = 0x07; // Prescaler (1:256) is assigned to the timer TMR0
  TMR0 = 0;          // Timer T0 counts from 0 to 255
  INTCON = 0xA0;     // Enable interrupt TMR0
  TRISB = 0x00;          // set direction to be output
  PORTB = 0;         // Turn OFF LEDs on PORTB
  do {
    PORTB =cnt ;
  } while(PORTB < 0x0f);  // Till PORTB < 15
 }

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Recent Comments

Popular Projects

Give Support

Give Support
Encourage Me through Comments

Microcontroller Projects

Total Pageviews