如何使用開關結構增加狀態。我需要一個計數++嗎?我在交換機(expr)中放置了[(((button_in & 0x0040)!= 0)]表達式。這給了我前兩個我想要的狀態。 (1)按按鈕1產生0001.(2)按鈕2產生0010. 我不確定如何編程按鈕1 TWICE爲了產生0010.我在while循環中實現了一個計數嗎?我可以使用while表達式作爲計數嗎?我應該在級聯中放置另一個while循環嗎?我想增加我的狀態。有7個州:0000,0001(5美分),0010(10美分),0011(15美分),0100(20美分),1000(25美分),0111(更改)。 我更新了我的問題和代碼,試圖清楚地反映我的意圖。 我不是程序員,我的朋友提到,當我按下按鈕;我應該檢查當前狀態;然後編程我的代碼。他還提到了一個二進制計算器。哪種方法最有效?由於C開關計數增加狀態
int main()
{
char A; //placed for switch expression... (not needed?)
int button_in = 0; // button is set for 0 (not-engaged)
DeviceInit(); //set LED1 thru LED4 as digital output
DelayInit(); //Initialize timer for delay
int count; //maybe required for 5 button pushes. Requesting help with this
while (1) //Can I initiate a count? for a second button push?
{
button_in = PORTReadBits(IOPORT_A, BIT_6 | BIT_7); //Button 1 and button 2 defined
if (button_in != 0) //if button is engaged utilize switch statement
{
switch ((button_in & 0x0040) != 0) //if button1 is engaged
{
case 0:
((button_in & 0x0080) != 0); //Statement: Button2 engages case0
PORTWrite(IOPORT_B, BIT_11); //State goes to 010 (BIT_11 lights up).
break;
default: ((button_in & 0x0040) != 0); //Statement: Button1 engages default.
PORTWrite(IOPORT_B, BIT_10); //This is state 0001 (BIT_10) lights up.
break;
}
DelayMs(100); //100millisecond delay for light shine
PORTClearBits(IOPORT_B, BIT_10 | BIT_11 | BIT_12 | BIT_13); //ClearLEDs
}
}
}
我想你需要的是一個狀態機 – Mobius