From: Jan Michel Date: Tue, 11 Oct 2022 15:20:45 +0000 (+0200) Subject: couple update to make MDC DCDC ADC work X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=e333eb7bed7a96b797f40ffd91eed435b75ad289;p=avr.git couple update to make MDC DCDC ADC work --- diff --git a/atmega32u4/dcdc_mdc/README.md b/atmega32u4/dcdc_mdc/README.md index 94e51c0..5c0165e 100644 --- a/atmega32u4/dcdc_mdc/README.md +++ b/atmega32u4/dcdc_mdc/README.md @@ -49,18 +49,19 @@ Each group has two channels (0, 1) ## Register definition -| Registers | Description | -|---------------|---------------------------------------------------------------| -| 0 | DCDC (Group) ON/OFF | -| 1 | DCDC (Channel) set voltage adjustment resistors | -| 2 | Voltage V_in (RO) | -| 3 | Current C_in (RO) | -| 4 | Temperature (DCDC 2/Group 3) (RO) | -| 5 | [15:4] Firmware; [3:2] reserved; [1] Switch ; [0] LED (RO) | -| 6 | Current Offset | -| 7 | Voltage V_out (RO) | -| 8 | Current C_out (NA) | -| 9 | Sense GND (RO) | +| Registers | Description | Name | +|---------------|---------------------------------------------------------------|---------| +| 0 | DCDC (Group) ON/OFF | switch | +| 1 | DCDC (Channel) set voltage adjustment resistors | adjust | +| 2 | Voltage V_in (RO) | Vin | +| 3 | Current C_in (RO) | Cin | +| 4 | Temperature (DCDC 2/Group 3) (RO) | temp | +| 5 | [15:4] Firmware; [3:2] reserved; [1] Switch ; [0] LED (RO) | info | +| 6 | Current Offset | Ioffset| +| 7 | Voltage V_out (RO) | Vout | +| 8 | Current C_out (NA) | | +| 9 | Sense GND (NA) | | + NA := not available RO := read only diff --git a/atmega32u4/dcdc_mdc/main.c b/atmega32u4/dcdc_mdc/main.c index d9c40d1..1cc5fdc 100644 --- a/atmega32u4/dcdc_mdc/main.c +++ b/atmega32u4/dcdc_mdc/main.c @@ -27,6 +27,8 @@ #include #include +void getdata(uint8_t buf); + /* //ADC // 0 Sense1 Vin @@ -306,21 +308,26 @@ void setInfo(uint8_t chan, uint8_t val) { ISR(ADC_vect) { // interupt for reading adc and save the values static uint8_t channel = 0; - adc[channel] = ADC; + static uint8_t dummyread = 1; + + if (dummyread == 1) { + adc[channel] = ADC; - if (channel == 1) channel = 4; - else if (channel == 7) channel = 13; - else if (channel == 13) channel = 0; - else channel++; + if (channel == 1) channel = 4; + else if (channel == 7) channel = 10; + else if (channel == 11) channel = 0; + else channel++; + } ADMUX &= 0xe0; - ADMUX |= (channel & 0xf); - if (channel == 13) + ADMUX |= (channel & 0x7); + if (channel >= 10) ADCSRB |= (1 << MUX5); else ADCSRB &= ~(1 << MUX5); ADCSRA |= (1 << ADSC); + dummyread = 1 - dummyread; } ISR(USART1_RX_vect) { @@ -474,13 +481,13 @@ void getdata(uint8_t buf) { // get voltage V_in if (hex_to_int(rxbuf[5]) == 2) { - V_in = (5 * adc[0]) / 2; + V_in = (adc[0]); send_answer_hex(&rxbuf[0], V_in); } // get current C_in if (hex_to_int(rxbuf[5]) == 3) { - C_in = (5 * adc[11]) / 2; + C_in = (adc[11]); send_answer_hex(&rxbuf[0], C_in); } @@ -506,22 +513,22 @@ void getdata(uint8_t buf) { // DCDC 0 if (hex_to_int(rxbuf[3]) == 0) { - V_out = (5 * adc[10]) / 2; // 2.5 (5/2) stepsize of adc + V_out = (adc[10]); // 2.5mV/LSB * 0.5 divider send_answer_hex(&rxbuf[0], V_out); } // DCDC 1 if (hex_to_int(rxbuf[3]) == 1) { - V_out = (5 * adc[7]) / 2; + V_out = (adc[7]); send_answer_hex(&rxbuf[0], V_out); } // DCDC 2 if (hex_to_int(rxbuf[3]) == 2) { - V_out = (5 * adc[6]) / 2; + V_out = (adc[6]); send_answer_hex(&rxbuf[0], V_out); } // DCDC 3 if (hex_to_int(rxbuf[3]) == 3) { - V_out = (5 * adc[4]) / 2; + V_out = (adc[4]); send_answer_hex(&rxbuf[0], V_out); } } @@ -599,11 +606,11 @@ __attribute__((naked)) void main(void) { _delay_ms(10); UCSR1B |= (1 << RXEN1); - // ADMUX = (3 << REFS0); //reference 2.56V internal - // ADCSRA = (1 << ADEN) | (0 << ADSC) | (1 << ADIE) | (7 << ADPS0); - // //enable, start, irq, /128 DIDR0 = 0b11111111; DIDR2 = (1 << ADC13D); - // ADMUX &= 0xe0; ADMUX |= 1; ADCSRB |= (1< 3; $SEL_channel = 0 unless defined $SEL_channel && $SEL_channel =~ /^\d$/; @@ -123,21 +123,18 @@ sub PrintAnswerNice { if ($command == 1) { # resistor adjustment values print "Level selected: "; - print (($answ>> 0)&0xF)."\n"; + print (($answ&0xF)."\n"); } if ($command == 2) { # Voltage - $answ = $answ >> 4; - my $calc = (($answ&0x7FF)-(($answ>>11)&0x1)*2048)*$FSR*11;# *11 to calculate real input value from voltage divider + my $calc = ($answ&0xFFFF)*417/27*2.5/1000; printf "measured Voltage @ Input : %.3f Volt (RAW: 0x%x)\n", ($calc) , ($answ); } if ($command == 3) { # Current - $answ = $answ >> 4; - my $calc = (($answ&0x7FF)-(($answ>>11)&0x1)*2048)*$FSR*2; # 500mV/A -> Thats why multiplied by 2 + my $calc = ($answ&0xFFF)/800.*2.5; # 800mV/A printf "measured Current @ Input : %.3f Ampere (RAW: 0x%x)\n", ($calc) , ($answ); } if ($command == 4) { # Temperature - $answ = $answ >> 4; - my $calc = (($answ&0x7FF)-(($answ>>11)&0x1)*2048)*0.125; + my $calc = ((($answ&0x7FF)*2-250)/9.5)+25; printf "measured temperature : %.2f°C (RAW: 0x%x)\n", ($calc) , ($answ); } if ($command == 5) { #Infos @@ -146,11 +143,14 @@ sub PrintAnswerNice { print "LED status : ".($answ & 0x1)."\n"; } if ($command == 6) { #Current Offset - $answ = $answ >> 4; my $calc = (($answ&0x7F)-(($answ>>7)&0x1)*128)*$FSR*2; # 500mV/A -> Thats why multiplied by 2 printf "set Current offset : %.3f Ampere (RAW: 0x%x)\n", ($calc) , ($answ); } - + if ($command == 7) { # Voltage + + my $calc = (($answ&0xFFFF)*2*2.5/1000);#-(($answ>>11)&0x1)*2048)*$FSR*11;# *11 to calculate real input value from voltage divider + printf "measured Voltage @ Input : %.3f Volt (RAW: 0x%x)\n", ($calc) , ($answ); + } print "\n";