main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. unsigned char getkey(void){
  15. static unsigned char old;
  16. unsigned char key;
  17. DDRA = 0;
  18. PORTA = 0xff;
  19. DDRC |= _BV(PC7);
  20. _delay_ms(2);
  21. key = ~PINA;
  22. DDRC &= ~_BV(PC7);
  23. PORTA = 0;
  24. DDRA = 0xff;
  25. if(key == old) return 0;
  26. old = key;
  27. return key;
  28. }
  29. struct settings {
  30. float coeff_generator_volt_A;
  31. float coeff_generator_volt_B;
  32. float coeff_generator_curr_A;
  33. float coeff_generator_curr_B;
  34. float coeff_disch_curr_A;
  35. float coeff_disch_curr_B;
  36. float coeff_volt_A;
  37. float coeff_volt_B;
  38. float coeff_curr_A;
  39. float coeff_curr_B;
  40. };
  41. struct settings settings;
  42. EEMEM struct settings eep_settings = {
  43. .coeff_generator_volt_A = 1597.035901,
  44. .coeff_generator_volt_B = 1317.235211,
  45. .coeff_generator_curr_A = 65535.0/20,
  46. .coeff_generator_curr_B = 1310,
  47. .coeff_disch_curr_A = 65535.0/20,
  48. .coeff_disch_curr_B = 1000,
  49. .coeff_volt_A = 1.55337778893448E-07,
  50. .coeff_volt_B = -0.092784032021951,
  51. .coeff_curr_A = 9.03353481586364E-08,
  52. .coeff_curr_B = -0.124868806047484,
  53. // .coeff_curr_A = .001,
  54. // .coeff_curr_B = 0,
  55. };
  56. unsigned int vdac = 0;
  57. unsigned int cdac = 0;
  58. void dac_update(void){
  59. dac8571_set(cdac, CURR_DAC);
  60. dac8571_set(vdac, VOLT_DAC);
  61. }
  62. void set_voltage(float u){
  63. vdac = u * settings.coeff_generator_volt_A + settings.coeff_generator_volt_B;
  64. dac_update();
  65. }
  66. void set_charge_curr(float i){
  67. cdac = i * settings.coeff_generator_curr_A + settings.coeff_generator_curr_B;
  68. dac_update();
  69. }
  70. void set_discharge_curr(float i){
  71. cdac = i * settings.coeff_disch_curr_A + settings.coeff_disch_curr_B;
  72. dac_update();
  73. }
  74. #define CONV_ENABLE _BV(PD5)
  75. #define DISCH_ENABLE _BV(PD1)
  76. #define REL_K1 _BV(PD6)
  77. #define REL_K2 _BV(PD7)
  78. #define K1_ON() {PORTD |= REL_K1;}
  79. #define K1_OFF() {PORTD &= ~REL_K1;}
  80. #define K2_ON() {PORTD |= REL_K2;}
  81. #define K2_OFF() {PORTD &= ~REL_K2;}
  82. #define POWER_OFF() {PORTD &= ~(CONV_ENABLE | DISCH_ENABLE);}
  83. #define CONV_ON() {PORTD |= CONV_ENABLE;}
  84. #define DISCH_ON() {PORTD |= DISCH_ENABLE;}
  85. void output_off(void){
  86. cdac = 0;
  87. vdac = 0;
  88. POWER_OFF();
  89. dac_update();
  90. K1_OFF();
  91. K2_OFF();
  92. }
  93. void charge_on(void){
  94. output_off();
  95. K1_ON();
  96. CONV_ON();
  97. }
  98. void discharge_on(void){
  99. output_off();
  100. K2_ON();
  101. DISCH_ON();
  102. }
  103. #define MEAS_VOLT 0
  104. #define MEAS_CURR 1
  105. float meas[2];
  106. signed long int adc_results[2];
  107. unsigned char adc_process_data(void){
  108. static unsigned char adc_channel = AIN1;
  109. static signed long int adc;
  110. if(adc_data_ready){
  111. atomic_set_slint(&adc, adc_val);
  112. switch(adc_channel){
  113. case AIN1:
  114. adc_results[MEAS_VOLT] = adc;
  115. meas[MEAS_VOLT] = adc * settings.coeff_volt_A + settings.coeff_volt_B;
  116. adc_channel = AIN2;
  117. ads1224_mux(AIN2);
  118. break;
  119. case AIN2: default:
  120. adc_results[MEAS_CURR] = adc;
  121. meas[MEAS_CURR] = adc * settings.coeff_curr_A + settings.coeff_curr_B;
  122. adc_channel = AIN1;
  123. ads1224_mux(AIN1);
  124. break;
  125. }
  126. adc_data_ready = 0;
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. void main(void){
  132. unsigned char i=0;
  133. /* unsigned char state = STATE_DEFAULT;
  134. unsigned char oldstate = state;*/
  135. eeprom_read_block(&settings, &eep_settings, sizeof(struct settings));
  136. OCR2A = 250; // 16ms
  137. TCCR2A = _BV(WGM21);
  138. TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
  139. TIMSK2 = _BV(OCIE2A);
  140. sei();
  141. PORTC &= _BV(PC7); // KEY
  142. DDRB |= _BV(PB2); // TESTPOINT
  143. DDRD |= REL_K1 | REL_K2 | DISCH_ENABLE | CONV_ENABLE;
  144. GLCD_Initialize();
  145. GLCD_Clear();
  146. ads1224_init();
  147. ads1224_mux(AIN1);
  148. charge_on();
  149. set_voltage(12.6);
  150. set_charge_curr(.8);
  151. for(;;){
  152. _delay_ms(1);
  153. if((i = getkey())){
  154. cursor(0,0);
  155. disp_num(i, 0);
  156. clearline();
  157. }
  158. dac_update();
  159. if(adc_process_data()){
  160. cursor(0,1);
  161. disp_num(meas[MEAS_VOLT] * 1000, 3);
  162. putchar('V');
  163. clearline();
  164. cursor(0,2);
  165. disp_num(meas[MEAS_CURR] * 1000, 3);
  166. // disp_num(adc_results[MEAS_CURR], 0);
  167. putchar('A');
  168. clearline();
  169. }
  170. // dac8571_set(++dac1, VOLT_DAC);
  171. // dac8571_set(++dac2, CURR_DAC);
  172. /* gettemp();
  173. control();
  174. state = state_processors[state](state);
  175. if(oldstate != state){
  176. GLCD_Clear();
  177. settings_update = 1;
  178. }
  179. oldstate = state;
  180. send_595(&led, 1);*/
  181. }
  182. }
  183. ISR(TIMER2_COMPA_vect){ // 16ms
  184. unsigned int *volatile ctimer;
  185. unsigned char i;
  186. for(i=0; i<sizeof(timers)/sizeof(unsigned int); i++){
  187. ctimer = ((unsigned int *)&timers) + i;
  188. if(*ctimer)
  189. (*ctimer)--;
  190. }
  191. }