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_MAX 400
#define SERVO_STEPS 125
#define BUFSIZE 32
@ -37,12 +38,18 @@ void reset_input_buffer(void) {
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) {
// clock is 8MHz
TCCR1B |= _BV(WGM12) | _BV(CS11) | _BV(CS10); // CTC Mode for Timer 1 (16Bit) with prescale of 64
OCR1A = 2250; // Neutralposition ((2500-2312)*0.008ms)=1,5ms)
TIMSK = _BV(OCIE1A);
TCCR1A = (1<<COM1A0); // Togglen bei Compare Match
// CTC Mode for Timer 1 (16Bit) with prescale of 64
TCCR1B |= _BV(WGM12) | _BV(CS11) | _BV(CS10);
OCR1A = 2250; // set Servo to max. position
TIMSK |= _BV(OCIE1A); // enable timer interrupt
TCCR1A |= (1<<COM1A0); // toggle OCR1A on overflow
}
static void ports_init(void) {
@ -50,7 +57,6 @@ static void ports_init(void) {
}
static void process_command(char *command) {
if(command[0] == 'A') {
// command must be in the following format:
// A$voltage_gen,$voltage_reg,$current_gen,loadsw,batsw,gensw\n
@ -67,7 +73,8 @@ static void process_command(char *command) {
while( token ) {
switch(tokencounter) {
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;
case 1:
pd.voltage_reg = atoi(token);
@ -92,41 +99,24 @@ static void process_command(char *command) {
tokencounter++;
token = strtok(NULL, ",");
}
update_servo_position();
}
}
static void work_uart() {
uint16_t c = uart_getc();
if ( !(c & UART_NO_DATA) ) {
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') {
process_command(data_in);
reset_input_buffer();
}
data_count++;
if(data_count >= BUFSIZE) {
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 = display * 10; // shift, since we have to divide by 3,2 (32)
display = display / 32; // instead of dividing by 3,2
display = display + 125;
display = display * 10; // *10 otherwise we need float
display = display / ( (POWER_MAX*10) / SERVO_STEPS );
display = display + SERVO_STEPS;
cli(); // read and write atomic
if( display < 125 ) display = 125;
if( display > 250 ) display = 250;
uart_puts_P("display = ");
uart_print_uint16(display);
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;
sei();
TIMSK |= _BV(OCIE1A);
}
@ -178,21 +175,16 @@ int main(void) {
reset_input_buffer();
demo_display();
uint32_t power;
while(1) {
work_uart();
if(syscounter >= 100) { // about every two seconds
memset(data_in, 0, BUFSIZE);
data_count = 0;
if((syscounter % 25) == 0) { // about every 500ms
reset_input_buffer();
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);