switch statement for pins

Thread Starter

Chalma

Joined May 19, 2013
54
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.
Rich (BB code):
 :code:
//------------------------------------------------------------------------------
// Port 0  bit definitions
//------------------------------------------------------------------------------

sbit    CEX0 =         P0^4;        // PWM Output 0
sbit    CEX1 =         P0^5;        // PWM Output 1
sbit    CEX2 =         P0^6;        // PWM Output 2
sbit    CEX3 =         P0^7;        // PWM Output 3

//------------------------------------------------------------------------------
// Port 1  bit definitions
//------------------------------------------------------------------------------


sbit    RXEN =         P1^0;        // RS485 RX Enable 
sbit    TXEN =        P1^1;         // RS485 TX Enabale
sbit    PUSH2 =     P1^2;        // Start Swtich 2
sbit    PUSH3 =     P1^3;        // Start Swtich 3 
sbit    VOFF0 =     P1^4;        // VREG 0 ON/OFF 
sbit    VOFF1 =     P1^5;        // VREG 1 ON/OFF   
sbit     VOFF2 =        P1^6;        // VREG 2 ON/OFF
sbit    VOFF3 =     P1^7;        // VREG 3 ON/OFF
:/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:
Rich (BB code):
 :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
Rich (BB code):
 :code:
while (1)
     {
    //VOFF0 = 0;
    if(CPT0CN == 0xB5;Charge_State[0]++;)  printf("Channel 0 Mode Change\n");
    if(CPT1CN == 0xB5;Charge_State[1]++;)  printf("Channel 1 Mode Change\n");
    if(PUSH2 == 1;Charge_State[2]++;)        printf("Channel 2 Mode Change\n");
    if(PUSH3 == 1;Charge_State[3]++;)        printf("Channel 3 Mode Change\n");
//PCA0CN &= ~0x40;
    if(++count == 5) //MAIN LOOP
    {get_voltages();count=0;
    batt_mode();
    
    
    } //GET and DISPLAY position.volt.amp
:/code:


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

Rich (BB code):
 :code:
void batt_mode(void) //BATTERY MODE 0==NOTHING, 1==CHARGE, 2==DISCHARGE  change PWM mode for more/less current
{
int i=0;
for(;i>=3;i++)
{
switch(Charge_State)
{
case(0):{VOFF=1;CEX=0;break;}  //just read voltages and amp(none)
case(1):{VOFF=0;CEX=1;break;}     //Charge battery, main read volt and amp
case(2):{VOFF=1;CEX=1;break;}     //Discharge battery, main read volt and amp
default:{Charge_State[0];break;}
}//switch
}//for

}
:/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:

Thread Starter

Chalma

Joined May 19, 2013
54
I was thinking of doing something like this
Rich (BB code):
:code:

data byte VOFFN = {VOFF0,VOFF1,VOFF2,VOFF3};
data byte CEXN    = {CEX0,CEX1,CEX2,CEX3};

:/code:
am I thinking along the right lines?
 
Top