main.c 8.6 KB

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