Commit 6b68f03d by karudu

Mõõtmisperioodi muutmine, kursorid

parent ac21eb86
Showing with 89 additions and 22 deletions
......@@ -2,13 +2,17 @@
#define PIN_BUTTON_2 4
#define PIN_BUTTON_3 3
#define PIN_BUTTON_4 2
#define PIN_ADC 0 // A0
#define PIN_ADC 7 // A7
#define BUTTON_1 0
#define BUTTON_2 1
#define BUTTON_3 2
#define BUTTON_4 3
// Cursor values
#define CURSOR_1 "5.00"
#define CURSOR_2 "0.00"
// https://www.gammon.com.au/adc
unsigned long G_LastDebounceTime[4];
......@@ -34,33 +38,108 @@ byte ReadButton(int Button)
return 0;
}
// Error occured, light the onboard LED and stop the program
void Error()
{
cli();
digitalWrite(LED_BUILTIN, HIGH);
for(;;) {}
}
// Set up the ADC registers
void InitADC()
{
ADCSRA = bit(ADEN) | bit(ADIE) | bit(ADIF); // Enable the ADC and the read complete interrupt
ADCSRA |= bit(ADPS2); // Prescaler 16
ADMUX = bit(REFS0) | PIN_ADC;
ADCSRB = bit(ADTS0) | bit(ADTS2); // Timer 1 CompareB
ADCSRA |= bit(ADATE); // Enable auto trigger
}
#define PERIOD_10MS 0
#define PERIOD_1MS 1
#define PERIOD_100US 2
#define PERIOD_10US 3
#define PERIOD_100MS 4
#define PERIOD_1S 5
// Set timer 1 to a specified measuring interval
void SetTimerPeriod()
void SetTimerPeriod(int Period)
{
cli();
TCCR1A = 0;
TCCR1B = 0; // Reset TCCR
TCNT0 = 0; // Reset the counter
TCCR1B = bit(CS11) | bit(WGM12); // CTC mode, prescaler 8
OCR1A = 19999; // 100 Hz ((16 MHz / 8 * 100 Hz) - 1)
// http://www.8bit-era.cz/arduino-timer-interrupts-calculator.html
switch(Period)
{
case PERIOD_1S:
{
TCCR1B = bit(CS12) | bit(WGM12); // CTC mode, prescaler 256
OCR1A = 62499; // 1 Hz ((16 MHz / 256 * 1 Hz) - 1)
} break;
case PERIOD_100MS:
{
TCCR1B = bit(CS10) | bit(CS11) | bit(WGM12); // CTC mode, prescaler 64
OCR1A = 24999; // 10 Hz ((16 MHz / 64 * 10 Hz) - 1)
} break;
case PERIOD_10MS:
{
TCCR1B = bit(CS11) | bit(WGM12); // CTC mode, prescaler 8
OCR1A = 19999; // 100 Hz ((16 MHz / 8 * 100 Hz) - 1)
} break;
case PERIOD_1MS:
{
TCCR1B = bit(CS10) | bit(WGM12); // CTC mode, prescaler 1
OCR1A = 15999; // 1000 Hz ((16 MHz / 1000 Hz) - 1)
} break;
case PERIOD_100US:
{
TCCR1B = bit(CS10) | bit(WGM12); // CTC mode, prescaler 1
OCR1A = 1599; // 10000 Hz ((16 MHz / 10000 Hz) - 1)
} break;
case PERIOD_10US:
{
TCCR1B = bit(CS10) | bit(WGM12); // CTC mode, prescaler 1
OCR1A = 159; // 100000 Hz ((16 MHz / 100000 Hz) - 1)
} break;
default:
{
Serial.print("SetTimerPeriod: Invalid period ");
Serial.println(Period);
Error();
} break;
}
TIMSK1 |= bit(OCIE1B); // Enable CompareB interrupt
sei();
}
volatile int results[100];
volatile int resultNumber = 0;
char G_PrintfBuf[32];
// ADC complete ISR
ISR(ADC_vect)
{
if(resultNumber <= 99) results[resultNumber++] = ADC;
//int tstart = micros();
// Range is 0-4.8 V
// TODO the printing here needs to be faster
sprintf(G_PrintfBuf, "%s\t%s\t", CURSOR_1, CURSOR_2);
Serial.print(G_PrintfBuf);
Serial.println((ADC / 1023.0) * 4.8);
//Serial.println(5);
//sprintf(G_PrintfBuf, "%d\n", micros() - tstart);
//Serial.println(G_PrintfBuf);
}
EMPTY_INTERRUPT (TIMER1_COMPB_vect);
char G_PrintFBuf[32];
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(PIN_BUTTON_1, INPUT_PULLUP);
pinMode(PIN_BUTTON_2, INPUT_PULLUP);
pinMode(PIN_BUTTON_3, INPUT_PULLUP);
......@@ -68,13 +147,8 @@ void setup()
Serial.begin(2000000);
SetTimerPeriod(); // Set timer to 100 Hz
ADCSRA = bit(ADEN) | bit(ADIE) | bit(ADIF); // Enable the ADC and the read complete interrupt
ADCSRA |= bit(ADPS2); // Prescaler 16
ADMUX = bit(REFS0) | PIN_ADC;
ADCSRB = bit(ADTS0) | bit(ADTS2); // Timer 1 CompareB
ADCSRA |= bit(ADATE); // Enable auto trigger
SetTimerPeriod(PERIOD_100US);
InitADC();
for(int i = 0; i < 4; i++) G_LastDebounceTime[i] = 0;
for(int i = 0; i < 4; i++) G_PrevButtonState[i] = HIGH;
......@@ -87,15 +161,8 @@ void loop()
byte State = ReadButton(i);
if(State == 1)
{
sprintf(G_PrintFBuf, "Button %d is %d\n", i, State);
Serial.println(G_PrintFBuf);
sprintf(G_PrintfBuf, "Button %d is %d\n", i, State);
Serial.println(G_PrintfBuf);
}
}
if(resultNumber == 100)
{
for(int i = 0; i < 100; i++) Serial.println(results[i]);
resultNumber = 0;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment