Cleanup in some of the reference materials

This commit is contained in:
Josh Stewart 2013-09-08 20:02:25 +10:00
parent 8bfa4e29f3
commit a38df08ab9
2 changed files with 90 additions and 47 deletions

View File

@ -3,20 +3,33 @@
//#define clockspeed 16000000
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
void (*schedule2StartCallback)();
void (*schedule1EndCallback)(); //End Callback function for schedule1
void (*schedule2EndCallback)();
*/
void initialiseSchedulers();
void setSchedule1(void (*startCallback)(), unsigned long timeout, unsigned long duration, void(*endCallback)());
void setFuelSchedule1(void (*startCallback)(), unsigned long timeout, unsigned long duration, void(*endCallback)());
void setSchedule2(void (*startCallback)(), unsigned long timeout, unsigned long duration, void(*endCallback)());
enum ScheduleStatus {OFF, PENDING, RUNNING};
struct Schedule {
unsigned long duration;
ScheduleStatus Status;
void (*StartCallback)(); //Start Callback function for schedule1
void (*EndCallback)(); //Start Callback function for schedule1
};
Schedule fuelSchedule1;
Schedule fuelSchedule2;
Schedule ignitionSchedule1;
Schedule ignitionSchedule2;
void setup()
{
Serial.begin(9600);
@ -29,7 +42,7 @@ void loop()
unsigned long uSInFuture = 50000; //50mS in the future
unsigned long expectedTime = curTime + uSInFuture;
setSchedule1(callback, uSInFuture, uSInFuture, callback);
setFuelSchedule1(callback, uSInFuture, uSInFuture, callback);
Serial.print("Expected time: ");
Serial.println(expectedTime);
@ -49,68 +62,65 @@ void initialiseSchedulers()
{
// Much help in this from http://arduinomega.blogspot.com.au/2011/05/timer2-and-overflow-interrupt-lets-get.html
//Schedule 1, which is uses timer 3
TCCR3B = 0x00; //Disbale Timer2 while we set it up
//Fuel Schedules, which uses timer 3
TCCR3B = 0x00; //Disbale Timer3 while we set it up
TCNT3 = 0; //Reset Timer Count
TIFR3 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
//TIMSK3 = 0x01; //Timer2 INT Reg: Timer2 Overflow Interrupt Enable
TCCR3A = 0x00; //Timer2 Control Reg A: Wave Gen Mode normal
TCCR3B = (1 << CS12); //Timer2 Control Reg B: Timer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
TIFR3 = 0x00; //Timer3 INT Flag Reg: Clear Timer Overflow Flag
TCCR3A = 0x00; //Timer3 Control Reg A: Wave Gen Mode normal
TCCR3B = (1 << CS12); //Timer3 Control Reg B: Timer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
fuelSchedule1.Status = OFF;
fuelSchedule2.Status = OFF;
/*
//Schedule 2, which is uses timer 4
TCCR4B = 0x00; //Disbale Timer2 while we set it up
//Ignition Schedules, which uses timer 3
TCCR4B = 0x00; //Disbale Timer3 while we set it up
TCNT4 = 0; //Reset Timer Count
TIFR4 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
TIMSK4 = 0x01; //Timer2 INT Reg: Timer2 Overflow Interrupt Enable
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
*/
schedule1Status = 0;
schedule2Status = 0;
TIFR4 = 0x00; //Timer3 INT Flag Reg: Clear Timer Overflow Flag
TCCR4A = 0x00; //Timer3 Control Reg A: Wave Gen Mode normal
TCCR4B = (1 << CS12); //Timer3 Control Reg B: Timer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
ignitionSchedule1.Status = OFF;
ignitionSchedule2.Status = OFF;
}
/*
This turns schedule 1 on, gives it callback functions and resets the relevant timer based on the time in the future that this should be triggered
These 4 function turn a schedule on, provides the time to start and the duration and gives it callback functions.
All 4 functions operate the same, just on different schedules
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
startCallback: The function to be called once the timeout is reached
timeout: The number of uS in the future that the callback should be triggered
duration: The number of uS after startCallback is called before endCallback is called
endCallback: This function is called once the duration time has been reached
*/
void setSchedule1(void (*startCallback)(), unsigned long timeout, unsigned long duration, void(*endCallback)())
void setFuelSchedule1(void (*startCallback)(), unsigned long timeout, unsigned long duration, void(*endCallback)())
{
if(schedule1Status == 2) { return; } //Check that we're not already part way through a schedule
if(fuelSchedule1.Status == RUNNING) { return; } //Check that we're not already part way through a schedule
//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))
unsigned int absoluteTimeout = TCNT3 + (timeout / 16); //Each tick occurs every 16uS with the 256 prescaler, so divide the timeout by 16 to get ther required number of ticks. Add this to the current tick count to get the target time. This will automatically overflow as required
OCR3A = absoluteTimeout;
schedule1Duration = duration;
schedule1StartCallback = startCallback; //Name the start callback function
schedule1EndCallback = endCallback; //Name the start callback function
schedule1Status = 1; //Turn this schedule on and set it
fuelSchedule1.duration = duration;
fuelSchedule1.StartCallback = startCallback; //Name the start callback function
fuelSchedule1.EndCallback = endCallback; //Name the start callback function
fuelSchedule1.Status = PENDING; //Turn this schedule on
TIMSK3 |= (1 << OCIE3A); //Turn on the compare unit (ie turn on the interrupt)
}
//Timer3 (schedule 1) Overflow Interrupt Vector
//Timer3A (schedule 1) Overflow Compare Vector
//This needs to call the callback function if one has been provided and rest the timer
ISR(TIMER3_COMPA_vect)
{
if (schedule1Status == 1) //Check to see if this schedule is turn on
if (fuelSchedule1.Status == PENDING) //Check to see if this schedule is turn on
{
schedule1StartCallback(); //Replace with user provided callback
schedule1Status = 2; //Turn off the callback
unsigned int absoluteTimeout = TCNT3 + (schedule1Duration / 16);
fuelSchedule1.StartCallback();
fuelSchedule1.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
unsigned int absoluteTimeout = TCNT3 + (fuelSchedule1.duration / 16);
OCR3A = absoluteTimeout;
}
else if (schedule1Status == 2)
else if (fuelSchedule1.Status == RUNNING)
{
schedule1EndCallback();
schedule1Status = 0; //Turn off the callback
TIMSK3 &= ~(1 << OCIE3A); //Turn off this output compare unit
fuelSchedule1.EndCallback();
fuelSchedule1.Status = OFF; //Turn off the schedule
TIMSK3 &= ~(1 << OCIE3A); //Turn off this output compare unit (This simply writes 0 to the OCIE3A bit of TIMSK3)
}
TIFR3 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
TIFR3 = 0x00; //Timer3 INT Flag Reg: Clear Timer Overflow Flag. I'm not 100% sure this is necessary, but better to be safe
}

View File

@ -7,11 +7,15 @@
#define MPH_PIN 12
#define POT_PIN 0
#define PULSE_DURATION 20
#define MAX_DELAY 20000
#define MIN_DELAY 833
#define PULSE_DURATION 50
int mph_toggle = 1 ;
int val;
unsigned int pulse_gap;
int RPMdirection = 0;
// the setup routine runs once when you press reset:
void setup()
@ -19,6 +23,9 @@ void setup()
pinMode(PULSE_PIN, OUTPUT);
pinMode(CAM_PIN, OUTPUT);
pinMode(MPH_PIN, OUTPUT);
Serial.begin(9600);
pulse_gap = MIN_DELAY;
}
@ -72,7 +79,26 @@ void loop()
// read potentiometer wiper pin 0?
// analog A/D channel 0
val = analogRead(POT_PIN);
pulse_gap = map(val, 0, 1023, 500, 50000);
pulse_gap = map(val, 0, 1023, MIN_DELAY, MAX_DELAY);
Serial.println(pulse_gap);
/*
if (RPMdirection == 0)
{
if (pulse_gap < MAX_DELAY) { pulse_gap++; }
else
{
RPMdirection = 1;
}
}
else
{
if (pulse_gap > MIN_DELAY) { pulse_gap--; }
else
{
RPMdirection = 0;
}
}
*/
// for loop 36 counts , 150 uS to 1000 uS or 5000 to 800 rpm
for (int i = 0; i < 11; i++)
{
@ -80,10 +106,17 @@ void loop()
digitalWrite(PULSE_PIN, HIGH);
delayMicroseconds(PULSE_DURATION);
digitalWrite(PULSE_PIN, LOW);
delayMicroseconds( (pulse_gap - PULSE_DURATION) );
if (pulse_gap < 15000) //delayMicroseconds() only works with values up to 16383. Switch to delay() at 15000 to be safe
{
delayMicroseconds( (pulse_gap - PULSE_DURATION) );
}
else
{
delay ( (pulse_gap - PULSE_DURATION) / 1000 );
}
}
// simulate the missing tooth next
delayMicroseconds( (pulse_gap - PULSE_DURATION) );
delayMicroseconds( (pulse_gap) );
}
// end main loop version 7 , now perfect 800 rpm to 5000