]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
Added Current Offset
authorOle Artz <ole.artz@t-online.de>
Tue, 28 Jul 2020 14:02:36 +0000 (16:02 +0200)
committerOle Artz <ole.artz@t-online.de>
Tue, 28 Jul 2020 14:02:36 +0000 (16:02 +0200)
atmega32u4/dcdc_rich/README.md
atmega32u4/dcdc_rich/dcdc.pl
atmega32u4/dcdc_rich/main.c

index 6e345b8d9209d73cc45e2b9088fbe48e98b5d16a..e1461ed6de3362d21c0d60b684c1a5b7b6f49dd2 100644 (file)
@@ -32,7 +32,7 @@ ________________________________________________________________________________
 |   3       |  Current C_in                                            |
 |   4       |  Temperature                                             |
 |   5       |  [15:4] Firmware; [3:2] reserved; [1] Switch; [0] LED    |
-
+|   6       |  Current Offst                                           |
 ______________________________________________________________________________________________________________
 
 ## Protokoll definition
index 0c2c145c022b5b56b8307185ab37430a0af50e59..577baa1259953ff872a845ec8b4d5b5e5836c9a1 100755 (executable)
@@ -25,7 +25,7 @@ my $port;
 my $help;
 my $ser_dev;
 my $isTrbNet = 0;
-Getopt::Long::Configure(qw(gnu_getopt));
+Getopt::Long::Configure(qw(gnu_getopt pass_through));
 GetOptions(
            'help|h' => \$help,
            'device|d=s' => \$ser_dev,
@@ -64,7 +64,7 @@ sub PrintAnswerNice {
     my $ch   = hex(substr($s,4,1));
     my $uC   = hex(substr($s,1,2));
     my $answ = hex(substr($s,6));
-
+    print $s."\n";
     print "-----------------------------------------------\n";
     print "Board:   ".$uC."\t";
     print "Channel: ".$ch."\n";
@@ -97,6 +97,13 @@ sub PrintAnswerNice {
         print "SEL status : ".(($answ>>1) & 0x1)."\n";
         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);
+    }
+    
+    
     print "\n";
 
 }
@@ -120,7 +127,7 @@ sub SendCmdShort {
         }
         my $rw = "R";
         my $val = 0x0000;
-        my $cmd = sprintf("%s%02x0%01x%01x%04x",$rw,$uC,$ch,$reg,$val);
+        my $cmd = sprintf("%s%02x0%01x%01x%04x",$rw,$uC,$ch,$reg,$val&0xFFFF);
         #print $cmd."\n";
         PrintAnswerNice(Cmd($cmd)) #Answer without \n
     } else {
@@ -254,7 +261,7 @@ if ($mode == 1) {
         # Register
         $reg = $ARGV[4];
         if (substr($reg,0,2) eq "0x") {$reg= hex(substr($reg,2));}
-        if ($reg >= 6) {
+        if ($reg >= 7) {
             die "This register does not exist\n";
         }
 
@@ -268,7 +275,9 @@ if ($mode == 1) {
         if ($val > 0xFFFF) {
             die "The value is greater than 16 bit! Only 16 bit are allowed\n";
         }
-        my $cmd = sprintf("%s%02x0%01x%01x%04x",$rw,$uC,$ch,$reg,$val);
+        if($reg == 6) {$val *= 16;}
+        
+        my $cmd = sprintf("%s%02x0%01x%01x%04x",$rw,$uC,$ch,$reg,$val&0xffff);
         print $cmd."\n";
         PrintAnswerNice(Cmd($cmd)) #Answer without \n
     } else {
index 4e2a520b6c1861fc9234eed7e2c9926889f515de..ba4911f65386d6a85e952849532d297f653af456 100644 (file)
@@ -42,6 +42,7 @@ uint16_t adc_curr = 0;
 uint16_t adc_temp = 0;
 uint16_t information = 1;
 uint8_t  isMaster = 0; 
+int8_t curr_offset = 0;
 
 uint8_t nib_to_hex(uint16_t in, uint8_t nib) {
   uint8_t t = (in >> (nib*4)) & 0xF;
@@ -223,6 +224,7 @@ uint16_t read_ADC(void) {
  *     3       |  Current C_in
  *     4       |  Temperature
  *     5       |  [15:4] Firmware; [3:2] reserved; [1] Switch ; [0] LED
+ *     6       |  Current Offset
  ***/
 
 void getdata(uint8_t buf) {
@@ -280,6 +282,13 @@ void getdata(uint8_t buf) {
                   send_answer_hex(&rxbuf[0],hex_to_int(rxbuf[9]));
                   break;
 
+          // set current offset
+          case 6: curr_offset = (hex_to_int(rxbuf[7])*16+hex_to_int(rxbuf[8]))&0xFF;
+                  eeprom_update_byte((uint8_t*)(0x20+hex_to_int(rxbuf[4])),curr_offset);                 
+                  send_answer_hex(&rxbuf[0],curr_offset*16);
+                  
+                  break;
+
           // set Infos register
           case 5: setInfo(hex_to_int(rxbuf[4]),/* hex_to_int(rxbuf[6])*4096
                                                 +hex_to_int(rxbuf[7])*256
@@ -317,7 +326,6 @@ void getdata(uint8_t buf) {
         if (hex_to_int(rxbuf[5]) == 2) {
           send_answer_hex(&rxbuf[0],adc_volt);
         }
-
         // get current C_in
         if (hex_to_int(rxbuf[5]) == 3) {
           send_answer_hex(&rxbuf[0],adc_curr);
@@ -333,6 +341,11 @@ void getdata(uint8_t buf) {
           information |= (FIRMWARE_VERSION<<4);
           send_answer_hex(&rxbuf[0],information);
         }
+
+        // get current offset
+        if (hex_to_int(rxbuf[5]) == 6) {
+          send_answer_hex(&rxbuf[0],curr_offset*16);
+        }        
       }
     }
   }
@@ -442,6 +455,8 @@ __attribute__((naked)) int main(void) {
   dcdc_settings[3][2] = (eeprom_value >> 2)& 0x1;
   dcdc_settings[3][3] = (eeprom_value >> 3)& 0x1;
 
+  curr_offset = eeprom_read_byte((uint8_t*)0x26);
+
   setVoltages();
   SHIFT_EN_OUTPUT(); // Enable output of shift register to mosfet
   sei();
@@ -460,7 +475,7 @@ __attribute__((naked)) int main(void) {
       //~330us for each conversion
       if (ADC_State == 3) {adc_temp = read_ADC(); ADC_State = 0;}
       if (ADC_State == 2) {adc_volt = read_ADC(); ADC_State = 3;}
-      if (ADC_State == 1) {adc_curr = read_ADC(); ADC_State = 2;}
+      if (ADC_State == 1) {adc_curr = read_ADC() + curr_offset*16; ADC_State = 2;}
       if (ADC_State == 0) ADC_State = 1;
 
       conversion_ADC(ADC_State - 1);