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 handling function for a timer

Status
Not open for further replies.

winnie mong

Junior Member level 3
Junior Member level 3
Joined
Sep 8, 2013
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
366
hi friends...
i am using pic16f84 in my project and i need help in producing the interrupt ISR function, so as to keep track of time and avoid overflows right at the initialization stage.
this is what i already have,


Code C - [expand]
1
2
3
4
5
6
7
8
unsigned char   pulses;
interrupt isr(void)
{
    if(T0IF) {
        ++pulses;                               // increment pulses
        T0IF=0;                                 // reset TMR0 flag
    }
}

 
Last edited by a moderator:

That is fine in most compilers. Some may use a different word than 'interrupt' as the function type but the principle is the same.

If you are using the variable 'pulses' elsewhere in the program outside the ISR you should really define it as volatile like this:
volatile unsigned char pulses;
This will warn the compiler that the variable is liable to change without warning (in the ISR) so it can ensure the main code doesn't trust it to stay the same before and after the interrupt.

You also have to set the prescaler and clock source elsewhere in your program and if you want the ISR to occur more frequently than every 256 counts of TMR0 you have to pre-load it with a new start value just before clearing T0IF.

Brian.
 

hi, i read on the pic16f84 manual and learnt that:
An overflow (FFh - 00h) in TMR0 will set flag bit T0IF (INTCON<2>). The interrupt can be enabled/disabled by setting/clearing enable bit T0IE (INTCON<5>).i am still not able to apply this information to my code in setting the prescaler and the clock source.HELP!
here is the code i am working on.

#include <htc.h>
#include "timer.h"
#ifdef XT
__CONFIG(XT&PWRTEN&WDTDIS); // config XT
#else
__CONFIG(0x3ff1); // config XT
#define OPTION OPTION_REG
#endif

bit GeyserFlag;

extern void cput(unsigned char c);
extern void scroll(const char *mess, unsigned char ntimes);
extern void display(const char *message);
extern void clrscr(void);
extern char choice(const char *const *menu);
extern char getch(void);
extern void delay(unsigned char time);
extern char choice(const char *const *fmenu);
extern char sbuf[15];
extern char line[13];
extern unsigned char pulses;

const char *const menu1[]={
"--SET TIME--",
"--VIEW TIME--",0};

main()
{
INTCON=0x20; // TMR0 allowed to interrupt
OPTION=0x07; // prescaler 1:128 to TMR0, enable pullup resistors on PORTA
TRISB=0x00; // RB7 is input RB6:RB0 are inputs
TRISA=0x07; // port A RA2:RA0 input
pulses=0;
GIE=1; // enable all interrputs
relayoff();
RW0_HIGH(); RW1_HIGH(); // hold WR high to prevent writing to display
TRISA=0x00;

char GeyserOffTime[4];
char GeyserOnTime[4];
char CurrentTime[4];

if(GeyserFlag)
{
if(strcmp(GeyserOffTime, CurrentTime) != 0);//check if it is time to turn OFF
{
GeyserFlag = 0;
relayoff();
}//end if

}//end if

else
{ //if Flag is off: ie Geyser is OFF
if(strcmp(GeyserOnTime, CurrentTime) != 0);//check if it is time to turn it ON
{
relayon();
GeyserFlag = 1;

}//end if
}//end else


unsigned char i,j;
int State,e;
enum State {state_0,state_1,state_2};
enum Event {mode,select,set};

void scrolling();
{
printf("make a choice by pressing select");
}
void do_nothing();
{
printf("Dont bother");
}
void present_choice_on_screen();
{
printf("present choice on screen");
}
void perform_the_choice();
{
printf("do menu[j] ");
}

State = state_0;
/*eventQueue[EVENT_QUEUE_SIZE] = {mode,select,set};
int eventIndex = 0;

for(eventIndex<EVENTS_COUNT;eventIndex++)


EVENT evt = eventQueue[eventIndex];*/

switch(State)
{
case state_0:
switch(e)
{
case select:
{
do_nothing();
break;
}
case set:
{
do_nothing();
break;
}
case mode:
{
State = state_1;
scrolling();
break;
}
}
break;

case state_1:
switch(e)
{
case select:
{
State = state_2;
present_choice_on_screen();
break;
}
case set:
{
do_nothing();
break;
}
case mode:
{
do_nothing();
break;
}
}
break;

case 2:
switch(e)
{
case select:
{
perform_the_choice();
break;
}
case set:
{
do_nothing();
break;
}
case mode:
{
do_nothing();
break;
}
}
break;
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top