实验六 考试

《嵌入式系统设计》考试

考试要求:
在Linux系统下编写LED、按键及蜂鸣器程序,
通过交叉编译出来的目标文件通过串口上传到开发板,
并在开发板上执行程序并观察结果。
具体要求:
	初始频率为10,LED处于全灭状态,
	定义3个按键,
		按键1:
			每按一次按键频率加1,
			当频率大于等于19时恢复到初始值,
			频率每次加2时点亮一个LED灯
			(即:频率为18时LED处于全亮的状态);
		按键2:
			每按一次按键频率减1,
			当频率小于等于1时恢复到初始值,
			频率每次减2时熄灭一个LED灯(只针对有LED点亮时);
		按键3:
			关闭蜂鸣器、LED全灭、并退出程序。

实验步骤:

实验代码:

#include
#include
#include

int main() {

    int fdk, fdp, fdl;
    int i;
    int frq = 10;

    int twice_inc = 0;
    int twice_dec = 0;
    //第几盏灯, 小于which_led的亮
    int which_led = 0;

    char key[4] = {'0', '0', '0', '0'};

    fdk = open("/dev/buttons", O_RDONLY);
    fdp = open("/dev/pwm", O_RDONLY);
    fdl = open("/dev/leds",O_WRONLY);
    if(fdk<0) {
        printf("Open buttons ERROR!\n");
        exit(1);
    }
    if(fdp<0) {
        printf("Open PWM ERROR!\n");
        exit(1);
    }
    if(fdl<0) {
        printf("Open leds ERROR!\n");
        exit(1);
    }

    for(i = 0; i < 4; i++) ioctl(fdl, 0, i);

    //主程序
    for(;;) {
        char cukey[4] = {'0', '0', '0', '0'};
        read(fdk, cukey, sizeof cukey);
        for(i = 0; i < 4; i++) {
            if(key[i] != cukey[i]) {
                key[i] = cukey[i];
                if(key[i] == '1') {
                    switch(i) {
                    case 0:
                        frq++;
                        twice_dec = 0;
                        twice_inc++;
                        if(frq == 19) frq = 10;
                        if(twice_inc == 2) {
                            if(which_led != 4) {
                                ioctl(fdl, 1, which_led);
                                which_led++;
                            }
                            twice_inc = 0;
                        }
                        break;
                    case 1:
                        frq--;
                        twice_inc = 0;
                        twice_dec++;
                        if(twice_dec == 2) {
                            if(which_led != 0) {
                                which_led--;
                                ioctl(fdl, 0, which_led);
                            }
                            twice_dec = 0;
                        }
                        if(frq == 1) frq = 10;
                        break;
                    case 2:
                        ioctl(fdp, 0);
                        for(i = 0; i < 4; i++) ioctl(fdl, 0, i);
                        close(fdk);
                        close(fdp);
                        close(fdl);
                        exit(1);
                        break;
                    default:
                        printf("Invalid button!\n");
                    }
                    ioctl(fdp, 1, frq);

                    printf("------------------------------\n");
                    printf("which_led = %d\n", which_led);
                    printf("twice_inc = %d\ntwice_dec = %d\n", twice_inc, twice_dec);
                    printf("frq = %d\n", frq);
                    printf("------------------------------\n\n");

                }
            }
        }
    }
    return 0;
}

你可能感兴趣的:(嵌入式系统设计,单片机,stm32,c语言)