Timer/Counter Library

Timer/Counter: (Page # 61)

The timer library can be used to configure the timer module of the PIC18. The timer library consists of the following functions.

OpenTimerx(arguments)

This function is used to configure and enable any time, x stands for the timer number. This function has different arguments for different timers. Also one can configure a timer to act as a counter by using this function.

WriteTimerx(arguments)

This function is used to write values in to the specific timer [registers].

ReadTimerx()

This register is used to read the specific timer [registers].

CloseTimerx()

This function is used to close(disable) the specific timer.

Here is the example code, it uses Timer0. The same format can be used for the other timers with appropriate arguments.

Timer Code

//This is a program that shows the use of Timers with

//Microchip's C18 Libraries.It simply blinks an LED(RB0)

//The Timer Section is documented on P#61 of C18 Libraries

//Use 181320Basic.DSN

#include<p18f1320.h>// Include files and definitions for the Processor

#include<timers.h>// Include function definitions for the Timer library

#pragma config LVP=OFF,OSC=INTIO2,WDT=OFF // Configuration settings for the 18f1320.

# define LED LATBbits.LATB0 // Define the Port pin LEd is connected to.

void main(void)

{

OSCCON=0x60; //Internal 4MHZ Oscillator

ADCON1=0X7F; //Make all ports Digital

TRISBbits.TRISB0=0; //Make the LED pin an output

OpenTimer0(TIMER_INT_OFF &// We are using timer 0 in 16bit mode, witht the

T0_16BIT &// clock source being internal

T0_SOURCE_INT);

while(1)

{ INTCONbits.TMR0IF=0; //Enable timer detection

LED=~LED; //Invert LED

WriteTimer0( 00); //Start timer and Reload

while(!INTCONbits.TMR0IF); //Stay here until timer overflows

}

CloseTimer0(); //Close timer

}

The code for the Timer Function can also be downloaded from:

http://www.box.net/shared/6dp6v0xcju

Here is the code for using the same timer being used as a counter:

Counter Code

//This is a program that displays the use of Counters with

//Microchips Libraries.It simply counts Low to High transitions

//from a switch and displays the count on the LED's connected to

//PORTB.

//The Timer Section is documented on P#57 of C18 Libraries

//Use 181320Basic.DSN

#include<p18f1320.h>// Include files and definitions for the Processor

#include<timers.h>// Include function definitions for the Timer library

#pragma config LVP=OFF,OSC=INTIO2,WDT=OFF// Configuration settings for the 18f1320.

void main(void)

{ TRISAbits.TRISA4=1;//Make RA4 an i/p for the pulses

OSCCON=0x60;//Internal 4MHZ Oscillator

ADCON1=0X7F;//Make all ports Digital

TRISB=0;//Make PORTB an o/p so we can see the count

OpenTimer0(TIMER_INT_OFF &// Using Timer 0, turn interrupts

T0_8BIT &//off, 8bit counter, clock source

T0_SOURCE_EXT&// external, rising edge

T0_EDGE_RISE);

while(1)

{

WriteTimer0( 0);//Start timer and Reload.count for max count possible

while(!INTCONbits.TMR0IF)//Do this until counter reaches 256

LATB=TMR0L;//Give current count on PORTB

CloseTimer0();//Close timer

}

}

The Code for the Timer functions being used as a counter can also be downloaded from:

http://www.box.net/shared/kav0s2g49u