Commit 37b9536f by karudu

Mõõtepiirkonna muutmine, pinge hoiatus

parent beb6ac6c
......@@ -5,18 +5,34 @@
int main(int argc, char **argv)
{
FILE *File = fopen("adc_vtable.ino", "w");
printf("Enter max voltage\n");
double Max;
scanf("%lf", &Max);
double Max1, Max10;
printf("Enter 1x max voltage\n");
scanf("%lf", &Max1);
printf("Enter 10x max voltage\n");
scanf("%lf", &Max10);
fprintf(File, "const char FLASH_VoltageTable[1024 * 3] PROGMEM =\n");
fprintf(File, "const char FLASH_VoltageTable1x[1024 * 3] PROGMEM =\n");
fprintf(File, "{\n");
for(int i = 0; i < 1024; i++)
{
char Buf[16];
sprintf(Buf, "%.02f", ((double)i / 1023.0) * Max);
sprintf(Buf, "%.02f", ((double)i / 1023.0) * Max1);
fprintf(File, "\t'%c', '%c', '%c',\n", Buf[0], Buf[2], Buf[3]);
}
fprintf(File, "};\n\n");
fprintf(File, "const char FLASH_VoltageTable10x[1024 * 4] PROGMEM =\n");
fprintf(File, "{\n");
for(int i = 0; i < 1024; i++)
{
char Buf[16];
sprintf(Buf, "%.02f", ((double)i / 1023.0) * Max10);
size_t Length = strlen(Buf);
if(Length == 4)
fprintf(File, "\t0, '%c', '%c', '%c',\n", Buf[0], Buf[2], Buf[3]);
else if(Length == 5)
fprintf(File, "\t'%c', '%c', '%c', '%c',\n", Buf[0], Buf[1], Buf[3], Buf[4]);
}
fprintf(File, "};\n");
return 0;
......
/* Configuration options */
// Cursor values in format xx.x
#define CURSOR_1 "5.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
//#define ONESHOT_MODE
// Period to measure in
#define ONESHOT_PERIOD PERIOD_1MS
// How many points to measure before stopping (Serial Monitor display is 49 points wide)
#define ONESHOT_POINTS 49
/* Defines */
#define PIN_BUTTON_1 5 // BUTTON_RANGE_DN
#define PIN_BUTTON_2 4 // BUTTON_RANGE_UP
#define PIN_BUTTON_3 3 // BUTTON_FREQ
#define PIN_BUTTON_4 2 // BUTTON_SHUTDOWN
#define PIN_LED 6
#define PIN_RANGE_ADJUST 7
#define PIN_BEEPER 8
#define PIN_ADC 7 // A7
#define BUTTON_RANGE_DN 0
......@@ -16,16 +42,9 @@
#define PERIOD_100MS 4
#define PERIOD_1S 5
/* Configuration options */
// Cursor values in format xx.x
#define CURSOR_1 "5.0"
#define CURSOR_2 "0.0"
// Uncomment to run in oneshot mode
//#define ONESHOT_MODE
// Period to measure in
#define ONESHOT_PERIOD PERIOD_1MS
// How many points to measure before stopping (Serial Monitor display is 49 points wide)
#define ONESHOT_POINTS 49
#define RANGE_5V 0
#define RANGE_10V 1
#define RANGE_30V 2
// https://www.gammon.com.au/adc
......@@ -36,6 +55,18 @@ char G_MeasureFreq;
char G_TimeCounter;
#define TIME_L_VALUE '0'
#define TIME_H_VALUE "0.5"
char G_Warning;
#define BLINK_PERIOD_US 100000 // 0.1s
#define BEEP_PERIOD_US 562 // 1780 Hz
unsigned long G_BlinkTime;
unsigned long G_BeepTime;
char G_BlinkState;
char G_BeepState;
short G_MaxVoltage;
#define RANGE_4_8V 0
#define RANGE_48V 1
char G_Range;
unsigned long G_AutoAdjTime;
#ifdef ONESHOT_MODE
int G_OneshotPoints;
#endif
......@@ -46,7 +77,8 @@ int G_PrevButtonState[4];
#define DEBOUNCE_DELAY_MS 25
/* Flash data */
extern const char FLASH_VoltageTable[1024 * 3] PROGMEM;
extern const char FLASH_VoltageTable1x[1024 * 3] PROGMEM;
extern const char FLASH_VoltageTable10x[1024 * 4] PROGMEM;
byte ReadButton(int Button)
{
......@@ -73,23 +105,15 @@ void Stop()
TCCR1A = 0;
TCCR1B = 0; // Stop the timer
Serial.end();
Serial.begin(9600);
ADCSRA = bit(ADEN);
ADCSRA |= bit(ADPS2) | bit(ADPS1) | bit(ADPS0); // Prescaler 128
ADMUX = bit(REFS0) | PIN_ADC;
ADCSRB = bit(ADTS0);
digitalWrite(PIN_LED, HIGH);
for(;;) {}
}
// 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()
{
......@@ -145,7 +169,7 @@ void SetTimerPeriod(int Period)
{
Serial.print("SetTimerPeriod: Invalid period ");
Serial.println(Period);
Error();
Stop();
} break;
}
......@@ -163,13 +187,45 @@ ISR(ADC_vect)
//unsigned long tstart = micros();
//Serial.println((ADC / 1023.0) * 4.8);
volatile unsigned int Value = ADC; // Read the ADC
Serial.write(G_CursorBuf);
volatile unsigned int Value = ADC;
if(G_Range == RANGE_4_8V)
{
int P = Value * 3;
Serial.write(pgm_read_word_near(FLASH_VoltageTable + P + 0));
Serial.write(pgm_read_byte_near(FLASH_VoltageTable1x + P + 0));
Serial.write('.');
Serial.write(pgm_read_byte_near(FLASH_VoltageTable1x + P + 1));
Serial.write(pgm_read_byte_near(FLASH_VoltageTable1x + P + 2));
#ifndef NO_AUTO_ADJ
if(Value >= AUTO_ADJ_MAX_4_8V)
{
// Go to 48 V range once voltage goes above threshold
G_Range = RANGE_48V;
digitalWrite(PIN_RANGE_ADJUST, HIGH);
G_AutoAdjTime = millis();
}
#endif
}
else
{
int P = Value * 4;
char Byte1 = pgm_read_byte_near(FLASH_VoltageTable10x + P + 0);
if(Byte1) Serial.write(Byte1);
Serial.write(pgm_read_byte_near(FLASH_VoltageTable10x + P + 1));
Serial.write('.');
Serial.write(pgm_read_word_near(FLASH_VoltageTable + P + 1));
Serial.write(pgm_read_word_near(FLASH_VoltageTable + P + 2));
Serial.write(pgm_read_byte_near(FLASH_VoltageTable10x + P + 2));
Serial.write(pgm_read_byte_near(FLASH_VoltageTable10x + P + 3));
// Voltage warning is checked in 48 V range
if(Value >= G_MaxVoltage)
G_Warning = 1;
else
G_Warning = 0;
if(Value > AUTO_ADJ_MIN_48V) G_AutoAdjTime = millis();
}
if(G_MeasureFreq != PERIOD_100US)
{
Serial.write('\t');
......@@ -199,6 +255,13 @@ void setup()
pinMode(PIN_BUTTON_2, INPUT_PULLUP);
pinMode(PIN_BUTTON_3, INPUT_PULLUP);
pinMode(PIN_BUTTON_4, INPUT_PULLUP);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_RANGE_ADJUST, OUTPUT);
digitalWrite(PIN_RANGE_ADJUST, LOW);
G_Range = RANGE_4_8V;
digitalWrite(PIN_LED, LOW);
digitalWrite(PIN_BEEPER, LOW);
Serial.begin(2000000);
#ifndef ONESHOT_MODE
......@@ -217,23 +280,63 @@ void setup()
sprintf(G_CursorBuf, "%s\t%s\t", CURSOR_1, CURSOR_2); // Precalculate the cursor string
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;
void loop()
{
if(G_Warning)
{
unsigned long Time = micros();
if(Time - G_BlinkTime >= BLINK_PERIOD_US)
{
if(G_BlinkState == LOW) G_BlinkState = HIGH;
else G_BlinkState = LOW;
digitalWrite(PIN_LED, G_BlinkState);
G_BlinkTime = Time;
}
Time = micros();
if(Time - G_BeepTime >= BEEP_PERIOD_US)
{
if(G_BeepState == LOW) G_BeepState = HIGH;
else G_BeepState = LOW;
digitalWrite(PIN_BEEPER, G_BeepState);
G_BeepTime = Time;
}
}
else if(G_BlinkState == HIGH)
{
G_BlinkState = LOW;
digitalWrite(PIN_LED, LOW);
}
if(G_Range == RANGE_48V)
{
if(millis() - G_AutoAdjTime >= AUTO_ADJ_DELAY_MS)
{
G_Range = RANGE_4_8V;
digitalWrite(PIN_RANGE_ADJUST, LOW);
}
}
if(ReadButton(BUTTON_RANGE_UP) == 1)
{
if(lstate == LOW) lstate = HIGH;
else lstate = LOW;
digitalWrite(LED_BUILTIN, lstate);
if(G_MaxVoltage == LIMIT_5V) G_MaxVoltage = LIMIT_10V;
else if(G_MaxVoltage == LIMIT_10V) G_MaxVoltage = LIMIT_30V;
}
if(ReadButton(BUTTON_RANGE_DN) == 1)
{
if(lstate == LOW) lstate = HIGH;
else lstate = LOW;
digitalWrite(LED_BUILTIN, lstate);
if(G_MaxVoltage == LIMIT_30V) G_MaxVoltage = LIMIT_10V;
else if(G_MaxVoltage == LIMIT_10V) G_MaxVoltage = LIMIT_5V;
}
if(ReadButton(BUTTON_FREQ) == 1)
......
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