[PIC] Minimize the function

Status
Not open for further replies.

ajit_nayak87

Member level 5
Joined
Oct 30, 2017
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
981
I have the code here. I have 8 relay 8 combination where i could adjust 8 combination of Ontime and off time of device. I have key Select and advance function. Select key to decrement the Ontime of hour and advance key to increment the value.

How can i minimize the function . so i could able to write in 8 Relay 8 combination . these value are going to save in external eeprom 24LC08.
below code written for Relay1 with combination 1

Code:
const unsigned char DISPTABLE[30] = {0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09,0x09,0x09,0x03,
 //0 //1 //2 //3 //4 //5 //6 //7 //8 //9 //10 //11 //12
 0x03,0x03,0X31,0xFF,0X31,0X85,0X11,0X55,0X89,0XF5,0XA9,0X91,0X61,0X41 };
 //13 //14 //15 //16 //P //D //A //M //Y //r //W //H //C //G
 
unsigned char Hour_bit=0;
signed char Hour_ON1;
signed char Min_ON1;
signed char Hour_OFF1;
signed char MIN_OFF1;
signed char YEAR_SET=20;
unsigned char MONTH_SET=1;
unsigned char Day_SET=1;
unsigned char HOUR_SET=0;
unsigned char MIN_SET=0;
unsigned char SEC_SET=0;
unsigned char WK_DAY_SET=0;
unsigned char RLY_PATTERN=0;
unsigned char BLINKFLAG=0;

void Process_advance_SW() {
  unsigned char Temp = 0;
  if (pgm_run == 1) {
    if (SELECTPRESS == 0) {
      if ((Rly_No == 1) && (Comb_No == 1) && (on_offl == 1) && (Hour_bit == 1)) {
        Hour_ON1++;
        if (Hour_ON1 > 24) {
          Hour_ON1 = 0;
        }

        LEDBuffer_1[1] = DISPTABLE[Hour_ON1 / 10];
        LEDBuffer_1[0] = DISPTABLE[Hour_ON1 % 10];

      } else if ((Rly_No == 1) && (Comb_No == 1) && (on_offl == 1) && (Hour_bit == 0)) {
        Min_ON1++;
        if (Min_ON1 > 59) {
          Min_ON1 = 0;
        }
        LEDBuffer_1[1] = DISPTABLE[Min_ON1 / 10];
        LEDBuffer_1[0] = DISPTABLE[Min_ON1 % 10];

      } else if ((Rly_No == 1) && (Comb_No == 1) && (on_offl == 0) && (Hour_bit == 1)) {
        Hour_OFF1++;
        if (Hour_OFF1 > 24) {
          Hour_OFF1 = 0;
        }
        LEDBuffer_1[1] = DISPTABLE[Hour_OFF1 / 10];
        LEDBuffer_1[0] = DISPTABLE[Hour_OFF1 % 10];

      }
      if ((Rly_No == 1) && (Comb_No == 1) && (on_offl == 0) && (Hour_bit == 0)) {
        MIN_OFF1++;
        if (MIN_OFF1 > 59) {
          MIN_OFF1 = 0;
        }
        LEDBuffer_1[1] = DISPTABLE[MIN_OFF1 / 10];
        LEDBuffer_1[0] = DISPTABLE[MIN_OFF1 % 10];
      }

      DATE_TIME_SET_ADD();
    }
  }
}

void Process_select_SW() {
  if (pgm_run == 1) {
    if (SELECTPRESS == 0) {
      if ((Rly_No == 1) && (Comb_No == 1) && (on_offl == 1) && (Hour_bit == 1)) {
        Hour_ON1--;
        if (Hour_ON1 < 1) {
          Hour_ON1 = 24;
        }
        LEDBuffer_1[1] = DISPTABLE[Hour_ON1 / 10];
        LEDBuffer_1[0] = DISPTABLE[Hour_ON1 % 10];

      } else if ((Rly_No == 1) && (Comb_No == 1) && (on_offl == 1) && (Hour_bit == 0)) {
        Min_ON1--;
        if (Min_ON1 < 1) {
          Min_ON1 = 59;
        }
        LEDBuffer_1[1] = DISPTABLE[Min_ON1 / 10];
        LEDBuffer_1[0] = DISPTABLE[Min_ON1 % 10];

      } else if ((Rly_No == 1) && (Comb_No == 1) && (on_offl == 0) && (Hour_bit == 1)) {
        Hour_OFF1--;
        if (Hour_OFF1 < 1) {
          Hour_OFF1 = 24;
        }
        LEDBuffer_1[1] = DISPTABLE[Hour_OFF1 / 10];
        LEDBuffer_1[0] = DISPTABLE[Hour_OFF1 % 10];

      }
      if ((Rly_No == 1) && (Comb_No == 1) && (on_offl == 0) && (Hour_bit == 0)) {
        MIN_OFF1--;
        if (MIN_OFF1 < 1) {
          MIN_OFF1 = 59;
        }
        LEDBuffer_1[1] = DISPTABLE[MIN_OFF1 / 10];
        LEDBuffer_1[0] = DISPTABLE[MIN_OFF1 % 10];
      }

      DATE_TIME_SET_SUB();
    }

  }

}
 

Most of the code is missing so it is difficult to see exactly how it interacts with what you show.
My first thought is to minimize the timing by converting hours and minutes into a single minutes figure (0 - 14399) by multiplying hours by 60 and adding the minutes. That gives you a common timing element that is easy to store in the EEPROM. Then set up an array of start and finish times, something like 'start_times[8]' and 'end_times[8]' to hold the individual relay operating times. You can then check the time value against the array values in a loop to see if the relays should be activated or not.

Setting the start/end values is then reduced to increasing or decreasing the values stored in the array. You can use a simple function to display the values in hours and minutes from the clock or any of the start/end times:

void show HHMM(int TimeValue)
{
LEDBuffer_1[1] = DISPTABLE[TimeValue / 60];
LEDBuffer_1[0] = DISPTABLE[TimeValue % 60];
}

Or something similar, depending on how your display routines work.

Brian.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…