/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
uint16_t cnt = 0;
uint8_t STA = 0;
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART0 for printf */
UART0_Init();
while(1) {
STA <<= 1;
STA += PH0;
printf("%d \n", PH0);
TIMER_Delay(TIMER0, 1000000);
STA &= 3;
if(STA == 2) {
cnt++;
//printf("%d ", cnt);
}
//TIMER_Delay(TIMER0, 1000000);
}
}
STA 原始值 00000000(0)
二進制 (十進制)
STA :
第一圈 |
|
結果 |
00000000 |
左移一次 |
00000000 |
00000000+ PH0(1) |
[沒有按] |
00000001 |
00000001 & 00000011(3) |
|
00000001(1) |
STA == 2 false |
|
|
第二圈 |
|
結果 |
00000001 |
左移一次 |
00000010 |
00000010 + PH0(1) |
[沒有按] |
00000011 |
00000011 & 00000011(3) |
|
00000011(3) |
STA == 2 false |
|
|
第三圈 |
|
結果 |
00000011 |
左移一次 |
00000110 |
00000110 + PH0(1) |
[沒有按] |
00000111 |
00000111 & 00000011(3) |
|
00000011(3) |
STA == 2 false |
|
|
第四圈 |
|
結果 |
00000011 |
左移一次 |
00000110 |
00000110 + PH0(1) |
[沒有按] |
00000111 |
00000111 & 00000011(3) |
|
00000011(3) |
STA == 2 false |
|
|
第五圈 |
|
結果 |
00000011 |
左移一次 |
00000110 |
00000110 + PH0(0) |
[按下去] |
00000110 |
00000110 & 00000011(3) |
|
00000010(2) |
STA == 2 true |
|
印數字 |
(CPU執行很快,可能不小心print到數個數字)
第六圈 |
|
結果 |
00000010 |
左移一次 |
00000100 |
00000100 + PH0(0) |
[還按著] |
00000100 |
00000100 & 00000011(3) |
|
00000000(0) |
STA == 2 false |
|
|
第七圈 |
|
結果 |
00000000 |
左移一次 |
00000000 |
00000000 + PH0(0) |
[還按著] |
00000000 |
00000000 & 00000011(3) |
|
00000000(0) |
STA == 2 false |
|
|
第八圈 (等於第一圈) |
|
結果 |
00000000 |
左移一次 |
00000000 |
00000000 + PH0(1) |
[沒有按] |
00000001 |
00000001 & 00000011(3) |
|
00000001(1) |
STA == 2 false |
|
|