[Icc-avr] Help with callback functions on ICCAVR 7
Modeltech
rudy at modeltech.be
Sat Sep 29 11:24:31 PDT 2007
This works for me on ATMega8, 16, 168, .
// Event type
typedef unsigned int event_Event_t;
// Event handler type
typedef event_Event_t (*event_EventHandler_t)(event_Event_t);
.
// Private variables
static event_EventHandler_t eventHandler; // Placeholder for system
tick eventhandler
.
// Scheduler that will be called cyclically
static event_Event_t Scheduler(event_Event_t event)
{
.
return EVENT_NONE_E;
}
/*!
****************************************************************************
**
* \brief Timer 2 interrupt service routine
*
* \par Description:
* Interrupt handler for timer 2 compare. The interrupt generates a time
tick
* to execute any real-time scheduled tasks. These tasks must be defined in
* the event handler registered for the system tick.
****************************************************************************
**
*/
#pragma interrupt_handler TimerCompareIsr:iv_TIMER2_COMP
static void TimerCompareIsr(void)
{
if (0 != eventHandler)
eventHandler(SYSTICK_TIMETICK_E);
}
// Public variables
// Public functions
/*!
****************************************************************************
**
* \brief Initialize system tick
*
* \par Description:
* Initializes timer and interrupt settings to generate a periodic interrupt
* to implement a system tick.
*
* \param divisor Terminal count value for divisor timer (compare mode)
* \param eventHandler Pointer to callback function, called when timer
* reaches compare value
* \retval 0 OK
* \retval -1 ERROR (No eventhandler specified)
****************************************************************************
**
*/
int systick_Init(unsigned char divisor,
event_EventHandler_t handler )
{
// Install eventhandler
if (0 == handler)
return -1;
eventHandler = handler;
TCCR2 = 0x00; // Stop timer2
OCR2 = --divisor; // Set timer2 resolution
(TOP)
TCCR2 = 0b00001011; // Timer2 clock = clk/32,
// CTC mode (auto reload),
start timer
TIMSK |= BIT(OCIE2); // Enable timer2 interrupt
(output compare)
return 0;
}
Somewhere in your application's initialization:
// Initialize scheduler
resultCode |= systick_Init(RAILIO_SCHEDULER_TIMER_DIVISOR, Scheduler);
Hope this helps.
Rudy
_____
From: icc-avr-bounces at imagecraft.com [mailto:icc-avr-bounces at imagecraft.com]
On Behalf Of Alain van Hanegem
Sent: zaterdag 29 september 2007 12:32
To: icc-avr at imagecraft.com
Subject: [Icc-avr] Help with callback functions on ICCAVR 7
Hi all,
What is the correct way of using callback functions for ICCAVR V7 ?
This for example using a timer array where I want different timer callback
methods.
Does anyone have a 8 line code example on how to define them and call them?
Project is for an ATMEGA2560
Thanks,
Alain
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://dragonsgate.net/pipermail/icc-avr/attachments/20070929/754a19be/attachment-0001.html
More information about the Icc-avr
mailing list