first working version, just sevensegments, still without leds

This commit is contained in:
Lucas Pleß 2013-06-14 23:18:48 +02:00
parent cc96b5baf3
commit 17be17b75c
2 changed files with 178 additions and 109 deletions

File diff suppressed because one or more lines are too long

View File

@ -7,6 +7,8 @@
#include "main.h"
#include "uart.h"
#define BUFSIZE 40
volatile uint16_t syscounter = 0;
volatile uint8_t digitbuffer[6] = { 0,0,0,0,0,0 };
volatile uint8_t leddigitbuffer[4] = { 0,0,0,0 };
@ -25,8 +27,8 @@ uint16_t power_gen = 0;
uint16_t power_load = 0;
unsigned char data_count = 0;
unsigned char data_in[20];
char command_in[20];
unsigned char data_in[BUFSIZE];
char command_in[BUFSIZE];
const uint8_t digit_translate[10] = {
63, 6, 91, 79, 102, 109, 125, 7, 127, 111
@ -57,7 +59,6 @@ static void ports_init(void) {
LED_PORT = 0;
}
static void print_sevenseg(uint8_t display, uint16_t value) {
uint8_t d[3];
d[2] = (value % 1000 / 100 );
@ -75,69 +76,125 @@ static void print_sevenseg(uint8_t display, uint16_t value) {
}
}
static void receive_values_from_powerboard(void) {
uint16_t uart_char;
static char buffer[20];
// send a to request data from powerboard
uart_putc('a');
#ifdef DEBUG
void pretty_print_all_values(void) {
uart_puts_P("Voltage: ");
uart_print_uint16(voltage);
uart_puts_P("mV\r\n");
// input format is:
// A$voltage,$current_in,$current_out,$power_in,$power_out,loadsw,dumpsw,gensw\n
uart_puts_P("Load: ");
uart_print_uint16(current_out);
uart_puts_P("mA ");
uart_print_uint16( power_load);
uart_puts_P("W\r\n");
uart_char = uart_getc();
uart_puts_P("Generator: ");
uart_print_uint16(current_in);
uart_puts_P("mA ");
uart_print_uint16( power_gen);
uart_puts_P("W\r\n");
if(uart_char != UART_NO_DATA) {
uart_puts_P("switches (load, dump, gen): ");
uart_putc(48 + loadsw);
uart_putc(',');
uart_putc(48 + dumpsw);
uart_putc(',');
uart_putc(48 + gensw);
uart_puts_P("\r\n");
}
#endif
void process_command() {
if(strstr(command_in,"A") != NULL) {
// we have an A and B (from check in work_uart()
// so our message should be complete and consist of:
// A$voltage,$current_in,$current_out,$power_in,$power_out,loadsw,dumpsw,gensw\n
//A12.5,65464,00000,00000,00000,1,0,1B
char *token;
uint8_t tokencounter = 0;
char *start = strrchr(command_in, 'A');
// remove first (B is ignored by atoi)
start++;
#ifdef DEBUG
uart_puts_P("from start: ");
uart_puts(start);
uart_puts_P("\r\n");
#endif
token = strtok(start, ",");
while( token ) {
#ifdef DEBUG
uart_puts_P("token= ");
uart_puts(token);
uart_puts_P("\r\n");
#endif
switch(tokencounter) {
case 0:
voltage = atoi(token);
break;
case 1:
current_in = atoi(token);
break;
case 2:
current_out = atoi(token);
break;
case 3:
power_gen = atoi(token);
break;
case 4:
power_load = atoi(token);
break;
case 5:
if(atoi(token) == 1) loadsw = 1;
else loadsw = 0;
break;
case 6:
if(atoi(token) == 1) dumpsw = 1;
else dumpsw = 0;
break;
case 7:
if(atoi(token) == 1) gensw = 1;
else gensw = 0;
break;
}
tokencounter++;
token = strtok(NULL, ",");
}
#ifdef DEBUG
pretty_print_all_values();
#endif
}
voltage = 12200;
current_in = 1500;
current_out = 1600;
power_gen += 1;
power_load += 2;
}
/*
void process_command() {
if(strstr(command_in,"a") != NULL) {
//
if(strcasestr(command_in,"?") != NULL)
print_value("goto", variable_goto);
else
variable_goto = parse_assignment(command_in);
}
else if(strcasestr(command_in,"A") != NULL){
if(strcasestr(command_in,"?") != NULL)
print_value("A", variable_A);
else
variable_A = parse_assignment(command_in);
}
} */
void work_uart(){
unsigned int c = uart_getc();
unsigned int c = uart_getc();
if ( !(c & UART_NO_DATA) ) {
if ( !(c & UART_NO_DATA) ) {
data_in[data_count] = c;
data_in[data_count] = c;
if (data_in[data_count] == '\n') { // finish reading when newline is received
data_count = 0;
if (data_in[data_count] == 'B') { // finish reading when newline is received
data_count = 0;
// copy_command
memcpy(command_in, data_in, 20);
// Now clear data_in, the UART can reuse it now
memset(data_in, 0, 20);
memcpy(command_in, data_in, BUFSIZE);
//process_command();
} else {
data_count++;
// Now clear data_in, the UART can reuse it now
memset(data_in, 0, BUFSIZE);
process_command();
} else {
data_count++;
}
}
}
}
@ -147,14 +204,14 @@ int main(void) {
ports_init();
timer_init();
uart_init(UART_BAUD_SELECT(19200,F_CPU));
memset(data_in, 0, BUFSIZE);
while(1) {
work_uart();
if(syscounter == 200) {
receive_values_from_powerboard();
if(syscounter >= 200) {
uart_putc('a'); // send a to receive values
print_sevenseg(0, power_gen);
print_sevenseg(1, power_load);