Add check for in progress scheduler. Clearer variable naming
This commit is contained in:
parent
3107854d64
commit
251886d8aa
|
@ -28,8 +28,8 @@ See page 136 of the processors datasheet: http://www.atmel.com/Images/doc2549.pd
|
|||
|
||||
//#define clockspeed 16000000
|
||||
|
||||
int schedule1Active; //Value=0 means do nothing, value=1 means call the startCallback, value=2 means call the endCallback
|
||||
int schedule2Active;
|
||||
int schedule1Status; //Value=0 means do nothing, value=1 means call the startCallback, value=2 means call the endCallback
|
||||
int schedule2Status; //As above, for 2nd scheduler
|
||||
unsigned long schedule1Duration; //How long (uS) after calling the start callback to we call the end callback
|
||||
unsigned long schedule2Duration;
|
||||
void (*schedule1StartCallback)(); //Start Callback function for schedule1
|
||||
|
|
|
@ -21,8 +21,8 @@ void initialiseScheduler()
|
|||
TCCR4A = 0x00; //Timer2 Control Reg A: Wave Gen Mode normal
|
||||
TCCR4B = (1 << CS12); //Timer2 Control Reg B: Timer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
|
||||
|
||||
schedule1Active = 0;
|
||||
schedule2Active = 0;
|
||||
schedule1Status = 0;
|
||||
schedule2Status = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -31,45 +31,47 @@ Args:
|
|||
startCallback: The function to be called once the timeout1 is reached
|
||||
timeout1: The number of uS in the future that the callback should be triggered
|
||||
duration: The number of uS before endCallback is called
|
||||
endCallback
|
||||
endCallback: This function is called once the duration time has been reached
|
||||
*/
|
||||
void setSchedule1(void (*startCallback)(), unsigned long timeout, unsigned long duration, void(*endCallback)())
|
||||
{
|
||||
//We need to calculate the value to reset the timer to (preload) in order to achieve the desired overflow time
|
||||
//As the timer is ticking every 16uS (Time per Tick = (Prescale)*(1/Frequency))
|
||||
//TODO: Need to add check for timeout > 1048576 ????
|
||||
if(schedule1Status == 2) { return; } //Check that we're note already part way through a schedule
|
||||
TCNT3 = 65536 - (timeout / 16); //Each tick occurs every 16uS with a 256 prescaler so divide the timeout by 16 to get ther required number of ticks. Subtract this from the total number of tick (65536 for 16-bit timer)
|
||||
schedule1Duration = duration;
|
||||
schedule1StartCallback = startCallback; //Name the start callback function
|
||||
schedule1EndCallback = endCallback; //Name the start callback function
|
||||
schedule1Active = 1; //Turn this schedule on and set it
|
||||
schedule1Status = 1; //Turn this schedule on and set it
|
||||
}
|
||||
|
||||
//As above, but for schedule2
|
||||
void setSchedule2(void (*startCallback)(), unsigned long timeout, unsigned long duration, void(*endCallback)())
|
||||
{
|
||||
//TODO: Need to add check for timeout > 1048576 ????
|
||||
if(schedule2Status == 2) { return; } //Check that we're note already part way through a schedule
|
||||
TCNT4 = 65536 - (timeout / 16); //Each tick occurs every 16uS with a 256 prescaler so divide the timeout by 16 to get ther required number of ticks. Subtract this from the total number of tick (65536 for 16-bit timer)
|
||||
schedule2Duration = duration;
|
||||
schedule2StartCallback = startCallback; //Name the callback function
|
||||
schedule2EndCallback = endCallback; //Name the callback function
|
||||
schedule2Active = 1; //Turn this schedule on
|
||||
schedule2Status = 1; //Turn this schedule on
|
||||
}
|
||||
|
||||
//Timer3 (schedule 1) Overflow Interrupt Vector
|
||||
//This needs to call the callback function if one has been provided and rest the timer
|
||||
ISR(TIMER3_OVF_vect)
|
||||
{
|
||||
if (schedule1Active == 1) //Check to see if this schedule is turn on
|
||||
if (schedule1Status == 1) //Check to see if this schedule is turn on
|
||||
{
|
||||
schedule1StartCallback(); //Replace with user provided callback
|
||||
schedule1Active = 2; //Turn off the callback
|
||||
schedule1Status = 2; //Turn off the callback
|
||||
TCNT3 = 65536 - (schedule1Duration / 16);
|
||||
}
|
||||
else if (schedule1Active == 2)
|
||||
else if (schedule1Status == 2)
|
||||
{
|
||||
schedule1EndCallback();
|
||||
schedule1Active = 0; //Turn off the callback
|
||||
schedule1Status = 0; //Turn off the callback
|
||||
TCNT3 = 0; //Reset Timer to 0 out of 255
|
||||
}
|
||||
|
||||
|
@ -79,16 +81,16 @@ ISR(TIMER3_OVF_vect)
|
|||
//AS above for schedule2
|
||||
ISR(TIMER4_OVF_vect)
|
||||
{
|
||||
if (schedule2Active == 1) //A value of 1 means call the start callback
|
||||
if (schedule2Status == 1) //A value of 1 means call the start callback
|
||||
{
|
||||
schedule2StartCallback();
|
||||
schedule2Active = 2; //Set to call the end callback on the next run
|
||||
schedule2Status = 2; //Set to call the end callback on the next run
|
||||
TCNT4 = 65536 - (schedule2Duration / 16);
|
||||
}
|
||||
else if (schedule2Active == 2)
|
||||
else if (schedule2Status == 2)
|
||||
{
|
||||
schedule2EndCallback();
|
||||
schedule2Active = 0; //Turn off the callback
|
||||
schedule2Status = 0; //Turn off the callback
|
||||
TCNT4 = 0; //Reset Timer to 0 out of 255
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue