fixed ram problem, made program working

This commit is contained in:
Lucas Pleß 2014-06-02 20:31:14 +02:00
parent 720ba155ad
commit 54c2f8bdca
5 changed files with 147 additions and 52 deletions

View File

@ -12,6 +12,9 @@
MCU = attiny2313 MCU = attiny2313
F_CPU = 8000000 F_CPU = 8000000
LFUSE = 0xe4
HFUSE = 0xd9
EFUSE = 0xff
# Output format. (can be srec, ihex, binary) # Output format. (can be srec, ihex, binary)
FORMAT = ihex FORMAT = ihex
@ -78,6 +81,7 @@ LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
AVRDUDE_PROGRAMMER = usbtiny AVRDUDE_PROGRAMMER = usbtiny
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep #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) AVRDUDE_FLAGS = -p $(MCU) -c $(AVRDUDE_PROGRAMMER)
@ -184,6 +188,8 @@ gccversion :
program: $(TARGET).hex $(TARGET).eep program: $(TARGET).hex $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) $(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. # Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf %.hex: %.elf

View File

@ -8,16 +8,29 @@
#include "main.h" #include "main.h"
#include "uart.h" #include "uart.h"
#define BUFSIZE 40
#define BUFSIZE 32
volatile uint16_t syscounter = 0; volatile uint16_t syscounter = 0;
// values send over uart from powerboard // values send over uart from powerboard
uint16_t power_gen = 0; typedef struct {
uint16_t voltage_gen;
uint16_t voltage_reg;
uint16_t current_gen;
unsigned char data_count = 0; struct {
unsigned char data_in[BUFSIZE]; uint8_t loadsw : 1;
char command_in[BUFSIZE]; 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) { static void timer_init(void) {
@ -26,35 +39,56 @@ static void timer_init(void) {
OCR1A = 2312; // Neutralposition ((2500-2312)*0.008ms)=1,5ms) OCR1A = 2312; // Neutralposition ((2500-2312)*0.008ms)=1,5ms)
TIMSK = _BV(OCIE1A); TIMSK = _BV(OCIE1A);
TCCR1A = (1<<COM1A0); // Togglen bei Compare Match TCCR1A = (1<<COM1A0); // Togglen bei Compare Match
sei(); // enable interrupts
} }
static void ports_init(void) { static void ports_init(void) {
DDRB |= _BV(PB3); DDRB |= _BV(PB3);
} }
static void process_command() { static void process_command(char *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 if(command[0] == 'A') {
// command must be in the following format:
// A$voltage_gen,$voltage_reg,$current_gen,loadsw,batsw,gensw\n
// examples:
// A24500,13400,12000,1,1,1B
// A19900,11000,15000,1,1,1B
// A34100,15100,01000,1,1,1B
char *token; char *token;
uint8_t tokencounter = 0; uint8_t tokencounter = 0;
char *start = strrchr(command_in, 'A'); token = strtok(command, ",");
// remove first (B is ignored by atoi)
start++;
token = strtok(start, ",");
while( token ) { while( token ) {
if (tokencounter == 3) { /*uart_puts_P("\r\ntoken: ");
power_gen = atoi(token); uart_puts(token);
uart_puts_P("\r\ntokencounter: ");
uart_print_uint16(tokencounter);*/
switch(tokencounter) {
case 0:
pd.voltage_gen = atoi(++token); // skip the A in front of the number
break;
case 1:
pd.voltage_reg = atoi(token);
break;
case 2:
pd.current_gen = atoi(token);
break;
case 3:
if(atoi(token) == 1) pd.loadsw = 1;
else pd.loadsw = 0;
break;
case 4:
if(atoi(token) == 1) pd.batsw = 1;
else pd.batsw = 0;
break;
case 5:
if(atoi(token) == 1) pd.gensw = 1;
else pd.gensw = 0;
break;
} }
tokencounter++; tokencounter++;
@ -65,66 +99,109 @@ static void process_command() {
static void work_uart() { static void work_uart() {
uint8_t c = uart_getc(); uint16_t c = uart_getc();
if ( !(c & UART_NO_DATA) ) { if ( !(c & UART_NO_DATA) ) {
data_in[data_count] = c;
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') {
//uart_puts_P("got b\r\n");
process_command(data_in);
data_count = 0; data_count = 0;
memcpy(command_in, data_in, BUFSIZE);
memset(data_in, 0, 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) { 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 / 32; // instead of dividing by 3,2
display = display + 125; display = display + 125;
cli(); // read and write atomic cli(); // read and write atomic
if( display < 125 ) display = 125;
if( display > 250 ) display = 250;
OCR1A = 2500-display; OCR1A = 2500-display;
sei(); sei();
} }
static void demo_display(void) { 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); set_servo(i*10);
wait(5); _delay_ms(50);
} }
for(uint16_t i = 40; i> 0;i--) {
for(uint8_t i = 40; i> 0;i--) {
set_servo(i*10); set_servo(i*10);
wait(5); _delay_ms(50);
} }
} }
int main(void) {
int main(void) {
sei();
ports_init(); ports_init();
timer_init(); timer_init();
uart_init(UART_BAUD_SELECT(19200,F_CPU)); uart_init(UART_BAUD_SELECT(38400,F_CPU));
memset(data_in, 0, BUFSIZE); memset(data_in, 0, BUFSIZE);
demo_display(); demo_display();
while(1) { while(1) {
work_uart(); work_uart();
if(syscounter >= 10) { if(syscounter >= 100) {
memset(data_in, 0, BUFSIZE);
data_count = 0;
uart_putc('a'); // send a to receive values 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; syscounter = 0;
} }
} }
@ -134,7 +211,6 @@ int main(void) {
SIGNAL(TIMER1_COMPA_vect) { SIGNAL(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

View File

@ -76,7 +76,7 @@ LICENSE:
#endif #endif
/** Size of the circular transmit buffer, must be power of 2 */ /** Size of the circular transmit buffer, must be power of 2 */
#ifndef UART_TX_BUFFER_SIZE #ifndef UART_TX_BUFFER_SIZE
#define UART_TX_BUFFER_SIZE 32 #define UART_TX_BUFFER_SIZE 8
#endif #endif
/* test if the size of the circular buffers fits into SRAM */ /* test if the size of the circular buffers fits into SRAM */

View File

@ -15,6 +15,18 @@ void uart_print_voltage(uint16_t x) {
uart_putc(48 + (x % 10000 /1000)); uart_putc(48 + (x % 10000 /1000));
uart_putc('.'); uart_putc('.');
uart_putc(48 + (x % 1000 / 100 )); 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 % 100 / 10 ));
//uart_putc(48 + (x % 10)); //uart_putc(48 + (x % 10));
} }

View File

@ -3,6 +3,7 @@
extern void wait(uint8_t count); extern void wait(uint8_t count);
extern void uart_print_voltage(uint16_t); 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_int16(int16_t);
extern void uart_print_uint16(uint16_t); extern void uart_print_uint16(uint16_t);