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.

Interrupts Using Timers in HITECH C

Status
Not open for further replies.

aredhel_vlsi

Member level 4
Joined
Aug 21, 2009
Messages
72
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,013
Hello people ! I ve been studying the timers and interrupts for a while, but I am not sure if I have understood right. And I haven't made a conclusion from the datasheet. After flashing a led with just a delay, I want to test it again using an interrupt with Timer 0, for example. TMR0 serves as a simple counter and is first example that our code is ok. So I use 10 MHz oscillator, and PIC 18F4331, I wrote this code after advised several tutorials.( Thanks to Avinash Gupta) I've also made my calculations , please have a look !

Fcpu=10MHz/4=2.5 million instructions per second
time period :Tcpu=1/Fcpu=400 nsec
prescale period=256x400ns=102.4 usec , so each increment of the timer 0 from 0-255 is 102.4 usec
so the overflow period is 102.4us x256=26.2 msec
Thus for 1 sec we need 1/ (overflow period)=1/26.2msec= 38.16=38 overflows, so we set the final value of the counter.

Code:
#include <htc.h>
#include <pic18f4331.h>
#define	XTAL_FREQ   10MHZ

unsigned char counter=0;
void boot_up_pic(void); 
void interrupt my_isr(void);

void main (void){ 
boot_up_pic();   // configure the timer 0
                    
while(1);
}

void interrupt my_isr(void){
	//check if it TMR0 overflowed 
	if (TMR0IE && TMR0IF) {  
		counter++;
		if  (counter==38) {
			//toggle RC3  LED
			if (RC3==0)
			  RC3=1;
			else
			  RC3=0;
			counter=0; //reset  the counter 	
		}
	TMR0IF=0; // clear flag
	}
}

// initializations routine ;

void boot_up_pic(void) {

PORTC=0b00000000; // initial state of the RC3 pin off
TRISC=0b11110111;  // RC3 is an output
//configure TMR0 : 111 for 256 prescaling ,8 bit timer, enable TMR0
T0CON=0b11000111;  
// GIE =1 PEIE=1  TMR0IE=1
INTCON=0b11100000; // ensure TMR0IF =0
ei(); // enable interrupt
}

Well this works for me, but is there any other way to program it? I thought we are able to give value to TMR0L register. So we just configure it and the counter does the rest?
1) Is there any case to ignore the counter variable and do it differently?

2)And another question, 26.2 msecx38=995.6 msec, and 26.2msecx39=1021.8 msec. It's close to 1 sec, but how can we be more accurate? How do we chose the right prescaler? I've made many calculations and variety of options but I can't be more accurate that this on time.

Thank you very much in advance.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top