switch statement for pins

Discussion in 'Embedded Systems and Microcontrollers' started by Chalma, Feb 11, 2014.

  1. Chalma

    Thread Starter Member

    May 19, 2013
    54
    2
    Greetings again,

    Still working on my same projects (from old posts). I'm sure it's due to my lack of experience but here goes the post.

    I have boards I am programming that contain 4 batteries each, I have a simple pushbutton for each battery and this button would do different things upon being pressed. no press (default mode) battery does nothing, 1 press and the battery charges, 2 presses and the battery discharges.
    Code ( (Unknown Language)):
    1.  :code:
    2. //------------------------------------------------------------------------------
    3. // Port 0  bit definitions
    4. //------------------------------------------------------------------------------
    5.  
    6. sbit    CEX0 =         P0^4;        // PWM Output 0
    7. sbit    CEX1 =         P0^5;        // PWM Output 1
    8. sbit    CEX2 =         P0^6;        // PWM Output 2
    9. sbit    CEX3 =         P0^7;        // PWM Output 3
    10.  
    11. //------------------------------------------------------------------------------
    12. // Port 1  bit definitions
    13. //------------------------------------------------------------------------------
    14.  
    15.  
    16. sbit    RXEN =         P1^0;        // RS485 RX Enable
    17. sbit    TXEN =        P1^1;         // RS485 TX Enabale
    18. sbit    PUSH2 =     P1^2;        // Start Swtich 2
    19. sbit    PUSH3 =     P1^3;        // Start Swtich 3
    20. sbit    VOFF0 =     P1^4;        // VREG 0 ON/OFF
    21. sbit    VOFF1 =     P1^5;        // VREG 1 ON/OFF  
    22. sbit     VOFF2 =        P1^6;        // VREG 2 ON/OFF
    23. sbit    VOFF3 =     P1^7;        // VREG 3 ON/OFF
    24.  
    25.  
    :/code:

    is what I used for my earlier definitions I did not want to change this because it interferes with my other stuff (I can't believe how large this has gotten already!)

    I have the array to tell the user (me) what state the batteries are in
    using:
    Code ( (Unknown Language)):
    1.  :code:data byte Charge_State[4] = {0,0,0,0};        // Define Charge State for 4 Channels, 0-3
    :/code:

    MAIN LOOP CODE
    just loops, looks for if buttons are pressed and does stuff for me
    Code ( (Unknown Language)):
    1.  :code:
    2. while (1)
    3.      {
    4.     //VOFF0 = 0;
    5.     if(CPT0CN == 0xB5;Charge_State[0]++;)  printf("Channel 0 Mode Change\n");
    6.     if(CPT1CN == 0xB5;Charge_State[1]++;)  printf("Channel 1 Mode Change\n");
    7.     if(PUSH2 == 1;Charge_State[2]++;)        printf("Channel 2 Mode Change\n");
    8.     if(PUSH3 == 1;Charge_State[3]++;)        printf("Channel 3 Mode Change\n");
    9. //PCA0CN &= ~0x40;
    10.     if(++count == 5) //MAIN LOOP
    11.     {get_voltages();count=0;
    12.     batt_mode();
    13.    
    14.    
    15.     } //GET and DISPLAY position.volt.amp
    16.  
    17.  
    :/code:


    my idea was to do something like this (I didn't think it would work but I tried)

    Code ( (Unknown Language)):
    1.  :code:
    2. void batt_mode(void) //BATTERY MODE 0==NOTHING, 1==CHARGE, 2==DISCHARGE  change PWM mode for more/less current
    3. {
    4. int i=0;
    5. for(;i>=3;i++)
    6. {
    7. switch(Charge_State[i])
    8. {
    9. case(0):{VOFF[i]=1;CEX[i]=0;break;}  //just read voltages and amp(none)
    10. case(1):{VOFF[i]=0;CEX[i]=1;break;}     //Charge battery, main read volt and amp
    11. case(2):{VOFF[i]=1;CEX[i]=1;break;}     //Discharge battery, main read volt and amp
    12. default:{Charge_State[0];break;}
    13. }//switch
    14. }//for
    15.  
    16. }
    17. [/i][/i][/i][/i][/i][/i][/i]
    :/code:

    I'm not very good with pointers and I am stuck as to what to do. Any ideas or suggestions? I'd like to make this as lean as possible without making too many headaches for myself. I had an idea to put one big giant switch statement but that doesn't seem "right".As stated earlier I do not want to change my VOFF1,2,3 or CEX1,2,3 because it interferes with all my other work. Thanks.

    *EDIT* I'm working with silabs IDE for compiling and the 8051F005 for programming via jtag (yes the hardware side is completed and working...I think LOL) If it's not clear in the code the nomenclature for the pins do the following for me:

    Default IDLE MODE does nothing
    VOFF[n] = 1
    CEX[n] = 0

    CHARGE MODE
    VOFF[n] = 0
    CEX[n] = 1

    DISCHARGE
    VOFF[n] = 1
    CEX[n] = 1

    the hardware is already set up with 4 similar circuits hooked up the one micro
     
    Last edited: Feb 11, 2014
  2. Chalma

    Thread Starter Member

    May 19, 2013
    54
    2
    I was thinking of doing something like this
    Code ( (Unknown Language)):
    1. :code:
    2.  
    3. data byte VOFFN = {VOFF0,VOFF1,VOFF2,VOFF3};
    4. data byte CEXN    = {CEX0,CEX1,CEX2,CEX3};
    5.  
    6. :/code:
    am I thinking along the right lines?
     
Loading...