Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Reducing RAM & ROM consumption in coding

Mithun_K_Das

Advanced Member level 3
Joined
Apr 24, 2010
Messages
849
Helped
23
Reputation
46
Reaction score
21
Trophy points
1,308
Location
Dhaka, Bangladesh, Bangladesh
Activity points
7,909
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.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
46,757
Helped
13,880
Reputation
28,008
Reaction score
12,519
Trophy points
1,393
Location
Bochum, Germany
Activity points
272,889
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.
 

Mithun_K_Das

Advanced Member level 3
Joined
Apr 24, 2010
Messages
849
Helped
23
Reputation
46
Reaction score
21
Trophy points
1,308
Location
Dhaka, Bangladesh, Bangladesh
Activity points
7,909
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.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
46,757
Helped
13,880
Reputation
28,008
Reaction score
12,519
Trophy points
1,393
Location
Bochum, Germany
Activity points
272,889
Of course you need to take care of the number range. Under circumstances, the multiplicand must be casted to long before multiplying with 62.
 
Toggle Sidebar

Part and Inventory Search


Welcome to EDABoard.com

Sponsor

Sponsor

Design Fast


×
Top