k4be 4 лет назад
Родитель
Сommit
c9f11d5a42
4 измененных файлов с 175 добавлено и 33 удалено
  1. 1 1
      soft/Makefile
  2. 11 9
      soft/display.c
  3. 162 22
      soft/main.c
  4. 1 1
      soft/main.h

+ 1 - 1
soft/Makefile

@@ -10,7 +10,7 @@ AVRDUDE        = avrdude -c usbasp -p $(MCU_TARGET)
 # You should not have to change anything below here.
 CC             = avr-gcc
 # Override is only needed by avr-lib build system.
-override CFLAGS        = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) -I/usr/avr/include -ffreestanding -fshort-enums -funsigned-bitfields
+override CFLAGS        = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) -I/usr/avr/include -ffreestanding -fshort-enums -funsigned-bitfields -funsigned-char
 #-save-temps
 override LDFLAGS       = -Wl,-Map,$(PRG).map
 OBJCOPY        = avr-objcopy

+ 11 - 9
soft/display.c

@@ -7,7 +7,7 @@ void disp_num(signed long int val, unsigned char dec){
 		putchar('-');
 		val = -val;
 	} else {
-		putchar(' ');
+//		putchar(' ');
 	}
 	if(val > 99999999){
 		putchar('!');
@@ -17,41 +17,43 @@ void disp_num(signed long int val, unsigned char dec){
 		if(digit){
 			putchar(digit + '0');
 			disp = 1;
-		} else putchar(' ');
+		}// else putchar(' ');
 	}
 	digit = (val/1000000)%10;
 	if(digit || disp){
 		putchar(digit + '0');
 		disp = 1;
-	} else putchar(' ');
+	}// else putchar(' ');
 	digit = (val/100000)%10;
 	if(digit || disp){
 		putchar(digit + '0');
 		disp = 1;
-	} else putchar(' ');
+	}// else putchar(' ');
 	digit = (val/10000)%10;
-	if(digit || disp){
+	if(digit || disp || dec > 3){
 		putchar(digit + '0');
 		disp = 1;
-	} else putchar(' ');
+	}// else putchar(' ');
 	digit = (val/1000)%10;
+	if(dec == 4)
+		putchar(',');
 	if(digit || disp || dec > 2){
 		putchar(digit + '0');
 		disp = 1;
-	} else putchar(' ');
+	}// else putchar(' ');
 	digit = (val/100)%10;
 	if(dec == 3)
 		putchar(',');
 	if(digit || disp || dec > 1){
 		putchar(digit + '0');
 		disp = 1;
-	} else putchar(' ');
+	}// else putchar(' ');
 	if(dec == 2)
 		putchar(',');
 	digit = (val/10)%10;
 	if(digit || disp || dec > 0){
 		putchar(digit + '0');
-	} else putchar(' ');
+	}// else putchar(' ');
 	if(dec == 1)
 		putchar(',');
 	digit = val%10;

+ 162 - 22
soft/main.c

@@ -14,6 +14,8 @@
 
 volatile struct timers timers;
 
+volatile unsigned char tick; // incremented every 100ms
+
 unsigned char getkey(void){
 	static unsigned char old;
 	unsigned char key;
@@ -53,8 +55,8 @@ EEMEM struct settings eep_settings = {
 	.coeff_disch_curr_B = 1000,
 	.coeff_volt_A = 1.55337778893448E-07,
 	.coeff_volt_B = -0.092784032021951,
-	.coeff_curr_A = 9.03353481586364E-08,
-	.coeff_curr_B = -0.124868806047484,
+	.coeff_curr_A = -9.03353481586364E-08,
+	.coeff_curr_B = 0.124868806047484,
 //	.coeff_curr_A = .001,
 //	.coeff_curr_B = 0,
 };
@@ -147,16 +149,44 @@ unsigned char adc_process_data(void){
 	return 0;
 }
 
+__flash const char text0[] = "stop";
+__flash const char text1[] = "\x80""aduj";
+__flash const char text2[] = "roz\x80""aduj";
+__flash const char text3[] = "reset";
+__flash const char text4[] = "Uset = ";
+__flash const char text5[] = "Iset = ";
+
+__flash const char *main_menu_texts[] = {
+	text0,
+	text1,
+	text2,
+	text3,
+	text4,
+	text5,
+};
+
+#define MENU_MAX 5
+
+#define MODE_STOP	0
+#define MODE_CHARGE	1
+#define MODE_DISCHARGE	2
+
 void main(void){
 	unsigned char i=0;
+	unsigned char pos=0;
+	unsigned char k;
+	unsigned char mode = MODE_STOP;
+	unsigned int volt_set = 126, curr_set = 8; // *0,1V/A
+	signed long long int charge_counter = 0, energy_counter = 0; // *0,1µC, *10µJ
+	unsigned long int time_counter = 0; // *0,1s
 /*	unsigned char state = STATE_DEFAULT;
 	unsigned char oldstate = state;*/
 	
 	eeprom_read_block(&settings, &eep_settings, sizeof(struct settings));
 	
-	OCR2A = 250; // 16ms
+	OCR2A = 100; // 100µs
 	TCCR2A = _BV(WGM21);
-	TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
+	TCCR2B = _BV(CS21);
 	TIMSK2 = _BV(OCIE2A);
 	sei();
 
@@ -171,20 +201,20 @@ void main(void){
 	ads1224_init();
 	ads1224_mux(AIN1);
 	
-	charge_on();
-	set_voltage(12.6);
-	set_charge_curr(.8);
+/*	charge_on();
+	set_voltage(volt_set);
+	set_charge_curr(curr_set);*/
 	
 	for(;;){
-		_delay_ms(1);
-		if((i = getkey())){
+/*		if((i = getkey())){
 			cursor(0,0);
 			disp_num(i, 0);
 			clearline();
-		}
+		}*/
 		dac_update();
+		adc_process_data();
 		
-		if(adc_process_data()){
+		/*if(adc_process_data()){
 			cursor(0,1);
 			disp_num(meas[MEAS_VOLT] * 1000, 3);
 			putchar('V');
@@ -194,22 +224,127 @@ void main(void){
 		//	disp_num(adc_results[MEAS_CURR], 0);
 			putchar('A');
 			clearline();
+		}*/
+		for(i=0; i<=MENU_MAX; i++){
+			cursor(0, i);
+			if(pos == i)
+				putchar('*');
+			else
+				putchar(' ');
+			putchar(' ');
+			puttext_P(main_menu_texts[i]);
+			putchar(' ');
+			switch(i){
+				default: break;
+				case 1: if(mode == MODE_CHARGE) puttext_P(PSTR("w\x80.")); break;
+				case 2: if(mode == MODE_DISCHARGE) puttext_P(PSTR("w\x80.")); break;
+				case 4: disp_num(volt_set, 1); putchar('V'); break;
+				case 5: disp_num(curr_set, 1); putchar('A'); break;
+			}
+			clearline();
 		}
-//		dac8571_set(++dac1, VOLT_DAC);
-//		dac8571_set(++dac2, CURR_DAC);
-/*		gettemp();
-		control();
-		state = state_processors[state](state);
-		if(oldstate != state){
-			GLCD_Clear();
-			settings_update = 1;
+		k = getkey();
+		switch(k){
+			default: break;
+			case KEY_UP: if(pos>0) pos--; break;
+			case KEY_DOWN: if(pos<MENU_MAX) pos++; break;
+			case KEY_LEFT:
+				switch(pos){
+					case 4:
+						if(mode != MODE_STOP)
+							break;
+						if(volt_set > 0)
+							volt_set -= 1;
+						break;
+					case 5:
+						if(mode != MODE_STOP)
+							break;
+						if(curr_set > 0)
+							curr_set -= 1;
+						break;
+				}
+				break;			
+			case KEY_RIGHT:
+				switch(pos){
+					case 4:
+						if(mode != MODE_STOP)
+							break;
+						if(volt_set < 200) // 20V
+							volt_set += 1;
+						break;
+					case 5:
+						if(mode != MODE_STOP)
+							break;
+						if(curr_set < 100)
+							curr_set += 1; // 10A
+						break;
+				}
+				break;
+			case KEY_OK:
+				switch(pos){
+					case 0:
+						mode = MODE_STOP;
+						output_off();
+						break;
+					case 1:
+						if(mode != MODE_STOP)
+							break;
+						mode = MODE_CHARGE;
+						charge_on();
+						set_voltage((float)volt_set/10.0);
+						set_charge_curr((float)curr_set/10.0);
+						break;
+					case 2:
+						break;
+					case 3:
+						if(mode != MODE_STOP)
+							break;
+						charge_counter = 0;
+						energy_counter = 0;
+						time_counter = 0;
+						break;
+				}
+				break;
+		}
+		
+		if(tick){
+			cli();
+			if(mode != MODE_STOP){
+				charge_counter += meas[MEAS_CURR] * (float)tick / 10.0 / 0.1e-6;
+				energy_counter += meas[MEAS_CURR] * meas[MEAS_VOLT] * (float)tick / 10.0 / 10e-6;
+				time_counter += tick;
+			}
+			tick = 0;
+			sei();
+			
+			cursor(0,6);
+			disp_num(meas[MEAS_VOLT] * 1000, 3);
+			putchar('V');
+			putchar(' ');
+			disp_num(meas[MEAS_CURR] * 1000, 3);
+			putchar('A');
+			clearline();
+			cursor(0,7);
+			disp_num(charge_counter * 0.1e-6 * 10000 / 3600.0, 4);
+			puttext_P(PSTR("Ah "));
+			disp_num(energy_counter * 10e-6 * 10000 / 3600.0, 4);
+			puttext_P(PSTR("Wh "));
+			disp_num(time_counter, 1);
+			puttext_P(PSTR("s"));
+			clearline();
 		}
-		oldstate = state; 
-		send_595(&led, 1);*/
 	}
 }
 
-ISR(TIMER2_COMPA_vect){ // 16ms
+ISR(TIMER2_COMPA_vect){ // 100µs
+	static unsigned char pre;
+	
+	if(++pre < 10)
+		return;
+	pre = 0;
+	
+	// 1ms
+	
 	unsigned int *volatile ctimer;
 	unsigned char i;
 	
@@ -218,5 +353,10 @@ ISR(TIMER2_COMPA_vect){ // 16ms
 		if(*ctimer)
 			(*ctimer)--;
 	}
+	
+	if(timers.tick == 0){
+		tick++; // 100ms
+		timers.tick = 100;
+	}
 }
 

+ 1 - 1
soft/main.h

@@ -17,7 +17,7 @@
 #define VOLT_DAC	0
 #define CURR_DAC	1
 
-#define ms(x) ((unsigned int)x/16)
+#define ms(x) (x)
 
 struct timers {
 	unsigned int tick;