📄 timer.c
字号:
/*
------------------------------------------------------------------*-
*** THIS IS A SCHEDULER FOR STANDARD 8051 / 8052 ***
*** Uses T2 for timing, 16-bit auto reload ***
*** 12 MHz oscillator -> 1 ms (precise) tick interval ***
COPYRIGHT
---------
This code is from the book:
PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont
[Pearson Education, 2001; ISBN: 0-201-33138-1].
This code is copyright (c) 2001 by Michael J. Pont.
--- Modefied by sylva zhu to apply for AVR Microcontroller .
--- Ver 1.0 Sept 25th , 2006 .
-*------------------------------------------------------------------
*/
#include <iom32.h>
#include <inavr.h>
#include <comp_a90.h>
//
#include "Timer.H"
#include "Main.H"
#include "Scheduler.H"
// ------ Public variable declarations -----------------------------
// The array of tasks (see Sch51.C)
extern sTask SCH_tasks_G[SCH_MAX_TASKS];
// Used to display the error code
// See Main.H for details of error codes
// See Port.H for details of the error port
extern unsigned char Error_code_G;
/*
------------------------------------------------------------------*-
SCH_Init()
Scheduler initialisation function. Prepares scheduler
data structures and sets up timer interrupts at required rate.
You must call this function before using the scheduler.
-*------------------------------------------------------------------
*/
void SCH_Init(void)
{
unsigned char i;
//
for (i = 0; i < SCH_MAX_TASKS; i++)
{
SCH_Delete_Task(i);
}
//
// Reset the global error variable
// - SCH_Delete_Task() will generate an error code,
// (because the task array is empty)
Error_code_G = 0;
// Now set up Timer 2
TCCR2 =(1<<CS20); // select precaler: 32.768 kHz / 1 = 7.8 mS between each overflow
// Set TCNT2=223 , 1mS per time tick
ASSR = (1<<AS2); // select asynchronous operation of Timer2
//TCNT2 = 0; // clear TCNT2
TCNT2 = 223 ; // Preset value
//
// wait for TCN2UB and TCR2UB to be cleared
// while((ASSR & 0x01) | (ASSR & 0x04));
while( (ASSR&(1<<TCN2UB)) || (ASSR&(1<<TCR2UB)) );
TIFR |= (1<<TOV2); // clear interrupt-flags
TIMSK |= (1<<TOIE2); // enable Timer2 overflow interrupt
}
/*
------------------------------------------------------------------*-
SCH_Start()
Starts the scheduler, by enabling interrupts.
NOTE: Usually called after all regular tasks are added,
to keep the tasks synchronised.
NOTE: ONLY THE SCHEDULER INTERRUPT SHOULD BE ENABLED!!!
-*------------------------------------------------------------------
*/
void SCH_Start(void)
{
//EA = 1;
__enable_interrupt();
}
/*
------------------------------------------------------------------*-
SCH_Update()
This is the scheduler ISR. It is called at a rate
determined by the timer settings in the 'init' function.
This version is triggered by Timer 2 interrupts:
timer is automatically reloaded.
-*------------------------------------------------------------------
*/
#pragma vector=TIMER2_OVF_vect
__interrupt void SCH_Update(void)
{
unsigned char Index;
// reload by manual .
TCNT2 = 223 ; // Preset value , for 1mS time tick .
//
// NOTE: calculations are in *TICKS* (not milliseconds)
for (Index = 0; Index < SCH_MAX_TASKS; Index++)
{
// Check if there is a task at this location
if (SCH_tasks_G[Index].pTask)
{
if (SCH_tasks_G[Index].Delay == 0)
{
// The task is due to run
SCH_tasks_G[Index].RunMe += 1; // Inc. the 'RunMe' flag
if (SCH_tasks_G[Index].Period)
{
// Schedule regular tasks to run again
SCH_tasks_G[Index].Delay = SCH_tasks_G[Index].Period;
}
}
else
{
// Not yet ready to run: just decrement the delay
SCH_tasks_G[Index].Delay -= 1;
}
}
}
}
/*
------------------------------------------------------------------*-
---- END OF FILE -------------------------------------------------
-*------------------------------------------------------------------
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -