4、GPIO应用程序
application.c
#include "led.h"
#include "key.h"
#include "buzzer.h"
#define MAX_VOL 20
#define MIN_VOL 1
int main(void){
unsigned char volume = 10;
//周期计数,忽略键盘扫描,用于蜂鸣器控制
//如volume = 10,即20个周期有10个输出高电平
unsigned char cycle = 0;
led_operate(0,1);
led_operate(1,1);
led_operate(2,1);
led_operate(3,1);
while(1){
unsigned char key_site = scan_keyboard(); //扫描按键状态
char led_status = -1;
//延迟10ms给蜂鸣器一个响应时间,否则在Proteus仿真环境下蜂鸣器可能由于得不到(电脑的)CPU而不响
delayms(10);
cycle++;
switch(key_site){
case 0: //按键一被按了一次
led_status = get_led_status(0);
if(led_status == 0){
led_operate(0,1); //D1状态改变
}else if(led_status == 1){
led_operate(0,0); //D1状态改变
}
break;
case 1:
led_status = get_led_status(1);
if(led_status == 0){
led_operate(1,1); //D2状态改变
}else if(led_status == 1){
led_operate(1,0); //D2状态改变
}
break;
case 2:
led_status = get_led_status(2);
if(led_status == 0){
led_operate(2,1); //D3状态改变
}else if(led_status == 1){
led_operate(2,0); //D3状态改变
}
break;
case 3:
led_status = get_led_status(3);
if(led_status == 0){
led_operate(3,1); //D4状态改变
}else if(led_status == 1){
led_operate(3,0); //D4状态改变
}
break;
case 4: //蜂鸣器音量+
if(volume < MAX_VOL){
volume++;
}
break;
case 5: //蜂鸣器音量-
if(volume > MIN_VOL){
volume--;
}
break;
default:
break;
}
if(cycle >= 0){
buzzer_open();
}if(cycle >= volume){
buzzer_off();
}if(cycle >= (MAX_VOL+1)){
cycle = 0;
}
}
return 0;
}
|