minor changes, made counters 8bit

This commit is contained in:
Lucas Pleß 2013-06-14 12:47:35 +02:00
parent 7fa7355afa
commit 15f63351c0
3 changed files with 73 additions and 62 deletions

View File

@ -11,15 +11,15 @@ uint16_t voltage = 0;
int16_t current_in = 0; int16_t current_in = 0;
int16_t current_out = 0; int16_t current_out = 0;
uint16_t overvoltage_counter1 = 0; uint8_t overvoltage_counter1 = 0;
uint16_t overvoltage_off_counter1 = 0; uint8_t overvoltage_off_counter1 = 0;
uint16_t overvoltage_counter2 = 0; uint8_t overvoltage_counter2 = 0;
uint16_t overvoltage_off_counter2 = 0; uint8_t overvoltage_off_counter2 = 0;
uint16_t undervoltage_counter = 0; uint8_t undervoltage_counter = 0;
uint16_t undervoltage_off_counter = 0; uint8_t undervoltage_off_counter = 0;
void timer_init(void) { static void timer_init(void) {
// clock is 8MHz // clock is 8MHz
TCCR1B |= _BV(WGM12) | _BV(CS11) | _BV(CS10) ; // CTC Mode for Timer 1 (16Bit) with prescale of 64 TCCR1B |= _BV(WGM12) | _BV(CS11) | _BV(CS10) ; // CTC Mode for Timer 1 (16Bit) with prescale of 64
OCR1A = 1250; // 100Hz OCR1A = 1250; // 100Hz
@ -27,7 +27,7 @@ void timer_init(void) {
sei(); // enable interrupts sei(); // enable interrupts
} }
void ports_init(void) { static void ports_init(void) {
DDR_SW |= _BV(LOADSW) | _BV(GENSW) | _BV(DUMPSW); DDR_SW |= _BV(LOADSW) | _BV(GENSW) | _BV(DUMPSW);
PORT_SW &= ~(_BV(LOADSW) | _BV(GENSW) | _BV(DUMPSW)); PORT_SW &= ~(_BV(LOADSW) | _BV(GENSW) | _BV(DUMPSW));
} }
@ -49,6 +49,7 @@ uint8_t get_power(uint16_t voltage, int16_t currents) {
return (voltage/100 * (currents/100)) / 100 ; return (voltage/100 * (currents/100)) / 100 ;
} }
void pretty_print_all_values(void) { void pretty_print_all_values(void) {
uart_puts_P("Voltage: "); uart_puts_P("Voltage: ");
uart_print_uint16(voltage); uart_print_uint16(voltage);
@ -65,6 +66,14 @@ void pretty_print_all_values(void) {
uart_puts_P("mA "); uart_puts_P("mA ");
uart_print_uint16(get_power(voltage, current_in)); uart_print_uint16(get_power(voltage, current_in));
uart_puts_P("W\r\n"); uart_puts_P("W\r\n");
uart_puts_P("switches (load, dump, gen): ");
uart_putc(48 + (IS_LOAD_ON >> LOADSW));
uart_putc(',');
uart_putc(48 + (IS_DUMP_ON >> DUMPSW));
uart_putc(',');
uart_putc(48 + (IS_GEN_ON >> GENSW));
uart_puts_P("\r\n");
} }
void handle_over_and_undervoltage(void) { void handle_over_and_undervoltage(void) {
@ -125,45 +134,73 @@ void handle_over_and_undervoltage(void) {
#ifdef DEBUG #ifdef DEBUG
uart_puts_P("overvoltage_counter1="); uart_puts_P("ov1=");
uart_print_uint16(overvoltage_counter1); uart_print_uint8(overvoltage_counter1);
uart_puts_P(" overvoltage_off_counter1="); uart_puts_P(" ovo1=");
uart_print_uint16 (overvoltage_off_counter1); uart_print_uint8 (overvoltage_off_counter1);
uart_puts_P("\r\n"); uart_puts_P("\r\n");
uart_puts_P("overvoltage_counter2="); uart_puts_P("ov2=");
uart_print_uint16(overvoltage_counter2); uart_print_uint8(overvoltage_counter2);
uart_puts_P(" overvoltage_off_counter2="); uart_puts_P(" ovo2=");
uart_print_uint16 (overvoltage_off_counter2); uart_print_uint8 (overvoltage_off_counter2);
uart_puts_P("\r\n"); uart_puts_P("\r\n");
uart_puts_P("undervoltage_counter="); uart_puts_P("uv =");
uart_print_uint16(undervoltage_counter); uart_print_uint8(undervoltage_counter);
uart_puts_P(" undervoltage_off_counter="); uart_puts_P(" uvo =");
uart_print_uint16(undervoltage_off_counter); uart_print_uint8(undervoltage_off_counter);
uart_puts_P("\r\n"); uart_puts_P("\r\n");
#endif #endif
} }
static void work_uart(void) {
uint16_t uart_char = uart_getc();
if(uart_char != UART_NO_DATA) {
switch(uart_char & 0xff) {
case 'p':
pretty_print_all_values();
break;
case 'a':
uart_putc('A');
uart_print_voltage(voltage);
uart_putc(',');
uart_print_uint16(current_in);
uart_putc(',');
uart_print_uint16(current_out);
uart_putc(',');
uart_print_uint16(get_power(voltage, current_in));
uart_putc(',');
uart_print_uint16(get_power(voltage, current_out));
uart_putc(',');
uart_putc(48 + (IS_LOAD_ON >> LOADSW));
uart_putc(',');
uart_putc(48 + (IS_DUMP_ON >> DUMPSW));
uart_putc(',');
uart_putc(48 + (IS_GEN_ON >> GENSW));
break;
}
uart_putc('\n');
}
}
int main(void) { int main(void) {
ports_init(); ports_init();
adc_init();
timer_init();
uart_init(UART_BAUD_SELECT(19200,F_CPU));
LOAD_OFF; LOAD_OFF;
GEN_ON; GEN_ON;
DUMP_OFF; DUMP_OFF;
adc_init();
timer_init();
uart_init(UART_BAUD_SELECT(19200,F_CPU));
uint16_t uart_char;
while(1) { while(1) {
if(syscounter == 100) { if(syscounter >= 100) {
syscounter = 0; syscounter = 0;
measure(); measure();
@ -173,41 +210,7 @@ int main(void) {
handle_over_and_undervoltage(); handle_over_and_undervoltage();
} }
uart_char = uart_getc(); work_uart();
if(uart_char != UART_NO_DATA) {
switch(uart_char & 0xff) {
case 'p':
pretty_print_all_values();
break;
case 'v':
uart_print_voltage(voltage);
break;
case 'i':
uart_print_uint16(current_in);
break;
case 'o':
uart_print_uint16(current_out);
case 'g':
uart_print_uint16(get_power(voltage, current_in));
break;
case 'c':
uart_print_uint16(get_power(voltage, current_in));
break;
case 'a':
uart_print_voltage(voltage);
uart_putc(':');
uart_print_uint16(current_in);
uart_putc(':');
uart_print_uint16(current_out);
uart_putc(':');
uart_print_uint16(get_power(voltage, current_in));
uart_putc(':');
uart_print_uint16(get_power(voltage, current_out));
break;
}
uart_putc('\n');
}
} }
return(0); return(0);

View File

@ -19,6 +19,13 @@ void uart_print_voltage(uint16_t x) {
//uart_putc(48 + (x % 10)); //uart_putc(48 + (x % 10));
} }
void uart_print_uint8(uint8_t x) {
uart_putc(48 + (x / 100 ));
uart_putc(48 + (x % 100 / 10 ));
uart_putc(48 + (x % 10));
}
void uart_print_int16(int16_t x) { void uart_print_int16(int16_t x) {
if(x <0) uart_putc('-'); if(x <0) uart_putc('-');
uart_putc(48 + (x / 10000)); uart_putc(48 + (x / 10000));

View File

@ -3,6 +3,7 @@
extern void wait(uint8_t count); extern void wait(uint8_t count);
extern void uart_print_voltage(uint16_t); extern void uart_print_voltage(uint16_t);
extern void uart_print_uint8_t(uint8_t);
extern void uart_print_int16(int16_t); extern void uart_print_int16(int16_t);
extern void uart_print_uint16(uint16_t); extern void uart_print_uint16(uint16_t);