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,7 +57,6 @@ 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
@ -67,7 +73,8 @@ static void process_command(char *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
pd.voltage_gen = atoi(++token);
break; break;
case 1: case 1:
pd.voltage_reg = atoi(token); pd.voltage_reg = atoi(token);
@ -92,41 +99,24 @@ static void process_command(char *command) {
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);
/*
uart_print_uint16(data_count);
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') { if (data_in[data_count] == 'B') {
process_command(data_in); process_command(data_in);
reset_input_buffer(); reset_input_buffer();
} }
data_count++; data_count++;
if(data_count >= BUFSIZE) { if(data_count >= BUFSIZE) { // buffer overflow
reset_input_buffer(); 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");
//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; OCR1A = 2500-display;
sei();
TIMSK |= _BV(OCIE1A);
} }
@ -178,21 +175,16 @@ int main(void) {
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;
set_servo(power & 0xffff);
syscounter = 0;
} }
syscounter %= 1000;
} }
return(0); return(0);