View previous topic :: View next topic |
Author |
Message |
MiniMe
Joined: 17 Nov 2009 Posts: 50
|
Multiple interrupts |
Posted: Wed Dec 02, 2009 3:34 pm |
|
|
Can I use multiple same (same type) interrupts.
Briefly can I use more than 1 #INT_TIMER1 ?
-----
For example my area of use is LCD menu subsection ( very separated function) To check inputs frequently. Every time timer overflows.
-----
How ?
Any example you can point out ?
-----
If no ...
Will better programming fix a need to use multiple functions that act very a like. Any experience ? Points of view ?
Thank you in advance ! |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Wed Dec 02, 2009 3:47 pm |
|
|
You cannot have more than one instance of a particular timer. However, in my case I use a single timer that fires every 1ms or every 10ms as a master clock and service sub procedures at pre-selected time intervals.
For example:
Using a timer that has been set up for a 1ms timeout. Increment a set of counter variables in the ISR (one for each process you want to monitor). When a particular counter reaches the selected count for servicing a function (i.e. scan user input buttons every 8ms) set a flag for that function, reset the counter variable then exit the ISR and and service the flag in Main. This actually creates a crude form of RTOS. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
MiniMe
Joined: 17 Nov 2009 Posts: 50
|
|
Posted: Thu Dec 03, 2009 2:00 am |
|
|
Thank you !
Your reply helped me much !
There's a small example code.
Code: |
#int_RTCC
RTCC_isr()
{
//Before timer counter register was 0.
set_timer0(64); // RTCC and TIMER0 - same thing, different name.
//Now timer counter register is 64 - less time to overflow.
count++; //global variable
if(count==10) {
count = 11;
//rest of code 1
}
if(count==20) {
count = 0;
//rest of code 2
}
}
|
|
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Thu Dec 03, 2009 8:10 am |
|
|
You really don't want servicing routines inside the ISR. The rule is to keep
it as short as possible and get out of the ISR.
Below is a rough example I threw together. You will need to take into
account rollover of the 1ms counter variable but overall it should give you
an idea of what I am saying.
*******************************
Code: |
you need to define _1mscntr is an int16
you should to define all flags as boolean or int1
#INT_TIMER1
void Timer1_int() // 1ms timer
{
set_timer1(TIMER1START); // restart the timer for the next 1ms interval
// *****
// count the 1ms intervals
if (--_1mscntr < 1)
{
_1mscntr = 1000; // reset for one second
// increment the seconds counter
seconds++;
// Set flag to Toggle heartbeat LED here
HB=1;
} // if --1mscntr < 1
// ** end of timekeeping section
// *****
if (__1mscntr = 12) // we want to set a flag to run task1 here
task1flag=1;
if (_1mscntr = 35) // we want to set a flag to run task2 here
task2flag=1;
if (Delay_Ctr > 0)
Delay_Ctr--; // used to keep track of a delay somewhere
if (Response_Timer > 0)
Response_Timer--;
//
} // end of Timer1 ISR
// start of main routine
void main()
{
while (1)
{
// Heartbeat routine
if (HB)
{ // this heartbeat routine ensures the interrupt timer and
// main routine are working
( do something here to toggle a heartbeat LED)
HB=0; // reset the flag for the next round
}
if (response_timer==0)
response(); // print response message to the display etc.
if (TASK1FLAG)
{ // do something for task1
TASK1FLAG=0; // and finally reset the flag
}
}
}
|
_________________ Google and Forum Search are some of your best tools!!!! |
|
|
Sergeant82d
Joined: 01 Nov 2009 Posts: 55 Location: Central Oklahoma
|
|
Posted: Sun Dec 06, 2009 9:36 pm |
|
|
Thanks dyeatman, for a great explanation - I have read of that technique, but your little demo code really clarified the concept for me. |
|
|
mbradley
Joined: 11 Jul 2009 Posts: 118 Location: California, USA
|
|
Posted: Tue Dec 08, 2009 11:39 am |
|
|
I have thrown a little task manager together for just such an item. I needed to do several things at intervals, and the code started to look sloppy, so I wrapped it all up in a qued task manager.
Basicaly, each function executes at specific intervals
I just have to define some parameters for each task.
Code: |
// set task parameter ( task id, how many tics)
mqTaskSetTmr(TASKID_LEDBLINK, 1000000);
mqTaskEnable(TASKID_LEDBLINK);
mqTaskSetPri(TASKID_LEDBLINK, QTPRI_HIGHEST); // set highest priority
|
Code: |
while(true)
{
mqTaskManager()
{
// tell it what function to execute per task id
mqTaskFunction(TASKID_LEDBLINK, task_blink_led());
mqTaskFunction(TASKID_COUNT, task_count());
}
}
|
The source is here, written in CCS C: http://www.mculabs.com/drivers/qtask.html _________________ Michael Bradley
www.mculabs.com
Open Drivers and Projects |
|
|
|