diff --git a/schaltungen/displayboard_servo/software/Makefile b/schaltungen/displayboard_servo/software/Makefile index 2de5850..99f5a40 100644 --- a/schaltungen/displayboard_servo/software/Makefile +++ b/schaltungen/displayboard_servo/software/Makefile @@ -12,6 +12,9 @@ MCU = attiny2313 F_CPU = 8000000 +LFUSE = 0xe4 +HFUSE = 0xd9 +EFUSE = 0xff # Output format. (can be srec, ihex, binary) FORMAT = ihex @@ -78,6 +81,7 @@ LDFLAGS = -Wl,-Map=$(TARGET).map,--cref AVRDUDE_PROGRAMMER = usbtiny AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep +AVRDUDE_FUSE = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m AVRDUDE_FLAGS = -p $(MCU) -c $(AVRDUDE_PROGRAMMER) @@ -184,6 +188,8 @@ gccversion : program: $(TARGET).hex $(TARGET).eep $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) +fuse: $(TARGET).hex $(TARGET).eep + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_FUSE) # Create final output files (.hex, .eep) from ELF output file. %.hex: %.elf diff --git a/schaltungen/displayboard_servo/software/src/main.c b/schaltungen/displayboard_servo/software/src/main.c index 34e9de0..36cf47f 100644 --- a/schaltungen/displayboard_servo/software/src/main.c +++ b/schaltungen/displayboard_servo/software/src/main.c @@ -8,53 +8,87 @@ #include "main.h" #include "uart.h" -#define BUFSIZE 40 + +#define BUFSIZE 32 volatile uint16_t syscounter = 0; // values send over uart from powerboard -uint16_t power_gen = 0; - -unsigned char data_count = 0; -unsigned char data_in[BUFSIZE]; -char command_in[BUFSIZE]; +typedef struct { + uint16_t voltage_gen; + uint16_t voltage_reg; + uint16_t current_gen; + + struct { + uint8_t loadsw : 1; + uint8_t gensw : 1; + uint8_t batsw : 1; + }; +} PWR_DATA; +static PWR_DATA pd; +uint8_t data_count = 0; +char data_in[BUFSIZE]; +extern unsigned char __heap_start; + + 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 = 2312; // Neutralposition ((2500-2312)*0.008ms)=1,5ms) TIMSK = _BV(OCIE1A); TCCR1A = (1<> 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') { + //uart_puts_P("got b\r\n"); + + process_command(data_in); + data_count = 0; - - memcpy(command_in, data_in, BUFSIZE); memset(data_in, 0, BUFSIZE); - - process_command(); - } else { - data_count++; - } + } + + data_count++; + if(data_count >= BUFSIZE) { + data_count = 0; + memset(data_in, 0, BUFSIZE); + //uart_puts_P("overflow\r\n"); + } + } + + } static void set_servo(uint16_t display) { - display = display * 10; // shift, since we have to divide by 3,2 (32) + if( display < 1 ) display = 1; + if( display > 400 ) display = 400; + + 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; cli(); // read and write atomic + if( display < 125 ) display = 125; + if( display > 250 ) display = 250; OCR1A = 2500-display; - sei(); - + sei(); } static void demo_display(void) { - for(uint8_t i = 0; i< 40;i++) { + for(uint16_t i = 0; i<= 40;i++) { set_servo(i*10); - wait(5); + _delay_ms(50); } - - for(uint8_t i = 40; i> 0;i--) { + for(uint16_t i = 40; i> 0;i--) { set_servo(i*10); - wait(5); - } - + _delay_ms(50); + } } -int main(void) { +int main(void) { + sei(); ports_init(); timer_init(); - uart_init(UART_BAUD_SELECT(19200,F_CPU)); + uart_init(UART_BAUD_SELECT(38400,F_CPU)); memset(data_in, 0, BUFSIZE); - + demo_display(); while(1) { + work_uart(); - if(syscounter >= 10) { + if(syscounter >= 100) { + memset(data_in, 0, BUFSIZE); + data_count = 0; uart_putc('a'); // send a to receive values - - set_servo(power_gen); + + //uart_puts_P("RAM="); uart_print_uint16(SP - (uint16_t) &__heap_start); uart_puts_P("\r\n"); + + set_servo((pd.voltage_gen/1000) * (pd.current_gen/1000)); + + /* + uart_puts_P("voltage_gen = "); + uart_print_voltage(pd.voltage_gen); + uart_puts_P(" voltage_reg = "); + uart_print_voltage(pd.voltage_reg); + uart_puts_P(" current_gen = "); + uart_print_current(pd.current_gen); + uart_puts_P("\r\n"); + */ syscounter = 0; } } @@ -134,10 +211,9 @@ int main(void) { SIGNAL(TIMER1_COMPA_vect) { syscounter++; - OCR1A = 2500-OCR1A; // Das Servosignal wird aus der Differenz von // Periodenlänge (2500*0,008ms=20ms) und letztem - // Vergleichswert (OCR1A) gebildet + // Vergleichswert (OCR1A) gebildet } diff --git a/schaltungen/displayboard_servo/software/src/uart.h b/schaltungen/displayboard_servo/software/src/uart.h index 12f69fb..9f11070 100644 --- a/schaltungen/displayboard_servo/software/src/uart.h +++ b/schaltungen/displayboard_servo/software/src/uart.h @@ -76,7 +76,7 @@ LICENSE: #endif /** Size of the circular transmit buffer, must be power of 2 */ #ifndef UART_TX_BUFFER_SIZE -#define UART_TX_BUFFER_SIZE 32 +#define UART_TX_BUFFER_SIZE 8 #endif /* test if the size of the circular buffers fits into SRAM */ diff --git a/schaltungen/displayboard_servo/software/src/utils.c b/schaltungen/displayboard_servo/software/src/utils.c index 6a5caca..97db470 100644 --- a/schaltungen/displayboard_servo/software/src/utils.c +++ b/schaltungen/displayboard_servo/software/src/utils.c @@ -15,6 +15,18 @@ void uart_print_voltage(uint16_t x) { uart_putc(48 + (x % 10000 /1000)); uart_putc('.'); uart_putc(48 + (x % 1000 / 100 )); + uart_putc(48 + (x % 100 / 10 )); + //uart_putc(48 + (x % 10)); + uart_putc('V'); +} + +void uart_print_current(uint16_t x) { + uart_putc(48 + (x / 10000)); + uart_putc(48 + (x % 10000 /1000)); + uart_putc('.'); + uart_putc(48 + (x % 1000 / 100 )); + uart_putc(48 + (x % 100 / 10 )); + uart_putc('A'); //uart_putc(48 + (x % 100 / 10 )); //uart_putc(48 + (x % 10)); } diff --git a/schaltungen/displayboard_servo/software/src/utils.h b/schaltungen/displayboard_servo/software/src/utils.h index e3dec72..9cfe05b 100644 --- a/schaltungen/displayboard_servo/software/src/utils.h +++ b/schaltungen/displayboard_servo/software/src/utils.h @@ -3,6 +3,7 @@ extern void wait(uint8_t count); extern void uart_print_voltage(uint16_t); + extern void uart_print_current(uint16_t); extern void uart_print_int16(int16_t); extern void uart_print_uint16(uint16_t);