add more macros for code explicitness

This commit is contained in:
Bruno Bousquet 2019-07-19 17:11:15 -04:00
parent bea951ed84
commit e0f7804034
1 changed files with 43 additions and 24 deletions

View File

@ -4,11 +4,17 @@
#include "auxiliaries.h"
// Prescaler values for timers 1-3-4-5. Refer to www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
const uint8_t TIMER_PRESCALER_1 = ((0<<CS12)|(0<<CS11)|(1<<CS10));
const uint8_t TIMER_PRESCALER_8 = ((0<<CS12)|(1<<CS11)|(0<<CS10));
const uint8_t TIMER_PRESCALER_64 = ((0<<CS12)|(1<<CS11)|(1<<CS10));
const uint8_t TIMER_PRESCALER_256 = ((1<<CS12)|(0<<CS11)|(0<<CS10));
const uint8_t TIMER_PRESCALER_1024 = ((1<<CS12)|(0<<CS11)|(1<<CS10));
#define TIMER_PRESCALER_OFF ((0<<CS12)|(0<<CS11)|(0<<CS10))
#define TIMER_PRESCALER_1 ((0<<CS12)|(0<<CS11)|(1<<CS10))
#define TIMER_PRESCALER_8 ((0<<CS12)|(1<<CS11)|(0<<CS10))
#define TIMER_PRESCALER_64 ((0<<CS12)|(1<<CS11)|(1<<CS10))
#define TIMER_PRESCALER_256 ((1<<CS12)|(0<<CS11)|(0<<CS10))
#define TIMER_PRESCALER_1024 ((1<<CS12)|(0<<CS11)|(1<<CS10))
#define TIMER_MODE_NORMAL ((0<<WGM01)|(0<<WGM00))
#define TIMER_MODE_PWM ((0<<WGM01)|(1<<WGM00))
#define TIMER_MODE_CTC ((1<<WGM01)|(0<<WGM00))
#define TIMER_MODE_FASTPWM ((1<<WGM01)|(1<<WGM00))
void initBoard()
{
@ -23,9 +29,9 @@ void initBoard()
* Auxilliaries
*/
//PWM used by the Boost and VVT outputs. C Channel is used by ign5
TCCR1B = 0x00; //Disbale Timer1 while we set it up
TCNT1 = 0; //Reset Timer Count
TCCR1A = 0x00; //Timer1 Control Reg A: Wave Gen Mode normal (Simply counts up from 0 to 65535 (16-bit int)
TCCR1B = TIMER_PRESCALER_OFF; //Disbale Timer1 while we set it up
TCNT1 = 0; //Reset Timer Count
TCCR1A = TIMER_MODE_NORMAL; //Timer1 Control Reg A: Wave Gen Mode normal (Simply counts up from 0 to 65535 (16-bit int)
TCCR1B = TIMER_PRESCALER_256; //Timer1 Control Reg B: Timer Prescaler set to 256. 1 tick = 16uS.
TIFR1 = (1 << OCF1A) | (1<<OCF1B) | (1<<OCF1C) | (1<<TOV1) | (1<<ICF1); //Clear the compare flags, overflow flag and external input flag bits
@ -38,10 +44,10 @@ void initBoard()
* Timers
*/
//Configure Timer2 for our low-freq interrupt code.
TCCR2B = 0x00; //Disbale Timer2 while we set it up
TCNT2 = 131; //Preload timer2 with 131 cycles, leaving 125 till overflow. As the timer runs at 125Khz, this causes overflow to occur at 1Khz = 1ms
TIMSK2 = 0x01; //Timer2 Set Overflow Interrupt enabled.
TCCR2A = 0x00; //Timer2 Control Reg A: Wave Gen Mode normal
TCCR2B = TIMER_PRESCALER_OFF; //Disbale Timer2 while we set it up
TCNT2 = 131; //Preload timer2 with 131 cycles, leaving 125 till overflow. As the timer runs at 125Khz, this causes overflow to occur at 1Khz = 1ms
TIMSK2 = (1<<TOIE2); //Timer2 Set Overflow Interrupt enabled.
TCCR2A = TIMER_MODE_NORMAL; //Timer2 Control Reg A: Wave Gen Mode normal
/* Now configure the prescaler to CPU clock divided by 128 = 125Khz */
TCCR2B = (1<<CS22) | (1<<CS20); // Set bits. This timer uses different prescaler values, thus we cannot use the defines above.
TIFR2 = (1 << OCF2A) | (1<<OCF2B) | (1<<TOV2); //Clear the compare flag bits and overflow flag bit
@ -56,17 +62,17 @@ void initBoard()
* */
//Much help in this from www.arduinomega.blogspot.com.au/2011/05/timer2-and-overflow-interrupt-lets-get.html
//Fuel Schedules, which uses timer 3
TCCR3B = 0x00; //Disable Timer3 while we set it up
TCNT3 = 0; //Reset Timer Count
TCCR3A = 0x00; //Timer3 Control Reg A: Wave Gen Mode normal
TCCR3B = TIMER_PRESCALER_64; //Timer3 Control Reg B: Timer Prescaler set to 64.
TCCR3B = TIMER_PRESCALER_OFF; //Disable Timer3 while we set it up
TCNT3 = 0; //Reset Timer Count
TCCR3A = TIMER_MODE_NORMAL; //Timer3 Control Reg A: Wave Gen Mode normal
TCCR3B = TIMER_PRESCALER_64; //Timer3 Control Reg B: Timer Prescaler set to 64.
TIFR3 = (1 << OCF3A) | (1<<OCF3B) | (1<<OCF3C) | (1<<TOV3) | (1<<ICF3); //Clear the compare flags, overflow flag and external input flag bits
//Ignition Schedules, which uses timer 5. This is also used by the fast version of micros(). If the speed of this timer is changed from 4uS ticks, that MUST be changed as well. See globals.h and timers.ino
TCCR5B = 0x00; //Disable Timer5 while we set it up
TCNT5 = 0; //Reset Timer Count
TCCR5A = 0x00; //Timer5 Control Reg A: Wave Gen Mode normal
TCCR5B = TIMER_PRESCALER_64; //Timer5 Control Reg B: Timer Prescaler set to 64.
TCCR5B = TIMER_PRESCALER_OFF; //Disable Timer5 while we set it up
TCNT5 = 0; //Reset Timer Count
TCCR5A = TIMER_MODE_NORMAL; //Timer5 Control Reg A: Wave Gen Mode normal
TCCR5B = TIMER_PRESCALER_64; //Timer5 Control Reg B: Timer Prescaler set to 64.
TIFR5 = (1 << OCF5A) | (1<<OCF5B) | (1<<OCF5C) | (1<<TOV5) | (1<<ICF5); //Clear the compare flags, overflow flag and external input flag bits
#if defined(TIMER5_MICROS)
@ -75,10 +81,10 @@ void initBoard()
#endif
//The remaining Schedules (Fuel schedule 4 and ignition schedules 4 and 5) use Timer4
TCCR4B = 0x00; //Disable Timer4 while we set it up
TCNT4 = 0; //Reset Timer Count
TCCR4A = 0x00; //Timer4 Control Reg A: Wave Gen Mode normal
TCCR4B = TIMER_PRESCALER_64; //Timer4 Control Reg B: Timer Prescaler set to 64.
TCCR4B = TIMER_PRESCALER_OFF; //Disable Timer4 while we set it up
TCNT4 = 0; //Reset Timer Count
TCCR4A = TIMER_MODE_NORMAL; //Timer4 Control Reg A: Wave Gen Mode normal
TCCR4B = TIMER_PRESCALER_64; //Timer4 Control Reg B: Timer Prescaler set to 64.
TIFR4 = (1 << OCF4A) | (1<<OCF4B) | (1<<OCF4C) | (1<<TOV4) | (1<<ICF4); //Clear the compare flags, overflow flag and external input flag bits
}
@ -115,4 +121,17 @@ static inline unsigned long micros_safe()
}
#endif //TIMER5_MICROS
// Those are not needed anymore
#undef TIMER_PRESCALER_OFF
#undef TIMER_PRESCALER_1
#undef TIMER_PRESCALER_8
#undef TIMER_PRESCALER_64
#undef TIMER_PRESCALER_256
#undef TIMER_PRESCALER_1024
#undef TIMER_MODE_NORMAL
#undef TIMER_MODE_PWM
#undef TIMER_MODE_CTC
#undef TIMER_MODE_FASTPWM
#endif //CORE_AVR