main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/pgmspace.h>
  4. #include <avr/eeprom.h>
  5. #include "main.h"
  6. #include "1wire.h"
  7. #include "gtext.h"
  8. #include "sed1335.h"
  9. #include "ds18b20.h"
  10. #include "dac8571.h"
  11. #include "ads1224.h"
  12. #include "display.h"
  13. volatile struct timers timers;
  14. volatile unsigned char tick; // incremented every 100ms
  15. unsigned char getkey(void){
  16. static unsigned char old;
  17. unsigned char key;
  18. DDRA = 0;
  19. PORTA = 0xff;
  20. DDRC |= _BV(PC7);
  21. _delay_ms(2);
  22. key = ~PINA;
  23. DDRC &= ~_BV(PC7);
  24. PORTA = 0;
  25. DDRA = 0xff;
  26. if(key == old) return 0;
  27. old = key;
  28. return key;
  29. }
  30. struct settings {
  31. float coeff_generator_volt_A;
  32. float coeff_generator_volt_B;
  33. float coeff_generator_curr_A;
  34. float coeff_generator_curr_B;
  35. float coeff_disch_curr_A;
  36. float coeff_disch_curr_B;
  37. float coeff_volt_A;
  38. float coeff_volt_B;
  39. float coeff_curr_A;
  40. float coeff_curr_B;
  41. };
  42. struct settings settings;
  43. EEMEM struct settings eep_settings = {
  44. .coeff_generator_volt_A = 1597.035901,
  45. .coeff_generator_volt_B = 1317.235211,
  46. .coeff_generator_curr_A = 65535.0/20,
  47. .coeff_generator_curr_B = 1310,
  48. .coeff_disch_curr_A = 65535.0/20,
  49. .coeff_disch_curr_B = 1000,
  50. .coeff_volt_A = 1.55337778893448E-07,
  51. .coeff_volt_B = -0.092784032021951,
  52. .coeff_curr_A = -9.03353481586364E-08,
  53. .coeff_curr_B = 0.124868806047484,
  54. // .coeff_curr_A = .001,
  55. // .coeff_curr_B = 0,
  56. };
  57. unsigned int vdac = 0;
  58. unsigned int cdac = 0;
  59. void dac_update(void){
  60. dac8571_set(cdac, CURR_DAC);
  61. dac8571_set(vdac, VOLT_DAC);
  62. }
  63. void set_voltage(float u){
  64. vdac = u * settings.coeff_generator_volt_A + settings.coeff_generator_volt_B;
  65. dac_update();
  66. }
  67. void set_charge_curr(float i){
  68. cdac = i * settings.coeff_generator_curr_A + settings.coeff_generator_curr_B;
  69. dac_update();
  70. }
  71. void set_discharge_curr(float i){
  72. cdac = i * settings.coeff_disch_curr_A + settings.coeff_disch_curr_B;
  73. dac_update();
  74. }
  75. #define CONV_ENABLE _BV(PD5)
  76. #define DISCH_ENABLE _BV(PD1)
  77. #define REL_K1 _BV(PD6)
  78. #define REL_K2 _BV(PD7)
  79. #define K1_ON() {PORTD |= REL_K1;}
  80. #define K1_OFF() {PORTD &= ~REL_K1;}
  81. #define K2_ON() {PORTD |= REL_K2;}
  82. #define K2_OFF() {PORTD &= ~REL_K2;}
  83. #define POWER_OFF() {PORTD &= ~(CONV_ENABLE | DISCH_ENABLE);}
  84. #define CONV_ON() {PORTD |= CONV_ENABLE;}
  85. #define DISCH_ON() {PORTD |= DISCH_ENABLE;}
  86. void output_off(void){
  87. cdac = 0;
  88. vdac = 0;
  89. POWER_OFF();
  90. dac_update();
  91. K1_OFF();
  92. K2_OFF();
  93. }
  94. void charge_on(void){
  95. output_off();
  96. K1_ON();
  97. CONV_ON();
  98. }
  99. void discharge_on(void){
  100. output_off();
  101. K2_ON();
  102. DISCH_ON();
  103. }
  104. #define MEAS_VOLT 0
  105. #define MEAS_CURR 1
  106. float meas[2];
  107. signed long int adc_results[2];
  108. unsigned char adc_process_data(void){
  109. static unsigned char adc_channel = AIN1;
  110. static signed long int adc;
  111. if(adc_data_ready){
  112. atomic_set_slint(&adc, adc_val);
  113. switch(adc_channel){
  114. case AIN1:
  115. adc_results[MEAS_VOLT] = adc;
  116. meas[MEAS_VOLT] = adc * settings.coeff_volt_A + settings.coeff_volt_B;
  117. adc_channel = AIN2;
  118. ads1224_mux(AIN2);
  119. break;
  120. case AIN2: default:
  121. adc_results[MEAS_CURR] = adc;
  122. meas[MEAS_CURR] = adc * settings.coeff_curr_A + settings.coeff_curr_B;
  123. adc_channel = AIN1;
  124. ads1224_mux(AIN1);
  125. break;
  126. }
  127. adc_data_ready = 0;
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. __flash const char text0[] = "stop";
  133. __flash const char text1[] = "\x80""aduj";
  134. __flash const char text2[] = "roz\x80""aduj";
  135. __flash const char text3[] = "reset";
  136. __flash const char text4[] = "Uset = ";
  137. __flash const char text5[] = "Iset = ";
  138. __flash const char *main_menu_texts[] = {
  139. text0,
  140. text1,
  141. text2,
  142. text3,
  143. text4,
  144. text5,
  145. };
  146. #define MENU_MAX 5
  147. #define MODE_STOP 0
  148. #define MODE_CHARGE 1
  149. #define MODE_DISCHARGE 2
  150. void main(void){
  151. unsigned char i=0;
  152. unsigned char pos=0;
  153. unsigned char k;
  154. unsigned char mode = MODE_STOP;
  155. unsigned int volt_set = 126, curr_set = 8; // *0,1V/A
  156. signed long long int charge_counter = 0, energy_counter = 0; // *0,1µC, *10µJ
  157. unsigned long int time_counter = 0; // *0,1s
  158. /* unsigned char state = STATE_DEFAULT;
  159. unsigned char oldstate = state;*/
  160. eeprom_read_block(&settings, &eep_settings, sizeof(struct settings));
  161. OCR2A = 100; // 100µs
  162. TCCR2A = _BV(WGM21);
  163. TCCR2B = _BV(CS21);
  164. TIMSK2 = _BV(OCIE2A);
  165. sei();
  166. PORTC &= _BV(PC7); // KEY
  167. DDRB |= _BV(PB2); // TESTPOINT
  168. DDRD |= REL_K1 | REL_K2 | DISCH_ENABLE | CONV_ENABLE;
  169. GLCD_Initialize();
  170. GLCD_Clear();
  171. ads1224_init();
  172. ads1224_mux(AIN1);
  173. /* charge_on();
  174. set_voltage(volt_set);
  175. set_charge_curr(curr_set);*/
  176. for(;;){
  177. /* if((i = getkey())){
  178. cursor(0,0);
  179. disp_num(i, 0);
  180. clearline();
  181. }*/
  182. dac_update();
  183. adc_process_data();
  184. /*if(adc_process_data()){
  185. cursor(0,1);
  186. disp_num(meas[MEAS_VOLT] * 1000, 3);
  187. putchar('V');
  188. clearline();
  189. cursor(0,2);
  190. disp_num(meas[MEAS_CURR] * 1000, 3);
  191. // disp_num(adc_results[MEAS_CURR], 0);
  192. putchar('A');
  193. clearline();
  194. }*/
  195. for(i=0; i<=MENU_MAX; i++){
  196. cursor(0, i);
  197. if(pos == i)
  198. putchar('*');
  199. else
  200. putchar(' ');
  201. putchar(' ');
  202. puttext_P(main_menu_texts[i]);
  203. putchar(' ');
  204. switch(i){
  205. default: break;
  206. case 1: if(mode == MODE_CHARGE) puttext_P(PSTR("w\x80.")); break;
  207. case 2: if(mode == MODE_DISCHARGE) puttext_P(PSTR("w\x80.")); break;
  208. case 4: disp_num(volt_set, 1); putchar('V'); break;
  209. case 5: disp_num(curr_set, 1); putchar('A'); break;
  210. }
  211. clearline();
  212. }
  213. k = getkey();
  214. switch(k){
  215. default: break;
  216. case KEY_UP: if(pos>0) pos--; break;
  217. case KEY_DOWN: if(pos<MENU_MAX) pos++; break;
  218. case KEY_LEFT:
  219. switch(pos){
  220. case 4:
  221. if(mode != MODE_STOP)
  222. break;
  223. if(volt_set > 0)
  224. volt_set -= 1;
  225. break;
  226. case 5:
  227. if(mode != MODE_STOP)
  228. break;
  229. if(curr_set > 0)
  230. curr_set -= 1;
  231. break;
  232. }
  233. break;
  234. case KEY_RIGHT:
  235. switch(pos){
  236. case 4:
  237. if(mode != MODE_STOP)
  238. break;
  239. if(volt_set < 200) // 20V
  240. volt_set += 1;
  241. break;
  242. case 5:
  243. if(mode != MODE_STOP)
  244. break;
  245. if(curr_set < 100)
  246. curr_set += 1; // 10A
  247. break;
  248. }
  249. break;
  250. case KEY_OK:
  251. switch(pos){
  252. case 0:
  253. mode = MODE_STOP;
  254. output_off();
  255. break;
  256. case 1:
  257. if(mode != MODE_STOP)
  258. break;
  259. mode = MODE_CHARGE;
  260. charge_on();
  261. set_voltage((float)volt_set/10.0);
  262. set_charge_curr((float)curr_set/10.0);
  263. break;
  264. case 2:
  265. break;
  266. case 3:
  267. if(mode != MODE_STOP)
  268. break;
  269. charge_counter = 0;
  270. energy_counter = 0;
  271. time_counter = 0;
  272. break;
  273. }
  274. break;
  275. }
  276. if(tick){
  277. cli();
  278. if(mode != MODE_STOP){
  279. charge_counter += meas[MEAS_CURR] * (float)tick / 10.0 / 0.1e-6;
  280. energy_counter += meas[MEAS_CURR] * meas[MEAS_VOLT] * (float)tick / 10.0 / 10e-6;
  281. time_counter += tick;
  282. }
  283. tick = 0;
  284. sei();
  285. cursor(0,6);
  286. disp_num(meas[MEAS_VOLT] * 1000, 3);
  287. putchar('V');
  288. putchar(' ');
  289. disp_num(meas[MEAS_CURR] * 1000, 3);
  290. putchar('A');
  291. clearline();
  292. cursor(0,7);
  293. disp_num(charge_counter * 0.1e-6 * 10000 / 3600.0, 4);
  294. puttext_P(PSTR("Ah "));
  295. disp_num(energy_counter * 10e-6 * 10000 / 3600.0, 4);
  296. puttext_P(PSTR("Wh "));
  297. disp_num(time_counter, 1);
  298. puttext_P(PSTR("s"));
  299. clearline();
  300. }
  301. }
  302. }
  303. ISR(TIMER2_COMPA_vect){ // 100µs
  304. static unsigned char pre;
  305. if(++pre < 10)
  306. return;
  307. pre = 0;
  308. // 1ms
  309. unsigned int *volatile ctimer;
  310. unsigned char i;
  311. for(i=0; i<sizeof(timers)/sizeof(unsigned int); i++){
  312. ctimer = ((unsigned int *)&timers) + i;
  313. if(*ctimer)
  314. (*ctimer)--;
  315. }
  316. if(timers.tick == 0){
  317. tick++; // 100ms
  318. timers.tick = 100;
  319. }
  320. }