+ Post New Thread
Results 1 to 4 of 4
  1. #1
    Advanced Member level 3
    Points: 5,484, Level: 17
    Achievements:
    Created Blog entry 7 years registered
    Mithun_K_Das's Avatar
    Join Date
    Apr 2010
    Location
    Dhaka, Bangladesh, Bangladesh
    Posts
    838
    Helped
    22 / 22
    Points
    5,484
    Level
    17
    Blog Entries
    3

    Reducing RAM & ROM consumption in coding

    How can I rewrite this code to use less RAM & ROM?
    ADC: 10bit. Speed: 1MSPS.
    Data input range: 0-5V.
    CPU: DSPIC30F2010; 16bit.
    This code is working fine. But consuming 25% of total ROM & 40% RAM. I need suggestion to rewrite this code so that I can save some RAM & ROM.
    I need at-least 20 samples to get the best result. Signal is both AC & DC. Other programs running in interrupt of 64uS which takes around 16us to complete.

    Code:
    //sensing
    /******************************************************************************/
    unsigned int input_voltage_sample[21];
    unsigned int output_voltage_sample[21];
    unsigned int battery_voltage_sample[21];
    unsigned int ampere_sample[21];
    unsigned data int input_voltage_sample_old=0;
    unsigned data int in_avg_value=0,in_raw_value=0;
    unsigned data int out_avg_value=0,out_raw_value=0;
    unsigned data int curr_avg_value=0,curr_raw_value=0;
    unsigned data int in_temp=0,in_max=0,out_temp=0,out_max=0,curr_temp=0,curr_max=0;
    unsigned data int batt_avg_val=0;
    int sample_step_cnt=0;
    int adc_sample_cnt=0;
    /******************************************************************************/
    void input_fault_check()
    {
       if(input_voltage_sample[sample_step_cnt]<(input_voltage_sample_old-10) || input_voltage<170)input_down_bit=1;
       else
       {
          if(input_voltage>170) input_down_bit=0;
       }
       input_voltage_sample_old=input_voltage_sample[sample_step_cnt];
    }
    /******************************************************************************/
    void read_adcs(void)
    {
      input_voltage_sample[sample_step_cnt] = ADC1_Get_Sample(2);
      output_voltage_sample[sample_step_cnt] = ADC1_Get_Sample(3);
      battery_voltage_sample[sample_step_cnt] = ADC1_Get_Sample(0);
      ampere_sample[sample_step_cnt] = ADC1_Get_Sample(4);
      input_fault_check();
      
      
      sample_step_cnt++;
      if(sample_step_cnt>20)
      {
         sample_step_cnt=0;
         //extract input voltage
         in_temp=0;
         in_max=0;
         out_temp=0;
         out_max=0;
         curr_temp=0;
         curr_max=0;
         batt_avg_val=0;
         
         
         ac_calibration = EEPROM_Read(address1);
         output_calibration = EEPROM_Read(address3);
         amp_calibration = EEPROM_Read(address5);
         
         for(k=0;k<10;k++)
         {
           //input max finding
           if(in_temp=input_voltage_sample[k],in_temp>in_max)
           {
              in_max = in_temp;
           }
           //output max finding
           if(out_temp=output_voltage_sample[k],out_temp>out_max)
           {
              out_max = out_temp;
           }
           //current max finding
           if(curr_temp=ampere_sample[k],curr_temp>curr_max)
           {
              curr_max = curr_temp;
           }
           //battery avg finding
           bat_calibration = EEPROM_Read(address2);
           batt_avg_val+= battery_voltage_sample[k]*(0.3+bat_calibration*0.005);;
         }
         
         in_avg_value+=(in_max-270)*(0.62+ac_calibration*0.01);
         out_avg_value+=(out_max-270)*(0.62+output_calibration*0.01);
         curr_avg_value+=(curr_max-270)*(0.62+amp_calibration*0.01);
    
         //calculate battery
         battery=batt_avg_val/20;
         bat_read_ok=1;
    
    
         adc_sample_cnt++;
         if(adc_sample_cnt>5)
         {
            adc_sample_cnt=0;
    
            input_voltage=in_avg_value/5;
            input_voltage_read_ok=1;//read bit set
            in_avg_value=0;
            if(input_voltage<100)input_voltage=0;
            if(input_voltage>170 && input_voltage<250)mains_status=1; //mains detection level
            else mains_status=0;
    
            output_voltage = out_avg_value/5;
            feedback_voltage_read_ok=1;
            out_avg_value=0;
    
            current = curr_avg_value/5;
            curr_avg_value=0;
            current_read_ok=1;
         }
      }
    }
    Thanks in advance.
    I love my profession, I love my M's Lab. __MKDas

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 264,047, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,141
    Helped
    14034 / 14034
    Points
    264,047
    Level
    100

    Re: Reducing RAM & ROM consumption in coding

    You have implemented float math for a few calculations that can also use integer math. Dropping the float library will save a lot of ROM, unless you need it otherwhere. dsPIC30F2010 has little RAM (only 512 bytes), if you want more data storage, you'll refer to a different chip.


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  3. #3
    Advanced Member level 3
    Points: 5,484, Level: 17
    Achievements:
    Created Blog entry 7 years registered
    Mithun_K_Das's Avatar
    Join Date
    Apr 2010
    Location
    Dhaka, Bangladesh, Bangladesh
    Posts
    838
    Helped
    22 / 22
    Points
    5,484
    Level
    17
    Blog Entries
    3

    Re: Reducing RAM & ROM consumption in coding

    That means if I write 62/100 instead of 0.62 (if use integer math) it will consume less ROM&RAM. Yes! I changed it and it saved a lot of RAM & ROM.
    I love my profession, I love my M's Lab. __MKDas



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 264,047, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,141
    Helped
    14034 / 14034
    Points
    264,047
    Level
    100

    Re: Reducing RAM & ROM consumption in coding

    Of course you need to take care of the number range. Under circumstances, the multiplicand must be casted to long before multiplying with 62.


    1 members found this post helpful.

--[[ ]]--