123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/pgmspace.h>
- #include <avr/eeprom.h>
- #include "main.h"
- #include "1wire.h"
- #include "gtext.h"
- #include "sed1335.h"
- #include "ds18b20.h"
- #include "dac8571.h"
- #include "ads1224.h"
- #include "display.h"
- volatile struct timers timers;
- unsigned char getkey(void){
- static unsigned char old;
- unsigned char key;
- DDRA = 0;
- PORTA = 0xff;
- DDRC |= _BV(PC7);
- _delay_ms(2);
- key = ~PINA;
- DDRC &= ~_BV(PC7);
- PORTA = 0;
- DDRA = 0xff;
- if(key == old) return 0;
- old = key;
- return key;
- }
- struct settings {
- float coeff_generator_volt_A;
- float coeff_generator_volt_B;
- float coeff_generator_curr_A;
- float coeff_generator_curr_B;
- float coeff_disch_curr_A;
- float coeff_disch_curr_B;
- float coeff_volt_A;
- float coeff_volt_B;
- float coeff_curr_A;
- float coeff_curr_B;
- };
- struct settings settings;
- EEMEM struct settings eep_settings = {
- .coeff_generator_volt_A = 1597.035901,
- .coeff_generator_volt_B = 1317.235211,
- .coeff_generator_curr_A = 65535.0/20,
- .coeff_generator_curr_B = 1310,
- .coeff_disch_curr_A = 65535.0/20,
- .coeff_disch_curr_B = 1000,
- .coeff_volt_A = 1,
- .coeff_volt_B = 0,
- .coeff_curr_A = 1,
- .coeff_curr_B = 0,
- };
- unsigned int vdac = 0;
- unsigned int cdac = 0;
- void dac_update(void){
- dac8571_set(cdac, CURR_DAC);
- dac8571_set(vdac, VOLT_DAC);
- }
- void set_voltage(float u){
- vdac = u * settings.coeff_generator_volt_A + settings.coeff_generator_volt_B;
- dac_update();
- }
- void set_charge_curr(float i){
- cdac = i * settings.coeff_generator_curr_A + settings.coeff_generator_curr_B;
- dac_update();
- }
- void set_discharge_curr(float i){
- cdac = i * settings.coeff_disch_curr_A + settings.coeff_disch_curr_B;
- dac_update();
- }
- #define CONV_ENABLE _BV(PD5)
- #define DISCH_ENABLE _BV(PD1)
- #define REL_K1 _BV(PD6)
- #define REL_K2 _BV(PD7)
- #define K1_ON() {PORTD |= REL_K1;}
- #define K1_OFF() {PORTD &= ~REL_K1;}
- #define K2_ON() {PORTD |= REL_K2;}
- #define K2_OFF() {PORTD &= ~REL_K2;}
- #define POWER_OFF() {PORTD &= ~(CONV_ENABLE | DISCH_ENABLE);}
- #define CONV_ON() {PORTD |= CONV_ENABLE;}
- #define DISCH_ON() {PORTD |= DISCH_ENABLE;}
- void output_off(void){
- cdac = 0;
- vdac = 0;
- POWER_OFF();
- dac_update();
- K1_OFF();
- K2_OFF();
- }
- void charge_on(void){
- output_off();
- K1_ON();
- CONV_ON();
- }
- void discharge_on(void){
- output_off();
- K2_ON();
- DISCH_ON();
- }
- void main(void){
- unsigned char i=0;
- static signed long int adc;
- /* unsigned char state = STATE_DEFAULT;
- unsigned char oldstate = state;*/
-
- eeprom_read_block(&settings, &eep_settings, sizeof(struct settings));
-
- OCR2A = 250; // 16ms
- TCCR2A = _BV(WGM21);
- TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
- TIMSK2 = _BV(OCIE2A);
- sei();
- PORTC &= _BV(PC7); // KEY
- DDRB |= _BV(PB2); // TESTPOINT
-
- DDRD |= REL_K1 | REL_K2 | DISCH_ENABLE | CONV_ENABLE;
-
- GLCD_Initialize();
- GLCD_Clear();
-
- ads1224_init();
- ads1224_mux(AIN1);
-
- charge_on();
- set_voltage(12.6);
- set_charge_curr(1.0);
-
- for(;;){
- _delay_ms(1);
- if((i = getkey())){
- cursor(0,0);
- disp_num(i, 0);
- clearline();
- }
- dac_update();
-
- if(adc_data_ready){
- adc_data_ready = 0;
- atomic_set_slint(&adc, adc_val);
- cursor(0,1);
- disp_num((adc * 1.55337778893448E-07 + -0.092784032021951) * 1000, 3);
- clearline();
- }
- // dac8571_set(++dac1, VOLT_DAC);
- // dac8571_set(++dac2, CURR_DAC);
- /* gettemp();
- control();
- state = state_processors[state](state);
- if(oldstate != state){
- GLCD_Clear();
- settings_update = 1;
- }
- oldstate = state;
- send_595(&led, 1);*/
- }
- }
- ISR(TIMER2_COMPA_vect){ // 16ms
- unsigned int *volatile ctimer;
- unsigned char i;
-
- for(i=0; i<sizeof(timers)/sizeof(unsigned int); i++){
- ctimer = ((unsigned int *)&timers) + i;
- if(*ctimer)
- (*ctimer)--;
- }
- }
|