Commit 2617d115 by karudu

Programm valmis

parent 37b9536f
Showing with 57 additions and 70 deletions
...@@ -1026,7 +1026,7 @@ const char FLASH_VoltageTable1x[1024 * 3] PROGMEM = ...@@ -1026,7 +1026,7 @@ const char FLASH_VoltageTable1x[1024 * 3] PROGMEM =
'4', '8', '0', '4', '8', '0',
}; };
const char FLASH_VoltageTable10x[1024 * 4] PROGMEM = // TODO const char FLASH_VoltageTable10x[1024 * 4] PROGMEM =
{ {
0, '0', '0', '0', 0, '0', '0', '0',
0, '0', '0', '5', 0, '0', '0', '5',
......
...@@ -2,28 +2,35 @@ ...@@ -2,28 +2,35 @@
// Cursor values in format xx.x // Cursor values in format xx.x
#define CURSOR_1 "5.0" #define CURSOR_1 "5.0"
#define CURSOR_2 "0.0" #define CURSOR_2 "0.0"
// Voltage warning limits (indexes for FLASH_VoltageTable10x)
#define LIMIT_5V 107
#define LIMIT_10V 214
#define LIMIT_30V 640
// Range auto adjust thresholds (indexes for FLASH_VoltageTable1x/10x)
#define AUTO_ADJ_MAX_4_8V 958 // 4.5 V
#define AUTO_ADJ_MIN_48V 96 // 4.5 V
// Delay before changing from 48 V to 4.8 V range if voltage is below threshold
#define AUTO_ADJ_DELAY_MS 2500
// Uncomment to disable 48 V range
//#define NO_AUTO_ADJ
// Uncomment to run in oneshot mode // Uncomment to run in oneshot mode
//#define ONESHOT_MODE //#define ONESHOT_MODE
// Period to measure in // Period to measure in
#define ONESHOT_PERIOD PERIOD_1MS #define ONESHOT_PERIOD PERIOD_1MS
// How many points to measure before stopping (Serial Monitor display is 49 points wide) // How many points to measure before stopping (Serial Monitor display is 49 points wide)
#define ONESHOT_POINTS 49 #define ONESHOT_POINTS 49
// Uncomment to disable 48 V range
//#define NO_AUTO_ADJ
// Delay before changing from 48 V to 4.8 V range if voltage is below threshold
#define AUTO_ADJ_DELAY_MS 2500
// Range auto adjust thresholds (indexes for FLASH_VoltageTable1x/10x)
#define AUTO_ADJ_MAX_4_8V 958 // 4.5 V
#define AUTO_ADJ_MIN_48V 96 // 4.5 V
// Voltage warning limits (indexes for FLASH_VoltageTable10x)
#define LIMIT_5V 107
#define LIMIT_10V 214
#define LIMIT_30V 640
// Warning blink and beep frequencies
#define BLINK_PERIOD_US 100000 // 0.1s
#define BEEP_PERIOD_US 562 // 1780 Hz
// Time division low/high values, high value shown every 10 measurements
#define TIME_L_VALUE '0'
#define TIME_H_VALUE "0.5"
/* Defines */ /* Defines */
// Pin assignments
#define PIN_BUTTON_1 5 // BUTTON_RANGE_DN #define PIN_BUTTON_1 5 // BUTTON_RANGE_DN
#define PIN_BUTTON_2 4 // BUTTON_RANGE_UP #define PIN_BUTTON_2 4 // BUTTON_RANGE_UP
#define PIN_BUTTON_3 3 // BUTTON_FREQ #define PIN_BUTTON_3 3 // BUTTON_PERIOD
#define PIN_BUTTON_4 2 // BUTTON_SHUTDOWN #define PIN_BUTTON_4 2 // BUTTON_SHUTDOWN
#define PIN_LED 6 #define PIN_LED 6
#define PIN_RANGE_ADJUST 7 #define PIN_RANGE_ADJUST 7
...@@ -32,45 +39,32 @@ ...@@ -32,45 +39,32 @@
#define BUTTON_RANGE_DN 0 #define BUTTON_RANGE_DN 0
#define BUTTON_RANGE_UP 1 #define BUTTON_RANGE_UP 1
#define BUTTON_FREQ 2 // Change frequency #define BUTTON_PERIOD 2
#define BUTTON_SHUTDOWN 3 #define BUTTON_SHUTDOWN 3
#define PERIOD_10MS 0 #define PERIOD_10MS 0
#define PERIOD_1MS 1 #define PERIOD_1MS 1
#define PERIOD_100US 2 #define PERIOD_100US 2
#define PERIOD_10US 3
#define PERIOD_100MS 4
#define PERIOD_1S 5
#define RANGE_5V 0
#define RANGE_10V 1
#define RANGE_30V 2
// https://www.gammon.com.au/adc #define RANGE_4_8V 0
#define RANGE_48V 1
/* Globals */ /* Globals */
char G_PrintfBuf[32]; char G_PrintfBuf[32];
char G_CursorBuf[5+1+5+1+1]; char G_CursorBuf[5+1+5+1+1];
char G_MeasureFreq; char G_MeasureFreq;
char G_TimeCounter; char G_TimeCounter;
#define TIME_L_VALUE '0'
#define TIME_H_VALUE "0.5"
char G_Warning; char G_Warning;
#define BLINK_PERIOD_US 100000 // 0.1s
#define BEEP_PERIOD_US 562 // 1780 Hz
unsigned long G_BlinkTime; unsigned long G_BlinkTime;
unsigned long G_BeepTime; unsigned long G_BeepTime;
char G_BlinkState; char G_BlinkState;
char G_BeepState; char G_BeepState;
short G_MaxVoltage; short G_MaxVoltage;
#define RANGE_4_8V 0
#define RANGE_48V 1
char G_Range; char G_Range;
unsigned long G_AutoAdjTime; unsigned long G_AutoAdjTime;
#ifdef ONESHOT_MODE #ifdef ONESHOT_MODE
int G_OneshotPoints; int G_OneshotPoints;
#endif #endif
// 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];
...@@ -80,6 +74,7 @@ int G_PrevButtonState[4]; ...@@ -80,6 +74,7 @@ int G_PrevButtonState[4];
extern const char FLASH_VoltageTable1x[1024 * 3] PROGMEM; extern const char FLASH_VoltageTable1x[1024 * 3] PROGMEM;
extern const char FLASH_VoltageTable10x[1024 * 4] PROGMEM; extern const char FLASH_VoltageTable10x[1024 * 4] PROGMEM;
// Read the state of a button, returns 1 if pressed or 0 if not
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 };
...@@ -135,16 +130,6 @@ void SetTimerPeriod(int Period) ...@@ -135,16 +130,6 @@ void SetTimerPeriod(int Period)
// http://www.8bit-era.cz/arduino-timer-interrupts-calculator.html // http://www.8bit-era.cz/arduino-timer-interrupts-calculator.html
switch(Period) 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: case PERIOD_10MS:
{ {
TCCR1B = bit(CS11) | bit(WGM12); // CTC mode, prescaler 8 TCCR1B = bit(CS11) | bit(WGM12); // CTC mode, prescaler 8
...@@ -160,11 +145,6 @@ void SetTimerPeriod(int Period) ...@@ -160,11 +145,6 @@ void SetTimerPeriod(int Period)
TCCR1B = bit(CS10) | bit(WGM12); // CTC mode, prescaler 1 TCCR1B = bit(CS10) | bit(WGM12); // CTC mode, prescaler 1
OCR1A = 1599; // 10000 Hz ((16 MHz / 10000 Hz) - 1) OCR1A = 1599; // 10000 Hz ((16 MHz / 10000 Hz) - 1)
} break; } break;
case PERIOD_10US:
{
TCCR1B = bit(CS10) | bit(WGM12); // CTC mode, prescaler 1
OCR1A = 159; // 100000 Hz ((16 MHz / 100000 Hz) - 1)
} break;
default: default:
{ {
Serial.print("SetTimerPeriod: Invalid period "); Serial.print("SetTimerPeriod: Invalid period ");
...@@ -177,19 +157,14 @@ void SetTimerPeriod(int Period) ...@@ -177,19 +157,14 @@ void SetTimerPeriod(int Period)
sei(); sei();
} }
unsigned long AdcT = 0; // ADC complete interrupt
// ADC complete ISR
ISR(ADC_vect) ISR(ADC_vect)
{ {
/*Serial.println(micros() - AdcT);
AdcT = micros();*/
//unsigned long tstart = micros();
//Serial.println((ADC / 1023.0) * 4.8);
volatile unsigned int Value = ADC; // Read the ADC volatile unsigned int Value = ADC; // Read the ADC
Serial.write(G_CursorBuf); Serial.write(G_CursorBuf); // Send cursors
// Send measured voltage
if(G_Range == RANGE_4_8V) if(G_Range == RANGE_4_8V)
{ {
int P = Value * 3; int P = Value * 3;
...@@ -226,6 +201,7 @@ ISR(ADC_vect) ...@@ -226,6 +201,7 @@ ISR(ADC_vect)
if(Value > AUTO_ADJ_MIN_48V) G_AutoAdjTime = millis(); if(Value > AUTO_ADJ_MIN_48V) G_AutoAdjTime = millis();
} }
// Send time divisions (except for 100us period)
if(G_MeasureFreq != PERIOD_100US) if(G_MeasureFreq != PERIOD_100US)
{ {
Serial.write('\t'); Serial.write('\t');
...@@ -237,19 +213,23 @@ ISR(ADC_vect) ...@@ -237,19 +213,23 @@ ISR(ADC_vect)
} }
else Serial.write(TIME_L_VALUE); else Serial.write(TIME_L_VALUE);
} }
// Send newline
Serial.write('\r'); Serial.write('\r');
Serial.write('\n'); Serial.write('\n');
#ifdef ONESHOT_MODE #ifdef ONESHOT_MODE
G_OneshotPoints++; G_OneshotPoints++;
if(G_OneshotPoints == ONESHOT_POINTS + 1) Stop(); if(G_OneshotPoints == ONESHOT_POINTS + 1) Stop();
#endif #endif
//Serial.println(micros() - tstart);
} }
EMPTY_INTERRUPT (TIMER1_COMPB_vect); EMPTY_INTERRUPT (TIMER1_COMPB_vect); // Timer interrupt is not used
// Startup initialization
void setup() void setup()
{ {
Serial.begin(2000000);
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
pinMode(PIN_BUTTON_1, INPUT_PULLUP); pinMode(PIN_BUTTON_1, INPUT_PULLUP);
pinMode(PIN_BUTTON_2, INPUT_PULLUP); pinMode(PIN_BUTTON_2, INPUT_PULLUP);
...@@ -263,7 +243,14 @@ void setup() ...@@ -263,7 +243,14 @@ void setup()
digitalWrite(PIN_LED, LOW); digitalWrite(PIN_LED, LOW);
digitalWrite(PIN_BEEPER, LOW); digitalWrite(PIN_BEEPER, LOW);
Serial.begin(2000000); G_Warning = 0;
G_BlinkTime = 0;
G_BeepTime = 0;
G_BlinkState = LOW;
G_BeepState = LOW;
G_MaxVoltage = LIMIT_5V;
#ifndef ONESHOT_MODE #ifndef ONESHOT_MODE
SetTimerPeriod(PERIOD_10MS); SetTimerPeriod(PERIOD_10MS);
G_MeasureFreq = PERIOD_10MS; G_MeasureFreq = PERIOD_10MS;
...@@ -280,24 +267,18 @@ void setup() ...@@ -280,24 +267,18 @@ void setup()
sprintf(G_CursorBuf, "%s\t%s\t", CURSOR_1, CURSOR_2); // Precalculate the cursor string sprintf(G_CursorBuf, "%s\t%s\t", CURSOR_1, CURSOR_2); // Precalculate the cursor string
G_TimeCounter = 10; G_TimeCounter = 10;
G_Warning = 0;
G_BlinkTime = 0;
G_BeepTime = 0;
G_BlinkState = LOW;
G_BeepState = LOW;
G_MaxVoltage = LIMIT_5V;
} }
int lstate = LOW; // Main loop, handles idle tasks
void loop() void loop()
{ {
// Voltage warning
if(G_Warning) if(G_Warning)
{ {
unsigned long Time = micros(); unsigned long Time = micros();
if(Time - G_BlinkTime >= BLINK_PERIOD_US) if(Time - G_BlinkTime >= BLINK_PERIOD_US)
{ {
// LED blink
if(G_BlinkState == LOW) G_BlinkState = HIGH; if(G_BlinkState == LOW) G_BlinkState = HIGH;
else G_BlinkState = LOW; else G_BlinkState = LOW;
digitalWrite(PIN_LED, G_BlinkState); digitalWrite(PIN_LED, G_BlinkState);
...@@ -306,47 +287,53 @@ void loop() ...@@ -306,47 +287,53 @@ void loop()
Time = micros(); Time = micros();
if(Time - G_BeepTime >= BEEP_PERIOD_US) if(Time - G_BeepTime >= BEEP_PERIOD_US)
{ {
// Warning beep
if(G_BeepState == LOW) G_BeepState = HIGH; if(G_BeepState == LOW) G_BeepState = HIGH;
else G_BeepState = LOW; else G_BeepState = LOW;
digitalWrite(PIN_BEEPER, G_BeepState); digitalWrite(PIN_BEEPER, G_BeepState);
G_BeepTime = Time; G_BeepTime = Time;
} }
} }
else if(G_BlinkState == HIGH) else if(G_BlinkState == HIGH) // Clear LED once warning is off
{ {
G_BlinkState = LOW; G_BlinkState = LOW;
digitalWrite(PIN_LED, LOW); digitalWrite(PIN_LED, LOW);
} }
// Input range switching
if(G_Range == RANGE_48V) if(G_Range == RANGE_48V)
{ {
/* Go from 48 V back to 4.8 V once the voltage
has been below the threshold for long enough */
if(millis() - G_AutoAdjTime >= AUTO_ADJ_DELAY_MS) if(millis() - G_AutoAdjTime >= AUTO_ADJ_DELAY_MS)
{ {
G_Range = RANGE_4_8V; G_Range = RANGE_4_8V;
digitalWrite(PIN_RANGE_ADJUST, LOW); digitalWrite(PIN_RANGE_ADJUST, LOW);
G_Warning = 0;
} }
} }
if(ReadButton(BUTTON_RANGE_UP) == 1) // Buttons
if(ReadButton(BUTTON_RANGE_UP) == 1) // Warning range up
{ {
if(G_MaxVoltage == LIMIT_5V) G_MaxVoltage = LIMIT_10V; if(G_MaxVoltage == LIMIT_5V) G_MaxVoltage = LIMIT_10V;
else if(G_MaxVoltage == LIMIT_10V) G_MaxVoltage = LIMIT_30V; else if(G_MaxVoltage == LIMIT_10V) G_MaxVoltage = LIMIT_30V;
} }
if(ReadButton(BUTTON_RANGE_DN) == 1) if(ReadButton(BUTTON_RANGE_DN) == 1) // Warning range down
{ {
if(G_MaxVoltage == LIMIT_30V) G_MaxVoltage = LIMIT_10V; if(G_MaxVoltage == LIMIT_30V) G_MaxVoltage = LIMIT_10V;
else if(G_MaxVoltage == LIMIT_10V) G_MaxVoltage = LIMIT_5V; else if(G_MaxVoltage == LIMIT_10V) G_MaxVoltage = LIMIT_5V;
} }
if(ReadButton(BUTTON_FREQ) == 1) if(ReadButton(BUTTON_PERIOD) == 1) // Change measurement period
{ {
G_MeasureFreq++; G_MeasureFreq++;
if(G_MeasureFreq == PERIOD_100US + 1) G_MeasureFreq = PERIOD_10MS; if(G_MeasureFreq == PERIOD_100US + 1) G_MeasureFreq = PERIOD_10MS;
SetTimerPeriod(G_MeasureFreq); SetTimerPeriod(G_MeasureFreq);
} }
if(ReadButton(BUTTON_SHUTDOWN) == 1) if(ReadButton(BUTTON_SHUTDOWN) == 1) // Shutdown
{ {
Stop(); Stop();
} }
......
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