เป็นโปรแกรมให้รับค่าจาก Sw4x3 สั่งให้มอเตอร์หมุน 180 องศา ตามเข็ม เมื่อกด 1 หมุน270ทวนเข็ม เมื่อรับค่าอื่น
อยากทราบผู้รู้อธิบายกระบวนการทำงานของโปรแกรมนี้หน่อยครับ ขอบคุณอย่างยิ่ง
#include
#define STEP_DEGREE 90
#define ROWS 4
#define COLS 3
char keyMap[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
sbit R0 = P1^0;
sbit R1 = P1^1;
sbit R2 = P1^2;
sbit R3 = P1^3;
sbit C0 = P1^4;
sbit C1 = P1^5;
sbit C2 = P1^6;
void delay_ms(num) {
int i, j;
for (i = 0; i < num; i++) {
for (j = 0; j < 100; j++) {}
}
}
char keypad() {
char key = '\0';
R0=0;R1=1;R2=1;R3=1;
if (C0==0){ key = keyMap[0][0]; P1 = 0xF0; delay_ms(500); }
if (C1==0){ key = keyMap[0][1]; P1 = 0xF0; delay_ms(500); }
if (C2==0){ key = keyMap[0][2]; P1 = 0xF0; delay_ms(500); }
R0=1;R1=0;R2=1;R3=1;
if (C0==0){ key = keyMap[1][0]; P1 = 0xF0; delay_ms(500); }
if (C1==0){ key = keyMap[1][1]; P1 = 0xF0; delay_ms(500); }
if (C2==0){ key = keyMap[1][2]; P1 = 0xF0; delay_ms(500); }
R0=1;R1=1;R2=0;R3=1;
if (C0==0){ key = keyMap[2][0]; P1 = 0xF0; delay_ms(500); }
if (C1==0){ key = keyMap[2][1]; P1 = 0xF0; delay_ms(500); }
if (C2==0){ key = keyMap[2][2]; P1 = 0xF0; delay_ms(500); }
R0=1;R1=1;R2=1;R3=0;
if (C0==0){ key = keyMap[3][0]; P1 = 0xF0; delay_ms(500); }
if (C1==0){ key = keyMap[3][1]; P1 = 0xF0; delay_ms(500); }
if (C2==0){ key = keyMap[3][2]; P1 = 0xF0; delay_ms(500); }
return key;
}
void rotateDegree(int value) {
int step, state, n;
step = value / STEP_DEGREE;
state = P2;
for (n = 0; n < (step < 0 ? -step : step); n++) {
if (value > 0) {
state *= 2;
if (state > 8) {
state = 1;
}
} else {
state /= 2;
if (state < 1) {
state = 8;
}
}
P2 = state;
delay_ms(300);
}
}
void main() {
P1 = 0xF0;
P2 = 0x01;
do {
char key = keypad();
if (key != '\0') {
if (key == '1') {
rotateDegree(180);
} else {
rotateDegree(-270);
}
}
} while (1);
}
ช่วยอธิบายโปรแกรมภาษาCนี้หน่อยครับ
อยากทราบผู้รู้อธิบายกระบวนการทำงานของโปรแกรมนี้หน่อยครับ ขอบคุณอย่างยิ่ง
#include
#define STEP_DEGREE 90
#define ROWS 4
#define COLS 3
char keyMap[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
sbit R0 = P1^0;
sbit R1 = P1^1;
sbit R2 = P1^2;
sbit R3 = P1^3;
sbit C0 = P1^4;
sbit C1 = P1^5;
sbit C2 = P1^6;
void delay_ms(num) {
int i, j;
for (i = 0; i < num; i++) {
for (j = 0; j < 100; j++) {}
}
}
char keypad() {
char key = '\0';
R0=0;R1=1;R2=1;R3=1;
if (C0==0){ key = keyMap[0][0]; P1 = 0xF0; delay_ms(500); }
if (C1==0){ key = keyMap[0][1]; P1 = 0xF0; delay_ms(500); }
if (C2==0){ key = keyMap[0][2]; P1 = 0xF0; delay_ms(500); }
R0=1;R1=0;R2=1;R3=1;
if (C0==0){ key = keyMap[1][0]; P1 = 0xF0; delay_ms(500); }
if (C1==0){ key = keyMap[1][1]; P1 = 0xF0; delay_ms(500); }
if (C2==0){ key = keyMap[1][2]; P1 = 0xF0; delay_ms(500); }
R0=1;R1=1;R2=0;R3=1;
if (C0==0){ key = keyMap[2][0]; P1 = 0xF0; delay_ms(500); }
if (C1==0){ key = keyMap[2][1]; P1 = 0xF0; delay_ms(500); }
if (C2==0){ key = keyMap[2][2]; P1 = 0xF0; delay_ms(500); }
R0=1;R1=1;R2=1;R3=0;
if (C0==0){ key = keyMap[3][0]; P1 = 0xF0; delay_ms(500); }
if (C1==0){ key = keyMap[3][1]; P1 = 0xF0; delay_ms(500); }
if (C2==0){ key = keyMap[3][2]; P1 = 0xF0; delay_ms(500); }
return key;
}
void rotateDegree(int value) {
int step, state, n;
step = value / STEP_DEGREE;
state = P2;
for (n = 0; n < (step < 0 ? -step : step); n++) {
if (value > 0) {
state *= 2;
if (state > 8) {
state = 1;
}
} else {
state /= 2;
if (state < 1) {
state = 8;
}
}
P2 = state;
delay_ms(300);
}
}
void main() {
P1 = 0xF0;
P2 = 0x01;
do {
char key = keypad();
if (key != '\0') {
if (key == '1') {
rotateDegree(180);
} else {
rotateDegree(-270);
}
}
} while (1);
}