内容介绍
内容介绍
Funpack 第七期 DM164137 PIC16F18446
实现功能
判断按键按下时间(0-1s 1-5s 5s以上)在串口打印不同信息,并在蜂鸣器发出不同响声
编程思路
程序循环判断按键是否按下并进行按键消抖,按下按键后开始计时。继续判断按键是否释放并进行按键消抖,若释放按键就停止计时,然后判断按键时间长短在串口打印不同消息,并输出不同频率的PWM信号控制蜂鸣器响起。
MCC配置
主要程序
void main(void) {
SYSTEM_Initialize();
TMR0_WriteTimer(0x0000);
adc_result_t convertedValue;
uint16_t press_time;
while(1) { //main loop
if(Button_IsPressed()) {
TMR0_StartTimer(); //start counting
LED_D4_SetHigh(); //light up LED_D4 while button is pressed
while(1) {
if(Button_IsReleased()) {
TMR0_StopTimer(); //stop counting
LED_D4_SetLow(); //clear LED_D4
press_time = TMR0_ReadTimer(); //get press time
TMR0_WriteTimer(0x0000); //reset timer0
convertedValue = ADCC_GetSingleConversion(potentiometer);
printf("ADC result:%d ", convertedValue);
Handle_PressTime(press_time); //handle press time
break;
}
else continue;
break;
}
continue;
}
else continue;
}
}
void Handle_PressTime(uint16_t press_time){
if(press_time<=1953) { //press time less than 1s
TMR2_LoadPeriodRegister(0x46); //buzzer "do"
TMR2_StartTimer();
printf("Less than 1s\n\r");
}
else if(press_time<=9765) { //press time longer than 1s
TMR2_LoadPeriodRegister(0x3E); //buzzer "re"
TMR2_StartTimer();
printf("Longer than 1s\n\r");
}
else { //press time longer than 5s
TMR2_LoadPeriodRegister(0x37); //buzzer "mi"
TMR2_StartTimer();
printf("Longer than 5s\n\r");
}
__delay_ms(200); //beep 0.2s
TMR2_StopTimer();
}
心得体会
这学期的课程中有一门内容是AVRmega16的使用,funpack第七期非常巧的推出了同样是8位单片机的这款PIC16f446,对比着学习两种不同的单片机可以比较它们对外设的操作有什么异同,让我对单片机的了解更加的深入了。
附件下载
Lab01.X.zip
团队介绍
居然是一个人
团队成员
滕氏蓝
不会单片机
评论
0 / 100
查看更多