X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;ds=sidebyside;f=main.c;h=39f3f5cc7a80ffd68fd6733cc3d987d0fd6b4a67;hb=1d6d9149f9b438f797a7af85c0454e38a2767a95;hp=24cb433ccec04b685d78e76dc7560d88301c372e;hpb=cff29240303d385ea5e4cd2d6016774662218b4d;p=digitaldcpower diff --git a/main.c b/main.c index 24cb433..39f3f5c 100644 --- a/main.c +++ b/main.c @@ -19,10 +19,12 @@ #include "lcd.h" #include "dac.h" #include "kbd.h" +#include "uart.h" #include "analog.h" #include "hardware_settings.h" -#define SWVERSION "ver: ddcp-0.6.0" +// change this when you compile: +#define SWVERSION "ver: ddcp-0.6.2" //#define DEBUGDISP 1 //debug LED: @@ -34,73 +36,89 @@ #define LEDISOFF PORTD&(1< { + int ist_start_of_line=1; while(ms){ - _delay_ms(0.96); + _delay_ms(0.85); +#ifdef USE_UART + if(uart_has_one_line==0 && uart_getchar_noblock(&uartstr[uartstrpos])){ + uart_sendchar(uartstr[uartstrpos]); // echo back + if (uartstr[uartstrpos]=='\n'||uartstr[uartstrpos]=='\r'){ + uartstr[uartstrpos]='\0'; + uart_has_one_line=1; + uart_sendchar('\n'); // the echo back puts a \r + } + // ignore leading white space on the line: + if (!(uartstr[uartstrpos]==' ' || uartstr[uartstrpos]=='\t')){ + ist_start_of_line=0; + uartstrpos++; + }else{ + // white space + if (ist_start_of_line==0){ + uartstrpos++; + } + } + if (uartstrpos>UARTSTRLEN){ + uart_sendstr_P("\r\nERROR\r\n"); + uartstrpos=0; // empty buffer + uartstr[uartstrpos]='\0'; // just print prompt + uart_has_one_line=1; + } + } +#endif ms--; } } // Convert an integer which is representing a float into a string. -// decimalpoint_pos sets the decimal point after 2 pos from the right: e.g 74 becomes "0.74" -// The integer may not be larger than 999. +// Our display is always 4 digits long (including one +// decimal point position). decimalpoint_pos defines +// after how many positions from the right we set the decimal point. +// The resulting string is fixed width and padded with leading space. +// +// decimalpoint_pos=2 sets the decimal point after 2 pos from the right: +// e.g 74 becomes "0.74" +// The integer should not be larger than 999. // The integer must be a positive number. -// spacepadd can be used to add a leading speace if number is less than 10 -//static void int_to_ascii(uint16_t inum,char *outbuf,int8_t decimalpoint_pos){ -// char chbuf[8]; -// itoa(inum,chbuf,10); // convert integer to string -static void int_to_ascii(int16_t inum,char *outbuf,int8_t decimalpoint_pos,uint8_t spacepadd){ +// decimalpoint_pos can be 0, 1 or 2 +static void int_to_dispstr(uint16_t inum,char *outbuf,int8_t decimalpoint_pos){ int8_t i,j; char chbuf[8]; - j=0; - while(inum>9 && j<7){ - // zero is ascii 48: - chbuf[j]=(char)48+ inum-((inum/10)*10); - inum=inum/10; - j++; - if(decimalpoint_pos==j){ - chbuf[j]='.'; - j++; - } - } - chbuf[j]=(char)48+inum; // most significant digit - decimalpoint_pos--; - while(j (decimalpoint_pos+2)){ - // no leading space padding needed - spacepadd=0; - } - if(decimalpoint_pos==j){ - j++; - chbuf[j]='.'; - j++; - chbuf[j]='0'; // leading zero - } - if (spacepadd){ - j++; - chbuf[j]=' '; // leading space padding: "9.50" becomes " 9.50" - } - // now reverse the order - i=0; - while(j>=0){ - outbuf[i]=chbuf[j]; + itoa(inum,chbuf,10); // convert integer to string + i=strlen(chbuf); + if (i>3) i=3; //overflow protection + strcpy(outbuf," 0"); //decimalpoint_pos==0 + if (decimalpoint_pos==1) strcpy(outbuf," 0.0"); + if (decimalpoint_pos==2) strcpy(outbuf,"0.00"); + j=4; + while(i){ + outbuf[j-1]=chbuf[i-1]; + i--; j--; - i++; + if (j==4-decimalpoint_pos){ + // jump over the pre-set dot + j--; + } } - outbuf[i]='\0'; } // convert voltage values to adc values, disp=10 is 1.0V @@ -160,7 +178,7 @@ static void store_permanent(void){ eeprom_write_word((uint16_t *)0x02,set_val[0]); eeprom_write_word((uint16_t *)0x04,set_val[1]); }else{ - if (bpress> 3){ + if (bpress> 2){ // display software version after long press lcd_puts_P(SWVERSION); lcd_gotoxy(0,1); @@ -169,11 +187,94 @@ static void store_permanent(void){ lcd_puts_P("already stored"); } } - delay_ms(200); + delay_ms_uartcheck(200); } // check the keyboard static uint8_t check_buttons(void){ + uint8_t uartprint_ok=0; + uint8_t cmdok=0; +#ifdef USE_UART + char buf[21]; +#endif + // +#ifdef USE_UART + if (uart_has_one_line){ + if (uartstr[0]=='i' && uartstr[1]=='=' && uartstr[2]!='\0'){ + set_val[0]=atoi(&uartstr[2]); + if(set_val[0]>I_MAX){ + set_val[0]=I_MAX; + } + if(set_val[0]<0){ + set_val[0]=0; + } + uartprint_ok=1; + } + // version + if (uartstr[0]=='v' && uartstr[1]=='e'){ + uart_sendstr_p(P(" ")); + uart_sendstr_p(P(SWVERSION)); + uart_sendstr_p(P("\r\n")); + cmdok=1; + } + // store + if (uartstr[0]=='s' && uartstr[1]=='t'){ + store_permanent(); + uartprint_ok=1; + } + if (uartstr[0]=='u' && uartstr[1]=='=' && uartstr[2]!='\0'){ + set_val[1]=atoi(&uartstr[2]); + if(set_val[1]>U_MAX){ + set_val[1]=U_MAX; + } + if(set_val[1]<0){ + set_val[1]=0; + } + uartprint_ok=1; + } + // help + if (uartstr[0]=='h' || uartstr[0]=='H'){ + uart_sendstr_p(P(" Usage: u=V*10|i=mA/10|store|help|version\r\n")); + uart_sendstr_p(P(" Examples:\r\n")); + uart_sendstr_p(P(" set 6V: u=60\r\n")); + uart_sendstr_p(P(" max 200mA: i=20\r\n")); + cmdok=1; + } + if (uartprint_ok){ + cmdok=1; + uart_sendstr_p(P(" ok\r\n")); + } + if (uartstr[0]!='\0' && cmdok==0){ + uart_sendstr_p(P(" command unknown\r\n")); + } + + int_to_dispstr(measured_val[1],buf,1); + uart_sendstr(buf); + uart_sendchar('V'); + uart_sendchar(' '); + uart_sendchar('['); + int_to_dispstr(set_val[1],buf,1); + uart_sendstr(buf); + uart_sendchar(']'); + uart_sendchar(','); + int_to_dispstr(measured_val[0],buf,2); + uart_sendstr(buf); + uart_sendchar('A'); + uart_sendchar(' '); + uart_sendchar('['); + int_to_dispstr(set_val[0],buf,2); + uart_sendstr(buf); + uart_sendchar(']'); + if (is_current_limit()){ + uart_sendchar('I'); + }else{ + uart_sendchar('U'); + } + uart_sendchar('>'); + uart_has_one_line=0; + uartstrpos=0; + } +#endif if (check_u_button(&(set_val[1]))){ if(set_val[1]>U_MAX){ set_val[1]=U_MAX; @@ -198,9 +299,12 @@ int main(void) char out_buf[21]; uint8_t i=0; uint8_t ilimit=0; - // debug led + +#ifndef USE_UART + // debug led, you can not have an LED if you use the uart DDRD|= (1< 10){ // somebody pressed permanetly the button=>scroll fast - delay_ms(30); + delay_ms_uartcheck(30); }else{ bpress++; - delay_ms(180); + delay_ms_uartcheck(180); } } }