reformatting, lowered the polling interval, changed the code so the new value gets display faster

This commit is contained in:
Lucas Pleß 2014-06-03 03:42:28 +02:00
parent ee10cdf4ef
commit 37031ed445
1 changed files with 92 additions and 100 deletions

View File

@ -10,6 +10,7 @@
#define POWER_MIN 0 #define POWER_MIN 0
#define POWER_MAX 400 #define POWER_MAX 400
#define SERVO_STEPS 125
#define BUFSIZE 32 #define BUFSIZE 32
@ -37,12 +38,18 @@ void reset_input_buffer(void) {
memset(data_in, 0, BUFSIZE); memset(data_in, 0, BUFSIZE);
} }
static void update_servo_position() {
uint32_t power;
power = ( (pd.voltage_gen / 100) * (pd.current_gen / 100) ) / 100;
set_servo(power & 0xffff);
}
static void timer_init(void) { static void timer_init(void) {
// clock is 8MHz // 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 TCCR1B |= _BV(WGM12) | _BV(CS11) | _BV(CS10);
OCR1A = 2250; // Neutralposition ((2500-2312)*0.008ms)=1,5ms) OCR1A = 2250; // set Servo to max. position
TIMSK = _BV(OCIE1A); TIMSK |= _BV(OCIE1A); // enable timer interrupt
TCCR1A = (1<<COM1A0); // Togglen bei Compare Match TCCR1A |= (1<<COM1A0); // toggle OCR1A on overflow
} }
static void ports_init(void) { static void ports_init(void) {
@ -50,86 +57,69 @@ static void ports_init(void) {
} }
static void process_command(char *command) { static void process_command(char *command) {
if(command[0] == 'A') {
if(command[0] == 'A') {
// command must be in the following format: // command must be in the following format:
// A$voltage_gen,$voltage_reg,$current_gen,loadsw,batsw,gensw\n // A$voltage_gen,$voltage_reg,$current_gen,loadsw,batsw,gensw\n
// examples: // examples:
// A24500,13400,12000,1,1,1B // A24500,13400,12000,1,1,1B
// A19900,11000,15000,1,1,1B // A19900,11000,15000,1,1,1B
// A20000,12300,10000,1,1,1B // A20000,12300,10000,1,1,1B
char *token; char *token;
uint8_t tokencounter = 0; uint8_t tokencounter = 0;
token = strtok(command, ","); token = strtok(command, ",");
while( token ) { while( token ) {
switch(tokencounter) { switch(tokencounter) {
case 0: case 0:
pd.voltage_gen = atoi(++token); // skip the A in front of the number // skip the A in front of the number
break; pd.voltage_gen = atoi(++token);
case 1: break;
pd.voltage_reg = atoi(token); case 1:
break; pd.voltage_reg = atoi(token);
case 2: break;
pd.current_gen = atoi(token); case 2:
break; pd.current_gen = atoi(token);
case 3: break;
if(atoi(token) == 1) pd.loadsw = 1; case 3:
else pd.loadsw = 0; if(atoi(token) == 1) pd.loadsw = 1;
break; else pd.loadsw = 0;
case 4: break;
if(atoi(token) == 1) pd.batsw = 1; case 4:
else pd.batsw = 0; if(atoi(token) == 1) pd.batsw = 1;
break; else pd.batsw = 0;
case 5: break;
if(atoi(token) == 1) pd.gensw = 1; case 5:
else pd.gensw = 0; if(atoi(token) == 1) pd.gensw = 1;
break; else pd.gensw = 0;
} break;
}
tokencounter++; tokencounter++;
token = strtok(NULL, ","); token = strtok(NULL, ",");
} }
}
update_servo_position();
}
} }
static void work_uart() { static void work_uart() {
uint16_t c = uart_getc();
uint16_t c = uart_getc(); if ( !(c & UART_NO_DATA) ) {
if ( !(c & UART_NO_DATA) ) {
data_in[data_count] = (c & 0xff); data_in[data_count] = (c & 0xff);
/* if (data_in[data_count] == 'B') {
uart_print_uint16(data_count); process_command(data_in);
uart_puts_P(" char: ");
uart_putc(c >> 8);
uart_putc(c & 0xff);
uart_puts_P(";\r\n");
*/
/*
uart_puts_P("data: ");
for(uint8_t i = 0; i < BUFSIZE; i++) {
uart_putc(data_in[i]);
}
uart_puts_P("\r\n");
*/
if (data_in[data_count] == 'B') {
process_command(data_in);
reset_input_buffer();
}
data_count++;
if(data_count >= BUFSIZE) {
reset_input_buffer(); reset_input_buffer();
} }
}
data_count++;
if(data_count >= BUFSIZE) { // buffer overflow
reset_input_buffer();
}
}
} }
/** /**
@ -144,15 +134,22 @@ void set_servo(uint16_t display) {
display = POWER_MAX-display; // invert the value / servo display = POWER_MAX-display; // invert the value / servo
display = display * 10; // shift, since we have to divide by 3,2 (32) display = display * 10; // *10 otherwise we need float
display = display / 32; // instead of dividing by 3,2 display = display / ( (POWER_MAX*10) / SERVO_STEPS );
display = display + 125; display = display + SERVO_STEPS;
cli(); // read and write atomic uart_puts_P("display = ");
if( display < 125 ) display = 125; uart_print_uint16(display);
if( display > 250 ) display = 250; uart_puts_P("\r\n");
OCR1A = 2500-display;
sei(); //cli(); // read and write atomic
TIMSK &= ~_BV(OCIE1A);
if( display < 125 ) display = 125; // just make sure, the timer
if( display > 250 ) display = 250; // is never out ouf 20ms grid
OCR1A = 2500-display;
TIMSK |= _BV(OCIE1A);
} }
@ -174,25 +171,20 @@ int main(void) {
sei(); sei();
ports_init(); ports_init();
timer_init(); timer_init();
uart_init(UART_BAUD_SELECT(38400,F_CPU)); uart_init(UART_BAUD_SELECT(38400,F_CPU));
reset_input_buffer(); reset_input_buffer();
demo_display(); demo_display();
uint32_t power;
while(1) { while(1) {
work_uart(); work_uart();
if(syscounter >= 100) { // about every two seconds if((syscounter % 25) == 0) { // about every 500ms
memset(data_in, 0, BUFSIZE); reset_input_buffer();
data_count = 0; uart_putc('a'); // send a to receive values from master box
uart_putc('a'); // send a to receive values from master box }
power = ( (pd.voltage_gen / 100) * (pd.current_gen / 100) ) / 100; syscounter %= 1000;
set_servo(power & 0xffff);
syscounter = 0;
}
} }
return(0); return(0);
@ -204,9 +196,9 @@ int main(void) {
*/ */
ISR(TIMER1_COMPA_vect) { ISR(TIMER1_COMPA_vect) {
syscounter++; syscounter++;
OCR1A = 2500-OCR1A; // Das Servosignal wird aus der Differenz von OCR1A = 2500-OCR1A; // Das Servosignal wird aus der Differenz von
// Periodenlänge (2500*0,008ms=20ms) und letztem // Periodenlänge (2500*0,008ms=20ms) und letztem
// Vergleichswert (OCR1A) gebildet // Vergleichswert (OCR1A) gebildet
} }