简体   繁体   中英

Is there a more ethical way of dong this?? Embedded in C - PIC16F18326 - MPLAB

there must be a better way of coding this, it all works but takes up alot of space, is there a more ethical way of doing it?

So to start with, my code basically flashes an led, it also has different speed setting that can be programmed via button presses.

So my program knows which mode it is in i have the following code which changes when the mode is changed, for example if changing to mode 5 from mode 1, M1 would = 0 and M5 would = 1 etc.

        M1 = 1; // We are in Mode 1
        M2 = 0;
        M3 = 0;
        M4 = 0;
        M5 = 0;
        M6 = 0;
        M7 = 0;
        M8 = 0;
        M9 = 0;
        M10 = 0;
        M11 = 0;
        M12 = 0;
        M13 = 0;
        M14 = 0;
        M15 = 0;
        M16 = 0;
        M17 = 0;
        M18 = 0;

Then when the speed is changed, the program would go through this chunk of code to save the speed to whatever mode it was in at the time. This way i can have different speeds saved to different modes. Again this is alot of code and im sure im over-complicating it. Is there a better way of doing this?

void Speeds(void)
{            
    if (M1 == 1) // if in mode 1 when changing speed
    {
        counter = S1; // save value to this mode only
    }

    if (M2 == 1) // if in mode 2 when changing speed
    {
        counter = S2; // save value to this mode only
    }

    if (M3 == 1) // if in mode 3 when changing speed
    {
        counter = S3; // save value to this mode only
    }

    if (M4 == 1) // if in mode 4 when changing speed
    {
        counter = S4; // save value to this mode only   
    }

    if (M5 == 1) // if in mode 5 when changing speed
    {
        counter = S5; // save value to this mode only   
    }

    if (M6 == 1) // if in mode 6 when changing speed
    {
        counter = S6; // save value to this mode only
    }

    if (M7 == 1) // if in mode 7 when changing speed
    {
        counter = S7; // save value to this mode only
    }

    if (M8 == 1) // if in mode 8 when changing speed
    {
        counter = S8; // save value to this mode only
    }

    if (M9 == 1) // if in mode 9 when changing speed
    {
        counter = S9; // save value to this mode only   
    }

    if (M10 == 1) // if in mode 10 when changing speed
    {
        counter = S10; // save value to this mode only   
    }

    if (M11 == 1) // if in mode 11 when changing speed
    {
        counter = S11; // save value to this mode only
    }

    if (M12 == 1) // if in mode 12 when changing speed
    {
        counter = S12; // save value to this mode only
    }

    if (M13 == 1) // if in mode 13 when changing speed
    {
        counter = S13; // save value to this mode only
    }

    if (M14 == 1) // if in mode 14 when changing speed
    {
        counter = S14; // save value to this mode only   
    }
}

Is it possible to change the representation of the mode that use one variable to represent different mode? For example:

Mode = 0  <=>  M1 = 1
Mode = 1  <=>  M2 = 1
Mode = 2  <=>  M3 = 1
....
Mode = 17  <=> M18 = 1

If it is possible, then the if-else statements in the Speeds() function can be replaced to the switch-case statement. Then the expression will become concise. For example:

switch(Mode){
    case 0:
      counter = S1; // save value to this mode only
      break;
    case 1:
      counter = S2; // save value to this mode only
      break;
     ...
    case 17:
      counter = S18; // save value to this mode only
      break;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM