也许我不太了解静态变量。但我的代码不能正常工作。每次变量 Spedometer_Mode 在中断中发生变化时,所有变量都将重置为0.可以重置静态变量吗?这里的省略在哪里?
speedometer.h:
volatile static uint16_t Speedometer_WheelLength_mm; // Length of the wheel in mm
volatile static bool Speedometer_IsRunning; // When this variable is true, speedometer is processing, OCR0B Interrupt is true
volatile uint8_t Speedometer_Mode;
volatile static uint32_t Speedometer_Speed_mmps; // Value, in that stored current speed (mm per seconds)
volatile static uint8_t Speedometer_RevolutionCounter; // Counter of the quantity of revolutions (External interrupts)
volatile static uint8_t Speedometer_TimerCounter; //
volatile static uint32_t totalDistance;
volatile static uint32_t totalTime;
volatile static uint32_t averageVelocity;
volatile static uint32_t maxVelocity;
volatile static uint32_t currentDistance;
speedometer.c:
#include "Speedometer.h"
volatile static uint16_t Speedometer_WheelLength_mm = 2074; // Default value
volatile uint8_t Speedometer_Mode = SPEEDOMETER_SPEED_MEASUREMENT_MODE;
volatile static bool Speedometer_IsRunning = false;
volatile static uint32_t totalDistance = 0;
volatile static uint32_t totalTime = 0;
volatile static uint32_t averageVelocity = 0;
volatile static uint32_t maxVelocity = 0;
volatile static uint32_t currentDistance = 0;
ISR (INT0_vect)
{
if (!Speedometer_IsRunning)
{
Speedometer_IsRunning = true;
Speedometer_RevolutionCounter = 1;
Speedometer_TimerCounter = 1;
OCR1A = 0x03D0; // If F_CPU = 1MHz and prescaler = 1024, T = 1 second
OCR1B = 0x03D0;//?????????????????????
TCCR1B |= 1 << WGM12 | 1 << CS12 | 0 << CS11 | 1 << CS10; // Prescaler = F_CPU / 1024
TIMSK1 |= 1 << OCIE1A | 1 << OCIE1B; // Enable timer interrupts
}
else Speedometer_RevolutionCounter++;
}
ISR (TIMER1_COMPA_vect)
{
if (Speedometer_TimerCounter < SPEEDOMETER_GENERAL_SAMPLE_TIME)
{
Speedometer_Speed_mmps = (Speedometer_WheelLength_mm * Speedometer_RevolutionCounter) / Speedometer_TimerCounter;
Speedometer_TimerCounter++;
}
else
{
Speedometer_Speed_mmps = (Speedometer_WheelLength_mm * Speedometer_RevolutionCounter) / Speedometer_TimerCounter;
if (Speedometer_Speed_mmps == 0)
{
TCCR1B &= ~(1 << WGM12 | 1 << CS12 | 0 << CS11 | 1 << CS10);
TIMSK1 &= ~(1 << OCIE1A | OCIE1B);
Speedometer_IsRunning = false;
return;
}
Speedometer_RevolutionCounter = 0;
Speedometer_TimerCounter = 1;
}
}
ISR (TIMER1_COMPB_vect)
{
uint32_t currentVelocity = Speedometer_Speed_mmps / 1000;
totalDistance += currentVelocity;
currentDistance += currentVelocity;
totalTime++;
averageVelocity = totalDistance / totalTime;
if (currentVelocity > maxVelocity) maxVelocity = currentVelocity;
}
ISR (TIMER0_COMPA_vect) // Button's state polling
{
...
case GEEKSPORT_SPEEDOMETER_MODE:
{
if (Speedometer_Mode < (SPEEDOMETER_LAST_MODE - 1)) Speedometer_Mode++;
else Speedometer_Mode = SPEEDOMETER_SPEED_MEASUREMENT_MODE;
break;
}
...
}
如何纠正这种情况?
P.S。:对不起我的英语,我正在研究它。