Commit 837bf1e0 by karudu

Kiirem saatmine ~100us jaoks, mõõtesageduse muutmine

parent 6b68f03d
Showing with 68 additions and 37 deletions
This diff is collapsed. Click to expand it.
......@@ -4,21 +4,36 @@
#define PIN_BUTTON_4 2
#define PIN_ADC 7 // A7
#define BUTTON_1 0
#define BUTTON_2 1
#define BUTTON_3 2
#define BUTTON_4 3
#define BUTTON_RANGE_DN 0
#define BUTTON_RANGE_UP 1
#define BUTTON_FREQ 2 // Change frequency
#define BUTTON_RUNSTOP 3
// Cursor values
#define CURSOR_1 "5.00"
#define CURSOR_2 "0.00"
#define PERIOD_10MS 0
#define PERIOD_1MS 1
#define PERIOD_100US 2
#define PERIOD_10US 3
#define PERIOD_100MS 4
#define PERIOD_1S 5
// Cursor values in format xx.x
#define CURSOR_1 "5.0"
#define CURSOR_2 "0.0"
// https://www.gammon.com.au/adc
// Globals
char G_PrintfBuf[32];
char G_CursorBuf[5+1+5+1+1];
char G_MeasureFreq;
// ReadButton globals
unsigned long G_LastDebounceTime[4];
int G_ButtonState[4];
int G_PrevButtonState[4];
#define DEBOUNCE_DELAY_MS 25
// Flash data
extern const char FLASH_VoltageTable[1024 * 3] PROGMEM;
byte ReadButton(int Button)
{
const int ButtonPins[4] = { PIN_BUTTON_1, PIN_BUTTON_2, PIN_BUTTON_3, PIN_BUTTON_4 };
......@@ -56,12 +71,6 @@ void InitADC()
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(int Period)
{
......@@ -69,7 +78,7 @@ void SetTimerPeriod(int Period)
TCCR1A = 0;
TCCR1B = 0; // Reset TCCR
TCNT0 = 0; // Reset the counter
// http://www.8bit-era.cz/arduino-timer-interrupts-calculator.html
switch(Period)
{
......@@ -115,24 +124,23 @@ void SetTimerPeriod(int Period)
sei();
}
volatile int results[100];
volatile int resultNumber = 0;
char G_PrintfBuf[32];
// ADC complete ISR
ISR(ADC_vect)
{
//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);
//unsigned long tstart = micros();
//Serial.println((ADC / 1023.0) * 4.8);
Serial.write(G_CursorBuf);
volatile unsigned int Value = ADC;
int P = Value * 3;
Serial.write(pgm_read_word_near(FLASH_VoltageTable + P + 0));
Serial.write('.');
Serial.write(pgm_read_word_near(FLASH_VoltageTable + P + 1));
Serial.write(pgm_read_word_near(FLASH_VoltageTable + P + 2));
Serial.write('\r');
Serial.write('\n');
//Serial.println(micros() - tstart);
}
EMPTY_INTERRUPT (TIMER1_COMPB_vect);
......@@ -147,22 +155,45 @@ void setup()
Serial.begin(2000000);
SetTimerPeriod(PERIOD_100US);
SetTimerPeriod(PERIOD_10MS);
G_MeasureFreq = PERIOD_10MS;
InitADC();
for(int i = 0; i < 4; i++) G_LastDebounceTime[i] = 0;
for(int i = 0; i < 4; i++) G_PrevButtonState[i] = HIGH;
sprintf(G_CursorBuf, "%s\t%s\t", CURSOR_1, CURSOR_2); // Precalculate the cursor string
}
int lstate = LOW;
void loop()
{
for(int i = 0; i < 4; i++)
if(ReadButton(BUTTON_RANGE_UP) == 1)
{
byte State = ReadButton(i);
if(State == 1)
{
sprintf(G_PrintfBuf, "Button %d is %d\n", i, State);
Serial.println(G_PrintfBuf);
}
if(lstate == LOW) lstate = HIGH;
else lstate = LOW;
digitalWrite(LED_BUILTIN, lstate);
}
if(ReadButton(BUTTON_RANGE_DN) == 1)
{
if(lstate == LOW) lstate = HIGH;
else lstate = LOW;
digitalWrite(LED_BUILTIN, lstate);
}
if(ReadButton(BUTTON_FREQ) == 1)
{
G_MeasureFreq++;
if(G_MeasureFreq == PERIOD_10US + 1) G_MeasureFreq = PERIOD_10MS;
//if(G_MeasureFreq == PERIOD_1S + 1) G_MeasureFreq = PERIOD_10MS;
SetTimerPeriod(G_MeasureFreq);
}
if(ReadButton(BUTTON_RUNSTOP) == 1)
{
if(lstate == LOW) lstate = HIGH;
else lstate = LOW;
digitalWrite(LED_BUILTIN, lstate);
}
}
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