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.

Understanding timer interrupt

Status
Not open for further replies.

ipunished

Junior Member level 3
Joined
Mar 15, 2011
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
United Kingdom
Activity points
1,504
Im trying to write code so that when timer0 reaches zero it check port b if any button is pressed, it should do this every 5 ms.. but im having confusions in this

I was reading up on timers and found this code in a book:

Code:
#include<system.h>
void config(void);
void interrupt(void);
unsigned char seconds;

void main()
{
	config();
	while(1);
}

void config(void)
{
	trisa=0xff;
	trisb=0x00;
	portb=0;

	//now setting up timer0

	clear_bit(option_reg,T0CS);
	clear_bit(option_reg,PSA);
	set_bit(option_reg,PS0);
	clear_bit(option_reg,PS1);
	set_bit(option_reg,PS2);

	//enabling interrupts

	set_bit(intcon,T0IE);
	set_bit(intcon,GIE);
}

void interrupt(void)
{
	portb++;
	clear_bit(intcon,T0IF);
}


I believe this just raised more questions, and id appreciate it if someone could explain a few things to me.

first off why are the seconds defined when they are not even used?

also where in the code do we tell the microcontroller where to count from, i mean under what conditions will the T0IF flag get enabled?

also how does the compiler know to go to interrupt() when it gets interrupted as we did not tell it to do so in our main.

furthermore how can I modify this program to work for me.

Thank you
 

The timer counts up and triggers an interrupt when it overflows. therefore you need to consider that in your code. The interrupt jumps to the vector at org 0x0004, i do not know how this is setup in 'C' as I only use assembler. But i have found this if it is of any help FAQ for PIC micros and Hi-Tech C
 

you have to set the timer so that it overflows every 5ms, at that time you can read the port....
you can consult the datasheet to know how to set the timers overflow time....
 

hi

first of all please read the data sheet and any books about PIC so that you will understand the concept of interrupt and timer.secondly the codes you found somewhere is written by someone may contains some bugs/unwanted definitions. here a char seconds is not used.the code shown is not the answer for your application. you want to check the port ststus and here they jaust change the port outputs. still it's good to see how to do programming the interrupt. if you want to make a 5 ms interrupt with timer you must mention the clock frequency first. and what do you want to do with the port b status?
store the status in some buffer or toggle the led?


ml
 

What compiler version are you using and the controller model?

---------- Post added at 10:11 ---------- Previous post was at 09:57 ----------

It looks like you're using a BoostC compiler, here are some tutorials on interrupts using BoostC:



BoostC - C18 Tutorials

Hope they help.
 

what microcontroller you are using and let me know the frequency of the crystal
See picture you will easy to understan timer operation


fig4-6.gif
 
Last edited:

Why not set the interrupt to trigger on bit change on port b.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top