Commit 837bf1e0 by karudu

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

parent 6b68f03d
Showing with 65 additions and 34 deletions
This diff is collapsed. Click to expand it.
...@@ -4,21 +4,36 @@ ...@@ -4,21 +4,36 @@
#define PIN_BUTTON_4 2 #define PIN_BUTTON_4 2
#define PIN_ADC 7 // A7 #define PIN_ADC 7 // A7
#define BUTTON_1 0 #define BUTTON_RANGE_DN 0
#define BUTTON_2 1 #define BUTTON_RANGE_UP 1
#define BUTTON_3 2 #define BUTTON_FREQ 2 // Change frequency
#define BUTTON_4 3 #define BUTTON_RUNSTOP 3
// Cursor values #define PERIOD_10MS 0
#define CURSOR_1 "5.00" #define PERIOD_1MS 1
#define CURSOR_2 "0.00" #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 // 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]; unsigned long G_LastDebounceTime[4];
int G_ButtonState[4]; int G_ButtonState[4];
int G_PrevButtonState[4]; int G_PrevButtonState[4];
#define DEBOUNCE_DELAY_MS 25 #define DEBOUNCE_DELAY_MS 25
// Flash data
extern const char FLASH_VoltageTable[1024 * 3] PROGMEM;
byte ReadButton(int Button) byte ReadButton(int Button)
{ {
const int ButtonPins[4] = { PIN_BUTTON_1, PIN_BUTTON_2, PIN_BUTTON_3, PIN_BUTTON_4 }; const int ButtonPins[4] = { PIN_BUTTON_1, PIN_BUTTON_2, PIN_BUTTON_3, PIN_BUTTON_4 };
...@@ -56,12 +71,6 @@ void InitADC() ...@@ -56,12 +71,6 @@ void InitADC()
ADCSRA |= bit(ADATE); // Enable auto trigger 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 // Set timer 1 to a specified measuring interval
void SetTimerPeriod(int Period) void SetTimerPeriod(int Period)
{ {
...@@ -115,24 +124,23 @@ void SetTimerPeriod(int Period) ...@@ -115,24 +124,23 @@ void SetTimerPeriod(int Period)
sei(); sei();
} }
volatile int results[100];
volatile int resultNumber = 0;
char G_PrintfBuf[32];
// ADC complete ISR // ADC complete ISR
ISR(ADC_vect) ISR(ADC_vect)
{ {
//int tstart = micros(); //unsigned long tstart = micros();
// Range is 0-4.8 V //Serial.println((ADC / 1023.0) * 4.8);
// TODO the printing here needs to be faster Serial.write(G_CursorBuf);
sprintf(G_PrintfBuf, "%s\t%s\t", CURSOR_1, CURSOR_2); volatile unsigned int Value = ADC;
Serial.print(G_PrintfBuf); int P = Value * 3;
Serial.println((ADC / 1023.0) * 4.8); Serial.write(pgm_read_word_near(FLASH_VoltageTable + P + 0));
//Serial.println(5); Serial.write('.');
Serial.write(pgm_read_word_near(FLASH_VoltageTable + P + 1));
//sprintf(G_PrintfBuf, "%d\n", micros() - tstart); Serial.write(pgm_read_word_near(FLASH_VoltageTable + P + 2));
//Serial.println(G_PrintfBuf); Serial.write('\r');
Serial.write('\n');
//Serial.println(micros() - tstart);
} }
EMPTY_INTERRUPT (TIMER1_COMPB_vect); EMPTY_INTERRUPT (TIMER1_COMPB_vect);
...@@ -147,22 +155,45 @@ void setup() ...@@ -147,22 +155,45 @@ void setup()
Serial.begin(2000000); Serial.begin(2000000);
SetTimerPeriod(PERIOD_100US); SetTimerPeriod(PERIOD_10MS);
G_MeasureFreq = PERIOD_10MS;
InitADC(); InitADC();
for(int i = 0; i < 4; i++) G_LastDebounceTime[i] = 0; for(int i = 0; i < 4; i++) G_LastDebounceTime[i] = 0;
for(int i = 0; i < 4; i++) G_PrevButtonState[i] = HIGH; 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() void loop()
{ {
for(int i = 0; i < 4; i++) if(ReadButton(BUTTON_RANGE_UP) == 1)
{ {
byte State = ReadButton(i); if(lstate == LOW) lstate = HIGH;
if(State == 1) 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)
{ {
sprintf(G_PrintfBuf, "Button %d is %d\n", i, State); G_MeasureFreq++;
Serial.println(G_PrintfBuf); 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