Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

interrupt time calculation

Status
Not open for further replies.

cipi-cips

Member level 4
Joined
Jun 30, 2008
Messages
76
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Activity points
1,867
Can someone explain me how to calculate interrupt time for timers in PIC

for example I have PIC 18F4550 and external oscillator of 20Mhz

from "googling" i found out this:

20Mhz oscillator kicks up 18f4550 to freq. of 48Mhz

now I divide 48Mhz with 4 (cause of 4 cycles per 1 instruction) I get 12mega instruction per cycle.

Now I divide this 12M/ins with prescaler (I will take 1:4), so it is 3000000

now I divide this 3000000 with 65535 (16 bit timer) and it is 45,777

when I use formula for frequency and apply it to 45,777

1/45,777 = 0,021 = 21ms

Is this correct or not, cause when I put .hex file in proteus and try to simulate it On Oscilloscope this interrupt doesnt ocur every 21ms.


Thx
 

Hi,

That formula sounds about right, however you do not say what time is actually given, other than its not the expected 21ms.

It might be late in the evening but not sure how 20mhz can be 'kicked up to 48mhz'

The way you set your Config bits is very important and if you are still having problems please show them.

Also have you enabled the interrupts correctly and clearing it each time its set / overflows ?

To make things simpler, why not test your routine using the simple internal osc at 4 or 8 mhz and use MPlab Sims Stopwatch to check the time.
 

these are my config
Code:
#pragma config PLLDIV = 5				// 20 MHz external clock / PLL prescaler value of 5 = 4 MHz required input to PLL circuit
#pragma config CPUDIV = OSC1_PLL2		// non-PLL postscale / 1 OR PLL postscale / 2 for CPU clock speed, depending on FOSC setting below
#pragma config USBDIV = 2				// USB clock source = 96 MHz PLL source / 2, (full-speed USB mode)

						// if desired, could change this line to "FOSC = HS" & "oscillator postscaler" gate would be used 
						// (not the "PLL postscaler" gate), CPU speed would be 20MHz, USB circuitry would still receive 48Mhz clock
#pragma config FOSC = HSPLL_HS			// use high-speed external osc crystal, & use PLL postscaler gate to feed CPU (CPU speed = 48 MHz)

timer ocnfiguration

Code:
T0CONbits.TMR0ON = 0;
	 T1CONbits.TMR1ON = 0;			// timer 1,2 off	
	
	RCONbits.IPEN = 1;		// enable priority level on interrupts
	INTCONbits.GIEH = 1;		// enable high-priority interrupts
								// timer 1 config
	PIE1bits.TMR1IE = 1;			// enable timer 1 overflow interrupt
	IPR1bits.TMR1IP = 1;			// timer 1 overflow interrupt priority set to high
	
	T1CONbits.T1CKPS1 = 1;			// timer 1 prescale 1:4
	T1CONbits.T1CKPS0 = 0;			//

	T1CONbits.T1OSCEN = 0;			// turn off separate oscillator that is internal to timer 1
	T1CONbits.TMR1CS = 0;			// use internal clock to increment timer 1
								// end timer 1 config
	// Timer0 config

	INTCONbits.TMR0IE = 1;			// enable timer 0 overflow interrupt
	INTCON2bits.TMR0IP = 1;			// set timer 0 overflow interrupt to high priority
	T0CONbits.T08BIT = 0;			// set timer 0 to 16 bits
	T0CONbits.T0CS = 0;				// use internal clock to increment timer 0
	T0CONbits.PSA = 0;				// use prescaler with timer 0
	
	T0CONbits.T0PS2 = 0;			// timer 0 prescaler 1:4
	T0CONbits.T0PS1 = 0;			//
	T0CONbits.T0PS0 = 1;
	
		// turn timers on
T1CONbits.TMR1ON = 1;		
T0CONbits.TMR0ON = 1;

Interrupt rutine

Code:
if(INTCONbits.TMR0IF == 1) {		// if timer 0 interrupt occurred . . .					
INTCONbits.TMR0IF == 0;
my code....
					
}
if(PIR1bits.TMR1IF == 1) {						// if timer 1 interrupt occurred . . .
	PIR1bits.TMR1IF = 0;
	my code....

here are two timers set to get interrupt at 20ms (21 with the formula at 1 post)
I have tried with only timer1, and then with timer0 and I didnt get 20ms at output
 

PIC18F, Clock 48 MHz, Timer0 20 ms


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Timer0
//Prescaler 1:16; TMR0 Preload = 5535; Actual Interrupt Time : 20.000041667 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
  T0CON  = 0x83;
  TMR0H  = 0x15;
  TMR0L  = 0x9F;
  GIE_bit    = 1;
  TMR0IE_bit     = 1;
}
 
void Interrupt(){
  if (TMR0IF_bit){ 
    TMR0IF_bit = 0;
    TMR0H    = 0x15;
    TMR0L    = 0x9F;
    //Enter your code here
  }
}



Timer 1, 10 ms


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Timer1
//Prescaler 1:8; TMR1 Preload = 5535; Actual Interrupt Time : 10 ms
 
//Place/Copy this part in declaration section
void InitTimer1(){
  T1CON  = 0x31;
  TMR1IF_bit     = 0;
  TMR1H  = 0x15;
  TMR1L  = 0x9F;
  TMR1IE_bit     = 1;
  INTCON     = 0xC0;
}
 
void Interrupt(){
  if (TMR1IF_bit){ 
    TMR1IF_bit = 0;
    TMR1H    = 0x15;
    TMR1L    = 0x9F;
    //Enter your code here
  }
}

 

This is your friend: **broken link removed**

It's the most comprehensive timer calculator I have ever seen and will produce code for you in several languages as well. I have no connection with the author but please support him.

Brian.
 

can you explain me how did you calculate that interrupt time ?

and one more question if anyone knows, in proteus when you pick PIC, at properties you can set Processor clock frequency. If I use external oscillator do I have to set this frequency or leave it to default ?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top