main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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,
  50. .coeff_volt_B = 0,
  51. .coeff_curr_A = 1,
  52. .coeff_curr_B = 0,
  53. };
  54. unsigned int vdac = 0;
  55. unsigned int cdac = 0;
  56. void dac_update(void){
  57. dac8571_set(cdac, CURR_DAC);
  58. dac8571_set(vdac, VOLT_DAC);
  59. }
  60. void set_voltage(float u){
  61. vdac = u * settings.coeff_generator_volt_A + settings.coeff_generator_volt_B;
  62. dac_update();
  63. }
  64. void set_charge_curr(float i){
  65. cdac = i * settings.coeff_generator_curr_A + settings.coeff_generator_curr_B;
  66. dac_update();
  67. }
  68. void set_discharge_curr(float i){
  69. cdac = i * settings.coeff_disch_curr_A + settings.coeff_disch_curr_B;
  70. dac_update();
  71. }
  72. #define CONV_ENABLE _BV(PD5)
  73. #define DISCH_ENABLE _BV(PD1)
  74. #define REL_K1 _BV(PD6)
  75. #define REL_K2 _BV(PD7)
  76. #define K1_ON() {PORTD |= REL_K1;}
  77. #define K1_OFF() {PORTD &= ~REL_K1;}
  78. #define K2_ON() {PORTD |= REL_K2;}
  79. #define K2_OFF() {PORTD &= ~REL_K2;}
  80. #define POWER_OFF() {PORTD &= ~(CONV_ENABLE | DISCH_ENABLE);}
  81. #define CONV_ON() {PORTD |= CONV_ENABLE;}
  82. #define DISCH_ON() {PORTD |= DISCH_ENABLE;}
  83. void output_off(void){
  84. cdac = 0;
  85. vdac = 0;
  86. POWER_OFF();
  87. dac_update();
  88. K1_OFF();
  89. K2_OFF();
  90. }
  91. void charge_on(void){
  92. output_off();
  93. K1_ON();
  94. CONV_ON();
  95. }
  96. void discharge_on(void){
  97. output_off();
  98. K2_ON();
  99. DISCH_ON();
  100. }
  101. void main(void){
  102. unsigned char i=0;
  103. static signed long int adc;
  104. /* unsigned char state = STATE_DEFAULT;
  105. unsigned char oldstate = state;*/
  106. eeprom_read_block(&settings, &eep_settings, sizeof(struct settings));
  107. OCR2A = 250; // 16ms
  108. TCCR2A = _BV(WGM21);
  109. TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
  110. TIMSK2 = _BV(OCIE2A);
  111. sei();
  112. PORTC &= _BV(PC7); // KEY
  113. DDRB |= _BV(PB2); // TESTPOINT
  114. DDRD |= REL_K1 | REL_K2 | DISCH_ENABLE | CONV_ENABLE;
  115. GLCD_Initialize();
  116. GLCD_Clear();
  117. ads1224_init();
  118. ads1224_mux(AIN1);
  119. charge_on();
  120. set_voltage(12.6);
  121. set_charge_curr(1.0);
  122. for(;;){
  123. _delay_ms(1);
  124. if((i = getkey())){
  125. cursor(0,0);
  126. disp_num(i, 0);
  127. clearline();
  128. }
  129. dac_update();
  130. if(adc_data_ready){
  131. adc_data_ready = 0;
  132. atomic_set_slint(&adc, adc_val);
  133. cursor(0,1);
  134. disp_num((adc * 1.55337778893448E-07 + -0.092784032021951) * 1000, 3);
  135. clearline();
  136. }
  137. // dac8571_set(++dac1, VOLT_DAC);
  138. // dac8571_set(++dac2, CURR_DAC);
  139. /* gettemp();
  140. control();
  141. state = state_processors[state](state);
  142. if(oldstate != state){
  143. GLCD_Clear();
  144. settings_update = 1;
  145. }
  146. oldstate = state;
  147. send_595(&led, 1);*/
  148. }
  149. }
  150. ISR(TIMER2_COMPA_vect){ // 16ms
  151. unsigned int *volatile ctimer;
  152. unsigned char i;
  153. for(i=0; i<sizeof(timers)/sizeof(unsigned int); i++){
  154. ctimer = ((unsigned int *)&timers) + i;
  155. if(*ctimer)
  156. (*ctimer)--;
  157. }
  158. }