Using Timers in Mechatronic Systems: A PIC Peripheral - Prof. David S. Fisher, Study notes of Mechanical Engineering

An introduction to using timers in mechatronic systems with the pic microcontroller. Timers are a background function that can be set up using the timer.h library and run independently of the main program. How to configure timers, their frequency, and provides an example code using timer0. It also mentions other peripherals like pwm and adc that can be used in conjunction with timers.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-s3g
koofers-user-s3g 🇺🇸

5

(1)

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ME430 Mechatronic Systems:
Using the Timer Peripheral
Timers are a PIC peripheral function that can run in the background without
interfering with the rest of the program. Other peripherals we’ll use include the PWM
peripheral and ADC peripheral.
On the 18F4520 there are 4 timers that you can use, which run via hardware in the
background. These timers are numbered Timer0, Timer1, Timer2, Timer3. Timers 0, 1,
and 3 are 16 bit timers that store a int value that counts from 0 to 65535. You can setup the
timer using the timer.h library functions. In this example code, I setup the timer 0 (zero):
// Setup the timer with a 1:4 prescaler with 16 bits resolution
// Therefore the timer0 freq is (clock frequency) / 4 / 4
OpenTimer0( TIMER_INT_OFF & T0_16BIT &
T0_SOURCE_INT & T0_PS_1_4 );
There is only one line of code there and the rest are comments (note, for big
commands you can go to the next line without any problems). In much the same way that
the instruction cycle frequency runs slower than the clock frequency, the timer frequency
can run slower than the instruction cycle frequency. The instruction cycle is always 1/4th
of the clock frequency, but the timer can be setup to different values.
So for the example code earlier we used T0_PS_1_4. So that made Timer 0 run at 1/4th
the speed of the instruction cycle. The timer frequency slow down can be set to many
Table 1: Timer Prescaler Options
Library config statement Divides the instruction cycle
frequency by how much?
T0_PS_1_1 1:1 prescale
T0_PS_1_2 1:2 prescale
T0_PS_1_4 1:4 prescale
T0_PS_1_8 1:8 prescale
T0_PS_1_16 1:16 prescale
T0_PS_1_32 1:32 prescale
T0_PS_1_64 1:64 prescale
T0_PS_1_128 1:128 prescale
T0_PS_1_256 1:256 prescale
pf3
pf4
pf5

Partial preview of the text

Download Using Timers in Mechatronic Systems: A PIC Peripheral - Prof. David S. Fisher and more Study notes Mechanical Engineering in PDF only on Docsity!

ME430 Mechatronic Systems: Using the Timer Peripheral

Timers are a PIC peripheral function that can run in the background without interfering with the rest of the program. Other peripherals we’ll use include the PWM peripheral and ADC peripheral.

On the 18F4520 there are 4 timers that you can use, which run via hardware in the background. These timers are numbered Timer0, Timer1, Timer2, Timer3. Timers 0, 1, and 3 are 16 bit timers that store a int value that counts from 0 to 65535. You can setup the timer using the timer.h library functions. In this example code, I setup the timer 0 (zero):

// Setup the timer with a 1:4 prescaler with 16 bits resolution // Therefore the timer0 freq is (clock frequency) / 4 / 4

OpenTimer0( TIMER_INT_OFF & T0_16BIT & T0_SOURCE_INT & T0_PS_1_4 );

There is only one line of code there and the rest are comments (note, for big commands you can go to the next line without any problems). In much the same way that the instruction cycle frequency runs slower than the clock frequency, the timer frequency can run slower than the instruction cycle frequency. The instruction cycle is always 1/4th of the clock frequency, but the timer can be setup to different values.

So for the example code earlier we used T0_PS_1_4. So that made Timer 0 run at 1/4th the speed of the instruction cycle. The timer frequency slow down can be set to many

Table 1: Timer Prescaler Options

Library config statement Divides the instruction cyclefrequency by how much?

T0_PS_1_1 1:1 prescale T0_PS_1_2 1:2 prescale T0_PS_1_4 1:4 prescale T0_PS_1_8 1:8 prescale T0_PS_1_16 1:16 prescale T0_PS_1_32 1:32 prescale T0_PS_1_64 1:64 prescale T0_PS_1_128 1:128 prescale T0_PS_1_256 1:256 prescale

different values. This can be handy for keeping the instruction cycle frequency very fast, but still timing things that are slow, a subtle but key difference.

The library functions do an excellent job of making the timers very easy to use. Go look at the C18 library section 2.9.

If you look at the example code below you can see some of the commands that might get used with a timer. This is not a complete program, but it shows you some of the timer commands.

2.9.2 Example of Use #include <p18F4520.h> #include <timers.h>

void main( void ) { int result; ADCON1 = 0x0F;

//A configure timer OpenTimer0( TIMER_INT_OFF & T0_SOURCE_INT & T0_PS_1_32 );

while( 1 ) { while(PORTAbits.RA4 == 1); // wait for RA4 to get pushed result = ReadTimer0(); // read timer

// could send the result to the LCD

WriteTimer0( 0 ); // restart timer } CloseTimer0(); // close modules }

For an example with a complete program you can download Timers Demo.c off the course website.

That code is also shown on the next few pages:

_asm goto high_isr _endasm }

#pragma code low_vector=0x void interrupt_at_low_vector(void) { _asm goto low_isr _endasm }

/** Global Variables *********************************************/ char tempString[5]; char line1[] = "Timer 0 reading"; unsigned int result;

  • Function: void main(void) ******************************************************************/ #pragma code

void main (void) { ADCON1 = 0x0F; TRISB = 0xF0; PORTB = 0x00; LCDInit(); DisplayClr(); LCDLine1(); text_display(line1);

//A configure timer OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_128 );

// Clock Frequency = 4 MHz // Instruction cycle Frequency = 1 MHz // Timer 0 Frequency = 1 MHz / 128 = 7812.5 Hz // Timer 0 Period = 128 uSeconds // Period in microseconds always equals prescaler when clock is 4 MHz // Overflow every 128 uSecond * 2^16 ticks = 8.4 seconds

// This code is only necessary for the interrupt only INTCON2bits.TMR0IP = 0;// Make Timer Interrupt High Priority

INTCONbits.TMR0IE = 1;// Enable the Timer zero interrupt INTCONbits.GIEH = 1;// Enable High priority interrupt

while( 1 ) { result = ReadTimer0(); // read timer sprintf(tempString," %u ticks",result); LCDLine2(); text_display(tempString); } CloseTimer0(); // close modules }

  • Function: void high_isr(void)
  • Input: Occurs on a timer overflow
  • Output: Modify the PORTB LEDs
  • Overview: The interrupt counts overflows using LEDs ******************************************************************/ #pragma interrupt high_isr// declare function as high priority isr void high_isr(void) { INTCONbits.TMR0IF = 0; PORTB++; }

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

  • Function: void low_isr(void)
  • Input:
  • Output:
  • Overview: ********************************************************************/ #pragma interruptlow low_isr// declare function as low priority isr void low_isr(void) { }